-1

I'm scanning through the order list using the standard OrderSelect() function. Since there is a great function to get the current _Symbol for an order, I expected to find the equivalent for finding the timeframe (_Period). However, there is no such function.

Here's my code snippet.

...
    for (int i=orderCount()-1; i>=0; i--) {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
            if (OrderMagicNumber()==magic && OrderSymbol()==_Symbol ) j++;
            // Get the timeframe here 
        }
    }
...

Q: How can I get the open order's timeframe given it's ticket number?

In other words, how can I roll my own OrderPeriod() or something like it?

not2qubit
  • 10,014
  • 4
  • 72
  • 101
  • Always equally interesting when people down-vote without any comment or suggestion for improvement. In that sense,I will start voting in meta, that people should not be able to down-vote without adding a comment. – not2qubit Nov 18 '19 at 13:25

1 Answers1

1

There is no such function. Two approaches might be helpful here.

First and most reasonable is to have a unique magic number for each timeframe. This usually helps to avoid some unexpected behavior and errors. You can update the input magic number so that the timeframe is automatically added to it, if your input magic is 123 and timeframe is M5, the new magic number will be 1235 or something similar, and you will use this new magic when sending orders and checking whether a particular order is from your timeframe. Or both input magic and timeframe-dependent, if you need that.

Second approach is to create a comment for each order, and that comment should include data of the timeframe, e.g. "myRobot_5", and you parse the OrderComment() in order to get timeframe value. I doubt it makes sense as you'll have to do useless parsing of string many times per tick. Another problem here is that the comment can be usually changed by the broker, e.g. if stop loss or take profit is executed (and you need to analyze history), and if an order was partially closed.

One more way is to have instances of some structure of a class inherited from CObject and have CArrayObj or array of such instances. You will be able to add as much data as needed into such structures, and even change the timeframe when needed (e.g., you opened a deal at M5, you trail it at M5, it performs fine so you close part and virtually change the timeframe of such deale to M15 and trail it at M15 chart). That is probably the most convenient for complex systems, even though it requires to do some coding (do not forget to write down the list of existing deals into a file or deserialize somehow in OnDeinit() and then serialize back in OnInit() functions).

Daniel Kniaz
  • 4,338
  • 2
  • 11
  • 18
  • Hi Daniel, thanks for that quick answer. I was looking into the first two options and also decided against parsing comments, but kept the *magic* as an option. Although it also need a little bit of parsing. As for the last option, it seem too complicated, unless you have some example or template code to refer to. At the end of the day I was hoping someone would come up with a more creative and simple solution as often happens here at SE. – not2qubit Nov 15 '19 at 17:13
  • So what is the problem with the 1st approach? why you would need same magic for different experts that work on different timeframes? – Daniel Kniaz Nov 15 '19 at 17:24