PaulParticipant
Master
Hi,
It’s about the dax 30 cash €1,- at a 1 minute timeframe and the Big Line Strategy Code.
To this code I’ve made small modifications compared to the one published in the library.
The code of the Big Line Strategy, it works using a market order.
But if you use a limit order then it works correctly in a live trading environment, but not in a back test.
Tick mode on or off don’t show a difference.
So why does the back test with a limit order not work correctly?
More specific, i.e. why does the back test skip a trade at 5 September 2018 at 11.24, but the same code in a live trading environment gets correctly activated?
Maybe I missed something and hope for some feedback.
//-------------------------------------------------------------------------
// Main code : Big Line Strategy
//-------------------------------------------------------------------------
// common rules
DEFPARAM CUMULATEORDERS = false
DEFPARAM PRELOADBARS = 10000
// optional
BreakEvenStop = 0 // BreakEvenStop and BreakEvenStop Minimum Gain
MFETrailing = 1 // MFETrailing
// time rules
ONCE entertime = 090000 //
ONCE lasttime = 133000
ONCE closetime = 240000 // greater then 23.59 means it continues position overnight
ONCE closetimeFriday=173000
tt1 = time >= entertime
tt2 = time <= lasttime
tradetime = tt1 and tt2
DayForbidden = 5 // no new entry on Friday
df = dayofweek <> dayforbidden
// positionsize and stops
positionsize = 1
sl = 1.00 // % Stoploss
pt = 1.50 // % Profit Target
ts = 1.20 // % MFETrailing
bs = 1.00 // % BreakEvenStop
bsm= 0.00 // % BreakEvenStop Minimum Gain
// setup number of trades intraday
if IntradayBarIndex = 0 then
longtradecounter = 0
Shorttradecounter = 0
endif
// general criteria
GeneralCriteria = tradetime and df and (barindex > tradeindex+1)
// trade criteria
tcLong = countoflongshares < 1 and longtradecounter < 1
tcShort = countofshortshares < 1 and shorttradecounter < 1
// line criteria
CurrentPrice = round(dclose(1))
MPmod = 100
Factor = 100 // defines lines
AboveLevel0 = CurrentPrice mod MPmod
StartLevel = CurrentPrice - AboveLevel0
Level00Up = Startlevel
Level10Up = StartLevel + (1*Factor)
Level20Up = StartLevel + (2*Factor)
Level30Up = StartLevel + (3*Factor)
Level40Up = StartLevel + (4*Factor)
Level50Up = StartLevel + (5*Factor)
Level60Up = StartLevel + (6*Factor)
Level70Up = StartLevel + (7*Factor)
Level80Up = StartLevel + (8*Factor)
Level90Up = StartLevel + (9*Factor)
Level00Down = Startlevel
Levell0Down = StartLevel - (1*Factor)
Level20Down = StartLevel - (2*Factor)
Level30Down = StartLevel - (3*Factor)
Level40Down = StartLevel - (4*Factor)
Level50Down = StartLevel - (5*Factor)
Level60Down = StartLevel - (6*Factor)
Level70Down = StartLevel - (7*Factor)
Level80Down = StartLevel - (8*Factor)
Level90Down = StartLevel - (9*Factor)
l0 = close crosses over Level00Up or close crosses over Level00Down
l1 = close crosses over Level10Up or close crosses over Levell0Down
l2 = close crosses over Level20Up or close crosses over Level20Down
l3 = close crosses over Level30Up or close crosses over Level30Down
l4 = close crosses over Level40Up or close crosses over Level40Down
l5 = close crosses over Level50Up or close crosses over Level50Down
l6 = close crosses over Level60Up or close crosses over Level60Down
l7 = close crosses over Level70Up or close crosses over Level70Down
l8 = close crosses over Level80Up or close crosses over Level80Down
l9 = close crosses over Level90Up or close crosses over Level90Down
s0 = close crosses under Level00Down or close crosses under Level00Up
s1 = close crosses under Levell0Down or close crosses under Level10Up
s2 = close crosses under Level20Down or close crosses under Level20Up
s3 = close crosses under Level30Down or close crosses under Level30Up
s4 = close crosses under Level40Down or close crosses under Level40Up
s5 = close crosses under Level50Down or close crosses under Level50Up
s6 = close crosses under Level60Down or close crosses under Level60Up
s7 = close crosses under Level70Down or close crosses under Level70Up
s8 = close crosses under Level80Down or close crosses under Level80Up
s9 = close crosses under Level90Down or close crosses under Level90Up
// trade criteria extra
min1 = MIN(dhigh(0),dhigh(1))
min2 = MIN(dhigh(1),dhigh(2))
max1 = MAX(dlow(0),dlow(1))
max2 = MAX(dlow(1),dlow(2))
tcxLong = high < MIN(min1,min2)
tcxShort = low > MAX(max1,max2)
// long entry
If GeneralCriteria then
If tcLong and tcxLong then
if (l0 or l1 or l2 or l3 or l4 or l5 or l6 or l7 or l8 or l9) then
longtradecounter=longtradecounter + 1
buy positionsize contract at high[1] limit
endif
endif
endif
// short entry
If GeneralCriteria then
if tcShort and tcxShort then
if (s0 or s1 or s2 or s3 or s4 or s5 or s6 or s7 or s8 or s9) then
shorttradecounter=shorttradecounter + 1
sellshort positionsize contract at low[1] limit
endif
endif
endif
// BreakEvenStop
If BreakEvenStop then
if not onmarket then
newSL=0
endif
If longonmarket and close-tradeprice(1)>=((tradeprice/100)*bs)*pipsize then
newSL = tradeprice(1)+((tradeprice/100)*bsm)*pipsize
endif
If shortonmarket and tradeprice(1)-close>=((tradeprice/100)*bs)*pipsize then
newSL = tradeprice(1)-((tradeprice/100)*bsm)*pipsize
endif
If newSL>0 then
sell at newSL Stop
exitshort at newSL Stop
endif
endif
// MFETrailing
If MFETrailing then
trailingstop = (tradeprice/100)*ts
if not onmarket then
MAXPRICE = 0
MINPRICE = close
priceexit = 0
endif
if longonmarket then
MAXPRICE = MAX(MAXPRICE,close)
if MAXPRICE-tradeprice(1)>=trailingstop*pipsize then
priceexit = MAXPRICE-trailingstop*pipsize
endif
endif
if shortonmarket then
MINPRICE = MIN(MINPRICE,close)
if tradeprice(1)-MINPRICE>=trailingstop*pipsize then
priceexit = MINPRICE+trailingstop*pipsize
endif
endif
If onmarket and priceexit>0 then
sell at market
exitshort at market
endif
endif
// exit at closetime
If onmarket then
if time >= closetime then
sell at market
exitshort at market
endif
endif
// exit friday at set closetime
if onmarket then
if (CurrentDayOfWeek=5 and time>=closetimefriday) then
sell at market
exitshort at market
endif
endif
// build-in exit
if BreakEvenStop=0 then
SET STOP %LOSS sl
elsif BreakEvenStop=1 then
SET STOP %LOSS bsm
endif
SET TARGET %PROFIT pt
//GRAPH 0 coloured(300,0,0) AS "zeroline"
//GRAPH (positionperf*100)coloured(0,0,0,255) AS "PositionPerformance"
What fixed spread did you have set on backtest?
Maybe try 0.6 spread and run bt again (see if it makes any difference as that is what DAX usually is at 11:24 AM)?
Makes no difference, but I have got a trade opening on 5 Sep 18 10:24 (you are 1 hour ahead of me) but at 30 sec TF.
This may be a red herring, but thought I’d post it anyway as I have spent time on it.
Makes no difference, but I have got a trade opening on 5 Sep 18 10:24 (you’re timezone is 1 hour ahead of me / UK) but at 30 sec TF.
This may be a red herring, but thought I’d post it anyway as I have spent time on it.
Also I have got a trade opening on 5 Sep 18 10:24 (you are 1 hour ahead of me) but at 5 min TF.
Are you comparing live account backtest with demo account one? Or real live trading?
Mmm good point Nicolas!
My tests were on Demo Backtest.
I asked Paul this question because of the 2 different version of the backtest engine that exist in demo and live account with IG.
PaulParticipant
Master
Thanks for the help guys.
@Nicolas
I ‘am running version v10.3 – 1.8.0_45 on a Mac (last update 30 july 2018)
That’s the version which does the backtest and the live trading.
The code has 1 point spread and tickmode is activated.
@GraHal
For 30 sec TF, I got the same as you, it entered a trade.
Also on 5 minutes it entered a trade at 11.25 my time on 5 sept.
But how’ is the 1 minute 10.24 your time zone?
Of this code interesting timeframes are 1, 3 and 30 minutes with 200k backtest. I attached the screenshot. The 30min is a bit zoomed in.
Also attached a screenshot of the 1 minute bar which is has my focus in this case.
I need to understand why the trade got skipped in backtest and get’s activated correctly live. It renders the back-test useless otherwise.
ok so the order is not triggered only in the 1 minute TF, right?
PaulParticipant
Master
Correct
The order in a back test does not get triggered only on the 1-minute TF when it’s a limit order. (5 September 2018 11:24:00)
details;
Close crosses the big line at 11.23 and meets all criteria
The limit order refers to high[1] which at 11.22 is 12.100,2.
The low at 11.24 is 12.100,2
A limit high[1] means buy at 12.100,2 or lower at the 11.24 bar, which traded 12.100.2
The spread on the DAX (DFB) is normally 1.0 at the time of the trade so this is the most likely reason that the trade did not open. The prices shown on the chart are mid prices (i.e halfway between the buy and sell prices). The mid price would need to have reached 12099.7 for a buy trade to have opened at 12100.2
What about a backtest without spread?
What about a backtest without spread?
Sorry – too early and not enough coffee yet. I just re-read the posts and now see that the problem is ‘the trade got skipped in backtest and get’s activated correctly live’ and that the OP tested with a 1.0 pip spread. A back test without spread should open the trade and so would be a nice test to try.
Correct … at 0 / zero spread, I get a trade opening on Demo Backtest at 1 min at 10:24 (I am UK so 1 hour behind the OP).