首页 > 解决方案 > 难以重置值

问题描述

我是新来的第一次发帖!我有一个包含 3 个指标的策略:Supertrend、Dziwne 和 200 SMA。有几件事我似乎无法做到。

  1. 我的第一次购买触发器正确激活,但在我第一次卖出之前我不想要任何其他购买触发器。
  2. 卖出也是一样,我不想要第二次卖出或第三次或第四次卖出,直到我先得到另一个买入。
  3. 我认为他们不断触发的原因之一是因为我无法在买入或卖出后重置这 4 个变量:entry_price、stop_loss、stop_distance 和 take_profit。

我已经设定了大部分时间都有效的买卖条件,但整个事情都是不稳定的。您能给我的任何帮助将不胜感激。

这是我所追求的策略:

这是我的完整代码:

//@version=4
// Copyright (c) 2019-present, Alex Orekhov (everget)
study("Selah SuperTrend", overlay=true)

//-------- Variables -----------



//---------------------------------------
// 200 SMA
sma200 = sma(close, 200)
plot(sma200, title="200 SMA", color = color.new(#FFFFFF, transp=0), linewidth=2)

//```````````````````````````````````````````````````````````````````````````

length = input(title="ATR Period", type=input.integer, defval=9, group = "SuperTrend")
mult = input(title="ATR Multiplier", type=input.float, step=0.1, defval=3.9)

atr = mult * atr(length)

longStop = hl2 - atr
longStopPrev = nz(longStop[1], longStop)
longStop := close[1] > longStopPrev ? max(longStop, longStopPrev) : longStop

shortStop = hl2 + atr
shortStopPrev = nz(shortStop[1], shortStop)
shortStop := close[1] < shortStopPrev ? min(shortStop, shortStopPrev) : shortStop

dir = 1
dir := nz(dir[1], dir)
dir := dir == -1 and close > shortStopPrev ? 1 : dir == 1 and close < longStopPrev ? -1 : dir


longColor = color.green
shortColor = color.red


//plot
plot(dir == 1 ? longStop : na, title="ST Green Line", style=plot.style_linebr, linewidth=2, color=longColor)
//plotshape(dir == 1 and dir[1] == -1 ? longStop : na, title="ST Buy", style=shape.labelup, location=location.absolute, size=size.normal, text="Buy", textcolor=color.new(color.white,transp=0), color = color.new(color.green, transp = 0))

plot(dir == 1 ? na : shortStop, title="ST Red Line", style=plot.style_linebr, linewidth=2, color=shortColor)
//plotshape(dir == -1 and dir[1] == 1 ? shortStop : na, title="ST Sell", style=shape.labeldown, location=location.absolute, size=size.normal, text="Sell", textcolor=color.new(color.white, transp=0), color = color.new(color.red, transp = 0))

barcolor(dir == 1 ? color.new(color.green, transp = 0) : color.new(color.red,transp = 0), title="ST Bar Color")

alertcondition(dir == 1 and dir[1] == -1 ? longStop : na, title="ST Buy1", message="")
alertcondition(dir == 1 and dir[1] == -1 ? longStop : na, title="ST Buy2", message="")
alertcondition(dir == 1 and dir[1] == -1 ? longStop : na, title="ST Buy3", message="")
alertcondition(dir == -1 and dir[1] == 1 ? shortStop : na, title="ST Sell", message="")



//————————————————————————————————————————————————————————————————————————————
//——————————————————————————— Auteur | © Dziwne ——————————————————————————————
//————————————————————————————————————————————————————————————————————————————

//————————————————————————————————————————————————————————————————————————————
//—————————————————————————————— I. Paramètres ———————————————————————————————
//————————————————————————————————————————————————————————————————————————————

ema = input(defval=52, minval=1, title="EMA Length", group="Dziwne Line")
ema_smooth = input(defval=10, minval=1, title="EMA Length (Smoothing)")

colors = input(title = "Color Scheme", defval="Classic", options=["Classic", "Dziwne Color Scheme"])
show_line = input(true, "Show Lines")
show_hl = input(false, "Show High & Low cloud")

//————————————————————————————————————————————————————————————————————————————
//——————————————————————————— II.1. Calcules, EMA ————————————————————————————
//————————————————————————————————————————————————————————————————————————————

o = ema(open, ema)
c = ema(close, ema)
h = ema(high, ema)
l = ema(low, ema)

//————————————————————————————————————————————————————————————————————————————
//————————————————————————— II.2. Calcules, Heikin Ashi ——————————————————————
//————————————————————————————————————————————————————————————————————————————

ha_t = heikinashi(syminfo.tickerid)

ha_o = security(ha_t, timeframe.period, o)
ha_c = security(ha_t, timeframe.period, c)
ha_h = security(ha_t, timeframe.period, h)
ha_l = security(ha_t, timeframe.period, l)

//————————————————————————————————————————————————————————————————————————————
//————————————————— II.3. Calcules, EMA (Réduction du bruit) —————————————————
//————————————————————————————————————————————————————————————————————————————

ha_o_smooth = ema(ha_o, ema_smooth)
ha_c_smooth = ema(ha_c, ema_smooth)
ha_h_smooth = ema(ha_h, ema_smooth)
ha_l_smooth = ema(ha_l, ema_smooth)

dif_ha_oc_smooth = ha_c_smooth - ha_o_smooth

//————————————————————————————————————————————————————————————————————————————
//————————————————— II.4. Calcules, Définition des couleurs ——————————————————
//————————————————————————————————————————————————————————————————————————————

couleur_positive = colors == "Classic" ? #26A69A : #FFBE3D
couleur_negative = colors == "Classic" ? #EF5350 : #D63031

couleur_trend = dif_ha_oc_smooth <= 0 ? couleur_negative : couleur_positive

couleur_show_line_positive = show_line ? couleur_positive : na
couleur_show_line_negative = show_line ? couleur_negative : na
couleur_show_line_trend = show_line ? couleur_trend : na

couleur_show_hl = show_hl ? #808080 : na

//————————————————————————————————————————————————————————————————————————————
//———————————————————————————— III. Mise en place ————————————————————————————
//————————————————————————————————————————————————————————————————————————————

o_line = plot(ha_o_smooth, color=couleur_show_line_trend, title="Dziwne Open Line")
c_line = plot(ha_c_smooth, color=couleur_show_line_trend, title="Dziwne Close Line")
h_line = plot(ha_h_smooth, color=color.new(color.blue, transp=0), title="Dziwne High Line")
l_line = plot(ha_l_smooth, color=couleur_show_line_negative, title="Dziwne Low Line")

fill(o_line, c_line, color=color.new(couleur_trend, transp=50), title="Dziwne Open/Close Trendcloud")
fill(h_line, l_line, color=color.new(couleur_show_hl, transp=80), title="Dziwne High/Low Trendcloud")

//————————————————————————————————————————————————————————————————————————————
//—————————————————————————————————— FIN —————————————————————————————————————
//————————————————————————————————————————————————————————————————————————————
    /***************** LONG SETUP *******************

dziwneGreen = dif_ha_oc_smooth > 0
stgreenLine = dir == 1
dziwneRed = dif_ha_oc_smooth <= 0
stredLine = dir == -1

dzw_high_value = ha_h_smooth[0]
dzw_low_value = ha_l_smooth[0]
entry_price = float(0)
stop_loss = float(0)
stop_distance = float(0)
take_profit = float(0)
var buyTrig = false
var sellTrig = false
price_above_sma = close > sma200
Price_above_dziwne_high = close > dzw_high_value
bought = false
sold = false
b_count = false

//*****************************

// These are the conditions for first buy
buyTrig := dziwneGreen and stgreenLine and price_above_sma //and Price_above_dziwne_high  

// Did we trigger a buy? If yes, bought is now true otherwise it’s false
bought := buyTrig[1] ? false : buyTrig[0] ? true : false 

// If we got a buy get the entry price
entry_price := bought[1] ? valuewhen(bought[1], open[1],0): valuewhen(bought[0], open[0],0)

// If we got a buy get the stop price
stop_loss  := bought[1] ? valuewhen(bought[1], dzw_low_value[1],0): valuewhen(bought[0], dzw_low_value[0],0)

// If we got a buy calculate the stop_distance Value at entry
stop_distance := bought[1] ? entry_price - stop_loss : entry_price - stop_loss

//If we got a buy calculate the take_profit target at entry
take_profit := bought[1] ?  entry_price + (2 * stop_distance) : entry_price + (2 * stop_distance)

price = tostring(entry_price)
profit = crossover(close, take_profit) 
stop = crossunder(close, stop_loss)
green_line = crossunder(close, longStop)


//******* Sell Setup *******
//*****************************

// See if we need to close
sellTrig := profit or stop or green_line

// Did we trigger a Sell? If yes, sold is now true otherwise it’s false
sold := sellTrig[1] ?  false :  sellTrig[0] ? true : false 

// If we got a sell reset the entry price
entry_price := sold[1] ? na : sold[0] ? na : entry_price

// If we got a sell reset the stop price
stop_loss  := sold[1] ? na: sold[0] ? na : stop_loss

// If we got a sell reset the stop_distance Value at entry
stop_distance := sold[1] ? na : sold[0] ? na : stop_distance

//If we got a sell reset  the take_profit target at entry
take_profit := sold[1] ? na : sold[0] ? na : take_profit

 
plotshape(bought[1]? na : bought, title="1st Buy! ", text="1st Buy!", location=location.belowbar, style=shape.arrowup, size=size.large, color=color.green, textcolor=color.white)
plotshape(sold[1]? na : sold, title="1st Sell", text="1st Sell", location=location.abovebar, style=shape.arrowdown, size=size.large, color=color.red, textcolor=color.white)
enter code here

标签: triggerspine-scriptreset

解决方案


推荐阅读