Hello!
I want to identify both last BUY and SELL order ticket to calculate their current profit and add more positions after X profit.
I have tried several ways but I don't know why it's not working. I have tried printing the ticket variable out but the ticket number is always 0
Here is what I have tried so far
Anyone know why it's not working?
Also, I'm new to coding
Appreciate any ideas/suggestions
Welcome to the wonderful world of programming. It's often the case that you will ask for constructive criticism, and in the interest of succinctness the response can seem as if they are attacking you, but take it as a sign of respect that someone would take their time to help you. With that being said your code could use a lot of work. You should study some C++ style guides and clean up your indentation and variable naming conventions. For example, CapWords are usually reserved for class type defs. Regarding your logic I think that you may be over-complicating it. In order to get the most recent orders you just need to loop through the order pool and assign the ticket number to a single variable if the time is greater than all previous orders evaluated before it. This is an example of how one might approach the challenge.
void OnStart() { if(OrderSelect(most_recently_opened(OP_BUY, 0, MODE_HISTORY), SELECT_BY_TICKET)) printf("Most recent: Ticket=%d, Time=%s", OrderTicket(), TimeToString(OrderOpenTime()) ); } int most_recently_opened(int order_type, int magic=0, int pool=MODE_TRADES) { int total = pool == MODE_TRADES ? OrdersTotal() : OrdersHistoryTotal(); int ticket = -1; datetime order_time = 0; for(int i = total - 1; i >= 0; i--) { if(OrderSelect(i, SELECT_BY_POS, pool) && OrderSymbol() == _Symbol && (magic == 0 || OrderMagicNumber() == magic) && OrderType() == order_type && OrderOpenTime() > order_time ){ order_time = OrderOpenTime(); ticket = OrderTicket(); } } return ticket; }
Thank you for your advice @nicholi shen I will surely try to get into programming as it's seems fun and challenging for me!
As for my problem, you even take the time to give me an example, really appreciate your help!
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello!
I want to identify both last BUY and SELL order ticket to calculate their current profit and add more positions after X profit.
I have tried several ways but I don't know why it's not working. I have tried printing the ticket variable out but the ticket number is always 0
Here is what I have tried so far
Anyone know why it's not working?
Also, I'm new to coding
Appreciate any ideas/suggestions