OrderLots() Can I have help with Coding please?

 

Hi,

I can do simple coding at best. I am trying to write an ea but I am getting "Invalid Lots" error.

double LotsOptomised()

{

double prevMACD = iMACD(Symbol(), 0, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, 2);

double curMACD = iMACD(Symbol(), 0, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, 1);

double lots = 0;

int total = OrdersTotal();

for(int i=0;i<total;i++)

{

OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

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

if(OrderLots()==0.1 && ((prevMACD>0 && curMACD<0) || (prevMACD 0)))lots =0.2;

if(OrderLots()==0.2 && ((prevMACD>0 && curMACD<0) || (prevMACD 0)))lots =0.4;

if(OrderLots()==0.4 && ((prevMACD>0 && curMACD<0) || (prevMACD 0)))lots =0.8;

if(OrderLots()==0.8 && ((prevMACD>0 && curMACD<0) || (prevMACD 0)))lots =0.8;

else lots = InitialLots;

}

return(lots);

}

This is what I have written. My ea has no stop loss so each time the MACD changes trend I want the new orders to be twice the lots of the previous orders. I have an external double Initial Lots = .1 and in the OrderSend I have put LotsOptomised() for the lots entry.

Can anyone help or offer any suggestions?

Much appreciated

waltini

 

Anyone?

thanks

waltini

 

Why don't you insert a print statement before your ordersend function to find out how many lots are really being sent out. Sometimes these little print statements help a lot to debug.

good luck.

 

I am getting old and my eyes aren't what they used to be but two areas to look at:

after if((OrderSymbol()==Symbol())&&( OrderMagicNumber()==magic)) there doesn't appear to be braces {} enclosing your other tests, therefore only the first would execute as you probably expect, all the others would execute even if the first test (OrderSymbol()==Symbol()...) failed.

also, I have gotten very odd results declaring a double value that is less than zero without the leading zero - I have had trouble with .5 but 0.5 works.

I don't know if either of these are the problem but it will give you a place to look.

Good luck

Scott

 

Try, this.

double LotsOptomised()

{

double prevMACD = iMACD(Symbol(), 0, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, 2);

double curMACD = iMACD(Symbol(), 0, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, 1);

double lots = 0.1;//default lots 0.1 - there will be no error if lots will not be set by the other part of the code

int total = OrdersTotal();

for(int i=0;i<total;i++)

{

OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

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

{

if(OrderLots()==0.1 && ((prevMACD>0 && curMACD<0) || (prevMACD 0)))lots =0.2;

else if(OrderLots()==0.2 && ((prevMACD>0 && curMACD<0) || (prevMACD 0)))lots =0.4;

else if(OrderLots()==0.4 && ((prevMACD>0 && curMACD<0) || (prevMACD 0)))lots =0.8;

else if(OrderLots()==0.8 && ((prevMACD>0 && curMACD<0) || (prevMACD 0)))lots =0.8;

else lots = InitialLots;

}

}

return(lots);

}

 

Sort of Working!

Thanks Maji, Scott B, and Kalenzo,

I have installed your code Kalenzo and put the print function in Maji. It now trades with 0.1 lots but the lots are not doubling with the change in MACD.

I have used the MakeGrid193 as my base. I have changed the MACD setting slightly to comply with the rest of the ea.

I have attached the ea, if you have the time would you mind having a quick look to see what I have done wrong?

thanks heaps

waltini

Files:
 
waltini:
Thanks Maji, Scott B, and Kalenzo,

I have installed your code Kalenzo and put the print function in Maji. It now trades with 0.1 lots but the lots are not doubling with the change in MACD.

I have used the MakeGrid193 as my base. I have changed the MACD setting slightly to comply with the rest of the ea.

I have attached the ea, if you have the time would you mind having a quick look to see what I have done wrong?

thanks heaps

waltini

Try this, this is more clean version.

double LotsOptomised()

{

double prevMACD = iMACD(Symbol(), 0, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, 2);

double curMACD = iMACD(Symbol(), 0, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, 1);

double lots = InitialLots;//default lots 0.1 - there will be no error if lots will not be set by the other part of the code

int total = OrdersTotal();

for(int i=0;i<total;i++)

{

OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

if( OrderSymbol() == Symbol() && OrderMagicNumber()==magic && ((prevMACD>0 && curMACD<0) || (prevMACD 0)))

{

switch(OrderLots())

{

case 0.1:return(0.2);

case 0.2:return(0.4);

case 0.4:return(0.8);

case 0.8:return(0.8);

default:return(InitialLots);

}

}

}

return(lots);

}
 

I dont have the time to look inside your code, but it looks that if no order is open the newly opened order will start in 0.1 lot.

If you want to change it u must look at the history too. or open the new order BEFORE closing the old one