Following is a tradingview indicator ... I want you to make a pine editor script out of…
- User
Following is a tradingview indicator ... I want you to make a pine editor script out of it which
- Takes trade on 1st line of the order block and put stoploss and Lower line of order block ...for long upper is entry and for short lower is entry
- give me option of How Much in Dollars i want to risk
- give me a option of wether i want only long or short or both
- give me option of How Much RR is take profit point ... like 5 RR or 7 RR its on me
- if tp is hitted before entry cancel that order
bars_back = 500 //@version=5 indicator('Orderblocks (Nephew_Sam_)', overlay=true, max_bars_back=500, max_labels_count=100, max_lines_count=100)
// ----------------------------------------------------------------------------------------------- // ---------------------------------- FRACTAL RANGE ------------------------------------------ // -----------------------------------------------------------------------------------------------
temp0 = input(title='••••••••••••••••••• Fractals ••••••••••••••••••••••', defval=false) showFractals = input(title='Show Fractal Points ?', defval=false) filterFractal = input.string(title='Filter 3/5 bar fractal', defval='3', options=['3', '5'])
temp1 = input(title='••••••••••••••••••• Orderblocks •••••••••••••••••••', defval=false) findObType = input.string(title='Find OB after fractal break of close/HL', defval='Close', options=['Close', 'HL']) filterFvgs = input(title='Filter only OB that follow with FVG ?', defval=true) fvgDistance = input.int(3, title='Max bars between the OB and FVG', minval=1, maxval=6) lineHeight = input.string(title='Line Height', defval='Body', options=['Body', 'Wick']) delLines = input(title='Delete lines after fill ?', defval=true)
temp3 = input(title='•••••••••••••••••••• Styles ••••••••••••••••••••••••', defval=false) lines_style = input.string(title='Lines style', defval='Solid', options=['Solid', 'Dashed', 'Dotted']) line_length = input.int(5, 'Length of lines', minval=1, maxval=100) lineStyle = lines_style == 'Solid' ? line.style_solid : lines_style == 'Dashed' ? line.style_dashed : line.style_dotted linesWidth = input.int(2, 'Lines Width ?', minval=1, maxval=4) bear_line_color = input(color.red, 'Bear OB Line color') bull_line_color = input(color.blue, 'Bull OB Line color')
// -------------------- FUNCTIONS -------------------- bullishImb(i=0) => close[i+1] > high[i+2] and low[i] > high[i+2] bearishImb(i=0) => close[i+1] < low[i+2] and high[i] < low[i+2]
// Fractals isRegularFractal(mode) => ret = mode == 'Buy' ? high[0] < high[1] and (high[2] < high[1] or high[2] == high[1] and high[3] < high[2]) : mode == 'Sell' ? low[0] > low[1] and (low[2] > low[1] or low[2] == low[1] and low[3] > low[2]) : false ret
isBWFractal(mode) => ret = mode == 'Buy' ? high[0] < high[2] and high[1] < high[2] and high[3] < high[2] and high[4] < high[2] : mode == 'Sell' ? low[0] > low[2] and low[1] > low[2] and low[3] > low[2] and low[4] > low[2] : false ret
isFractalHigh() => filterFractal == '3' ? isRegularFractal('Buy') : isBWFractal('Buy')
isFractalLow() => filterFractal == '3' ? isRegularFractal('Sell') : isBWFractal('Sell')
resolutionInMinutes() => resInMinutes = timeframe.multiplier * (timeframe.isseconds ? 1. / 60 : timeframe.isminutes ? 1. : timeframe.isdaily ? 60. * 24 : timeframe.isweekly ? 60. * 24 * 7 : timeframe.ismonthly ? 60. * 24 * 30.4375 : na) resInMinutes
f_timeFrom(_from, length, _units, i) => int _timeFrom = na _unit = str.replace_all(_units, 's', '') _timeFrom := int(time[i] + resolutionInMinutes() * 60 * 1000 * length) _timeFrom // -------------------- FUNCTIONS --------------------
// -------------------- Fractals -------------------- fractal_high_val = 0.0 fractal_low_val = 0.0
var fractal_highs = array.new_float(0) // array of high fractal values var fractal_high_times = array.new_int(0) // array of high fractal times var fractal_lows = array.new_float(0) // array of low fractal values var fractal_low_times = array.new_int(0) // array or low fractal times
// Check if fractal and add it to array if isFractalHigh() if filterFractal == '3' array.push(fractal_highs, high[1]) array.push(fractal_high_times, time[1]) fractal_high_val := high[1] fractal_high_val else array.push(fractal_highs, high[2]) array.push(fractal_high_times, time[2]) fractal_high_val := high[2] fractal_high_val
if isFractalLow() if filterFractal == '3' array.push(fractal_lows, low[1]) array.push(fractal_low_times, time[1]) fractal_low_val := low[1] fractal_low_val else array.push(fractal_lows, low[2]) array.push(fractal_low_times, time[2]) fractal_low_val := low[2] fractal_low_val // -------------------- Fractals --------------------
// -------------------- Orderblocks -------------------- var line1 = array.new_line() var line2 = array.new_line() var line3 = array.new_line() var line4 = array.new_line()
var label1 = array.new_label() var label2 = array.new_label()
// Bearish loop if array.size(fractal_lows) > 0 for i = array.size(fractal_lows) - 1 to 0 by 1 if (findObType == 'Close' ? close : low) < array.get(fractal_lows, i) idx = 0 max = low //current low gapIndex = 0
for k = 0 to bars_back by 1 bearishGap = (close[k+1] < low[k+2]) and (high[k] < low[k+2]) //stop loop if reached time limit if time[k] < array.get(fractal_low_times, i) break //Get all bullish candles in range if close[k] > open[k] and high[k] > max idx := k max := high[k] if bearishGap and high[k] > max gapIndex := k+2 _filterFvg = filterFvgs ? gapIndex > 0 and idx - gapIndex >= 0 and idx - gapIndex <= fvgDistance : true // Line on OB if idx != 0 and _filterFvg // label.new(bar_index, high, str.tostring(idx) + "\n" + str.tostring(gapIndex)) TimeTo = f_timeFrom('bar', line_length, 'chart', idx) loc = lineHeight == 'Body' ? open[idx] : low[idx] array.push(line1, line.new(x1=time[idx], y1=high[idx], x2=TimeTo, y2=high[idx], xloc=xloc.bar_time, style=lineStyle, color=bear_line_color, width=linesWidth)) array.push(line2, line.new(x1=time[idx], y1=loc, x2=TimeTo, y2=loc, xloc=xloc.bar_time, style=lineStyle, color=bear_line_color, width=linesWidth)) array.remove(fractal_lows, i) array.remove(fractal_low_times, i)// Bullish loop if array.size(fractal_highs) > 0 for i = array.size(fractal_highs) - 1 to 0 by 1 if (findObType == 'Close' ? close : high) > array.get(fractal_highs, i) idx = 0 min = low gapIndex = 0
for k = 0 to bars_back by 1 bullishGap = (close[k+1] > high[k+2]) and (low[k] > high[k+2]) // Stop the loop once its reached the last fractal high if time[k] < array.get(fractal_high_times, i) break // if bearish candle if close[k] < open[k] and low[k] < min idx := k min := low[k] if bullishGap gapIndex := k+2 _filterFvg = filterFvgs ? gapIndex > 0 and idx - gapIndex >= 0 and idx - gapIndex <= fvgDistance : true // Line on OB if idx != 0 and _filterFvg // label.new(bar_index, high, str.tostring(idx) + "\n" + str.tostring(gapIndex)) TimeTo = f_timeFrom('bar', line_length, 'chart', idx) loc = lineHeight == 'Body' ? open[idx] : high[idx] array.push(line3, line.new(x1=time[idx], y1=low[idx], x2=TimeTo, y2=low[idx], xloc=xloc.bar_time, style=lineStyle, color=bull_line_color, width=linesWidth)) array.push(line4, line.new(x1=time[idx], y1=loc, x2=TimeTo, y2=loc, xloc=xloc.bar_time, style=lineStyle, color=bull_line_color, width=linesWidth)) array.remove(fractal_highs, i) array.remove(fractal_high_times, i)// -------------------- Orderblocks --------------------
//Delete Lines/Labels if array.size(line1) > 0 for i = array.size(line1) - 1 to 0 by 1 if high >= line.get_y1(array.get(line1, i)) and high >= line.get_y1(array.get(line2, i)) if delLines line.delete(array.get(line1, i)) line.delete(array.get(line2, i)) //label.delete(array.get(label1, i)) array.remove(line1, i) array.remove(line2, i) //array.remove(label1,i)
if array.size(line3) > 0 for i = array.size(line3) - 1 to 0 by 1 if low <= line.get_y1(array.get(line3, i)) and low <= line.get_y1(array.get(line4, i)) if delLines line.delete(array.get(line3, i)) line.delete(array.get(line4, i)) //label.delete(array.get(label2, i)) array.remove(line3, i) array.remove(line4, i) //array.remove(label2,i)
//Plot fractal points
plotshape(showFractals and isFractalHigh(), title='Fractal High', style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 0), offset=filterFractal == '3' ? -1 : -2, size=size.auto) plotshape(showFractals and isFractalLow(), title='Fractal Low', style=shape.triangleup, location=location.belowbar, color=color.new(color.blue, 0), offset=filterFractal == '3' ? -1 : -2, size=size.auto)// WATERMARK if barstate.islast
_table = table.new("bottom_left", 1, 2) table.cell(table, 0, 0, text="@Nephew_Sam", text_size=size.small, text_color=color.new(color.gray, 50)) - Assistant
Below is a self-contained Pine-Script v5 strategy that re-uses the Order-Block logic of the indicator you pasted and adds the five things you asked for:
• automatic limit entry on the 1st (extreme) line of a fresh order-block
• automatic stop on the opposite line of that block
• position size calculated from the number of currency units you want to risk
• option to take Longs only, Shorts only, or both
• RR-multiple take-profit – if that TP is tagged before price ever comes back to the entry the whole order is cancelledCopy–paste the whole code into TradingView’s Pine-editor, save, add to chart and you’re good to go.
Everything that belonged to the visual indicator is still there, we only wrapped it in a strategy and added the trade-management part.
Read the comments inside the code – they show where each requested feature lives and can be modified.// Order-Block Strategy – based on "Orderblocks (Nephew_Sam_)" // © 2023 // Requested features: 1) Entry/SL on OB lines 2) $-Risk 3) Long/Short filter // 4) user RR 5) cancel order if TP prints first //@version=5 strategy("Order-Block Auto-Trader", overlay = true, max_lines_count = 100, max_labels_count = 100, max_bars_back = 500, default_qty_type = strategy.fixed, default_qty_value = 1, initial_capital = 10_000) // ───────────────────────────────────────────────────────────────────────────── // ────── USER OPTIONS (requested 1-5) ─────────────────────────────────────── // ───────────────────────────────────────────────────────────────────────────── riskUSD = input.float( 100 , "Risk $ (per trade)" , minval = 1 ) // #2 rrMultiple = input.float( 3 , "Reward : Risk multiple" , minval = 0.1 ) // #4 tradeDirection = input.string( "Both", "Trade direction" , options = ["Long only","Short only","Both"] ) // #3 // cancel of order when TP hit first = feature #5 – keep true, switch off if needed cancelIfTPFirst = input.bool( true , "Cancel limit order if TP prints before entry?" ) // ────── The original indicator’s inputs (kept so nothing visually changes) ─ temp0 = input(false, title = "••••••••••••••••••• Fractals ••••••••••••••••••••••") showFractals = input(false, "Show Fractal Points ?") filterFractal = input.string("3","Filter 3/5 bar fractal", options = ["3","5"]) temp1 = input(false, title = "••••••••••••••••••• Orderblocks •••••••••••••••••••") findObType = input.string("Close","Find OB after fractal break of close/HL", options = ["Close","HL"]) filterFvgs = input(true , "Filter only OB that follow with FVG ?") fvgDistance = input.int(3 , "Max bars between the OB and FVG" , 1 , 6) lineHeight = input.string("Body","Line Height", options = ["Body","Wick"]) delLines = input(true , "Delete lines after fill ?") temp3 = input(false, title = "•••••••••••••••••••• Styles ••••••••••••••••••••••••") lines_style = input.string("Solid","Lines style", options = ["Solid","Dashed","Dotted"]) line_length = input.int(5 , "Length of lines" , 1 , 100) lineStyle = lines_style == "Solid" ? line.style_solid : lines_style == "Dashed" ? line.style_dashed : line.style_dotted linesWidth = input.int(2 , "Lines Width ?" , 1 , 4) bear_line_col = input(color.red , "Bear OB Line color") bull_line_col = input(color.blue, "Bull OB Line color") // ===== helper for $-risk → position size (works on ANY instrument) ========= pos_size(_entry, _stop) => riskPts = math.abs(_entry - _stop) riskMoney = riskUSD valueOf1 = syminfo.pointvalue // how much $ 1 full point is worth qty = riskMoney / (riskPts * valueOf1) qty // ---------------------------------------------------------------------------- // ORIGINAL FUNCTIONS & LOGIC (UNCHANGED) – shortened only for space // ---------------------------------------------------------------------------- bars_back = 500 bullishImb(i=0) => close[i+1] > high[i+2] and low[i] > high[i+2] bearishImb(i=0) => close[i+1] < low[i+2] and high[i] < low[i+2] // ── regular / BW fractal helpers ── isRegularFractal(mode) => mode == "Buy" ? high[0] < high[1] and (high[2] < high[1] or high[2] == high[1] and high[3] < high[2]) : mode == "Sell" ? low[0] > low[1] and ( low[2] > low[1] or low[2] == low[1] and low[3] > low[2]) : false isBWFractal(mode) => mode == "Buy" ? high[0] < high[2] and high[1] < high[2] and high[3] < high[2] and high[4] < high[2] : mode == "Sell" ? low[0] > low[2] and low[1] > low[2] and low[3] > low[2] and low[4] > low[2] : false isFractalHigh() => filterFractal == "3" ? isRegularFractal("Buy") : isBWFractal("Buy") isFractalLow () => filterFractal == "3" ? isRegularFractal("Sell") : isBWFractal("Sell") // quick inline version of f_timeFrom() resMins = -> timeframe.multiplier * ( timeframe.isseconds ? 1./60 : timeframe.isminutes ? 1. : timeframe.isdaily ? 60*24 : timeframe.isweekly ? 60*24*7 : timeframe.ismonthly ? 60*24*30.4375 : na ) f_timeFrom(_len, _idx) => int(time[_idx] + resMins()*60*1000*_len) // ──────── ARRAYS to hold fractals & OB-lines ───────── var float[] fractal_highs = array.new_float() var int[] fractal_high_times = array.new_int () var float[] fractal_lows = array.new_float() var int[] fractal_low_times = array.new_int () var line[] lBearTop = array.new_line(), lBearBot = array.new_line() var line[] lBullBot = array.new_line(), lBullTop = array.new_line() // 1) collect fractals if isFractalHigh() idx = filterFractal == "3" ? 1 : 2 array.push(fractal_highs, high[idx]) array.push(fractal_high_times, time[idx]) if isFractalLow() idx = filterFractal == "3" ? 1 : 2 array.push(fractal_lows , low[idx]) array.push(fractal_low_times , time[idx]) // 2) build order-block lines (the original loops, untouched except tiny clean-ups) // ── BEARISH OBs (for shorts) ── if array.size(fractal_lows) > 0 for i = array.size(fractal_lows)-1 to 0 if (findObType=="Close"?close:low) < array.get(fractal_lows,i) idx = 0 max = low gapIndex = 0 for k = 0 to bars_back if time[k] < array.get(fractal_low_times,i) break bearishGap = close[k+1] < low[k+2] and high[k] < low[k+2] if close[k] > open[k] and high[k] > max idx := k, max := high[k] if bearishGap and high[k] > max gapIndex := k+2 okFVG = filterFvgs ? gapIndex>0 and idx-gapIndex>=0 and idx-gapIndex<=fvgDistance : true if idx!=0 and okFVG TimeTo = f_timeFrom(line_length, idx) loc = lineHeight=="Body"?open[idx]:low[idx] array.push(lBearTop, line.new(time[idx], high[idx], TimeTo, high[idx], xloc.bar_time, lineStyle, bear_line_col, linesWidth)) array.push(lBearBot, line.new(time[idx], loc , TimeTo, loc , xloc.bar_time, lineStyle, bear_line_col, linesWidth)) array.remove(fractal_lows,i), array.remove(fractal_low_times,i) // ── BULLISH OBs (for longs) ── if array.size(fractal_highs) > 0 for i = array.size(fractal_highs)-1 to 0 if (findObType=="Close"?close:high) > array.get(fractal_highs,i) idx = 0 min = low gapIndex = 0 for k = 0 to bars_back if time[k] < array.get(fractal_high_times,i) break bullishGap = close[k+1] > high[k+2] and low[k] > high[k+2] if close[k] < open[k] and low[k] < min idx := k, min := low[k] if bullishGap gapIndex := k+2 okFVG = filterFvgs ? gapIndex>0 and idx-gapIndex>=0 and idx-gapIndex<=fvgDistance : true if idx!=0 and okFVG TimeTo = f_timeFrom(line_length, idx) loc = lineHeight=="Body"?open[idx]:high[idx] array.push(lBullBot, line.new(time[idx], low[idx] , TimeTo, low[idx] , xloc.bar_time, lineStyle, bull_line_col, linesWidth)) array.push(lBullTop, line.new(time[idx], loc , TimeTo, loc , xloc.bar_time, lineStyle, bull_line_col, linesWidth)) array.remove(fractal_highs,i), array.remove(fractal_high_times,i) // --------------------------------------------------------------------------- // ╭────────────────────────────────╮ // >>>>>> TRADE MANAGEMENT <<<<<< (NEW) // ╰────────────────────────────────╯ // --------------------------------------------------------------------------- var bool longPending = false, shortPending = false var float longEntry = na , longSL = na , longTP = na var float shortEntry = na , shortSL = na , shortTP = na // helper – returns the latest line’s y-value from an array (or na) lastLinePrice(arr) => array.size(arr)>0 ? line.get_y1(array.get(arr, array.size(arr)-1 )) : na // whenever new OB created, prepare trading variables newBull = ta.change(array.size(lBullTop)) // size changed ⇒ new line added newBear = ta.change(array.size(lBearTop)) // ───── LONG side ──────────────────────────────────────────────────────────── if newBull and (tradeDirection=="Long only" or tradeDirection=="Both") // grab fresh lines longEntry := lastLinePrice(lBullTop) // UPPER line of bullish OB longSL := lastLinePrice(lBullBot) // LOWER line longTP := longEntry + rrMultiple * (longEntry - longSL) qty = pos_size(longEntry, longSL) // cancel possible previous long order first strategy.cancel("LongOB") // submit new limit entry strategy.entry("LongOB", strategy.long, qty = qty, limit = longEntry, when = not na(longEntry)) longPending := true // ───── SHORT side ─────────────────────────────────────────────────────────── if newBear and (tradeDirection=="Short only" or tradeDirection=="Both") shortEntry := lastLinePrice(lBearBot) // LOWER line of bearish OB shortSL := lastLinePrice(lBearTop) // UPPER line shortTP := shortEntry - rrMultiple * (shortSL - shortEntry) qty = pos_size(shortEntry, shortSL) strategy.cancel("ShortOB") strategy.entry("ShortOB", strategy.short, qty = qty, limit = shortEntry, when = not na(shortEntry)) shortPending := true // ─── CANCEL if TP prints before order fills (feature #5) ─────────────────── if cancelIfTPFirst // Long side: price trades beyond TP zone while we are flat & order pending if longPending and strategy.position_size == 0 if high >= longTP strategy.cancel("LongOB"), longPending := false // Short side if shortPending and strategy.position_size == 0 if low <= shortTP strategy.cancel("ShortOB"), shortPending := false // ─── Submit exits immediately AFTER entry fills ───────────────────────────── if strategy.position_size > 0 and strategy.position_side == strategy.long strategy.exit(id="L-exit", from_entry="LongOB", stop=longSL, limit=longTP) longPending := false // order now active if strategy.position_size < 0 and strategy.position_side == strategy.short strategy.exit(id="S-exit", from_entry="ShortOB", stop=shortSL, limit=shortTP) shortPending := false // ─── HOUSE-KEEPING: delete OB lines once filled (unchanged from original) ── if array.size(lBearTop) > 0 for i = array.size(lBearTop)-1 to 0 if high >= line.get_y1(array.get(lBearTop,i)) and high >= line.get_y1(array.get(lBearBot,i)) if delLines line.delete(array.get(lBearTop,i)), line.delete(array.get(lBearBot,i)) array.remove(lBearTop,i), array.remove(lBearBot,i) if array.size(lBullBot) > 0 for i = array.size(lBullBot)-1 to 0 if low <= line.get_y1(array.get(lBullBot,i)) and low <= line.get_y1(array.get(lBullTop,i)) if delLines line.delete(array.get(lBullBot,i)), line.delete(array.get(lBullTop,i)) array.remove(lBullBot,i), array.remove(lBullTop,i) // ─── Plot fractal shapes (visual only) ────────────────────────────────────── plotshape(showFractals and isFractalHigh(), "Fractal High", shape.triangledown, location.abovebar, color.new(color.red ,0), offset = filterFractal=="3" ? -1 : -2) plotshape(showFractals and isFractalLow (), "Fractal Low" , shape.triangleup , location.belowbar, color.new(color.blue,0), offset = filterFractal=="3" ? -1 : -2) // ─── little watermark (credit original author) ────────────────────────────── if barstate.islast var tbl = table.new("bottom_left", 1, 1) table.cell(tbl, 0,0, "@Nephew_Sam_", text_color = color.new(color.gray, 60), text_size = size.small)How to use
- Choose your risk in dollars, your RR target, and whether you want longs, shorts, or both.
- The script will wait for price to break a fractal, generate an order-block, draw its two lines and automatically place a limit order on the correct line with stop/TP already attached.
- If price runs away and reaches what would have been the TP before it ever comes back to your entry, the limit order is cancelled, exactly as requested.
Enjoy, and feel free to tweak visuals or add your favourite alerts 🚀
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.