Reversal long

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #238845 quote
    SnorreDK
    Participant
    Junior

    Hi.

    Idea:

    • 4 hits within a 10-point price range (e.g., 20000, 20007, 20002, 20007).
      • Find support
    • These hits should happen within a time limit of 20 bars.
    • each candle must be within a size of 35-85 points (the difference between its high and low).
    • enter long after fourth hit

     

    ChatGTP that did not get it right. Can Anyone help me out?

    // Strategy parameters
    rangeLimit = 10  // 10-point range for hits
    barLimit = 20    // Max 20 bars to evaluate for hits
    minCandleSize = 35  // Minimum candle size in points
    maxCandleSize = 85  // Maximum candle size in points
    
    // Initialize variables
    hits = 0
    lowestPrice = high
    highestPrice = low
    entrySignal = 0
    
    // Loop through the last 20 bars to check for hits
    FOR i = 1 TO barLimit DO
    candleSize = high[i] - low[i]  // Calculate the candle size
        
    // Check if the candle size is between 35 and 85 points
    IF (candleSize >= minCandleSize AND candleSize <= maxCandleSize) THEN
    // If price hits the bottom within a 10-point range
    IF (low[i] <= lowestPrice + rangeLimit AND low[i] >= lowestPrice) THEN
    hits = hits + 1  // Count the hit
    // Update lowest and highest hits within the range
    lowestPrice = MIN(low[i], lowestPrice)
    highestPrice = MAX(low[i], highestPrice)
    ENDIF
            
    // Check the spread between the lowest and highest hits (must stay within 10 points)
    hitRange = highestPrice - lowestPrice
    IF hitRange > rangeLimit THEN
    hits = 0  // Reset hits if the spread exceeds 10 points
    BREAK
    ENDIF
    ENDIF
    NEXT
    
    // Check if 4 hits have occurred within the range
    IF hits >= 4 THEN
    entrySignal = 1  // Generate an entry signal
    ENDIF
    
    // Place a market order when an entry signal is triggered
    IF entrySignal THEN
    BUY 1 CONTRACT AT MARKET
    ENDIF
    image001-1.png image001-1.png
    #238869 quote
    SnorreDK
    Participant
    Junior

    I still need Help to do this:

    Ivan, Roberto? 🙂


    Strategy Conditions:

    Range Calculation (RangeBars):

    The strategy calculates the range of price movement (highest high minus lowest low) over  20 bars.
    The range (referred to as RangeBars) must be between 30 and 80 points for the strategy to proceed.

    Bounce Detection:

    A “bounce” refers to a candlestick where the low is within 10 points of the lowest low over the last 20 bars. This low is considered as forming or aligning with a support line.
    The strategy loops through each of the last 20 bars and counts how many of these bars qualify as “bounces.”
    Entry Condition:

    The strategy will only enter a trade if 4 or more bounces (candles near the support line) are identified within the last 20 bars.

     

    Stop Loss:

    The stop loss is set to 20 points below the lowest identified bounce (support level) within the 20-bar window.

    Exit Condition:

    The strategy will exit the position if the price closes below the 14-period Exponential Moving Average (EMA14).

    Key Parameters:

    • nBars: 20 bars used for range and bounce detection.
    • rangeMin: Minimum range of 30 points for valid trades.
    • rangeMax: Maximum range of 80 points for valid trades.
    • bounceThreshold: Threshold of 10 points within which a candle’s low must fall to be counted as a bounce (support line validation).
    • stopLossBuffer: 20 points below the lowest detected bounce (support) for setting stop loss.
    • emaPeriod: 14-period EMA used for exit condition.

    The strategy aims to identify support levels by detecting multiple bounces (candles with lows near the support line) before entering a trade. The trade is initiated only when there is enough confirmation of support in the form of multiple bounces, and the stop loss is set just below this support level to minimize risk.

    DEFPARAM CumulateOrders = False // Cumulating positions deactivated
    
    // Define parameters
    nBars = 20 // Period to calculate RangeBars
    rangeMin = 30
    rangeMax = 80
    studsThreshold = 10
    stopLossBuffer = 20
    emaPeriod = 14
    
    // EMA for exit condition
    ema14 = ExponentialAverage[emaPeriod](close)
    
    // Define the RangeBars calculation
    highestHigh = highest[nBars](high)
    lowestLow = lowest[nBars](low)
    rangeBars = highestHigh - lowestLow
    // Store the lowest low over the last nBars
    lowestLow = lowest[nBars](low)
    
    // Initialize counter for studs and tracking the lowest bounce
    countStudsar = 0
    lowestBounce = low[0] // Start value for the lowest bounce
    
    // Check if the RangeBars over the last 20 bars is within 30-80 points
    IF RangeBars >= rangeMin AND RangeBars <= rangeMax THEN
    // Loop through the last nBars to identify studs
    FOR i = 0 TO nBars-1 DO
    // Check if candle lows are within the threshold (10 points) from the lowestLow
    IF ABS(Low[i] - lowestLow) <= studsThreshold THEN
    countStudsar = countStudsar + 1
    lowestBounce = MIN(lowestBounce, Low[i]) // Update the lowest bounce
    ENDIF
    NEXT
    
    // Entry condition: Only enter if 4 or more studs have been identified
    IF countStudsar >= 4 THEN
    // Ensure no multiple entries if already on the market
    IF NOT ONMARKET THEN
    // Buy at market when entry condition is met
    BUY 1 CONTRACT AT MARKET
                
    // Set stop loss 20 points below the lowest identified bounce
    SET STOP pLOSS (lowestBounce - stopLossBuffer)
    ENDIF
    ENDIF
    ENDIF
    
    // Exit condition: Close position if price crosses below the EMA14
    IF close < ema14 THEN
    SELL AT MARKET
    ENDIF
    #238876 quote
    Iván González
    Moderator
    Master

    Hi! Beyond the logic of the system there are some things that are not right. You must include the pipsize expression in the control ranges and levels for the calculation to make sense.

    RangeBars >= rangeMin*pipsize AND RangeBars <= rangeMax*pipsize)
     ABS(Low[i] - lowestLow) <= studsThreshold*pipsize
     SET STOP pLOSS stopLossBuffer

    I hope that with these changes you can draw conclusions and see if the strategy works or not.

    #238878 quote
    robertogozzi
    Moderator
    Master

    There you go:

    DEFPARAM CumulateOrders = False // Cumulating positions deactivated
    
    // Define parameters
    nBars          = 20              //20  Period to calculate RangeBars
    BounceBars     = 4               //4   bouce periods
    rangeMin       = 10 * PipSize
    rangeMax       = 80 * PipSize
    studsThreshold = 10 * PipSize
    stopLossBuffer = 20 * PipSize
    emaPeriod      = 14
    
    // EMA for exit condition
    ema14        = ExponentialAverage[emaPeriod](close)
    
    // Define the RangeBars calculation
    highestHigh  = highest[nBars](high)
    lowestLow    = lowest[nBars](low)
    Count1       = 0
    Count2       = 0
    lowestBounce = 9999999
    FOR i = 0 TO (nBars - 1)
    IF (range[i] >= rangeMin) AND (range[i] <= rangeMax) THEN
    Count1 = Count1 + 1
    ENDIF
    IF (low[i] <> lowestLow) AND (abs(low[i] - lowestLow) <= studsThreshold) THEN
    Count2       = Count2 + 1
    lowestBounce = min(lowestBounce,low[i])
    ENDIF
    NEXT
    Rangebars = (Count1 = nBars)
    Bounce    = (Count2 >= BounceBars)
    // Initialize counter for studs and tracking the lowest bounce
    
    IF (close > ema14) AND Not OnMarket AND RangeBars AND Bounce THEN
    BUY 1 Contract at Market
    StopLoss = lowestBounce - stopLossBuffer
    SET STOP PRICE StopLoss
    ENDIF
    
    // Exit condition: Close position if price crosses below the EMA14
    IF close < ema14 THEN
    SELL AT MARKET
    ENDIF
    //
    //graphonprice StopLoss   AS "Stop Loss"   coloured("Red")
    //graphonprice TradePrice AS "Entry Price" coloured("Blue")
    //graphonprice lowestBounce
    #238880 quote
    SnorreDK
    Participant
    Junior

    I am very grateful for your help, Roberto. However, I can see that we haven’t quite reached the finish line yet.

    As you can see from the image, the range here is 145.5 points within 20 bars.
    The condition is that the range should be at least 35 points and at most 85 points.

    too-big-range.jpg too-big-range.jpg
    #238900 quote
    robertogozzi
    Moderator
    Master

    This is what you just asked:

    DEFPARAM CumulateOrders = False // Cumulating positions deactivated
    
    // Define parameters
    nBars          = 20              //20  Period to calculate RangeBars
    BounceBars     = 4               //4   bouce periods
    rangeMin       = 10 * PipSize
    rangeMax       = 80 * PipSize
    studsThreshold = 10 * PipSize
    stopLossBuffer = 20 * PipSize
    emaPeriod      = 14
    
    // EMA for exit condition
    ema14        = ExponentialAverage[emaPeriod](close)
    
    // Define the RangeBars calculation
    highestHigh  = highest[nBars](high)
    lowestLow    = lowest[nBars](low)
    myRange      = highestHigh - lowestLow
    Rangebars    = (myRange >= rangeMin) AND (myRange <= rangeMax)
    
    Count        = 0
    lowestBounce = 9999999
    IF Rangebars THEN
    FOR i = 0 TO (nBars - 1)
    IF (low[i] <> lowestLow) AND (abs(low[i] - lowestLow) <= studsThreshold) THEN
    Count        = Count + 1
    lowestBounce = min(lowestBounce,low[i])
    ENDIF
    NEXT
    ENDIF
    Bounce = (Count >= BounceBars)
    // Initialize counter for studs and tracking the lowest bounce
    
    IF (close > ema14) AND Not OnMarket AND Bounce THEN
    BUY 1 Contract at Market
    StopLoss = lowestBounce - stopLossBuffer
    SET STOP PRICE StopLoss
    ENDIF
    
    // Exit condition: Close position if price crosses below the EMA14
    IF close < ema14 THEN
    SELL AT MARKET
    ENDIF
    //
    //graphonprice StopLoss   AS "Stop Loss"   coloured("Red")
    //graphonprice TradePrice AS "Entry Price" coloured("Blue")
    //graphonprice lowestBounce
    #238928 quote
    JS
    Participant
    Senior

    Hi,

    Try this one:

    DefParam CumulateOrders=False
    
    MinBounce=4
    Bars=20
    BounceThreshold=10*PipSize
    SLBuffer=20*PipSize
    TPBuffer=20*PipSize
    EMAPeriod=14
    EMA14=ExponentialAverage[EMAPeriod](Close)
    
    RangeBarsMin=30*PipSize
    RangeBarsMax=80*PipSize
    
    HH=Highest[Bars](High)
    LL=Lowest[Bars](Low)
    RangeBars=HH-LL
    
    CheckRB=Summation[Bars](RangeBars>=RangeBarsMin and RangeBars<=RangeBarsMax)=20
    CheckCondition=Summation[20](Low<=LL+BounceThreshold)=4
    
    If CheckRB and CheckCondition and Close>EMA14 then//Bounce=MinBounce then//and Close>EMA14 then
    Buy 1 contract at Market
    SL=LL-SLBuffer
    Set Stop Price SL
    TP=HH+TPBuffer
    Set Target Price TP
    EndIf
Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.

Reversal long


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
SnorreDK @snorredk Participant
Summary

This topic contains 6 replies,
has 4 voices, and was last updated by JS
1 year, 4 months ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 10/11/2024
Status: Active
Attachments: 2 files
Logo Logo
Loading...