pinescript 指标,有时标签不显示

问题描述 投票:0回答:1

我有一个返回警报和绘图的脚本。这个想法是在低 < limit price its open position and then close on tp/sl. however, I stuck with... I don't know why but the script is not consistent. sometimes it works on 1h, but the label won't appear on 1m timeframe. if I comment the wickCond or volumCond it will work (open and close label appear), but I'm not sure where the problem really is. any help or suggestion would be appreciated

之后放置一个限制多头头寸
indicator("Volume Candle Limit Long", overlay=true)

periodo = input(20,'Period')
fator=input.float(1.25,'Proportion to average:(1,25 = 125% of average', minval=0)
up = close > open 
down= open > close
pesado = volume>(ta.ema(volume,periodo)*fator)
barcolor((pesado and up)? color.lime:(pesado and down)?color.red:up?color.silver:color.gray)

//====================================================================================================
useSL               = input(true, "Use stop Loss")
StopTrailPerc       = input.float(title="Trail Loss (%)", minval=0.0, step=0.1, defval=1) * 0.01
use_SL_Trigger      = input(true, "Use stop Loss Trigger")
StopTrailTrigger    = input.float(0.5, "SL Trigger (%)",minval=0,step=0.5) * 0.01
useTP               = input(true, "Use take profit")
ProfitTrailPerc     = input.float(0.5, "Trailing Profit (%)",minval=0,step=0.5) * 0.01
use_TP_Trigger      = input(false, "Use Take Profit Trigger")
takeProfitTrigger   = input.float(1.0, "Take Profit Trigger (%)",minval=0,step=0.5) * 0.01                              
//====================================================================================================

FiboCalc(price1, price2, level) =>
    range12 = math.abs(price1 - price2)
    if level == 1
        price1
    if level > 1
        if price1 > price2
            price1+range12*(level-1)
        else
            price2-range12*(level-1)
    if level < 1 and level > 0
        if price1 > price2
            price1-range12*(1-level)
        else
            price2+range12*(1-level)

redCandle = close < open
greenCandle = open < close

lowWick = math.min(open, close) - low
highWick = high - math.max(open, close)
body = math.max(open, close) - math.min(open, close)

candleCond = redCandle[2] and greenCandle[1] and pesado[1]
volumCond = volume[2] > volume[1]
bodyCond = body[2] < body[1]
wickCond = highWick[1] > lowWick[1] and highWick[2] < lowWick[2] and highWick[1] < body[1] and lowWick[2] < body[2]

long1 = true
long2 = true

if not candleCond
    long1:=false
    long2:=false

if not volumCond
    long1:=false
    long2:=false

if not bodyCond
    long1:=false
    long2:=false
    
if not pesado[1]
    long1 :=false
else
    long2 := false

if not wickCond
    long1:=false
    long2:=false

var float pendingOrder = close*3
short = false
long = false

var float entry_price = na
var isShort = false
var isLong = false
var isOP = false
var isPO = false

placeLimit = true
if long1 and not isOP
    pendingOrder := (open[1])
    isPO:= true
else if long2 and not isOP
    pendingOrder := FiboCalc(close[1], open[1], 0.618)
    isPO:= true
else
    placeLimit := false
    
plotshape(placeLimit? high + ta.atr(30) :na, title="limit", text='Limit', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.silver, 0), textcolor=color.new(color.white, 0))

    
// plot(entry_price)
long := low <= pendingOrder and isPO and not isOP
short := false

if short or long
    isOP := true
    if short
        isShort := true
    if long 
        isLong := true
        isPO := false
        long := true
        entry_price := pendingOrder
        
StopLossPriceTrigger = 0.0
StopLossPriceTrigger := if (use_SL_Trigger)
    if isShort
        entry_price * (1 + StopTrailTrigger) 
    else if isLong
        entry_price * (1 - StopTrailTrigger)
else
    -1

var SL_Trigger_Long_HIT = false
SL_Trigger_Long_HIT := useSL and use_SL_Trigger and isLong and low <= StopLossPriceTrigger

var SL_Trigger_Short_HIT = false
SL_Trigger_Short_HIT := useSL and use_SL_Trigger and isShort and high >= StopLossPriceTrigger

display_long_SL_trigger     = useSL and isLong and use_SL_Trigger 
 and SL_Trigger_Long_HIT == false and StopLossPriceTrigger != -1
display_short_SL_trigger    = useSL and isShort and use_SL_Trigger 
 and SL_Trigger_Short_HIT == false and StopLossPriceTrigger != -1
display_SL_trigger          = display_long_SL_trigger or display_short_SL_trigger
// Determine trail stop loss prices
longStopPrice = 0.0, shortStopPrice = 0.0

longStopPrice := if useSL and isLong
    stopValue = low * (1 - StopTrailPerc)
    math.max(stopValue, longStopPrice[1])
else
    0

shortStopPrice := if useSL and isShort
    stopValue = high * (1 + StopTrailPerc)
    math.min(stopValue, shortStopPrice[1])
else
    99999

//////////////////////////////////////////////////////////////////////////////////////////
//*** STOP LOSS HIT CONDITIONS  ***//
//////////////////////////////////////////////////////////////////////////////////////////

cond_long_stop_loss_hit  = useSL  and ta.crossunder(low, longStopPrice[1]) 
 and (SL_Trigger_Long_HIT or use_SL_Trigger == false)
cond_short_stop_loss_hit = useSL  and ta.crossover(high, shortStopPrice[1]) 

takeprofitPriceTrigger = 0.0
takeprofitPriceTrigger := if (use_TP_Trigger)
    if (isLong)
        entry_price * (1 + takeProfitTrigger) 
    else if (isShort)
        entry_price * (1 - takeProfitTrigger)
else
    -1

var TP_Trigger_Long_HIT = false
TP_Trigger_Long_HIT := useTP and use_TP_Trigger and isLong and high >= takeprofitPriceTrigger


var TP_Trigger_Short_HIT = false
TP_Trigger_Short_HIT := useTP and use_TP_Trigger and isShort and low <= takeprofitPriceTrigger


display_long_TP_trigger     = useTP and isLong  and TP_Trigger_Long_HIT == false 
 and takeprofitPriceTrigger != -1
display_short_TP_trigger    = useTP and isShort and TP_Trigger_Short_HIT == false 
 and takeprofitPriceTrigger != -1
display_TP_trigger          = display_long_TP_trigger or display_short_TP_trigger


//🔷🔷🔷
// @hugo: Will display the TP trigger as long as not hit
// once the TP trigger is hit, the TP trailing will activate
// plot(display_TP_trigger ? takeprofitPriceTrigger : na, title='takeprofitPriceTrigger', transp=0, color=color.orange, 
//  style=plot.style_cross, linewidth=3)

longTrailTP= 0.0, shortTrailTP = 0.0

// Trailing Profit
// Start trailing once trigger is reached
longTrailTP := if useTP and isLong 
    tpValue = high * (1 + ProfitTrailPerc)
    math.max(tpValue, longTrailTP[1])
else
    0

shortTrailTP := if useTP and isShort
    tpValue = low * (1 - ProfitTrailPerc)
    math.min(tpValue, shortTrailTP[1])
else
    99999

// plot(StopLossPriceTrigger)
// plot(takeprofitPriceTrigger)
// plot(longTrailTP, title='debug longTrailTP', transp=100)

// plot(SL, title='debug shortTrailTP', transp=0)
// plot(shortTrailTP, title='debug shortTrailTP', transp=100)

//////////////////////////////////////////////////////////////////////////////////////////
//*** TRAILING TAKE PROFIT HIT CONDITIONS TO BE USED IN ALERTS  ***//
//////////////////////////////////////////////////////////////////////////////////////////


//🔷🔷🔷
// @hugo: I use crossover/crossunder for the alerts to trigger the events only once
cond_long_trail_tp_hit      = useTP  and isLong  and ta.crossover(high, longTrailTP[1]) 
 and (TP_Trigger_Long_HIT or use_TP_Trigger == false)
cond_short_trail_tp_hit     = useTP  and isShort  and ta.crossunder(low, shortTrailTP[1]) 
 and (TP_Trigger_Short_HIT or use_TP_Trigger == false)
// 🔷🔷🔷
shortclose = isOP and isShort and (cond_short_trail_tp_hit or TP_Trigger_Short_HIT or cond_short_stop_loss_hit or SL_Trigger_Short_HIT)
longclose = isOP and isLong and (cond_long_trail_tp_hit or TP_Trigger_Long_HIT or cond_long_stop_loss_hit or SL_Trigger_Long_HIT)

if shortclose or longclose
    isOP := false
    if longclose
        isLong := false
    if shortclose
        isShort := false

alertcondition(long,"Long Alert")
alertcondition(short,"Short Alert")
alertcondition(longclose, "Close Long Alert")
alertcondition(shortclose, "Close Short Alert")



plotshape(short? high + ta.atr(30) * 2 :na, title="short", text='Short', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.red, 0), textcolor=color.new(color.white, 0))
plotshape(shortclose? low - ta.atr(30) :na, title='close', text='close', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.blue, 0), textcolor=color.new(color.white, 0))
plotshape(long? low - ta.atr(30) * 2 :na, title="Long", text='Long', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.green, 0), textcolor=color.new(color.white, 0))
plotshape(longclose? high + ta.atr(30)  :na, title='close', text='close', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.blue, 0), textcolor=color.new(color.white, 0))

plot( pendingOrder) 
pine-script algorithmic-trading tradingview-api pine-script-v5
1个回答
0
投票

已经通过不在绘图上使用

ta.atr(30)
来解决这个问题。取而代之的是,我再添加一行来处理这个问题:

atrSpacing = ta.atr(30)
plotshape(short? high + atrSpacing * 2 :na, title="short", text='Short', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.red, 0), textcolor=color.new(color.white, 0))
plotshape(shortclose? low - atrSpacing :na, title='close', text='close', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.blue, 0), textcolor=color.new(color.white, 0))
plotshape(long? low - atrSpacing * 2 :na, title="Long", text='Long', location=location.absolute, style=shape.labelup, size=size.tiny, color=color.new(color.green, 0), textcolor=color.new(color.white, 0))
plotshape(longclose? high + atrSpacing :na, title='close', text='close', location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.new(color.blue, 0), textcolor=color.new(color.white, 0))
© www.soinside.com 2019 - 2024. All rights reserved.