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:
IF MyConditions AND Not OnMarket THEN
BUY 1 Contract at Market
SELL AT close + (3 * pipsize) LIMIT
SET STOP pLOSS 50
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:
TIMEFRAME(5 minute,UpdateOnClose)
MyConditions = .........
IF MyConditions AND Not OnMarket THEN
BUY 1 Contract at Market
SET STOP pLOSS 50 //better to always set a SL
SET TARGET pPROFIT 10 //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)
IF close >= (TradePrice + 3 * pipsize) THEN //exit when desired profit is reached
SELL AT MARKET
ENDIF
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?
if myconditions then
buy 1 contract at market
set target pprofit 3
endif
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.