Avoid Re enter before Reset
Forums › ProRealTime English forum › ProBuilder support › Avoid Re enter before Reset
- This topic has 11 replies, 2 voices, and was last updated 2 years ago by Kole96.
-
-
09/16/2022 at 7:36 PM #200906
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173DEFPARAM CalculateOnLastBars = 5000// Atributs :OffsetArrow = ArrowDistance * pipsizeOffsetText = TextDistance * pipsizeOffsetFifth = FifthDistance * pipsizeOffsetDistance = ContractDistance * pipsizeOffsetEntry = EntryText * pipsizePipi = pipsizeSMA50 = Average[50](close)// Conditions for Close > Bollinger UPindicator3 = Average[20](close)+0.382*std[20](close)c7 = (close > indicator3)SMA20 = Average[20](close)// Conditions for 5 Closes above Bollinger Upc9=SUMMATION[5](c7)=5if c9 and lastsig=0 thenif FifthCandle thendrawtext("5",barindex,low-OffsetFifth,SansSerif,bold,25)coloured(0,200,0)lastsig=1endifendif//Setup Reset if closes under Boll Upif close<indicator3 thenlastsig=0endif// Conditions for Closes < Bollinger Downindicator4 = Average[20](close)-0.382*std[20](close)c8 = (close < indicator4)// Conditions for 5 Closes under Bollinger downc10=SUMMATION[5](c8)=5if c10 and lastsig2=0 thenif FifthCandle thendrawtext("5",barindex,high+OffsetFifth,SansSerif,bold,25)coloured(200,0,0)lastsig2=1endifendif//Setup Reset if closes above Boll Downif close>indicator4 thenlastsig2=0endifatr14=averagetruerange[14]// BB setuplongComparison = close >= indicator3shortComparison = close <= indicator4if longComparison thenif countlong=0 and longComparison[1] thencountlong=0elsecountlong=countlong+1endifelsecountlong=0endifif shortComparison thenif countshort=0 and shortComparison[1] thencountshort=0elsecountshort=countshort+1endifelsecountshort=0endifpvHighInRange = barssince(pivothigh)<countlongpvLowInRange = barssince(pivotlow)<countshort// Pivot setuppivotH = high[1]>high[2] and high[1]>highpivotL = low[1]<low[2] and low[1]<lowif pivotH thenpivotHigh=high[1]starthigh=barindex[1]pivottype=1endifif pivotL thenpivotLow=low[1]startlow=barindex[1]pivottype=-1endifpvHighInRange = barssince(pivothigh)<countlongpvLowInRange = barssince(pivotlow)<countshort// Entry priceepLong = (pivotHigh) + ticksizeepShort = (pivotLow) - ticksizestopLong = epShortstopShort = epLong// Target pricetargetLong = 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>0canBuy = 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 signalscontractSizeL = round(contractSizeL,2)contractSizeS = round(contractSizeS,2)atr = average[100](range)*0.25//AverageTrueRange[100](close)signal=0if canBuy and order<>1 and order <> -1 then // order <> 1 not longonmarket order <> -1 not shortonmarketdrawarrowup(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=1takeprofit=targetlongstoploss=stoplongorder = 1 //on marketorderlevel = pivothighorderbar = barindexelsif canSell and order<>-1 and order <> 1 then // order <> 1 not longonmarket order <> -1 not shortonmarketdrawarrowdown(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=-1takeprofit=targetshortstoploss=stopshortorder = -1 //on marketorderlevel = pivotloworderbar = barindexendif//if canbuy and not onmarket then//BUY 1 shares at market//ENDIF//plot the tp & sl linesif order<>0 thendrawsegment(orderbar,takeprofit,barindex,takeprofit) coloured("darkgreen") style(line,2)drawsegment(orderbar,stoploss,barindex,stoploss) coloured("red") style(line,2)endif//reset the order/tp/slif order=1 then //case longif high crosses over takeprofit or low crosses under stoploss thenorder=0drawarrowdown(barindex,high+OffsetArrow) coloured("BlueViolet")drawtext("Exit L",barindex,high+OffsetText)endifendif//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//endifif order=-1 then //case shortif low crosses under takeprofit or high crosses over stoploss thenorder=0drawarrowup(barindex,low-OffsetArrow) coloured("BlueViolet")drawtext("Exit S",barindex,low-OffsetText)endifendifColorBetween (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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © EduardoMattje//@version=5strategy("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)// Constantsvar TRANSP = 5var LONG = strategy.direction.longvar SHORT = strategy.direction.shortvar ALL = strategy.direction.allvar S_BOLLINGER = "Bollinger settings"var S_SETUP = "Setup settings"// Inputssrc = 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 BBvar 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 setuplongComparison() => close >= uppershortComparison() => close <= lowervar countLong = 0var countShort = 0incCount(count, comparison) =>if comparisonif count == 0 and comparison[1]0elsecount + 1else0countLong := incCount(countLong, longComparison())countShort := incCount(countShort, shortComparison())// Pivot setuppivotHigh = ta.pivothigh(1, 1)pivotLow = ta.pivotlow(1, 1)pivotInRange(pivot, count) => ta.barssince(pivot) < countpvHighInRange = pivotInRange(pivotHigh, countLong)pvLowInRange = pivotInRange(pivotLow, countShort)// Entry priceepLong = fixnan(pivotHigh) + syminfo.mintickepShort = fixnan(pivotLow) - syminfo.mintick// Stop pricegetRange(currentPrice, pivot, cond, tickMod) =>if condcurrentPriceelsefixnan(pivot) + syminfo.mintick * tickModvar stopLong = 0.0var stopShort = 0.0stopLong := epShortstopShort := epLong// Target pricegetTarget(stopPrice, entryPrice) =>totalTicks = (entryPrice - stopPrice) * extensionentryPrice + totalTicksvar targetLong = 0.0var targetShort = 0.0targetLong := getTarget(stopLong, epLong)targetShort := getTarget(stopShort, epShort)// Entry conditioncanBuy = countLong >= consecutiveCloses and pvHighInRange and high < epLongcanSell = countShort >= consecutiveCloses and pvLowInRange and low > epShort// Entry ordersinMarket = strategy.opentrades != 0var plotTarget = 0.0var plotStop = 0.0strategy.risk.allow_entry_in(orderDirection)if not inMarketif canBuyplotTarget := targetLongplotStop := stopLongstrategy.entry("long", strategy.long, stop=epLong, comment="Entry long")else if canSellplotTarget := targetShortplotStop := stopShortstrategy.entry("short", strategy.short, stop=epShort, comment="Entry short")elsestrategy.cancel("long")strategy.cancel("short")// Exit ordersstrategy.exit("long", "long", stop=stopLong, limit=targetLong, comment="Exit long")strategy.exit("short", "short", stop=stopShort, limit=targetShort, comment="Exit short")elsecountLong := 0countShort := 0// Trailing stopif useTrailingStop and inMarketif strategy.position_entry_name == "long"strategy.exit("long", "long", stop=stopLong, limit=plotTarget, comment="Exit long", when=stopLong > plotStop)plotStop := stopLongelsestrategy.exit("short", "short", stop=stopShort, limit=plotTarget, comment="Exit short", when=stopShort < plotStop)plotStop := stopShort// Plot exitplotCond(price) => inMarket ? price : inMarket[1] ? price[1] : naplot(plotCond(plotStop), "Stop loss", color.red, style=plot.style_linebr)plot(plotCond(plotTarget), "Target", color.teal, style=plot.style_linebr)09/16/2022 at 8:38 PM #20091009/16/2022 at 8:39 PM #20091109/16/2022 at 8:40 PM #20091209/16/2022 at 8:42 PM #20091309/16/2022 at 9:00 PM #200914I 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
09/16/2022 at 9:11 PM #200916I 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.
09/16/2022 at 9:40 PM #20091809/17/2022 at 7:24 AM #200922For 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
09/17/2022 at 10:46 AM #200930I 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.
09/17/2022 at 12:38 PM #20093509/17/2022 at 1:15 PM #200937 -
AuthorPosts