Coding a limit order

 

I am a long-time C# coder, and a new (still paper trading) Forex trader, and new to this forum, so please forgive me if this is a RTFM question or if I should have asked this somehow/somwhere else. I have created a fairly simple EA to begin my framework for creating orders. I am testing to ensure that my EA adheres to all the rules for creating an order. I am currently running it against the EURUSD from 2012.02.27 - 2012.03.02 using 5 minute bars. My EA creates a Short Limit Order on 02.27 @ 12:50 with an Open Price of 1.34062. The next bar opens at this exact price which also ends up being the high of that bar. The bar has a low of 1.34007. I would expect that somewhere in that bar OR even in the next bar which is still moving below my original Open Price, the order would get opened, but it never does. The chart travels all the way down to 1.33658 before reversing and eventually, 46 bars later, the order opens once it has climbed back to the original open price and then is stopped out at the original stoploss price a bar later. I have done some experimenting and found that if I open a Stop Order instead of a Limit Order, the order opens normally. I have a few questions:

1) Is it coincidental, or am I correct in observing that the order doesn't open because the bar opens and/or hits the high at the open price? What rule am I violating so that I can change my code to adhere properly?

2) The problem with a Limit Order is that though I give the EA the price to open at, it may not open immediately (as I believe it shouldn't until the open price is hit and/or accepted), so I can't code to see if the order doesn't open as a Limit Order, to try opening it as a Stop Order?

I thought about checking each bar to see if a pending limit order should have opened, deleting it and opening a stop order, but in that case I have lost (at least) 5 minutes worth of movement which doesn't seem very efficient and should I try to run against say a 1 hour bar, would be even less efficient.

Thanks in advance.

- Jim

 
 
  1. A SELL_LIMIT should open at the open price or HIGHER. A SELL_STOP should open at the open price or LOWER.
  2. Why it didn't open I can only guess: You created a order price of 1.34062000001 so the limit wasn't reached.
    double NormalizePrice(double p, string pair=""){
        // https://www.mql5.com/en/forum/135345 zzuegg reports for non-currency DE30:
        // MarketInfo(chart.symbol,MODE_TICKSIZE) returns 0.5
        // MarketInfo(chart.symbol,MODE_DIGITS) return 1
        // Point = 0.1
        // Prices to open must be a multiple of ticksize
        if (pair == "") pair = Symbol();
        double ts = MarketInfo(pair, MODE_TICKSIZE)
        return( MathRound(p/ts) * ts );
    }
    

  3. Stops and Limits are nice for humans as you don't need to be there to press the sell button. For for EA's it's watching each tick, it can just wait for price to reach and then open. Only reasons to use limit/stop is to minimize slippage and/or open execution time such as during news events.
  4. Short Limit Order on 02.27 @ 12:50 with an Open Price of
    @ 12:50 ?
 
WHRoeder:
  1. A SELL_LIMIT should open at the open price or HIGHER. A SELL_STOP should open at the open price or LOWER.
  2. Why it didn't open I can only guess: You created a order price of 1.34062000001 so the limit wasn't reached.
  3. Stops and Limits are nice for humans as you don't need to be there to press the sell button. For for EA's it's watching each tick, it can just wait for price to reach and then open. Only reasons to use limit/stop is to minimize slippage and/or open execution time such as during news events.
  4. Short Limit Order on 02.27 @ 12:50 with an Open Price of
    @ 12:50 ?


Sorry, @ 12:50 (PM) - bar time.

Thanks for your insights. I will probably look into market orders as opposed to limit orders.

 
JimTrader75:


Sorry, @ 12:50 (PM) - bar time.

Then what is 02.27
 
WHRoeder:
Then what is 02.27
27th Feb
 
RaptorUK:
27th Feb
Doh! I was thinking time for both. (Stateside we don't write dates with dots.)
Reason: