简体   繁体   English

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

[英]Trimming position is not working in pine script

I've created a strategy that buys and sells based on an indicator. 我创建了一个基于指标进行买卖的策略。 After buying it will follow te SAR for stop-loss. 购买后,它将按照SAR进行止损。 the strategy is set to execute every tick. 该策略设置为执行每个刻度。

This parts works fine. 这部分工作正常。

I have however one more condition and that is that I close half the position when certain conditions are met. 但是,我还有一个条件,那就是在满足某些条件时我平仓一半。 However, the strategy keeps closing half the remaining position every tick, until the complete position is closed. 但是,该策略会在每个报价周期关闭一半剩余头寸,直到关闭完整头寸。

I've tried to use the position_size variable to check the remaining position against the initial position and also introduced a bool variable to indicate that trimming has been taken place, but it is not working. 我尝试使用position_size变量对照初始位置检查剩余位置,并且还引入了bool变量来指示已经进行了修剪,但是它不起作用。

What can I do to stop the strategy from completely closing the position? 我该怎么做才能阻止策略完全平仓?

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

Should be something more like this. 应该更像这样。 You are currently redeclaring a new, local trim var within local if block. 当前,您正在局部if块中重新声明一个新的局部trim var。 Need to use := when you want to change an existing variable's value after it has already been declared. 如果要在声明现有变量后更改其值,则需要使用:=

//@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