Strategy is closing based on wrong timeframe

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #211065 quote
    Jaanboy321
    Participant
    Average

    Hi,

    I’m trying to get the below strategy to work but its closing on the wrong timeframe.

    Sorry, but I can’t see an ‘insert PRT code’ button on the toolbar on this browser.

    For example the backtest shows trades opening when the 1minute MA50 is crossed when want I’m hoping for is for a trade to open within a minute or so of the 1hr MA50 being crossed. The same issue is happening on trade exits.  I’m hoping someone can easily spot an obvious error I’ve made! Thanks

     

    // Definition of code parameters
    DEFPARAM CumulateOrders = False // Cumulating positions deactivated
    
    //limits strategy to 1 trade per day
    Once TradeON = 1
    If IntraDayBarIndex = 0 then
    TradeON = 1
    Endif
    If OnMarket or StrategyProfit <> StrategyProfit[1] Then
    TradeON = 0
    Endif
    
    ///indicators
    TIMEFRAME(1hours)
    SD1upper = Average[20](close)+1*std[20](close)
    SD2upper = Average[20](close)+2*std[20](close)
    SD3upper = Average[20](close)+3*std[20](close)
    
    SD1lower = Average[20](close)-1*std[20](close)
    SD2lower = Average[20](close)-2*std[20](close)
    SD3lower = Average[20](close)-3*std[20](close)
    
    Av20offset = Average[20](close)*0.9995//(offset so if price gets nearly there it will trigger)
    Av50offset = Average[50](close)*0.9995
    Av200offset = Average[200](close)*0.9995
    
    Av20 = Average[20](close) //(offset so if price gets nearly there it will trigger)
    Av50 = Average[50](close)
    Av200 = Average[200](close)
    
    
    
    
    /// UPDATE BELOW UPDATE BELOW UPDATE BELOW UPDATE BELOW UPDATE BELOW UPDATE BELOW UPDATE BELOW UPDATE BELOW
    SELLCONDITION=Av50offset
    BUYCONDITION=SD3lower
    
    
    
    
    //risk reward ratio
    RRRatio=5
    
    /// UPDATE ABOVE UPDATE ABOVE UPDATE ABOVE UPDATE ABOVE UPDATE ABOVE UPDATE ABOVE UPDATE ABOVE UPDATE ABOVE
    
    TIMEFRAME(default, updateonclose)
    
    // Conditions to enter long positions
    IF high crosses over SELLCONDITION and Not OnMarket THEN //and TradeON
    SELLSHORT 0.52 PERPOINT AT MARKET
    ENDIF
    
    // Conditions to exit long positions
    IF low < BUYCONDITION THEN
    EXITSHORT AT MARKET
    ENDIF
    
    SET STOP PLOSS ((SELLCONDITION-BUYCONDITION)/RRRatio)
    #211066 quote
    PeterSt
    Participant
    Master

    Hi there,

    // Conditions to enter long positions
    IF high crosses over SELLCONDITION and Not OnMarket THEN //and TradeON
      SELLSHORT 0.52 PERPOINT AT MARKET
    ENDIF
    
    // Conditions to exit long positions
    IF low < BUYCONDITION THEN
      EXITSHORT AT MARKET
    ENDIF

     

     

    1. Buying Long goes by Buy … at Market – Selling long goes by Sell … at Market.
    2. Even if you’d have that right, the two commands as how you put them, may cancel out each other because both conditions may be true at the same time and there is no prevention against that; for that, the second condition can contain an And OnMarket.
    Jaanboy321 thanked this post
    #211067 quote
    JS
    Participant
    Senior

    Hi @Jaanboy321

    I tried your code and the positions are opened and closed according to the correct time frame…

    Jaanboy321 thanked this post
    Scherm­afbeelding-2023-03-08-om-05.42.14.png Scherm­afbeelding-2023-03-08-om-05.42.14.png
    #211154 quote
    GraHal
    Participant
    Master
    within a minute or so of the 1hr MA50 being crossed.
    At Line 14, instead of …
    TIMEFRAME(1hours)
    
    Try
    TIMEFRAME(1hour, updateonclose)
    Jaanboy321 thanked this post
    #211184 quote
    Jaanboy321
    Participant
    Average
    // Definition of code parameters
    DEFPARAM CumulateOrders = False // Cumulating positions deactivated
    
    //limits strategy to 1 trade per day
    Once TradeON = 1
    If IntraDayBarIndex = 0 then
    TradeON = 1
    Endif
    If OnMarket or StrategyProfit <> StrategyProfit[1] Then
    TradeON = 0
    Endif
    
    
    ///indicators
    TIMEFRAME(1hour) //TIMEFRAME(1hour, updateonclose)
    Donchmid = DonchianChannelCenter[50]
    
    SD1upper = Average[20](close)+1*std[20](close)
    SD2upper = Average[20](close)+2*std[20](close)
    SD3upper = Average[20](close)+3*std[20](close)
    SD4upper = Average[20](close)+4*std[20](close)
    
    SD1lower = Average[20](close)-1*std[20](close)
    SD2lower = Average[20](close)-2*std[20](close)
    SD3lower = Average[20](close)-3*std[20](close)
    SD4lower = Average[20](close)-4*std[20](close)
    
    Av20offset = Average[20](close)*1.00005 //(offset so if price gets nearly there it will trigger)
    Av50offset = Average[50](close)*1.00005
    Av200offset = Average[200](close)*1.00005
    
    Av20 = Average[20](close) //(offset so if price gets nearly there it will trigger)
    Av50 = Average[50](close)
    Av200 = Average[200](close)
    
    
    
    
    /// UPDATE BELOW          UPDATE BELOW          UPDATE BELOW         UPDATE BELOW         UPDATE BELOW          UPDATE BELOW          UPDATE BELOW          UPDATE BELOW
    BUYCONDITION=SD3lower
    SELLCONDITION=Av20
    TIMEFRAME(DEFAULT)
    
    //risk reward ratio
    RRRatio=5
    /// UPDATE ABOVE          UPDATE ABOVE          UPDATE ABOVE          UPDATE ABOVE         UPDATE ABOVE          UPDATE ABOVE          UPDATE ABOVE          UPDATE ABOVE
    
    
    
    
    
    // Conditions to enter long positions
    IF low crosses under BUYCONDITION and TradeON and Not OnMarket THEN ///and TradeON
    BUY 0.52 PERPOINT AT MARKET
    ENDIF
    
    // Conditions to exit long positions
    IF high crosses over SELLCONDITION THEN
    SELL AT MARKET
    ENDIF
    
    SET STOP PLOSS ((SELLCONDITION-BUYCONDITION)/RRRatio)
    
    Thanks everyone, much appreciated. Currently utilising this version to go long USDJPY (setting closing conditions according to the market) . Its not perfect but seems to do what I need and will experiment with your suggestions as required.
    PeterSt thanked this post
Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.

Strategy is closing based on wrong timeframe


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Jaanboy321 @jaanboy321 Participant
Summary

This topic contains 4 replies,
has 4 voices, and was last updated by Jaanboy321
2 years, 11 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 03/08/2023
Status: Active
Attachments: 1 files
Logo Logo
Loading...