I am trying to find the first open price from several open orders and symbols. Below is my initial attempt. Need help getting this to work.
I'm not sure that I understand what you want to do because your code seems far, far more complex than is necessary from your description. If you want to find the open price of the earliest order for a given symbol, type, and magic number, then the following should be sufficient:
double FindFirstOrderPrice(int magic, string Symb, int Type) { double Price = 0; datetime EarliestOrder = D'2099/12/31'; for (int i = 0; i < OrdersTotal(); i++) { if (OrderSelect(i, SELECT_BY_POS)) { if (OrderType() == Type && OrderSymbol() == Symb && OrderMagicNumber() == magic) { if (EarliestOrder > OrderOpenTime()) { EarliestOrder = OrderOpenTime(); Price = OrderOpenPrice(); } } } } return Price; // Returns 0 if no matching orders }
Even if I have misunderstood what you are trying to do, I still have two comments:
- You seem to be trying to return a price as an int when you should be returning it as a double
- The term "first" is ambiguous. For example, let's say that you place two orders, A and B, and then do a partial close of A. In MT4 you will then have orders B and C, where C is reported as having a later ticket number but earlier open-time than B. It's a matter of personal taste whether you consider that C is earlier than B. The above code assumes that you want to select orders by time. Your current code looks as though it picks the order with the earliest ticket.
I believe I did not explain my need well enough. I am trading on a FXCM-US account which requires that trades are closed using FIFO rules (First In First Out). I have open 5 orders each of 5 different pairs ( for a total of 25 open orders). My goal is to close all 25 orders when an AccountProfit() target is reached. Under FIFO I will need to sort and order all the pairs based on the opening time and close the trades based on the oldest open time and ticket. I was intending to find the first open price and the use a loop to close all other trades.
I'm not sure that I understand what you want to do because your code seems far, far more complex than is necessary from your description. If you want to find the open price of the earliest order for a given symbol, type, and magic number, then the following should be sufficient:
Even if I have misunderstood what you are trying to do, I still have two comments:
- You seem to be trying to return a price as an int when you should be returning it as a double
- The term "first" is ambiguous. For example, let's say that you place two orders, A and B, and then do a partial close of A. In MT4 you will then have orders B and C, where C is reported as having a later ticket number but earlier open-time than B. It's a matter of personal taste whether you consider that C is earlier than B. The above code assumes that you want to select orders by time. Your current code looks as though it picks the order with the earliest ticket.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I am trying to find the first open price from several open orders and symbols. Below is my initial attempt. Need help getting this to work.