I have an issue backtesting a strategy where I buy at a certain time for example 12:00, and I want to sell when I’m up 3 points. I’s not uncommon that this happens for a brief period within the candlestick, but in those cases the positions doesn’t seem to close (in the backtest). Instead the sell ordered is performed when the next candle starts, at whatever price the instrument is at then. Isn’t the tick-by-tick mode designed for this very purpose? Because it doesn’t seem to make a difference at all for my backtest.
You can use a pending order, once entered, to exit after 3 pips:
1
2
3
4
5
IFMyConditionsANDNotOnMarketTHEN
BUY1ContractatMarket
SELLATclose+(3*pipsize)LIMIT
SET STOPpLOSS50
ENDIF
but it’s very unlikely the broker allows to enter a pending order that close to the current price!
If you use a, say, 5-minute TF, you could take advantage of the Muti Time Frame support and check your profit on a low TF, say 10-second TF, and exiting at market instead of placing a pending order:
1
2
3
4
5
6
7
8
9
10
11
TIMEFRAME(5minute,UpdateOnClose)
MyConditions=.........
IFMyConditionsANDNotOnMarketTHEN
BUY1ContractatMarket
SET STOPpLOSS50//better to always set a SL
SET TARGETpPROFIT10//10 pips should meet the broker's distance requiremnents for most of the day
ENDIF
TIMEFRAME(default)//be it 1-minute or any lower TF (provided 5-minute is a multiple of it, for instance a 16-second TF cannot have 5-minute as a multiple)
IFclose>=(TradePrice+3*pipsize)THEN//exit when desired profit is reached
Code is always read at candle close, so your SELL instruction is therefore read and executed one bar later if your 3 points condition is fulfilled. Tick by tick is used to test price level intra candle.
Despite the excellent code that Roberto has shared, why not just using a target profit of 3 points directly attached to your order?
Ah, I may have been a bit stupid and forgotten about the pprofit function. It seems to do the trick. 🙂 Thanks a lot guys for your input and the clarification of the tick-by-tick functionality.