简体   繁体   English

Pine Script 有条件的多次退出交易

[英]Pine Script Multiple Exits with Conditions from trade

I created an indicator that works only for long and shows enters and exits(take-profits) for my position.But during development of strategy based on indicator i faced several problems:我创建了一个仅长期有效的指标,并为我的 position 显示进入和退出(获利)。但是在基于指标的策略开发过程中,我遇到了几个问题:

1.When i try to setup two different exits for strategy - stop-loss and take-profit, only one of exits works (take-profit triggers when condition to exit trade is true, condition becomes true when several indicators give signals to exit) 1.当我尝试为策略设置两个不同的出口- 止损和获利时,只有一个出口有效(当退出交易的条件为真时触发止盈,当多个指标发出退出信号时条件变为真)

2.If i try to setup stop-loss it most of the time works incorrect - i want to exit from trade by stop-loss by the price of current candle's low,but stop-loss triggers when stop price condition( ta.lowest(low,20) ) is true for future candle and not sticks for the one on which i entered to trade 2.如果我尝试设置止损,它在大多数情况下都是不正确的 - 我想通过当前蜡烛低价的止损退出交易,但当止损价格条件( ta.lowest(low,20) ) 对于未来的蜡烛是正确的,而不是我进入交易的蜡烛

Desired point of stop-loss is marked green and undesired marked red所需的止损点标记为绿色,不需要的止损点标记为红色

Here is example code for my strategy:这是我的策略的示例代码:

// Long enter conditions
emaConditionE = false  
macdConditionE = false    
rsiConditionE = false   
buyConditionE = false   

buyConditionEnter := emaConditionE and 
     macdConditionE and 
     rsiConditionE and 

stopPrice = ta.lowest(low,20)

// Long escape conditions
buyConditionEscape := macdConditionEs and rsiConditionEs 

// Strategy Orders
if (buyConditionEnter and inTradeWindow) 
    qty = [enter image description here](https://i.stack.imgur.com/c2XjW.png)
    strategy.entry("ENTER", strategy.long,qty,limit = high,stop = stopPrice)
if(buyConditionEscape and inTradeWindow)
    strategy.exit(id = "TAKE" ,from_entry = "ENTER",limit = low)


strategy.exit(id = "STOP LOSS" ,from_entry = "ENTER",stop = stopPrice)

I tried to unite stop-loss with entry strategy.order("ENTER", strategy.long,qty,limit = high,stop = stopPrice) but that still didn't work because my stop-loss won't trigger我试图将止损与入场strategy.order("ENTER", strategy.long,qty,limit = high,stop = stopPrice) ,但这仍然没有用,因为我的止损不会触发

You need use one strategy.exit call like this您需要像这样使用一个strategy.exit调用

// Long enter conditions
emaConditionE = false  
macdConditionE = false    
rsiConditionE = false   
buyConditionE = false   

buyConditionEnter := emaConditionE and 
     macdConditionE and 
     rsiConditionE and 

stopPrice = ta.lowest(low,20)

// Long escape conditions
buyConditionEscape := macdConditionEs and rsiConditionEs 

// Strategy Orders
if (buyConditionEnter and inTradeWindow) 
    qty = [enter image description here](https://i.stack.imgur.com/c2XjW.png)
    strategy.entry("ENTER", strategy.long,qty,limit = high,stop = stopPrice)

strategy.exit(id = "STOP LOSS" ,from_entry = "ENTER",stop = stopPrice,limit = buyConditionEscape and inTradeWindow ? low : na)

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

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