买单执行是当前蜡烛的限价单,而不是下一根蜡烛

时间:2021-06-15 12:43:50

标签: pine-script

存在下一个蜡烛图执行买单的问题。我想在当前蜡烛上的线上准确下订单,准确地在标签所在的位置。

//@version=4
strategy("VWAP Pyramiding", overlay = true, precision = 6,
     initial_capital = 100000, currency = currency.USD, default_qty_type = strategy.percent_of_equity, default_qty_value = 16.66, pyramiding = 6,
     commission_type = strategy.commission.percent, commission_value = 0.02)

// —————————— Constants {

// ————— Color constants
var color C_COLOR1      = #42F5E3ff
var color C_COLOR2      = #38C7B8ff
var color C_COLOR3      = #3790BDff
var color C_COLOR4      = #375BBDff
var color C_COLOR5      = #8137BDff
var color C_COLOR6      = #BF2647ff
// }

// —————————— Inputs {

bool    i_showVWAP = input(true, "Show VWAP")

float   i_vwap1 = input(-7.3, "VWAP 1")
bool    i_buy1 = input(true, "Buy line")
bool    i_sell1 = input(false, "Sell line")

float   i_vwap2 = input(-8.7, "VWAP 2")
bool    i_buy2 = input(true, "Buy line")
bool    i_sell2 = input(false, "Sell line")

float   i_vwap3 = input(-10, "VWAP 3")
bool    i_buy3 = input(true, "Buy line")
bool    i_sell3 = input(false, "Sell line")

float   i_vwap4 = input(-4, "VWAP 4")
bool    i_buy4 = input(false, "Buy line")
bool    i_sell4 = input(true, "Sell line")

float   i_vwap5 = input(1, "VWAP 5")
bool    i_buy5 = input(false, "Buy line")
bool    i_sell5 = input(true, "Sell line")

float   i_vwap6 = input(4, "VWAP 6")
bool    i_buy6 = input(false, "Buy line")
bool    i_sell6 = input(true, "Sell line")
// }

// —————————— Functions {

// ————— Function for a repainting/non-repainting version of the HTF data
f_security(_sym, _res, _src, _rep) => security(_sym, _res, _src[not _rep and barstate.isrealtime ? 1 : 0])[_rep or barstate.isrealtime ? 0 : 1]

// ————— Function to check a strategy signal
f_isSignal(_src, _vwapRate, _down) =>
    _condition = _down ? _src < _vwapRate : crossunder(_src, _vwapRate)
    _condition and (barssince(_condition[1]) > 10 or na(barssince(_condition[1])))
// }

// —————————— Calculations {

start = f_security(syminfo.tickerid, "D", time, false)

newSession = iff(start > start[1], 1, 0)

vwapsum = 0.0
volumesum = 0.0
vwapsum := iff(newSession[1], hlc3 * volume, vwapsum[1] + hlc3 * volume)
volumesum := iff(newSession[1], volume, volumesum[1] + volume)
vwap_now = vwapsum / volumesum

vwap1 = vwap_now + vwap_now / 100 * i_vwap1
vwap2 = vwap_now + vwap_now / 100 * i_vwap2
vwap3 = vwap_now + vwap_now / 100 * i_vwap3
vwap4 = vwap_now + vwap_now / 100 * i_vwap4
vwap5 = vwap_now + vwap_now / 100 * i_vwap5
vwap6 = vwap_now + vwap_now / 100 * i_vwap6
// }

// —————————— Plots {

plot(i_showVWAP ? vwap_now : na, title = "VWAP", color = color.white)

plot(vwap1, title = "VWAP 1", color = C_COLOR1, linewidth = 2)
plot(vwap2, title = "VWAP 2", color = C_COLOR2, linewidth = 2)
plot(vwap3, title = "VWAP 3", color = C_COLOR3, linewidth = 2)
plot(vwap4, title = "VWAP 4", color = C_COLOR4, linewidth = 2)
plot(vwap5, title = "VWAP 5", color = C_COLOR5, linewidth = 2)
plot(vwap6, title = "VWAP 6", color = C_COLOR6, linewidth = 2)

tier1Buy = f_isSignal(low, vwap1, true) and i_buy1
tier2Buy = f_isSignal(low, vwap2, true) and i_buy2
tier3Buy = f_isSignal(low, vwap3, true) and i_buy3
tier4Buy = f_isSignal(low, vwap4, true) and i_buy4
tier5Buy = f_isSignal(low, vwap5, true) and i_buy5
tier6Buy = f_isSignal(low, vwap6, true) and i_buy6
tier1Sell = f_isSignal(low, vwap1, false) and i_sell1
tier2Sell = f_isSignal(low, vwap2, false) and i_sell2
tier3Sell = f_isSignal(low, vwap3, false) and i_sell3
tier4Sell = f_isSignal(low, vwap4, false) and i_sell4
tier5Sell = f_isSignal(low, vwap5, false) and i_sell5
tier6Sell = f_isSignal(low, vwap6, false) and i_sell6
    
plotshape(tier1Buy ? vwap1 : na, style = shape.labelup, text = "Buy 1", location = location.absolute, color = color.new(color.green, 0), textcolor = color.new(color.white, 0))
plotshape(tier2Buy ? vwap2 : na, style = shape.labelup, text = "Buy 2", location = location.absolute, color = color.new(color.green, 0), textcolor = color.new(color.white, 0))
plotshape(tier3Buy ? vwap3 : na, style = shape.labelup, text = "Buy 3", location = location.absolute, color = color.new(color.green, 0), textcolor = color.new(color.white, 0))
plotshape(tier4Buy ? vwap1 : na, style = shape.labelup, text = "Buy 4", location = location.absolute, color = color.new(color.green, 0), textcolor = color.new(color.white, 0))
plotshape(tier5Buy ? vwap2 : na, style = shape.labelup, text = "Buy 5", location = location.absolute, color = color.new(color.green, 0), textcolor = color.new(color.white, 0))
plotshape(tier6Buy ? vwap3 : na, style = shape.labelup, text = "Buy 6", location = location.absolute, color = color.new(color.green, 0), textcolor = color.new(color.white, 0))

plotshape(tier1Sell ? vwap1 : na, style = shape.labeldown, text = "Sell 1", location = location.absolute, color = color.new(color.red, 0), textcolor = color.new(color.white, 0))
plotshape(tier2Sell ? vwap2 : na, style = shape.labeldown, text = "Sell 2", location = location.absolute, color = color.new(color.red, 0), textcolor = color.new(color.white, 0))
plotshape(tier3Sell ? vwap3 : na, style = shape.labeldown, text = "Sell 3", location = location.absolute, color = color.new(color.red, 0), textcolor = color.new(color.white, 0))
plotshape(tier4Sell ? vwap4 : na, style = shape.labeldown, text = "Sell 4", location = location.absolute, color = color.new(color.red, 0), textcolor = color.new(color.white, 0))
plotshape(tier5Sell ? vwap5 : na, style = shape.labeldown, text = "Sell 5", location = location.absolute, color = color.new(color.red, 0), textcolor = color.new(color.white, 0))
plotshape(tier6Sell ? vwap6 : na, style = shape.labeldown, text = "Sell 6", location = location.absolute, color = color.new(color.red, 0), textcolor = color.new(color.white, 0))
// }

// —————————— Strategy Logic {

if tier1Buy or tier2Buy or tier3Buy or tier4Buy or tier5Buy or tier6Buy
    strategy.entry("Buy", strategy.long)

if tier1Sell or tier2Sell or tier3Sell or tier4Sell or tier5Sell or tier6Sell
    strategy.close("Buy")
// }

enter image description here

0 个答案:

没有答案