How to code? - page 163

 

iCCI Question

I am new to MQL4 and am trying to get the previous CCI value. I have tried using the iCCI function with a Shift value of 1, but it does not give me the previous value. How would I do that without saving the current value to a variable? I am trying to determine the direction the CCI is going.

Thank you for your help.

 

The code is tick based, so if there is no tick at that time, then the code will miss the time check. I don't think you can get it to the exact second without some kind of loop that is contantly running.

 

Help With EA Editing

I am trying to figure this out... can you please help?

MB Trading - Stocks Options Futures Forex Online Discount Trading

I changed this:

OrderSend(Symbol(),OP_SELL,mylotsi, SellPrice,slippage,sl,tp,"MyMEFx EA"+MagicNumber,MagicNumber,0,Arrow sColor);

return(0);

To:

int ticket = OrderSend(Symbol(),OP_SELL,mylotsi, SellPrice,slippage,0,0,"MyMEFx EA"+MagicNumber,MagicNumber,0,Arrow sColor);

Sleep(1500);

OrderModify(ticket,OrderOpenPrice() ,sl,tp,0,ArrowsColor);

=================================== ======

I am getting these errors:

2009.02.28 17:32:59 2009.01.01 20:42 EATest AUDJPY,H1: invalid ticket for OrderModify function

2009.02.28 17:32:59 2009.01.01 20:42 EATest AUDJPY,H1: OrderSend error 131

2009.02.28 17:32:59 2009.01.01 20:42 EATest AUDJPY,H1: OrderModify error 4051

=====================

 
coach3131:
I am trying to figure this out... can you please help?

MB Trading - Stocks Options Futures Forex Online Discount Trading

I changed this:

OrderSend(Symbol(),OP_SELL,mylotsi, SellPrice,slippage,sl,tp,"MyMEFx EA"+MagicNumber,MagicNumber,0,Arrow sColor);

return(0);

To:

int ticket = OrderSend(Symbol(),OP_SELL,mylotsi, SellPrice,slippage,0,0,"MyMEFx EA"+MagicNumber,MagicNumber,0,Arrow sColor);

Sleep(1500);

OrderModify(ticket,OrderOpenPrice() ,sl,tp,0,ArrowsColor);

=================================== ======

I am getting these errors:

2009.02.28 17:32:59 2009.01.01 20:42 EATest AUDJPY,H1: invalid ticket for OrderModify function

2009.02.28 17:32:59 2009.01.01 20:42 EATest AUDJPY,H1: OrderSend error 131

2009.02.28 17:32:59 2009.01.01 20:42 EATest AUDJPY,H1: OrderModify error 4051

=====================

Your problem is in your lot size (volume) in your order send statment. which returns a -1 because of the error. the ticket veriable doesnot contain a ticket number it contains the errror flag of -1 which triggered the other errors with the ordermodify function. You must correct the lot size variable mylotsi, to contain an acceptable value. second you should check the return value in the ticket variable to see that it dosenot contain an error flag before calling the ordermodify routine.

keit

 

EA Max Position

can any one add max positions for this EA

or let it to run one position only ?

thanks in advance

 

You'd better use

MathAbs(NormalizeDouble(Bid,4)-NormalizeDouble(pivot,4))<Delta*Point,

Where Delta=2...5

 
jdun:
Could someone tell me the code for stop loss and tp. I need to add it to this EA.

this hidden SL and TP

int TakeProfit=20; // 20 pips take profit

int StopLoss =40; // 40 pips stoploss

int Slippage = 3;

int MagicNumber=1;

int i;

int start(){

//----- exit @ TP

if((ScanTrades()>=1)&& (ProfitInPips()>=TakeProfit)){

//----- This part will close all open orders and delete pending trades

for(i = OrdersTotal()-1; i >=0; i--)

{ OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

if (OrderSymbol() == Symbol()&& OrderMagicNumber()==MagicNumber )

{

if (OrderType()==OP_BUY)OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Plum);

if (OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Plum);

if (OrderType() == OP_SELLLIMIT) OrderDelete(OrderTicket());

if (OrderType() == OP_BUYLIMIT) OrderDelete(OrderTicket());

if (OrderType() == OP_BUYSTOP) OrderDelete(OrderTicket());

if (OrderType() == OP_SELLSTOP) OrderDelete(OrderTicket());

}

}

}

//------exit @ SL

if((ScanTrades()>=1)&& (ProfitInPips()<= -StopLoss)){

//----- This part will close all open orders and delete pending trades

for(i = OrdersTotal()-1; i >=0; i--)

{ OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

if (OrderSymbol() == Symbol()&& OrderMagicNumber()==MagicNumber )

{

if (OrderType()==OP_BUY)OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Plum);

if (OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Plum);

if (OrderType() == OP_SELLLIMIT) OrderDelete(OrderTicket());

if (OrderType() == OP_BUYLIMIT) OrderDelete(OrderTicket());

if (OrderType() == OP_BUYSTOP) OrderDelete(OrderTicket());

if (OrderType() == OP_SELLSTOP) OrderDelete(OrderTicket());

}

}

}

return (0);

}

//----------- Call functions

int ScanTrades()

{

int Tot = OrdersTotal();

int Numb = 0;

for(int cnt=0; cnt<=Tot-1; cnt++)

{

OrderSelect(cnt, SELECT_BY_POS);

if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) Numb++;

}

return(Numb);

}

double ProfitInPips()

{

RefreshRates();

double Prof=0;

int i;

int totalOrders=OrdersTotal();

for(i=0;i<totalOrders;i++)

{

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;

if(OrderMagicNumber()==MagicNumber)

{

if(OrderType()==0)

{

Prof+=(MarketInfo(OrderSymbol(),MODE_BID)-OrderOpenPrice())/MarketInfo(OrderSymbol(),MODE_POINT);

}

if(OrderType()==1)

{

Prof+=(OrderOpenPrice()-MarketInfo(OrderSymbol(),MODE_ASK))/MarketInfo(OrderSymbol(),MODE_POINT);

}

}

}

return(Prof);

}

 

Lets say I have a few Buy orders and Sell orders

What I want do is :

- Exit all the trades " Basket" at 5 pips more than the breakeven price of the open trades

What I am trying to do in the first for loop is find the value of open trades + swap and convert it to pips , and this is the part where I am getting stuck

I have tried few ideas but I have reached a dead end, I know the problem is in the first for statement but I can't solve it, any help is highly appropriated

SymbolPL = 0;

OrdLots = 0;

Equity = 0;

MinPro = 5;

for(i = 0; i < OrdersTotal(); i++)

{OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)

{if(OrderType() == OP_BUY) OrdLots += OrderLots();

if(OrderType() == OP_SELL) OrdLots -= OrderLots();

Equity += OrderProfit() + OrderSwap();

}

}

MinPro= MathRound (MathAbs(Equity /OrdLots)+MinPro);

//--- Count the open trades

int i;

int count=0;

for(i=0;i<OrdersTotal();i++)

{ if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;

if(OrderMagicNumber()==MagicNumber && OrderType()<2)

{ count++;

}

}

//--- find the profit in pips of the open trades

RefreshRates();

double profits=0,openPrice=0,points=0;

string sym="";

int i;

int totalOrders=OrdersTotal();

for(i=0;i<totalOrders;i++)

{

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;

if(OrderMagicNumber()==MagicNumber)

{

sym=OrderSymbol();

openPrice=OrderOpenPrice();

if(OrderType()==0)

{

profits+=(MarketInfo(sym,MODE_BID)-openPrice)/MarketInfo(sym,MODE_POINT);

}

if(OrderType()==1)

{

profits+=(OrderOpenPrice()-MarketInfo(sym,MODE_ASK))/MarketInfo(sym,MODE_POINT);

}

}

}

//--- Close when the open trades are 5 pips more than the breakeven price

if (count>1 && profits>MinPro )

{

for(i = OrdersTotal()-1; i >=0; i--)

{

OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

if (OrderSymbol() == Symbol()&& OrderMagicNumber()==MagicNumber )

{

OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Plum);

}

}

}

 

I think you can use an "equity_per_lotpoint" constant, which would be computed as:

double equity_per_lotpoint = MarketInfo( Symbol(), MODE_TICKVALUE ) / MarketInfo( Symbol(), MODE_TICKSIZE );

[/PHP]

and then you'd have what you need after the first loop, with:

[PHP]if ( Equity > 5 * equity_per_lotpoint * MathAbs( OrdLots ) ) { ...

 

Thanks Ralph but I would be missing the swap , and I want to include the swap profit/loss in the close of the orders decision