簡體   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