如何制定风险回报率策略
How to create strategy for Risk to Reward ratio
我正在为 R/R 比率策略而苦苦挣扎。我需要一个标准的 1.5 Win/Lose 比例。作为止损,我使用的是入场水平之前的前一个摆动低点的低点。这是代码。
//@version=4
strategy("TEST","T", true)
//₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿ ₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿
fastlen = input(12, "Fast Length", group="MACD")
slowlen = input(26, "Slow Length", group="MACD")
siglen = input(9, "Signal Length", group="MACD")
mcd_src = input(close, "Source", group="MACD")
_bar = input(1, "Swing Bar#", minval=1, group="Stop Loss Option")
//₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿ ₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿
[fast, slow, hist] = macd(mcd_src, fastlen, slowlen, siglen)
pivot_low_stop = pivotlow(_bar, _bar)
float entry_lvl = 0.00
entry_lvl := valuewhen(strategy.opentrades == 1, strategy.position_avg_price, 0)
float long_sl = 0.00
long_sl := valuewhen(strategy.opentrades == 1, pivot_low_stop, 0)
profit = (entry_lvl - long_sl) * 1.5 + entry_lvl
long = barstate.isconfirmed and fast and slow <= 0 and crossover(fast, slow)
if long
strategy.entry("Long", true)
strategy.exit("Long", profit=profit, limit=long_sl)
plot(entry_lvl,"Entry Level", color.yellow, 2, plot.style_linebr)
plot(long_sl,"Stop Loss Level", color.red, 2, plot.style_linebr )
plot(profit, "Profit Level", color.green, 2, plot.style_linebr)
来自 Pine 参考手册:
...
profit (float) An optional parameter. Profit target (specified in ticks). If it is specified, a limit order is placed to exit market position when the specified amount of profit (in ticks) is reached. The default value is 'NaN'.
limit (float) An optional parameter. Profit target (requires a specific price). If it is specified, a limit order is placed to exit market position at the specified price (or better). Priority of the parameter 'limit' is higher than priority of the parameter 'profit' ('limit' is used instead of 'profit', if its value is not 'NaN'). The default value is 'NaN'.
loss (float) An optional parameter. Stop loss (specified in ticks). If it is specified, a stop order is placed to exit market position when the specified amount of loss (in ticks) is reached. The default value is 'NaN'.
stop (float) An optional parameter. Stop loss (requires a specific price). If it is specified, a stop order is placed to exit market position at the specified price (or worse). Priority of the parameter 'stop' is higher than priority of the parameter 'loss' ('stop' is used instead of 'loss', if its value is not 'NaN'). The default value is 'NaN'.
...
所以,你的代码应该是:
strategy.exit("Long", limit=profit, stop=long_sl)
我正在为 R/R 比率策略而苦苦挣扎。我需要一个标准的 1.5 Win/Lose 比例。作为止损,我使用的是入场水平之前的前一个摆动低点的低点。这是代码。
//@version=4
strategy("TEST","T", true)
//₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿ ₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿
fastlen = input(12, "Fast Length", group="MACD")
slowlen = input(26, "Slow Length", group="MACD")
siglen = input(9, "Signal Length", group="MACD")
mcd_src = input(close, "Source", group="MACD")
_bar = input(1, "Swing Bar#", minval=1, group="Stop Loss Option")
//₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿ ₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿₿
[fast, slow, hist] = macd(mcd_src, fastlen, slowlen, siglen)
pivot_low_stop = pivotlow(_bar, _bar)
float entry_lvl = 0.00
entry_lvl := valuewhen(strategy.opentrades == 1, strategy.position_avg_price, 0)
float long_sl = 0.00
long_sl := valuewhen(strategy.opentrades == 1, pivot_low_stop, 0)
profit = (entry_lvl - long_sl) * 1.5 + entry_lvl
long = barstate.isconfirmed and fast and slow <= 0 and crossover(fast, slow)
if long
strategy.entry("Long", true)
strategy.exit("Long", profit=profit, limit=long_sl)
plot(entry_lvl,"Entry Level", color.yellow, 2, plot.style_linebr)
plot(long_sl,"Stop Loss Level", color.red, 2, plot.style_linebr )
plot(profit, "Profit Level", color.green, 2, plot.style_linebr)
来自 Pine 参考手册:
...
profit (float) An optional parameter. Profit target (specified in ticks). If it is specified, a limit order is placed to exit market position when the specified amount of profit (in ticks) is reached. The default value is 'NaN'.
limit (float) An optional parameter. Profit target (requires a specific price). If it is specified, a limit order is placed to exit market position at the specified price (or better). Priority of the parameter 'limit' is higher than priority of the parameter 'profit' ('limit' is used instead of 'profit', if its value is not 'NaN'). The default value is 'NaN'.
loss (float) An optional parameter. Stop loss (specified in ticks). If it is specified, a stop order is placed to exit market position when the specified amount of loss (in ticks) is reached. The default value is 'NaN'.
stop (float) An optional parameter. Stop loss (requires a specific price). If it is specified, a stop order is placed to exit market position at the specified price (or worse). Priority of the parameter 'stop' is higher than priority of the parameter 'loss' ('stop' is used instead of 'loss', if its value is not 'NaN'). The default value is 'NaN'.
...
所以,你的代码应该是:
strategy.exit("Long", limit=profit, stop=long_sl)