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.
//================================================
//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
You must be logged in to post a comment.
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
Hello, Thanks for sharing. Could you tell why you've chosen 3 days period for Long trade but 13 days instead for Short ?
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.
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,
Here's the performance summary of your SP500 backtested strategy on historical data from 2020 to 2025 Total Trades: 4 Winning Trades: 1 Losing Trades: 3 Win Rate: 25.0% Average P&L per Trade: -£80.89 Maximum Drawdown: -£323.55 Final Capital: £99,676.45 (starting from £100,000)