technoteze: I have a function for calculating the number of open buy or sell trades. I just don't know how to close the first trades in the series of buys or sells.
|
|
|
Hey whroeder1, thank you for the reply.
I'm not sure how to determine the earliest OrderOpenTime yet. I'll do some searching to see what I can find. If I manage to do that, how will I close the order using the time instead?
I'm really not a coder, I'm doing my best by cobbling my code together from what I can fin online. I really struggle with the logic behind programming unfortunately.
Thanks again!
technoteze:
how will I close the order using the time instead? |
|
Hey whroeder1, thank you for the reply.
I'm not sure how to determine the earliest OrderOpenTime yet. I'll do some searching to see what I can find. If I manage to do that, how will I close the order using the time instead?
I'm really not a coder, I'm doing my best by cobbling my code together from what I can fin online. I really struggle with the logic behind programming unfortunately.
Thanks again!
Ok cheers whroeder1, I'll need to work it out.
I've seen code for selecting the most recent order that returns the latest timestamp https://www.mql5.com/en/forum/109180. It looks like you were commenting there too :) I'm not sure how to use that to return the earliest timestamp.
If i do work it out, would I use Some_Variable = OrderSelect(0,SELECT_BY_POS,MODE_TRADES and try to retrieve the OrderOpenTime() of that order? And then if they are equal, close the order?
Would you mind explaining why I wouldn't simply look at all the sell orders, select the order in position 0 and it's price and if it's whatever distance away from the most recent order price, close the order in position 0?
The problem I have found in basket trading is that it can take on quite a bit of drawdown and if you hit a long trend without reversing it'll blow your account. The trades that kill you are the trades that are opened first and as you take on more trades the same direction, drawdown increases. My EA spaces it's trades but I would like the ability to close the first trade opened after my EA opens it 8th order (or whatever number of trades). I don't mind taking losses because the market will eventually reverse, I just need to survive until then.

- 2008.06.14
- www.mql5.com
technoteze: would I use Some_Variable = OrderSelect(0,SELECT_BY_POS,MODE_TRADES a
|
|
|
I guess my problem is I don't know how to convert the code for finding the newest to finding the oldest...
Am I correct in assuming that the change would be in the loop somewhere?
int LastOpenTicket(){ datetime lastTime = 0; int lastTicket = -1; // None open. for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if ( OrderSelect(pos, SELECT_BY_POS) // Only my orders w/ && OrderMagicNumber() == magic.number // my magic number && OrderSymbol() == Symbol() // and my pair. && OrderOpenTime() >= lastTime && OrderTicket() > lastTicket ){ lastTime = OrderOpenTime(); lastTicket = OrderTicket(); } return(lastTicket); }
&& OrderOpenTime() >= lastTime
&& OrderTicket() > lastTicket
Change >= to <
No need to check the OrderTicket
Hi Keith, thank you for your input. I didn't think it would be as simple as that. So will the below code return the time of the first opened order in my series of sell trades?
int EarliestOpenSellTicket(){ datetime EarliestSellTime = 0; int EarliestSellTicket = -1; // None open. for(int pos = OrdersTotal()-1; pos >= 0 ; pos--) if ( OrderSelect(pos, SELECT_BY_POS) // Only my orders w/ && OrderMagicNumber() == magic // my magic number && OrderSymbol() == Symbol() // and my pair. && OrderOpenTime() < EarliestSellTime && OrderType() == OP_SELL //Look at sell orders only ){ EarliestSellTime = OrderOpenTime(); } return(EarliestSellTicket); }
Shouldn't I be returning EarliestTime?
If the above is correct, how can I use whroeder1's suggestion of using OrderOpenTime() for closing the first order?
I simply want the ability to set a MaxBasketSellTrades limit and when this limit is hit, it will close the the first opened order in the basket. I have a function that will calculate the lowest sell trade price and another function that will calculate the highest sell trade price. Could I achieve what I want using the results of those? Is using OrderOpenTime() the only way to go?
I just don't seem to be getting it, I can't see the wood for the trees!
datetime EarliestSellTime = 0; : && OrderOpenTime() < EarliestSellTime
Do you really expect that you have trades opened earlier than 1970? Try:#define DATETIME_MIN 0 // 1 January, 1970 #define DATETIME_MAX D'3000.12.31' datetime EarliestSellTime = DATETIME_MAX;
return(EarliestSellTicket);
Where do you ever set that variable?
int EarliestOpenSellTicket() { datetime EarliestSellTime = TimeCurrent(); //initialize it to the current server time. You will have no orders newer than that. int EarliestSellTicket = -1; // None open. for(int pos = OrdersTotal()-1; pos >= 0 ; pos--) { if (!OrderSelect(pos, SELECT_BY_POS))return (-1); //If it can't select an order then bail out. Of course you could print something if you like. if (OrderType()!= OP_SELL)continue; //go to next iteration of loop if it's not a sell. if (OrderSymbol()!= Symbol())continue; //go to next iteration if it's not the correct pair. if (OrderMagicNumber()!= magic)continue; //go to next iteration if magic number is not the correct one. if (OrderOpenTime()>EarliestSellTime)continue; //if it's a newer order go to next iteration. It's not going to be newer than the current server time. //if it makes it past all of these filters save the time and the ticket number. EarliestSellTime = OrderOpenTime(); EarliestSellTicket = OrderTicket(); } //after the loop completes it's iterations thru all the open trades, return the ticket number of the order with the oldest time. return(EarliestSellTicket); }
I think there are as many different ways to write this as their are coders... this is just one way to do it. I just typed it into the browser so their could be some syntax errors.
In my example I use the process of elimination to get to the order we are interested in obtaining the time and most importantly... the ticket number of. Mr. Roeder makes a good point about how you are initializing your EarliestSellTime to zero. That equates to the first second of 1970 and therefore you are NEVER going to have a trade older than that. Thus it will never select an order...
He also rightly points out that, in your example, even if you were to somehow manage to select an older order, you never get the ticket number of it. It is always going to be returning the -1 to which you initialize it. In my example I simply use the current server time as a base to start from. Therefore the first order that passes the test of all the filters is going to be older than the current time. After that it will only save information about an order with an opening time older than the currently saved order time.
Keep in mind this is just way it could be written. You an put it all together into one big if statement if you like. Of course to make the 'buy order' version of this function you simply change the names of the function and variables and throw out the orders that are not buy orders.
Hope this helps.. Pip Pip... Jimdandy

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi guys,
I was hoping someone could help me out. Coding is not a strong point for me but I am trying!
I'm experimenting with basket trading and would like to try something out but I'm not sue how to do it.
Assuming I have multiple sell orders spaced by x amount. When I reach a certain number of trades in my basket, how can a close the very first sell order (in position 0). That would mean the second trade I opened as a part of my basket will become the new position 0. I would obviously need the same for a basket of buy trades.
I have a function for calculating the number of open buy or sell trades. I just don't know how to close the first trades in the series of buys or sells.
I hope I've made sense!
Some help would very much appreciated!