首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >要么重新绘制(因为calc_on_order_fills=true),要么我在strategy.exit中的跟踪停止丢失不起作用,松树,Tradingview

要么重新绘制(因为calc_on_order_fills=true),要么我在strategy.exit中的跟踪停止丢失不起作用,松树,Tradingview
EN

Stack Overflow用户
提问于 2022-07-06 06:34:24
回答 1查看 216关注 0票数 0

大家早上好!我已经在松树编辑中写了一份策略,但不幸的是,它并没有完全按照我所希望的方式工作。要么是重新绘制(因为calc_on_order_fills=true),要么是我在strategy.exit函数中的尾部停止丢失不能从第一个蜡烛中工作,而是只从下面的一个蜡烛开始工作。

最重要的是,战略警报过于频繁地发出警告。这是重新油漆的结果吗?

我已经尝试了以下几种方法:由ATR计算的-Using尾部停止损失,而不是trail_price和trail_offset在输入命令(没有改变任何东西)中的-Using参数(打开、关闭、高、低),用于计算我的输入订单条件(没有改变任何东西) -Add strategy.closedtrades.entry_time(strategy.closedtrades) != timestrategy.closedtrades.entry_bar_index(strategy.closedtrades) != bar_index (没有工作)

有人能帮我吗?我很乐意收到消息:)

这就是我现在的代码:

代码语言:javascript
运行
复制
//@version=5
strategy("My strategy", overlay=true, calc_on_every_tick = false, calc_on_order_fills = true)


// Set exit levels with input options (optional)
LimitPerc = input.int(title="Take Profit (0,1%)", minval=0, step=1, defval=10) * 0.001
StopPerc = input.int(title="Stop Loss / Trailing Offset (0,1%)", minval=0, step=1, defval=5) * 0.001
TrailPerc = input.int(title="Trailing Offset (0,1%)", minval=0, step=1, defval=2) * 0.001

 
//Movin Averages incl Plot
//EMA
emaplot = input (true, title="Show EMA on chart")
len = input.int(minval=1, step=1, defval=9, title="ema Length")
src = close[1]
out = ta.ema(src, len)
up = out > out[1]
down = out < out[1]
mycolor = up ? color.green : down ? color.red : color.blue
plot(out and emaplot ? out :na, title="EMA", color=mycolor, linewidth=3)

//SMA
smaplot = input (true, title="Show SMA on chart")
len2 = input.int(minval=1, step=1, defval=2, title="sma Length")
src2 = close[1]
out2 = ta.sma(src2, len2)
up2 = out2 > out2[1]
down2 = out2 < out2[1]
mycolor2 = up2 ? color.green : down2 ? color.red : color.blue
plot(out2 and smaplot ? out2 :na , title="SMA", color=mycolor2, linewidth=1)


//Stochastic Momentum Index incl Plot
//Stoch RSI
a = input.int(minval=1, step=1, defval=2, title = "Percent K Length")
b = input.int(minval=1, step=1, defval=2, title = "Percent D Length")
// Range Calculation
ll = ta.lowest (low[1], a)
hh = ta.highest (high[1], a)
diff = hh - ll
rdiff = close[1] - (hh+ll)/2
// Nested Moving Average for smoother curves
avgrel = ta.ema(ta.ema(rdiff,b),b)
avgdiff = ta.ema(ta.ema(diff,b),b)
// SMI calculations
SMI = avgdiff != 0 ? (avgrel/(avgdiff/2)*100) : 0
SMIsignal = ta.ema(SMI,b)
SMIup = SMI > SMI[1]
SMIdown = SMI < SMI[1]


//Heikin Ashi
HAopen  = (open[1]+close[1])/2
HAclose = ohlc4[1]
HAhigh  = math.max (high, open, close)
HAlow   = math.min (low, open, close)
HAup    = HAopen < HAclose
HAdown  = HAopen > HAclose


//Conditions for trades
LongCondition = HAup and SMIup and up and up2 and (strategy.opentrades == 0)
ShortCondition = HAdown and SMIdown and down and down2 and (strategy.opentrades == 0)


//Submit entry orders
if (LongCondition and time>timestamp(2022, 07, 04, 07, 00))
    strategy.entry(id = "Long", direction = strategy.long, alert_message = "Place Long Order", comment = "Long Entry")


if (ShortCondition and time>timestamp(2022, 07, 04, 07, 00))
    strategy.entry(id = "Short", direction = strategy.short, alert_message = "Place Short Order", comment = "Short Entry")

// and strategy.closedtrades.entry_time(strategy.closedtrades) != time
// and strategy.closedtrades.entry_bar_index(strategy.closedtrades) != bar_index


// Determine stop loss price
longLimitPrice  = strategy.position_avg_price * (1 + LimitPerc)
shortLimitPrice = strategy.position_avg_price * (1 - LimitPerc)
longStopPrice   = strategy.position_avg_price * (1 - StopPerc)
shortStopPrice  = strategy.position_avg_price * (1 + StopPerc)
trailingOffset  = (strategy.position_avg_price - (strategy.position_avg_price * (1 - TrailPerc))) / syminfo.mintick


//Submit exit orders
if (strategy.position_size > 0)
    strategy.exit(id = "Long", alert_trailing = "Exit Long Order", trail_price = strategy.position_avg_price, trail_offset = trailingOffset, comment = "Long Exit")

if (strategy.position_size < 0)
    strategy.exit(id = "Short", alert_trailing = "Exit Short Order", trail_price = strategy.position_avg_price, trail_offset = trailingOffset, comment = "Short Exit")

//END
EN

回答 1

Stack Overflow用户

发布于 2022-07-07 16:27:12

如果您的问题是“strategy.exit函数不能从第一个蜡烛上工作,而是只从下面的一个蜡烛中工作”,那么请删除if检查出口:

代码语言:javascript
运行
复制
//Submit exit orders
strategy.exit(id = "Long", alert_trailing = "Exit Long Order", trail_price = strategy.position_avg_price, trail_offset = trailingOffset, comment = "Long Exit")
strategy.exit(id = "Short", alert_trailing = "Exit Short Order", trail_price = strategy.position_avg_price, trail_offset = trailingOffset, comment = "Short Exit")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72878862

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档