Please Help – Take only the first Cross

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #220212 quote
    Khaled
    Participant
    Veteran

    Hello everyone,

    I would like the following code to take only the first cross over or cross under, as the more we progress in the trend the more likely to have fake out.

    Can you please indicate how can I modify the code to have only the first Cross Long or Short? and how to keep the trade all the way up or down?

    Thank you.

    DEFPARAM CUMULATEORDERS = FALSE
    
    ca=0
    cv=0
    
    ATR = AverageTrueRange[PATR](close)
    
    MAF = WeightedAverage[PF](WeightedClose)
    
    GRAPHONPRICE MAF coloured("cyan") as "MAF"
    
    ca = (close crosses over MAF )
    cv = (close crosses under MAF )
    
    if time>=080000 and time<=213000 then
    IF NOT LongOnMarket AND ca THEN
    BUY 1 CONTRACTS AT MARKET
    ENDIF
    // Conditions to enter short positions
    IF NOT ShortOnMarket AND cv THEN
    SELLSHORT 1 CONTRACTS AT MARKET
    ENDIF
    
    IF LONGONMARKET and cv THEN
    SELL AT MARKET
    SELLSHORT 1 CONTRACTS AT MARKET
    ENDIF
    IF SHORTONMARKET and ca THEN
    EXITSHORT AT MARKET
    BUY 1 CONTRACTS AT MARKET
    ENDIF
    ENDIF
    
    // Stops and targets : Enter your protection stops and profit targets here
    SET TARGET PPROFIT TP
    SET STOP PLOSS SL
    
    IF TIME>=214500 THEN
    SELL AT MARKET
    EXITSHORT AT MARKET
    ENDIF
    
    #220234 quote
    robertogozzi
    Moderator
    Master

    There you go (I couldn’t test it as some variables are missing):

    DEFPARAM CUMULATEORDERS = FALSE
    ONCE FirstCrossOver  = 1
    ONCE FirstCrossUnder = 1
    IF IntraDayBarIndex = 0 THEN
    FirstCrossOver  = 1
    FirstCrossUnder = 1
    ENDIF
    
    ca=0
    cv=0
    
    ATR = AverageTrueRange[PATR](close)
    
    MAF = WeightedAverage[PF](WeightedClose)
    
    GRAPHONPRICE MAF coloured("cyan") as "MAF"
    
    ca = (close crosses over MAF )  AND FirstCrossOver
    cv = (close crosses under MAF ) AND FirstCrossUnder
    
    if time>=080000 and time<=213000 then
    IF NOT LongOnMarket AND ca THEN
    BUY 1 CONTRACTS AT MARKET
    FirstCrossOver   = 0
    ENDIF
    // Conditions to enter short positions
    IF NOT ShortOnMarket AND cv THEN
    SELLSHORT 1 CONTRACTS AT MARKET
    FirstCrossUnder = 0
    ENDIF
    
    IF LONGONMARKET and cv THEN
    SELL AT MARKET
    SELLSHORT 1 CONTRACTS AT MARKET
    ENDIF
    IF SHORTONMARKET and ca THEN
    EXITSHORT AT MARKET
    BUY 1 CONTRACTS AT MARKET
    ENDIF
    ENDIF
    
    // Stops and targets : Enter your protection stops and profit targets here
    SET TARGET PPROFIT TP
    SET STOP PLOSS SL
    
    IF TIME>=214500 THEN
    SELL AT MARKET
    EXITSHORT AT MARKET
    ENDIF
    Khaled thanked this post
    #220239 quote
    Khaled
    Participant
    Veteran

    Thank you Roberto.

    #220244 quote
    Khaled
    Participant
    Veteran

    Thank you Roberto. The code works fine for the first Signal of the day, then doesn’t take any of the other Signals in the same direction. What I would like to achieve is to take the first Signal Long until the First Signal Short happens, then take a Short until the next Signal Long happens and take Long, etc.

    What I would like to avoid is taking two consecutive Long or two consecutive Short. If I exited the first Signal Long with TP or SL and then a second Signal Long appears, I d’ont want the system to take this second Signal in the same direction of the previous one.

    I’ve added the variables optimized with backtest on 2k units on 5 min chart.

    I hope my request is clearer now.

    DEFPARAM CUMULATEORDERS = FALSE
    ca=0
    cv=0
    
    PATR = 5
    PF = 12
    TP = 9
    SL = 20
    
    GRAPHONPRICE MAF coloured("cyan") as "MAF"
    
    ONCE FirstCrossOver  = 1
    ONCE FirstCrossUnder = 1
    IF IntraDayBarIndex = 0 THEN
    FirstCrossOver  = 1
    FirstCrossUnder = 1
    ENDIF
     
    ATR = AverageTrueRange[PATR](close)
    MAF = WeightedAverage[PF](WeightedClose)
     
    ca = (close crosses over MAF)  AND FirstCrossOver
    cv = (close crosses under MAF) AND FirstCrossUnder
     
    if time>=080000 and time<=213000 then
    IF NOT LongOnMarket AND ca THEN
    BUY 1 CONTRACTS AT MARKET
    FirstCrossOver   = 0
    ENDIF
    // Conditions to enter short positions
    IF NOT ShortOnMarket AND cv THEN
    SELLSHORT 1 CONTRACTS AT MARKET
    FirstCrossUnder = 0
    ENDIF
     
    IF LONGONMARKET and cv THEN
    SELL AT MARKET
    SELLSHORT 1 CONTRACTS AT MARKET
    ENDIF
    IF SHORTONMARKET and ca THEN
    EXITSHORT AT MARKET
    BUY 1 CONTRACTS AT MARKET
    ENDIF
    ENDIF
     
    // Stops and targets : Enter your protection stops and profit targets here
    SET TARGET PPROFIT TP
    SET STOP PLOSS SL
     
    IF TIME>=214500 THEN
    SELL AT MARKET
    EXITSHORT AT MARKET
    ENDIF
    
    #220312 quote
    robertogozzi
    Moderator
    Master

    There  CANNOT be two consecutive CROSSOVERs in the same direction!

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.

Please Help – Take only the first Cross


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
Khaled @khaled Participant
Summary

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

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