Using Custom Indicator in EA (MT4) - page 4

 

so as the for loop is, it will go through all the orders that meet its criteria?

mladen:
nduru22

Actual line was for (int i = OrdersTotal()-1; i>=0; i --) and it is apart of a loop that goes through all the currently opened orders and decides what to do with each selected order. You can invert the looping direction but the way it is it is covering the case even when you close orders (otherwise it would skip some orders) and I recommend that you use that form.

metaquotes states that orders are not sorted by any criteria (they do not guarantee any sort order) and you have to go through the whole list of orders regardless of what you are doing, so you might do it the way thet will prevent errors in some cases
 

That loop will go through all opened orders.

The criteria you have to add (the if block following the for loop that will filter out which orders you actually want to process). Fore example this "if block" filters out only the last order (time wise) of the currently opened orders since if any of the conditions is not met it resumes the loop and does not process anything found behind it within the loop :

if (magicNumber!=0)

if (OrderMagicNumber() != magicNumber) continue;

if (OrderSymbol() != Symbol()) continue;

if (OrderOpenTime() <= lastTime) continue;
nduru22:
so as the for loop is, it will go through all the orders that meet its criteria?
 

so after writing the initial for loop, i should add my own if statement for it to do what id like it to do?

mladen:
That loop will go through all opened orders.

The criteria you have to add (the if block following the for loop that will filter out which orders you actually want to process). Fore example this "if block" filters out only the last order (time wise) of the currently opened orders since if any of the conditions is not met it resumes the loop and does not process anything found behind it within the loop :

if (magicNumber!=0)

if (OrderMagicNumber() != magicNumber) continue;

if (OrderSymbol() != Symbol()) continue;

if (OrderOpenTime() <= lastTime) continue;
 

nduru22

Yes

nduru22:
so after writing the initial for loop, i should add my own if statement for it to do what id like it to do?
 

okay, let me see if i will be able to get the result im looking for. thanks

mladen:
nduru22 Yes
 

thanks for all the help that you have been giving me, im slowly becoming better but i realize i really have to work on my logic but i guess that comes with experience. Id like to ask how can i get this to work

i would like my e.a, for every order that it makes, to double the lot size of the preceding order. Im using the function that you gave me earlier to pick the lastprice but now im picking the last lotsize. the issue comes in when im trying to make sure that every time a new order is made, the lot size is a double of the preceding order. how do you think i can acheive this?

nduru22:
okay, let me see if i will be able to get the result im looking for. thanks
 

thanks for all the help that you have been giving me, im slowly becoming better but i realize i really have to work on my logic but i guess that comes with experience. Id like to ask how can i get this to work

i would like my e.a, for every order that it makes, to double the lot size of the preceding order. Im using the function that you gave me earlier to pick the lastprice but now im picking the last lotsize. the issue comes in when im trying to make sure that every time a new order is made, the lot size is a double of the preceding order. how do you think i can acheive this?

mladen:
nduru22 Yes
 

Use a function like this :

double lastOrderSize(int magicNumber=0)

{

datetime lastTime = 0;

double lastSize = 0;

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

{

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

if (magicNumber!=0)

if (OrderMagicNumber() != magicNumber) continue;

if (OrderSymbol() != Symbol()) continue;

if (OrderOpenTime() <= lastTime) continue;

lastTime = OrderOpenTime();

lastSize = OrderLots();

}

if (lastSize==0)

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

{

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

if (magicNumber!=0)

if (OrderMagicNumber() != magicNumber) continue;

if (OrderSymbol() != Symbol()) continue;

if (OrderCloseTime() <= lastTime) continue;

lastTime = OrderCloseTime();

lastSize = OrderLots();

}

return(lastSize);

}

And if it is > 0, multiply with whatever multiplier you want. If it returns 0, it means that there was no previous order (fo a reaquired magic number) and that you should start with some "start size" lot size

nduru22:
thanks for all the help that you have been giving me, im slowly becoming better but i realize i really have to work on my logic but i guess that comes with experience. Id like to ask how can i get this to work i would like my e.a, for every order that it makes, to double the lot size of the preceding order. Im using the function that you gave me earlier to pick the lastprice but now im picking the last lotsize. the issue comes in when im trying to make sure that every time a new order is made, the lot size is a double of the preceding order. how do you think i can acheive this?
 

thanks, so this should give me the size of the last order. So if I want the next order to be double the size of the last order, i should now just multiply the result of this function by whichever number that i want to multiply with, whether it is 2 or 3 right? I had not thought of the history, and the syntax that i was using was abit wrong compared to yours but i hope that it will now work.

thanks

mladen:
Use a function like this :
double lastOrderSize(int magicNumber=0)

{

datetime lastTime = 0;

double lastSize = 0;

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

{

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

if (magicNumber!=0)

if (OrderMagicNumber() != magicNumber) continue;

if (OrderSymbol() != Symbol()) continue;

if (OrderOpenTime() <= lastTime) continue;

lastTime = OrderOpenTime();

lastSize = OrderLots();

}

if (lastSize==0)

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

{

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

if (magicNumber!=0)

if (OrderMagicNumber() != magicNumber) continue;

if (OrderSymbol() != Symbol()) continue;

if (OrderCloseTime() <= lastTime) continue;

lastTime = OrderCloseTime();

lastSize = OrderLots();

}

return(lastSize);

}

And if it is > 0, multiply with whatever multiplier you want. If it returns 0, it means that there was no previous order (fo a reaquired magic number) and that you should start with some "start size" lot size
 

It will work, don't worry

nduru22:
thanks, so this should give me the size of the last order. So if I want the next order to be double the size of the last order, i should now just multiply the result of this function by whichever number that i want to multiply with, whether it is 2 or 3 right? I had not thought of the history, and the syntax that i was using was abit wrong compared to yours but i hope that it will now work. thanks
Reason: