繁体   English   中英

使用 strategy.exit() function 时 PineScript 策略不打开订单/不响应

[英]PineScript strategy don't open orders/not responding when use strategy.exit() function

因此,当我只使用 strategy.entry() 时,它可以正常工作,并且可以根据需要打开和关闭交易。 但是,一旦我设置了退出条件,脚本就会毫无问题地保存,但不会打开订单,也不会在图表上显示任何内容。

似乎我已经正确计算了 TP 和 SL 水平,因为当我 plot 图表上的值与 plot() function 时,它向我显示了正确的值。 但似乎我在 strategy.exit() function 上做错了什么

以下是部分代码:

//@version=4
//Buy and Sell Conditions
buy=c2>o2
sell=c2<o2

//Stoploss price (last top or bottom)
longstop = lowest(low,bars)
shortstop = highest(high,bars)


//Get stop values at the entry bar
entry_longstop = valuewhen(buy, longstop,0)
entry_shortstop = valuewhen(sell, shortstop,0)

//Calculate TP based on ratio of SL
longtake=strategy.position_avg_price + ((strategy.position_avg_price - entry_longstop) * rr)
shorttake= strategy.position_avg_price - ((entry_shortstop - strategy.position_avg_price) * rr)


 
strategy.entry("long", true, when=buy)
strategy.exit("TP", "long", limit=longtake, stop= entry_longstop)

strategy.entry("short", false, when=sell)
strategy.exit("TP", "short", limit=shorttake, stop=entry_shortstop)

我对 Pine(和编码)比较陌生,但我发现了你的问题并查看了代码。 我输入了一些额外的细节——不同的入场条件和你想要改回来的其他一些额外的细节——最终让空头和多头工作。

这是运行策略的图表的屏幕截图。 订单在一个简单的 EMA 交叉盘上打开。

我将我的尝试与您发布的代码进行了比较,并对脚本中的更改进行了一些评论。 我能看到的唯一显着区别是我明确了每个 strategy.exit function 的“from_entry”参数。例如,而不是strategy.exit("TP", "short", limit=shorttake, stop=entry_shortstop)我们有strategy.exit("exit", from_entry="short", limit=shorttake, stop=entry_shortstop)

让我知道该脚本是否像对我一样有效,以及您是否能够从中调整自己的脚本。

亲切的问候,

M。

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MattAnstey

//@version=4
strategy("My strategy", overlay=true, margin_long=100, margin_short=100)

///I added a simple ema cross and an RR ratio so the script would compile./// 

c2 = ema(close, 50)
o2 = ema(close, 100)
rr = 1.5

//Buy and Sell Conditions
///I added a condition to 'buy' and 'sell' which would ensure only one short or one long are open at a time.///

buy=crossover(c2,o2) and strategy.position_size == 0
sell=crossunder(c2,o2) and strategy.position_size == 0

//Stoploss price (last top or bottom)
longstop = lowest(low,20)
shortstop = highest(high,20)

//Get stop values at the entry bar
///I added a check for the entry price - the close price - at your buy and sell conditions, so I could plot the entry price later.  

entry_longstop = valuewhen(buy, longstop,0)
entry_shortstop = valuewhen(sell, shortstop,0)
entry_long = valuewhen(buy, close, 0) 
entry_short = valuewhen(sell, close, 0) 

//Calculate TP based on ratio of SL
///This formula is spot on. No changes.///

longtake=strategy.position_avg_price + ((strategy.position_avg_price - entry_longstop) * rr)
shorttake= strategy.position_avg_price - ((entry_shortstop - strategy.position_avg_price) * rr)

///Here I added conditions I could use to plot the entry, profit and stop levels when (and only when) the appropriate order was open.You probably have a better method. 

lorders = strategy.position_size > 0 
sorders = strategy.position_size < 0

///Now, the strategy.exit functions here are different. 
///I changed the id of each from "TP" to "exit", but that's incidental. 
///The only important difference I can see between the strategy.exit functions below and yours is that I added 'from_entry' and the name of the strategy.entry they apply to.
///Generally I type out all of the arguments in long form, e.g, strategy.exit(id="long", from_entry="long"...), etc. - maybe that was what helped? 

strategy.entry("long", true, when=buy)
strategy.exit("exit", from_entry="long", limit=longtake, stop=entry_longstop)

strategy.entry("short", false, when=sell)
strategy.exit("exit", from_entry="short", limit=shorttake, stop=entry_shortstop)

///Below are the plots I used to see what was going on. 

plot(lorders ? longtake: na, style=plot.style_linebr, color=color.green)
plot(sorders ? shorttake: na, style=plot.style_linebr, color=color.green)
plot(lorders ? entry_longstop: na, style=plot.style_linebr, color=color.red)
plot(sorders ? entry_shortstop : na, style=plot.style_linebr, color=color.red)
plot(c2)
plot(o2)
plot(lorders ? entry_long : na, style=plot.style_linebr, color=color.black)
plot(sorders ? entry_short: na, style=plot.style_linebr, color=color.black)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM