Here is a strategy on the SP500, which in its raw version is already effective long and short. With similar settings the code is also effective on other indexes.
It is a mixture of gap close and mean reversion. We buy intraday below a low from the daily chart. We are selling above a high from the daily chart. The exit is the close of the previous day. So theoretically a gap closure. We exit open positions every evening and are flat overnight. A simple stoploss based on the daily range is also included in the algo. That’s all. Improvements and/or thoughts on this algo are always welcome.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
//================================================ //SP500 //Spread 0.5 DEFPARAM CUMULATEORDERS = false defparam preloadbars = 10000 //Risk Management PositionSize = 5 //Max-Orders per Day/////////////////////////////////////// once maxOrdersL = 1 once maxOrdersS = 1 if intradayBarIndex = 0 then //reset orders count ordersCountL = 0 ordersCountS = 0 endif if longTriggered then //check if an order has opened in the current bar ordersCountL = ordersCountL + 1 endif if shortTriggered then //check if an order has opened in the current bar ordersCountS = ordersCountS + 1 endif /////////////////////////////////////// once TimeEx = 1 once TimeEx2 = 1 once Exit = 1 timeframe(1day,updateonclose) myhigh = high myclose= close mylow = low myopen = open mylowX = lowest[3](low) //1-20 myhighX = highest[13](high)//1-20 timeframe(default) area = myhigh-mylow long = close < mylowX and ordersCountL < maxOrdersL //crosses over short= close crosses under myhighX and ordersCountS < maxOrdersS // trading window ONCE BuyTime = 080000 ONCE SellTime = 210000 // position management IF Time >= BuyTime AND Time <= SellTime THEN If long Then Buy PositionSize CONTRACTS AT market SET STOP LOSS area*0.4 ENDIF If short Then sellshort PositionSize CONTRACTS AT market SET STOP LOSS area*1 ENDIF endif graphOnPrice mylowX coloured("Red") graphonprice myclose coloured("Black") graphOnPrice myhighX coloured("green") if longonmarket and close > myclose and Exit=1 then sell at market endif if shortonmarket and close < myclose and Exit=1 then exitshort at market endif //TimeEx if (time >= 220000 and TimeEx=1)then sell at market exitshort at market endif if time >= 220000 and dayofweek=5 and TimeEx2=1 then sell at market exitshort at market endif |
Share this
No information on this site is investment advice or a solicitation to buy or sell any financial instrument. Past performance is not indicative of future results. Trading may expose you to risk of loss greater than your deposits and is only suitable for experienced investors who have sufficient financial means to bear such risk.
ProRealTime ITF files and other attachments :PRC is also on YouTube, subscribe to our channel for exclusive content and tutorials
Very nice. I optimised the main variables over different years, then forward tested and back tested. It seems to be quite robust in most conditions.
Something I added that made sense to me, was a time of day to stop opening any new positions. I set this to 150000 (UTC), which allows time for trades to play out before the end of the day.
It also seemed to be more profitable in most conditions to carry the trade overnight. It’s nice to close out at the end of day but since we’re only in the market around 6% of the time and we carry over even less than that, the overnight funding costs are relatively low.
Thanks for sharing, this is an interesting strategy,
I was impressed by the robustness of the strategy. You can really only optimize individual years or even quarters and then test over the rest of the time… You don’t have any big negative surprises, which I really like. I chose the time window from 8:00 a.m. to 9:00 p.m., i.e. the maximum so as not to over-optimize. You can certainly reduce the time window if there are improvements. You can also increase the number of trades per day… but then the drawdown will be more uncomfortable.
Hello,
Thanks for sharing. Could you tell why you’ve chosen 3 days period for Long trade but 13 days instead for Short ?
Bonjour,
merci pour ce code.
j’ai juste un problème sur le code, j’ai un message “L’instruction “Graph” ne peut pas être utilisée en trading automatique” en le passant en réel, alors que en backtest cela fonctionne.
Quelqu’un sais comment résoudre le problème? merci.
Il faut supprimer cette ligne qui est utile uniquement pour visualiser des valeurs durant les backtests.
Super merci Nicolas cela fonctionne. Top
Which is the timezone’s setting of USA SP 500? I’ve differents results and I don’t know why… sorry
I have runit in the UTC+2 TZ and got matching results
Hey @banjoo78,
maybe its because of your License for US Indices?
Because I have the same probplem. If I backtest the strategy at S&P 15 minutes the strategy loses money. I dont have the full license package, so I can just choose Micro-E-mini S&P500 Only1224, Micro-E-mini S&P500 Only0325, Micro-E-mini S&P500 Full0924 (NG), Micro-E-mini S&P500 Only0925 (NG).
Maybe the is the problem?
Best regards
Bader