ORB только после того, как первые 4 свечи находятся в пределах диапазона 1-й свечи в сосне - PullRequest
0 голосов
/ 01 мая 2020

Я хочу написать условие, в котором, если на 15-минутном графике (например), если 2-я, 3-я и 4-я свечи находятся в пределах диапазона открытия, созданного 1-й 15-минутной свечой, то после 5-й свечи, если какая-либо свеча разбивается вне диапазона будет сработать покупка (прорыв вверх) или продажа (прорыв вниз).

У меня есть код для диапазона открытия, но я должен включить этот критерий.

//@version=4

study(title="ORB Indicator", shorttitle="ORB", overlay=true)

//User Input
showHistoricalORB = input(false, title="Show historical ORB", 
type=input.bool)
showAvg = input(false, title="Show average", type=input.bool)
orbTimeFrame = input("15", title="ORB timeframe", type=input.resolution)
sessSpec = input("0915-1530", title="Session time", type=input.session)

// Defaults
// Colors
aColor = color.gray
rColor = color.red
sColor = color.green

// Line style & Transparency
lStyle = plot.style_line
lTransp = 35

// Get High & Low
getSeries(_e, _timeFrame) => security(syminfo.tickerid, _timeFrame, _e, 
lookahead=barmerge.lookahead_on) 

is_newbar(res, sess) =>
    t = time(res, sess)
    na(t[1]) and not na(t) or t[1] < t

newbar = is_newbar("375", "0915-1530")

var float orbH = na
var float orbL = na
if newbar
    orbH := getSeries(high[0], orbTimeFrame)
    orbL := getSeries(low[0], orbTimeFrame)

orbA = (orbH + orbL)/2

// Today's Session Start timestamp
y = year(timenow)
m = month(timenow)
d = dayofmonth(timenow)

// Start & End time for Today
start = timestamp(y, m, d, 09, 15)
end = start + 86400000

// Plot only if session started
isToday = timenow > start

// Plot selected timeframe's High, Low & Avg
// Plot lines
if isToday
    _h = line.new(start, orbH, end, orbH, xloc.bar_time, 
color=color.new(rColor, lTransp))
    line.delete(_h[1])
    _l = line.new(start, orbL, end, orbL, xloc.bar_time, 
color=color.new(sColor, lTransp))
    line.delete(_l[1])
    if showAvg
        _a = line.new(start, orbA, end, orbA, xloc.bar_time, 
color=color.new(aColor, lTransp))
        line.delete(_a[1])

// Plot labels
if isToday
    l_h = label.new(start, orbH, text="High", xloc=xloc.bar_time, 
textcolor=rColor, style=label.style_none)
    label.delete(l_h[1])
    l_l = label.new(start, orbL, text="Low", xloc=xloc.bar_time, 
textcolor=sColor, style=label.style_none)
    label.delete(l_l[1])
    if showAvg
        l_a = label.new(start, orbA, text="Avg", xloc=xloc.bar_time, 
textcolor=aColor, style=label.style_none)
        label.delete(l_a[1])


plot(showHistoricalORB ? orbH : na, title=' High', color=rColor, 
transp=lTransp)
plot(showHistoricalORB ? orbL : na, title=' Low', color=sColor, 
transp=lTransp)
plot(showHistoricalORB ? showAvg ? orbA : na : na, title=' Avg', 
color=aColor, transp=lTransp)


// Display Buy & Sell signal
plotSignals = false
if showHistoricalORB
    plotSignals := true
else
    if year(time) == year(timenow) and month(timenow) == month(time) and 
dayofmonth(time) == dayofmonth(timenow) 
        plotSignals := true


//This is the point where i run into the problem of finding out if the 
2nd, 3rd, 4th bars are within the opening range of the 1st 15min bar
//After the 5th bar onwards, any bar if breaks the range, a buy or sell is 
triggered

//n_bar = bar_index

//Calc_candle(oH, oL, a)=>
//    for n_bar = 1 to barstate.islast by 1
//        var orb_break_up = 0
//        var orb_break_dn = 0
//        orb_break_up := close > oH ? 1 : na
//        orb_break_dn := close < oL ? 1 : na
//        [orb_break_up, orb_break_dn]

//[o1, o2] = Calc_candle(orbH, orbL, x)
//plotshape(plotSignals and o1 ? crossover(close, orbH) : na, 
style=shape.triangleup, location=location.belowbar, color=color.lime, 
text="Buy", textcolor=color.lime)
//plotshape(plotSignals and o2 ? crossover(orbL, close) : na, 
style=shape.triangledown, location=location.abovebar, color=color.red, 
text="Sell", textcolor=color.red)

1 Ответ

0 голосов
/ 07 мая 2020

Смотрите комментарии в коде. Вы использовали будущие данные в своих вызовах security(), и в любом случае вызов security() не был необходим, если вы хотите, чтобы значения бара открытия диаграммы:

//@version=4

study(title="ORB Indicator", shorttitle="ORB", overlay=true)

//User Input
showHistoricalORB = input(true, title="Show historical ORB", type=input.bool)
showAvg = input(false, title="Show average", type=input.bool)
sessSpec = input("0915-1530", title="Session time", type=input.session)

// Defaults
// Colors
aColor = color.gray
rColor = color.red
sColor = color.green

// Line style & Transparency
lStyle = plot.style_line
lTransp = 35

is_newbar(res, sess) =>
    t = time(res, sess)
    (na(t[1]) and not na(t)) or t[1] < t

newbar = is_newbar("D", "0915-1530")

var float orbH = na
var float orbL = na
var bars234InRange = false
var barNo = 0
// We're evaluating the condition on each bar because that's what needed for it to calculate correctly,
//  but only use its result when we are on bar 4.
last3BarsInRange = sum(min(open, close) > orbL and max(open, close) < orbH ? 1 : 0, 3) == 3
if newbar
    orbH := high
    orbL := low
    bars234InRange := false
    barNo := 1
else
    barNo := barNo + 1
    if barNo == 4
        // Bar 4 reached; check if bars 2,3 and 4 are within the range of the first.
        bars234InRange := last3BarsInRange

orbA = (orbH + orbL)/2

// Today's Session Start timestamp
y = year(timenow)
m = month(timenow)
d = dayofmonth(timenow)

// Start & End time for Today
start = timestamp(y, m, d, 09, 15)
end = start + 86400000

// Plot only if session started
isToday = timenow > start

// Plot selected timeframe's High, Low & Avg
// Plot lines
if isToday
    _h = line.new(start, orbH, end, orbH, xloc.bar_time, color=color.new(rColor, lTransp))
    line.delete(_h[1])
    _l = line.new(start, orbL, end, orbL, xloc.bar_time, color=color.new(sColor, lTransp))
    line.delete(_l[1])
    if showAvg
        _a = line.new(start, orbA, end, orbA, xloc.bar_time, color=color.new(aColor, lTransp))
        line.delete(_a[1])

// Plot labels
if isToday
    l_h = label.new(start, orbH, text="High", xloc=xloc.bar_time, textcolor=rColor, style=label.style_none)
    label.delete(l_h[1])
    l_l = label.new(start, orbL, text="Low", xloc=xloc.bar_time, textcolor=sColor, style=label.style_none)
    label.delete(l_l[1])
    if showAvg
        l_a = label.new(start, orbA, text="Avg", xloc=xloc.bar_time, textcolor=aColor, style=label.style_none)
        label.delete(l_a[1])


plot(showHistoricalORB ? orbH : na, title=' High', color=rColor, transp=lTransp)
plot(showHistoricalORB ? orbL : na, title=' Low', color=sColor, transp=lTransp)
plot(showHistoricalORB ? showAvg ? orbA : na : na, title=' Avg', color=aColor, transp=lTransp)


// Display Buy & Sell signal
plotSignals = false
if showHistoricalORB
    plotSignals := true
else
    if year(time) == year(timenow) and month(timenow) == month(time) and dayofmonth(time) == dayofmonth(timenow) 
        plotSignals := true


// Determine if long/short can occur. After one has occurred, no others will be issued until next session.
goLong = false
goShort = false
if bars234InRange and close > orbH
    goLong := true
    bars234InRange := false
else
    if bars234InRange and close < orbL
        goShort := true
        bars234InRange := false

plotshape(plotSignals and goLong ? crossover(close, orbH) : na, style=shape.triangleup, location=location.belowbar, color=color.lime, text="Buy", textcolor=color.lime)
plotshape(plotSignals and goShort ? crossover(orbL, close) : na, style=shape.triangledown, location=location.abovebar, color=color.red, text="Sell", textcolor=color.red)

// Debugging plots
plotchar(barNo, "barNo", "", location.top)
plotchar(bars234InRange, "bars234InRange", "•", location.top)
plotchar(goLong, "goLong", "▲", location.top)
plotchar(goShort, "goShort", "▼", location.top)
plotchar(time("D", "0915-1530"), 'time("D", "0915-1530")', "", location.top)
plotchar(newbar, "newbar", "►", location.top)

enter image description here

...