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)