首页 > 解决方案 > 在 Pine Script 中为我的策略出现每个蜡烛收盘后如何运行我的交易算法

问题描述

    strategy.exit("Exit Long Position",id = "long", stop = up, comment = "long exit")

以上是我从一个 strategy.long 头寸退出的代码。以上是策略退出的价格。

但我怀疑当蜡烛线触及上涨价格时,多头头寸就会退出。我希望算法应该等到蜡烛完全出现然后它应该决定是否以蜡烛的收盘价退出。

该怎么做...

这张图更能说明我的疑惑

以下是我的完整代码:

//@version=4
// study("Supertrend - Buy or Sell Signal", overlay = true)

strategy(title = "Supertrend", shorttitle = "ST", overlay = true, precision = 2)

//Time range for testing 

fromDay = input(defval = 1, title = "From Date", type = input.integer, minval = 1, maxval = 31)
fromMonth = input(defval = 1, title = "From Month", type = input.integer, minval = 1, maxval = 12)
fromYear = input(defval = 2005, title = "From Year", type = input.integer, minval = 1992, maxval = 2021)
toDay = input(defval = 1, title = "To Date", type = input.integer, minval = 1, maxval = 31)
toMonth = input(defval = 12, title = "To Month", type = input.integer, minval = 1, maxval = 12)
toYear = input(defval = 2021, title = "To Year", type = input.integer, minval = 1992, maxval = 2023)

from = timestamp(fromYear, fromMonth, fromDay, 00, 00)
finish = timestamp(toYear, toMonth, toDay, 23, 59)

window() => time >= from and time <= finish ? true : false

//inputs
emaPeriods = input(title = "emaPeriod", type = input.integer, defval = 50, step = 25)
Periods = input(title="ATR Period", type=input.integer, defval=10)
Source = input(close, title="Source")
Multiplier = input(title="ATR Multiplier", type=input.float, step=0.1, defval=3.0)

//Compute ATR Levels
atr=  atr(Periods) 
ema50 = ema(close, emaPeriods)

//Creating Upper Channel

up=Source-(Multiplier*atr)
up1 = nz(up[1],up)
// up1 = up[1]
up := close[1] > up1 ? max(up,up1) : up

//Creating Down Channel
dn=Source+(Multiplier*atr)
dn1 = nz(dn[1], dn)
// dn1 = dn[1]
dn := close[1] < dn1 ? min(dn, dn1) : dn


//Compute the Trend Stream +1/-1
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend

//Create Stoploss for Longs
upPlot = plot(trend == 1 ? up : na, title="Up Trend", style=plot.style_linebr, linewidth=2, color=color.green)

//Buy Signal
Buy = trend == 1 and close > ema50 

// plotshape(Buy ? up : na, title="Go Long", location=location.absolute, style=shape.circle, size=size.tiny, color=color.green, transp=0)
// plotshape(Buy ? up : na, title="Buy", text="Buy Mode", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)

dnPlot = plot(trend == 1 ? na : dn, title="Down Trend", style=plot.style_linebr, linewidth=2, color=color.red)

//Sell Signal
Sell = trend == -1 and close < ema50 

style=shape.circle, size=size.tiny, color=color.red, transp=0)
style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white, transp=0)

iPlot = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)

longFillColor =  trend == 1 ? color.green : color.white
shortFillColor = trend == -1 ? color.red : color.white

fill(iPlot, upPlot, title="UpTrend Highligter", color=longFillColor)
fill(iPlot, dnPlot, title="DownTrend Highligter", color=shortFillColor)


if Buy
    stopCond = close < up
    strategy.entry("Long", strategy.long, qty = 1000, id = "long", when = window())
    strategy.exit("Exit Long Position",id = "long", stop = up, comment = "long exit")

if Sell
    strategy.entry("Short", strategy.short, qty = 1000, id = "short", when = window())
    strategy.exit("Exit Short Position",id = "short", stop = dn, comment = "short exit")

plot(up, title = "UP", color = color.white, linewidth = 1, transp = 0)
// plot(dn, title = "DN", color = color.white, linewidth = 1, transp = 0)

plot(ema50, color = color.blue, transp =0)

标签: algorithmic-tradingpine-script-v4

解决方案


推荐阅读