动态止盈和止损

时间:2019-06-04 13:21:14

标签: pine-script

我正在努力寻找一种根据一些参数创建策略的方法:

enterCond = xxxxxxx

输入:当enterCond == true

初始止损:入场价-1.5%

目标1:进入+ 3% 卖出50%并更改止损输入价格

目标2:进入+ 5% 卖出50%并将止损更改为T1

目标3:进入+ 7% 卖出100%

有人可以帮我吗?

buyCond = xxxxxxxxx

sl_inp = input(1.5, title='Stop Loss %', type=float)/100
tp_inp1 = input(3.0, title='Take Profit 1 (%)', type=float)/100
tp_inp2 = input(5.0, title='Take Profit 2 (%)', type=float)/100
tp_inp3 = input(7.0, title='Take Profit 3 (%)', type=float)/100


// First Position
first_long = buyCond and strategy.position_size == 0
if first_long
    //initial stop loss: entry price - 1.5%
    strategy.entry("Long", strategy.long, stop=(strategy.position_avg_price*(1-sl_inp)))

//sl1 = strategy.position_avg_price * (1 - sl_inp)
tp1 = strategy.position_avg_price * (1 + tp_inp1)
tp2 = strategy.position_avg_price * (1 + tp_inp2)
tp3 = strategy.position_avg_price * (1 + tp_inp3)

//Target 1: entry + 3% sell 50% and change stop loss to enter price
strategy.order("Long", strategy.long, qty=strategy.equity/2, stop=strategy.position_avg_price, when=close==tp1)

//Target 2: entry + 5% => sell 50% and change stop loss to T1
strategy.order("Long", strategy.long, qty=strategy.equity/2, stop=tp1, when=close==tp2)

strategy.close("Long", when=close==tp3)

0 个答案:

没有答案