根据最后进入价格更新止损

时间:2019-12-08 08:57:36

标签: pine-script

我想根据每个新挂单的价格变化设置止损单。问题在于止损仅针对一阶订单执行。我是这样写代码的:

//@version=4
strategy("Mutual funds RSI Index",
 "MF_RSI_IDX",
 default_qty_type=strategy.percent_of_equity,
 default_qty_value=10,
 initial_capital=1000,
 calc_on_order_fills=true,
 currency=currency.USD,
 commission_type=strategy.commission.percent,
 commission_value=0.29,
 process_orders_on_close=true)


if (rsi(close, 14) < 30)
    strategy.entry("buy", strategy.long)
stopLoss = strategy.position_avg_price * 0.80
lastPeak = close * 0.80

sellSignal0 = rsi(close, 14) > 70
sellSignal1 = falling(close, 5)
sellSignal2 = stopLoss >= close
sellSignal3 = lastPeak >= close

strategy.close("buy", when = sellSignal2 or sellSignal3)
plotchar(lastPeak, char="x", location=location.absolute)
plot(strategy.equity)

有人可以向我解释这段代码有什么问题吗?

1 个答案:

答案 0 :(得分:1)

//@version=4
strategy("Mutual funds RSI Index",
 "MF_RSI_IDX",
 overlay=true,
 default_qty_type=strategy.percent_of_equity,
 default_qty_value=10,
 initial_capital=1000,
 currency=currency.USD,
 commission_type=strategy.commission.percent,
 commission_value=0.29)


strategy.entry("buy", strategy.long, when=rsi(close, 14) < 30)

stopLoss = 0.0
if strategy.position_avg_price != 0
    stopLoss := max(strategy.position_avg_price * 0.9, nz(stopLoss[1]))

limitPrice = float(na)
// force exit
if rsi(close, 14) > 70
    limitPrice := 0.0

strategy.exit("buy", stop=stopLoss, limit=limitPrice)

您正在寻找什么?我认为最好在此处exit处平仓并注意,我删除了calc_on_order_fillsprocess_orders_on_close,因为它们存在很大争议。