M15 SP500 LowBuyHighSell Strategy

Viewing 15 posts - 1 through 15 (of 47 total)
  • Author
    Posts
  • #223398 quote
    phoentzs
    Participant
    Master

    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
    
    M15-SP500-LBHS.jpg M15-SP500-LBHS.jpg M15-SP500-LBHS_2.jpg M15-SP500-LBHS_2.jpg 0-1-M15-SP-LowBuyHighSell.itf
    #223402 quote
    GraHal
    Participant
    Master

    Thank you for sharing your code.

    What time are overnight fees triggered in Germany?  In UK it is for trades open at 22:00.

    Your exit times of if time >= 220000 would allow overnight fees to be triggered in UK.

    #223403 quote
    phoentzs
    Participant
    Master

    Feel free to make improvements to the code and adjust the time. If you set TimeEx to 0 in line 29, the result changes only minimally. In theory, you even get money if you hold the SP500 overnight. It is a short-term strategy. Little time on market. There are not many positions that are held overnight. I also tried to add another exit with a bigger profit. But this variant works very reliably. And that’s what I wanted. Short time on the market and “reliability”.

    #223404 quote
    phoentzs
    Participant
    Master

    Also works great on NASDAQ. I haven’t tried Dow Jones yet.

    #223407 quote
    GraHal
    Participant
    Master

    As a matter of interest then … what time are overnight fees triggered in Germany?

    I’m just interrested if it is 22:00 same as UK even though Germany is currently UTC+1 and UK is currently UTC?

    I can’t see German IG website to find out, maybe you can?

    Whenever I have checked on UK IG re overnight fees it awlays says 22:00 so I guess it is 22:00 all year round regardless of the hour going forward or back (as now).

    Maybe UTC+1  countries are the same at 22:00 all year round … anybody can answer?

    End of hijacking your thread 🙂

    Your strategy shows a good equity curve on DJI 1 Hour timeframe (no changes except minus 1 hour for UK on the UTC +1 times and I use spread = 5 for DJI when backtesting).

    #223409 quote
    phoentzs
    Participant
    Master

    As far as I know, overnight fees always apply from 10 p.m. Here in Germany. Can you post a picture of Dow Jones? How big is the drawdown?

    #223410 quote
    GraHal
    Participant
    Master

    good equity curve on DJI 1 Hour timeframe

    I forget how I work is not same as most … above is only over 10k bars.

    Nearly blows the Account (£100k) at pos size = 1 over 100k bars so no point me posting an equity curve for 10K bars?

    #223412 quote
    phoentzs
    Participant
    Master

    Optimize lines 39 and 57 together and lines 40 and 62.

    #223418 quote
    phoentzs
    Participant
    Master

    @Admin

    @Roberto

    Can you test and approve my contribution to the library?

    #223435 quote
    Philstrading
    Participant
    Senior

    When backtesting u need to allow for varying spreads throughout the day, testing on 0.5 spread when spread can be 2 until US open wont give accurate results.

    Can have some drawdown… would need some time to look more closely, but spreads can make a significant difference in strategy success when testing….

    #223445 quote
    MauroPro
    Participant
    Veteran

    Hi Phoentzs, interesting code. I rewrote the code in the simplest form and added only a certain percentage to the exit. Up to 50k the results are similar, then are better.

    
    //SP "lowBuy-highSell" v2 15m [spread 0.5]  //TS by Phoentzs
    //https://www.prorealcode.com/topic/m15-sp500-lowbuyhighsell-strategy/
    //*********************************************************************************
    defParam cumulateOrders = false
    defParam preLoadBars = 10000
    defParam flatAfter = 220000
    cTime = time >= 080000 and time <= 210000
    positionSize = 5
    //-----------------------------------------------------------------------
    once maxOrdersL = 1                    //max-Orders per Day
    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
    //------------------------------------------------------------
    timeframe(1 day, updateOnClose)
    //*********************************
    periodL = 3
    periodS = 13
    multiplierL = 0.4
    multiplierS = 1
    //*********************************
    dailyHigh = high
    dailyClose = close
    dailyLow  = low
    dailyOpen = open
    dailyLowX  = lowest[periodL](low)
    dailyHighX = highest[periodS](high)
    areaDaily = dailyHigh - dailyLow
    //--------------------------------------------------------------
    timeframe(15 minutes)
    cLong = close < dailylowX
    cShort = close crosses under dailyHighX
    if cTime and cLong and ordersCountL < maxOrdersL then
    buy positionSize contracts at market
    set stop loss areaDaily * multiplierL
    endif
    if longOnMarket and close > dailyClose and positionPerf*100 > 0.1 then  // exit: gain > 0.1%
    sell at market
    endif
    //----------------------
    if cTime and cShort and ordersCountS < maxOrdersS then
    sellshort positionSize contracts at market
    set stop loss areaDaily * multiplierS
    endif
    if shortOnMarket and close < dailyClose and positionPerf*100 > 0.3 then // exit: gain > 0.3%
    exitShort at market
    endif
    //---------------------------------------------------------------------------------------
    graphOnPrice dailyLowX coloured("red")
    graphonprice dailyClose coloured("black")
    graphOnPrice dailyHighX coloured("green")
    //---------------
    once equity     = 0
    once performanceLastMonth = 0
    if month <> month[1] then
    performanceLastMonth = strategyProfit - equity
    equity     = strategyProfit
    endif
    graph performanceLastMonth
    Image-001.jpg Image-001.jpg
    #223448 quote
    phoentzs
    Participant
    Master

    Thanks @Mauropro, great work with you as always.

    I initially used “set target Price myclose”. But the exit brings a little more power. This system in combination with a breakout system should be enough for a portfolio.

    #223539 quote
    JohnScher
    Participant
    Veteran

    Here is the 200k backtest. The post-corona-volatility seems to be doing the system a lot of good.
    Thanks for sharing.

    Screenshot_6.jpg Screenshot_6.jpg
    #223558 quote
    phoentzs
    Participant
    Master

    Es ist wichtig, sich daran zu erinnern, dass wir hier über das SP500 sprechen. Vor fünf Jahren lag diese noch bei rund 2000 Punkten, die Volatilität war also sicherlich deutlich geringer. Wir arbeiten auf einen Lückenschluss hin, der auch heute noch manchmal nur wenige Punkte umfasst. Aber es scheint sicher, dass die Bewegung sehr oft auftreten wird, und wir können uns dieses Prinzip zunutze machen. Selbst wenn wir “highest and lowest” im Code auf “1” setzen, funktioniert das System trotzdem. Lediglich der Drawdown nimmt leicht zu und die Kurve ist etwas zackig. Probieren Sie das System an der Nasdaq aus… 😉

    f1_maik thanked this post
    #223612 quote
    fifi743
    Participant
    Master

    Test with a spread of 0.6 as this is the minimum at IG and the maximum is 1.4
    Added buy and sell conditions

     

     

    //SP "lowBuy-highSell" v2 15m [spread 0.5]  //TS by Phoentzs
    //https://www.prorealcode.com/topic/m15-sp500-lowbuyhighsell-strategy/
    //*********************************************************************************
    defParam cumulateOrders = false
    defParam preLoadBars = 10000
    defParam flatAfter = 220000
    cTime = time >= 080000 and time <= 210000
    positionSize = 1
    //-----------------------------------------------------------------------
    once maxOrdersL = 1                  //max-Orders per Day
    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
    //------------------------------------------------------------
    timeframe(1 day, updateOnClose)
    //*********************************
    periodL = 3
    periodS = 13
    multiplierL = 0.4
    multiplierS = 1
    //*********************************
    dailyHigh = high
    dailyClose = close
    dailyLow  = low
    dailyOpen = open
    dailyLowX  = lowest[periodL](low)
    dailyHighX = highest[periodS](high)
    areaDaily = dailyHigh - dailyLow
    //--------------------------------------------------------------
    timeframe(15 minutes)
    
    cLong = close < dailylowX and open>low and high>close and close-open>10
    cShort = close crosses under dailyHighX and high>open and close>low
    if cTime and cLong and ordersCountL < maxOrdersL then
    buy positionSize contracts at market
    set stop loss areaDaily * multiplierL
    endif
    if longOnMarket and close > dailyClose and positionPerf*100 > 0.1 then  // exit: gain > 0.1%
    sell at market
    endif
    //----------------------
    if cTime and cShort and ordersCountS < maxOrdersS then
    sellshort positionSize contracts at market
    set stop loss areaDaily * multiplierS
    endif
    if shortOnMarket and close < dailyClose and positionPerf*100 > 0.3 then // exit: gain > 0.3%
    exitShort at market
    endif
    //---------------------------------------------------------------------------------------
    graphOnPrice dailyLowX coloured("red")
    graphonprice dailyClose coloured("black")
    graphOnPrice dailyHighX coloured("green")
    //---------------
    once equity     = 0
    once performanceLastMonth = 0
    if month <> month[1] then
    performanceLastMonth = strategyProfit - equity
    equity     = strategyProfit
    endif
    graph performanceLastMonth
    
    KumoNoJuzza thanked this post
    Capture-decran-2023-11-12-171621.png Capture-decran-2023-11-12-171621.png Capture-decran-2023-11-12-171808.png Capture-decran-2023-11-12-171808.png
Viewing 15 posts - 1 through 15 (of 47 total)
  • You must be logged in to reply to this topic.

M15 SP500 LowBuyHighSell Strategy


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
phoentzs @phoentzs Participant
Summary

This topic contains 46 replies,
has 11 voices, and was last updated by nonetheless
2 years ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 11/09/2023
Status: Active
Attachments: 17 files
Logo Logo
Loading...