繁体   English   中英

修剪位置在pine脚本中不起作用

[英]Trimming position is not working in pine script

我创建了一个基于指标进行买卖的策略。 购买后,它将按照SAR进行止损。 该策略设置为执行每个刻度。

这部分工作正常。

但是,我还有一个条件,那就是在满足某些条件时我平仓一半。 但是,该策略会在每个报价周期关闭一半剩余头寸,直到关闭完整头寸。

我尝试使用position_size变量对照初始位置检查剩余位置,并且还引入了bool变量来指示已经进行了修剪,但是它不起作用。

我该怎么做才能阻止策略完全平仓?

strategy("MyStrategy", overlay=true, calc_on_every_tick=true, 
          commission_type=strategy.commission.percent, commission_value=0.075, slippage=2)
...
bool trim = na
if TDUp == 9 and not trim and high > strategy.position_avg_price and strategy.opentrades >= 
        strategy.opentrades[max(0, barssince(Buy)-1)]
    // this shall execute only once, but it is executed every tick
    strategy.order("triml", false, qty=abs(strategy.position_size / 2))
    trim = true

if TDDn == 9 and not trim and low < strategy.position_avg_price and strategy.opentrades <= 
        strategy.opentrades[max(0, barssince(Sell)-1)]
    // this shall execute only once, but it is executed every tick
    strategy.order("trims", true, qty=abs(strategy.position_size / 2))
    trim = true

应该更像这样。 当前,您正在局部if块中重新声明一个新的局部trim var。 如果要在声明现有变量后更改其值,则需要使用:=

//@version=4
study("My Script")
bool trim = false
if TDUp == 9 and not trim and high > strategy.position_avg_price and strategy.opentrades >= 
        strategy.opentrades[max(0, barssince(Buy)-1)]
    // this shall execute only once, but it is executed every tick
    strategy.order("triml", false, qty=abs(strategy.position_size / 2))
    trim := true

if TDDn == 9 and not trim and low < strategy.position_avg_price and strategy.opentrades <= 
        strategy.opentrades[max(0, barssince(Sell)-1)]
    // this shall execute only once, but it is executed every tick
    strategy.order("trims", true, qty=abs(strategy.position_size / 2))
    trim := true

暂无
暂无

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

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