Avoid Re enter before Reset

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #200906 quote
    Kole96
    Participant
    New

    Hi everyone

    i’m trying to finish this indicator, i got a lot of help from different members here to realise it.

    it’ based on Steven primo strategy. Bollinger Bands std 0.382

    i just want to avoid re entering a new a trade until the setup is no reset.

    Can you help to find the line that i have to change?

    Thank you

     

    here is a figure and the code

    DEFPARAM CalculateOnLastBars = 5000
    // Atributs :
    OffsetArrow   = ArrowDistance * pipsize
    OffsetText = TextDistance * pipsize
    OffsetFifth = FifthDistance * pipsize
    OffsetDistance = ContractDistance * pipsize
    OffsetEntry = EntryText * pipsize
    Pipi = pipsize
    SMA50 = Average[50](close)
    // Conditions for Close > Bollinger UP
    indicator3 = Average[20](close)+0.382*std[20](close)
    c7 = (close > indicator3)
    SMA20 = Average[20](close)
    // Conditions for 5 Closes above Bollinger Up
    c9=SUMMATION[5](c7)=5
    if c9  and lastsig=0 then
    if FifthCandle then
    drawtext("5",barindex,low-OffsetFifth,SansSerif,bold,25)coloured(0,200,0)
    lastsig=1
    endif
    endif
    //Setup Reset if closes under Boll Up
    if close<indicator3 then
    lastsig=0
    endif
    // Conditions for Closes < Bollinger Down
    indicator4 = Average[20](close)-0.382*std[20](close)
    c8 = (close < indicator4)
    // Conditions for 5 Closes under Bollinger down
    c10=SUMMATION[5](c8)=5
    if c10  and lastsig2=0 then
    if FifthCandle then
    drawtext("5",barindex,high+OffsetFifth,SansSerif,bold,25)coloured(200,0,0)
    lastsig2=1
    endif
    endif
    //Setup Reset if closes above Boll Down
    if close>indicator4 then
    lastsig2=0
    endif
    
    atr14=averagetruerange[14]
    // BB setup
    longComparison = close >= indicator3
    shortComparison = close <= indicator4
    
    if longComparison then
    if countlong=0 and longComparison[1] then
    countlong=0
    else
    countlong=countlong+1
    endif
    else
    countlong=0
    endif
    
    if shortComparison then
    if countshort=0 and shortComparison[1] then
    countshort=0
    else
    countshort=countshort+1
    endif
    else
    countshort=0
    endif
    pvHighInRange = barssince(pivothigh)<countlong
    pvLowInRange = barssince(pivotlow)<countshort
    // Pivot setup
    pivotH = high[1]>high[2] and high[1]>high
    pivotL = low[1]<low[2] and low[1]<low
    if pivotH then
    pivotHigh=high[1]
    starthigh=barindex[1]
    pivottype=1
    endif
    if pivotL then
    pivotLow=low[1]
    startlow=barindex[1]
    pivottype=-1
    endif
    
    
    pvHighInRange = barssince(pivothigh)<countlong
    pvLowInRange = barssince(pivotlow)<countshort
    
    // Entry price
    epLong = (pivotHigh) + ticksize
    epShort = (pivotLow) - ticksize
    
    stopLong = epShort
    stopShort = epLong
    
    // Target price
    targetLong = epLong+(abs(epLong-stopLong)*(extension/100))
    targetShort = epShort-(abs(epShort-stopShort)*(extension/100))
    contractSizeL = Risk/(epLong-stoplong)
    contractSizeS = Risk/(stopshort-epShort)
    
    
    // Entry condition
    //canBuy = countLong >= consecutiveCloses and pvHighInRange and high < epLong and epLong>0
    //canSell = countShort >= consecutiveCloses and pvLowInRange and low > epShort and epShort>0
    
    canBuy = countLong > consecutiveCloses and pvHighInRange  and epLong>0 and high crosses over epLong and orderLong and close > SMA50 and SMA20>SMA50 //modif car tradingview repaint!!
    canSell = countShort > consecutiveCloses and pvLowInRange and epShort>0 and low crosses under epShort and orderShort and close < SMA50 and SMA20<SMA50 //modif car tradingview repaint!!
    
    //trading signals
    contractSizeL = round(contractSizeL,2)
    contractSizeS = round(contractSizeS,2)
    atr = average[100](range)*0.25//AverageTrueRange[100](close)
    signal=0
    if canBuy and order<>1 and order <> -1  then // order <> 1 not longonmarket order <> -1 not shortonmarket
    drawarrowup(barindex,low-OffsetArrow) coloured("blue")
    drawtext("Long",barindex,low-OffsetText)
    drawtext(contractSizeL,barindex,low-OffsetDistance)
    drawtext(pivothigh+0.1,barindex,low-OffsetEntry)
    drawtext("▶",barindex[1],pivothigh) coloured("blue")
    signal=1
    takeprofit=targetlong
    stoploss=stoplong
    order = 1 //on market
    orderlevel = pivothigh
    orderbar = barindex
    
    elsif canSell and order<>-1 and order <> 1  then // order <> 1 not longonmarket order <> -1 not shortonmarket
    drawarrowdown(barindex,high+OffsetArrow) coloured("red")
    drawtext("Short",barindex,high+OffsetText)
    drawtext(contractSizeS,barindex,high+OffsetDistance)
    drawtext(pivotlow-0.1,barindex,high+OffsetEntry)
    drawtext("◀",barindex+1,pivotlow) coloured("red")
    signal=-1
    takeprofit=targetshort
    stoploss=stopshort
    order = -1 //on market
    orderlevel = pivotlow
    orderbar = barindex
    
    endif
    //if canbuy and not onmarket then
    //BUY 1 shares at market
    //ENDIF
    
    //plot the tp & sl lines
    if order<>0 then
    drawsegment(orderbar,takeprofit,barindex,takeprofit) coloured("darkgreen") style(line,2)
    drawsegment(orderbar,stoploss,barindex,stoploss) coloured("red") style(line,2)
    endif
    
    
    //reset the order/tp/sl
    if order=1 then //case long
    if high crosses over takeprofit or low crosses under stoploss then
    order=0
    drawarrowdown(barindex,high+OffsetArrow) coloured("BlueViolet")
    drawtext("Exit L",barindex,high+OffsetText)
    endif
    endif
    //if order=1 then // Trying to Put YES for Winning Trades
    //if high crosses over takeprofit then
    //drawtext("YES",barindex+1,takeprofit+atr*8) coloured(0,255,0)
    //endif
    //endif
    
    if order=-1 then //case short
    if low crosses under takeprofit or high crosses over stoploss then
    order=0
    drawarrowup(barindex,low-OffsetArrow) coloured("BlueViolet")
    drawtext("Exit S",barindex,low-OffsetText)
    endif
    endif
    ColorBetween (indicator3,indicator4,255,153,255,110)
    
    return indicator3 as "Upper band" coloured(128,0,92), indicator4 as "Lower band" coloured(128,0,92), SMA20 as "MA20" coloured(255,0,0)
    

     

    and here is the original code from Tradingview

     

    // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
    // © EduardoMattje
    
    //@version=5
    strategy("Steven Primo Bollinger Band", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, process_orders_on_close=true, max_labels_count=500)
    
    // Constants
    var TRANSP = 5
    var LONG = strategy.direction.long
    var SHORT = strategy.direction.short
    var ALL = strategy.direction.all
    var S_BOLLINGER = "Bollinger settings"
    var S_SETUP = "Setup settings"
    
    // Inputs
    src = math.log(input.source(close, "Price source", group=S_BOLLINGER))
    var bollingerLength = input.int(20, "Bollinger length", minval=3, group=S_BOLLINGER, inline=S_BOLLINGER)
    var mult = input.float(0.382, "Standard deviation", minval=0.0, step=0.1, group=S_BOLLINGER, inline=S_BOLLINGER)
    
    var orderDirection = input.string(LONG, "Order direction", options=[LONG, SHORT, ALL], group=S_SETUP)
    var useTrailingStop = input.bool(false, "Use trailing stop", group=S_SETUP)
    var consecutiveCloses = input.int(5, "Consecutive closes for the setup", minval=1, group=S_SETUP, inline=S_SETUP)
    var extension = input.int(100, "Extension (%)", minval=100, group=S_SETUP, inline=S_SETUP) / 100.0
    
    // Getting the BB
    [middle, upper, lower] = ta.bb(src, bollingerLength, mult)
    middle := math.exp(middle)
    upper := math.exp(upper)
    lower := math.exp(lower)
    
    // Plotting the BB
    var colorAtTheLimits = color.new(color.yellow, TRANSP)
    var colorAtTheMiddle = color.new(color.blue, TRANSP)
    
    plot(middle, "Middle band", colorAtTheMiddle, display=display.none)
    plot(upper, "Upper band", colorAtTheLimits)
    plot(lower, "Lower band", colorAtTheLimits)
    
    // MA setup
    
    
    // BB setup
    longComparison() => close >= upper
    shortComparison() => close <= lower
    
    var countLong = 0
    var countShort = 0
    
    incCount(count, comparison) =>
        if comparison
            if count == 0 and comparison[1]
                0
            else
                count + 1
        else
            0
    
    countLong := incCount(countLong, longComparison())
    countShort := incCount(countShort, shortComparison())
    
    // Pivot setup
    pivotHigh = ta.pivothigh(1, 1)
    pivotLow = ta.pivotlow(1, 1)
    
    pivotInRange(pivot, count) => ta.barssince(pivot) < count
    
    pvHighInRange = pivotInRange(pivotHigh, countLong)
    pvLowInRange = pivotInRange(pivotLow, countShort)
    
    // Entry price
    epLong = fixnan(pivotHigh) + syminfo.mintick
    epShort = fixnan(pivotLow) - syminfo.mintick
    
    // Stop price
    getRange(currentPrice, pivot, cond, tickMod) =>
        if cond
            currentPrice
        else
            fixnan(pivot) + syminfo.mintick * tickMod
    
    var stopLong = 0.0
    var stopShort = 0.0
    
    stopLong := epShort
    stopShort := epLong
            
    // Target price
    getTarget(stopPrice, entryPrice) =>
        totalTicks = (entryPrice - stopPrice) * extension
        entryPrice + totalTicks
    
    var targetLong = 0.0
    var targetShort = 0.0
    
    targetLong := getTarget(stopLong, epLong)
    targetShort := getTarget(stopShort, epShort)
    
    // Entry condition
    canBuy = countLong >= consecutiveCloses and pvHighInRange and high < epLong
    canSell = countShort >= consecutiveCloses and pvLowInRange and low > epShort
    
    // Entry orders
    inMarket = strategy.opentrades != 0
    
    var plotTarget = 0.0
    var plotStop = 0.0
    
    strategy.risk.allow_entry_in(orderDirection)
    
    if not inMarket
        if canBuy
            plotTarget := targetLong
            plotStop := stopLong
            strategy.entry("long", strategy.long, stop=epLong, comment="Entry long")
        else if canSell
            plotTarget := targetShort
            plotStop := stopShort
            strategy.entry("short", strategy.short, stop=epShort, comment="Entry short")
        else
            strategy.cancel("long")
            strategy.cancel("short")
            
        // Exit orders
        strategy.exit("long", "long", stop=stopLong, limit=targetLong, comment="Exit long")
        strategy.exit("short", "short", stop=stopShort, limit=targetShort, comment="Exit short")
    else
        countLong := 0
        countShort := 0
    
    // Trailing stop
    if useTrailingStop and inMarket
        if strategy.position_entry_name == "long"
            strategy.exit("long", "long", stop=stopLong, limit=plotTarget, comment="Exit long", when=stopLong > plotStop)
            plotStop := stopLong
        else
            strategy.exit("short", "short", stop=stopShort, limit=plotTarget, comment="Exit short", when=stopShort < plotStop) 
            plotStop := stopShort
    
    // Plot exit
    plotCond(price) => inMarket ? price : inMarket[1] ? price[1] : na
    plot(plotCond(plotStop), "Stop loss", color.red, style=plot.style_linebr)
    plot(plotCond(plotTarget), "Target", color.teal, style=plot.style_linebr)
    
    #200910 quote
    JS
    Participant
    Senior
    Hi @Kole96

    I tried to test the code but the indicator parameters are not included.

    If you want to share this information, please create an itf -file including the parameters

    #200911 quote
    Kole96
    Participant
    New
    Thank you for your answer, but there is an itf file
    #200912 quote
    JS
    Participant
    Senior

    Okay, sorry to my eyes… 😉

    #200913 quote
    Kole96
    Participant
    New
    no problem
    #200914 quote
    JS
    Participant
    Senior

    I was thinking, without immediately going very deep into your code…

    You probably know which variable(s) are causing the reset of your setup, when you check these variable(s) over the last 5 bars to make sure they are at the correct values, you can use that as an extra condition.

    If Signal = 0 and Signal[1] = 0 and Signal[2] = 0 and Signal[3] = 0 and Signal[4] = 0 then

    CheckReset = 1

    Else

    CheckReset = 0

    EndIf

    #200916 quote
    Kole96
    Participant
    New
    I need to reset the setup to get a new trade, not like what is hapenning. like i wrote in the picture, we need to have only one trade after the Setup is built, we can have an another trade in the same direction only if we close another time under the Upper Bollinger, After we look again for the Setup. You can see from line 10-25 , i have the condition to have 5 closes above the BB Up, after if we close under the BB Up, the signal is reset (lastsig=0) i need the same thing for the order entry.
    #200918 quote
    JS
    Participant
    Senior

    When it comes to order entry, can’t you check if you’ve been the last 5 bars “OnMarket”?

    #200922 quote
    Kole96
    Participant
    New
    For example for the 5 closes, once we have 5 closes , we will have the number 5 writen under the 5th candles. we will have again the number 5 writen only if we close again under the Bollinger Up and then we will have a new set of 5 closes. Now for the trade is the same, we will have one trade if we have all the conditions, we can not enter a new trade if we didn’t close again under the Bollinger Up et have a new Setup formed. Here in this code, you see that if we get out from a trade after take profit, we enter again if we cross above a new pivothigh. That’s what I want to avoid, we can enter a new trade only if the Setup is Reset. Thank you
    #200930 quote
    Kole96
    Participant
    New
    I think in “CANBUY” we have to add a condition that we have a new set of 5 Closes, it means that in the lines 151 when we exit a trade we need to put maybe that the signal=1 and “CANBUY” can be occur only if signal=0. That means that we already get one trade when all the conditions were met and now we wait for a new Setup to enter a new trade.
    #200935 quote
    JS
    Participant
    Senior
    I tried to tell you that 🙂 , check if a trade has been executed in the last five bars (OnMarket).
    #200937 quote
    Kole96
    Participant
    New
    It can not be exécuted in the first 5 bars, it has to get 5 bars and looking if we formed a pivot High in those 5 bars, if not, we wait for pivot High to form and we enter once we cross above this pivot high.
Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.

Avoid Re enter before Reset


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
Kole96 @kole96 Participant
Summary

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

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 09/16/2022
Status: Active
Attachments: 2 files
Logo Logo
Loading...