Hello, I have recently been screwed over by an algorithm for a reason that I cannot determine, I wonder whether anyone can help shed some light on the situation…
I am running an algorithm on both my live and demo accounts, mostly because I forgot to stop it on my demo account. When I backtest this algorithm it displays a small loss this morning, a large gain yesterday and a handful of other trades over the past few weeks. By contrast, the algorithm running on my demo account did not make any trades at all in the last couple of days, and the algorithm running on my live account made a big loss on a trade that ran both yesterday and today.
Is the platform broken? Why would a demo account and a live account make different trades using the same algorithm? Why would the backtest, even when running in tick-by-tick mode, show trades that didn’t happen, or not show trades that did happen? I am very confused about this whole thing, I know that a backtest isn’t 100% perfect but over 2 days it should be fairly accurate, right? Not showing a £300 gain when I actually made a £244 loss? Is there anything I can do to fix this that I might be missing?
I have attached the algorithm that I have been running on 30m GBP/USD. Disclaimer, I did not write it, I just ran a decent amount of tests against it and had it in a demo account for a few months.
I have not read your strategy code yet, but attached is a non-exhaustive list of the elements that can impact a live trading strategy and create differences with a demo account and/or backtests, that could help you:
- Spread
- Slippage
- Orders rejections due to one of the above reason, but also because of the allowed distance from current price to put pending orders (known as “minimal distance”)
- Different trading hours (ProOrder code launched in a different time zone / custom hours, by the user)
- Coding problem: division by zero error, null or negative periods for indicators, ..
- Lack of responsiveness of IG demo servers (if IG is the broker), although this has improved considerably since last year.
- Make backtests without tick-by-tick option
- “set stop trailing” instruction that give IG the total control of your stoploss, can be moved differently between accounts due to points above
- Limited risk accounts and their rules
- Guaranteed stoploss rules and fees
- Starting a strategy at a different time (1 hour or even 1 minute later): depending on the code of the strategy, the results of some calculations could be different.
- Margin required on the trading account (no demo or backtest tests are made on this subject)
- Overnight and overweekend fees
Because backtests are only tested on history *with no connection to live market* , you may encounter differences with real live trading environment subject to spread enlargement, slippage, etc. If your stop hasn’t move, there must be information of an error into your orders rejected list you can consult with CTRL+O.
IG orders list history may be also helpful!
Here is the full code for convenient read:
// Definizione dei parametri del codice
DEFPARAM CumulateOrders = False
TimeStart = time >= 000000
TimeStop = time <= 240000
// Le giornate come 1 maggio, 24, 25, 26, 30 e 31 dicembre, i sabati e le domeniche sono esclusi
GiornoTrading= NOT((Month = 5 AND Day = 1) OR (Month = 12 AND (Day = 24 OR Day = 25 OR Day = 26 OR Day = 30 OR Day = 31) OR OpenDayOfWeek = 6 OR OpenDayOfWeek = 0))
ONCE SIZE = 2 // nr. contratti
ONCE NDBARLIMIT = 3 // NR. OF BARS ON WHICH THE ORDER STOP REMAINS VALID
ONCE PXT = Pointsize // es. per EURUSD = 0.00001; per EURJPY = 0.001 per DAX = 1
AA = period // PERIOD OF LINEAR REGRESSION AND 1° PERIOD OF STOCHASTIC --> OTTIMIZZARE FROM 20 TO 300 STEP 10
SSTOC = SmoothedStochastic[AA,AA/4](Close)
SUT = SuperTrend[3,10]
// TREND - NO TREND
t=(LinearRegressionSlope[AA](close)-0)*SQRT(AA-2)/(STE[AA](close)/STD[AA](Barindex))
if t<1.96 then
beta=0
else
beta=1
endif
// Condizioni per entrare long
C1 = SSTOC[1] <= 30 AND SSTOC[0] > SSTOC[1] AND beta = 0 // No Trend = SWING
IF C1 then
OPENBUY = HIGH[0]+ 2*PXT
MYINDEX = Barindex
ENDIF
IF Barindex >= MYINDEX + NDBARLIMIT THEN
OPENBUY = 0
ENDIF
IF OPENBUY > 0 AND NOT LONGONMARKET AND GiornoTrading and TimeStart and TimeStop THEN
BUY SIZE CONTRACTS AT OPENBUY STOP
// si fissano stoploss e targetprofit
ST=((LOWEST[10](LOW))-OPENBUY)/PXT
IF ST > 150 OR ST <=0 THEN // Max stoploss in point
ST = 150
ENDIF
SET STOP pLOSS ST
ENDIF
// Condizioni per uscire da posizioni Long
IF LongOnMarket AND (Close[1]>SUT[1] AND Close<SUT) then // change color of supertrend = close position
SELL at Market
ENDIF
// Condizioni per entrare short
C21 = SSTOC[1] >= 70 AND SSTOC[0] < SSTOC[1] AND beta = 0 // No Trend = SWING
IF C21 then
OPENSELL = LOW[0]- 2*PXT
MYINDEX = Barindex
ENDIF
IF Barindex >= MYINDEX + NDBARLIMIT THEN
OPENSELL = 0
ENDIF
IF OPENSELL > 0 AND NOT SHORTONMARKET AND GiornoTrading and TimeStart and TimeStop THEN
SELLSHORT SIZE CONTRACTS AT OPENSELL STOP
// si fissano stoploss e targetprofit
ST=(OPENSELL-(HIGHEST[10](HIGH)))/PXT
IF ST > 150 OR ST <= 0 THEN
ST = 150
ENDIF
SET STOP pLOSS ST
ENDIF
//Condizioni per uscire da posizioni Short
IF ShortOnMarket AND (Close[1]<SUT[1] AND Close>SUT) then // change color of supertrend = close position
EXITSHORT at Market
ENDIF
Try to change line 13 with:
SSTOC = SmoothedStochastic[AA,round(AA/4)](Close)
to prevent period with decimal!