[英]PineScript strategy don't open orders/not responding when use strategy.exit() function
So when I just use the strategy.entry() it works fine and it opens and closes trades as demanded.因此,当我只使用 strategy.entry() 时,它可以正常工作,并且可以根据需要打开和关闭交易。 But once I put the exit conditions the script saves without any issue but doesn't open orders nor shows anything on chart.但是,一旦我设置了退出条件,脚本就会毫无问题地保存,但不会打开订单,也不会在图表上显示任何内容。
Seems likes I've calculated the TP and SL levels correct because when I plot the values on chart with the plot() function it shows me the correct values.似乎我已经正确计算了 TP 和 SL 水平,因为当我 plot 图表上的值与 plot() function 时,它向我显示了正确的值。 But seems like I'm doing something wrong on the strategy.exit() function但似乎我在 strategy.exit() function 上做错了什么
Here's part of the code:以下是部分代码:
//@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)
I'm relatively new to Pine (and to coding) but I spotted your question and had a look at the code.我对 Pine(和编码)比较陌生,但我发现了你的问题并查看了代码。 I entered a few extra details - different entry conditions and few other extras you'll want to change back - and eventually got shorts and longs working.我输入了一些额外的细节——不同的入场条件和你想要改回来的其他一些额外的细节——最终让空头和多头工作。
Here's a screenshot of a chart with the strategy running.这是运行策略的图表的屏幕截图。 Orders are opened on a simple EMA cross.订单在一个简单的 EMA 交叉盘上打开。
I compared my attempt with the code you posted and made some comments about the changes in the script.我将我的尝试与您发布的代码进行了比较,并对脚本中的更改进行了一些评论。 The only significant difference I could see was that I made the 'from_entry" argument of each strategy.exit function explicit. So for example, instead of strategy.exit("TP", "short", limit=shorttake, stop=entry_shortstop)
we have strategy.exit("exit", from_entry="short", limit=shorttake, stop=entry_shortstop)
我能看到的唯一显着区别是我明确了每个 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)
Let me know if the script works as it does for me, and if you're able to adapt your own script from it.让我知道该脚本是否像对我一样有效,以及您是否能够从中调整自己的脚本。
Kind regards,亲切的问候,
M. 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.