用风险回报率定义策略的问题
Problems to Define Strategy with RiskReward Ratio
我正在尝试根据 RR (RiskRewardIndex
) 的期望值来定义我的策略,并且风险回报率随货币和时间范围而变化。我使用 sma50
和 strategy.position_avg_price
之间的距离来设置 RR 值。问题是策略在RR=RiskRewardIndex
之前退出(例如:当RR=2
时退出在1.43
等),退出点与RR比率不完全匹配。我更喜欢使用报价而不是价格变化。
//@version=4
strategy("Strategy Profit Loss RiskReaward", overlay=true)
smastoploss=sma(close,50)
plot(smastoploss,color=color.blue)
longCondition = crossover(sma(close, 14), sma(close, 28)) and close>smastoploss
shortCondition = crossunder(sma(close, 14), sma(close, 28)) and close<smastoploss
//Risk to Rewars Ration
RiskRewardIndex=input(2, "Risk Reward Index", type=input.float)
//Stoploss will be in the low (Long position) /high(Sort position) of ENTRY CANDLE
float distancelong=0.00
float stoplosslong=0.00
float takeprofitlong=0.00
float distanceshort=0.00
float stoplossshort=0.00
float takeprofitshort=0.00
//var float smastoploss=na
if (longCondition)
strategy.entry("Long", strategy.long)
distancelong := strategy.position_avg_price-smastoploss
stoplosslong:=distancelong/syminfo.mintick
takeprofitlong:=RiskRewardIndex*distancelong/syminfo.mintick
tpl=(takeprofitlong*syminfo.mintick)/2.0+ strategy.position_avg_price
plot(tpl,color=color.black)
strategy.exit("exit", "Long", loss =stoplosslong ,profit=takeprofitlong,comment='ExitLongRR')
if (shortCondition)
strategy.entry("Short", strategy.short)
distanceshort :=smastoploss-strategy.position_avg_price
stoplossshort:=distanceshort/syminfo.mintick
takeprofitshort:=RiskRewardIndex*distanceshort/syminfo.mintick
strategy.exit("exit", "Short", loss = stoplossshort,profit=takeprofitshort,comment='ExitShortRR')
我用pine-script中的valuewhen(condition, source, occurrence) → series[float]
函数解决了这个问题,这个函数returns当condition
为真时source
的第n个值。另外,我忽略了前 200 根蜡烛来计算移动平均线。
//@version=4
strategy("Strategy Profit Loss RiskReaward",overlay=true,calc_on_every_tick=true, pyramiding = 0, default_qty_type=strategy.percent_of_equity, default_qty_value=20,
initial_capital=5000, calc_on_order_fills=false,commission_type=strategy.commission.percent, commission_value=0.1)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Risk to Rewars Ration
RiskRewardIndex=input(4, "Risk Reward Index", type=input.float)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//We should ignore someof first candles to check our strategy
lastBarsFilterInput = input(100, "Bars Count:",type=input.integer)
// Here, we store the 'last_bar_index' value that is known from the beginning of the script's calculation.
// The 'last_bar_index' will change when new real-time bars appear, so we declare 'lastbar' with the 'var' keyword.
//var int lastbar=0
lastbar = 4900
// Check if the current bar_index is 'lastBarsFilterInput' removed from the last bar on the chart, or the chart is traded in real-time.
int allowedToTrade=0 //Which candles we cand trade
if (lastbar-bar_index >=lastbar-lastBarsFilterInput) or barstate.isrealtime
allowedToTrade:=0
else
allowedToTrade:=bar_index
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//CalculateLong/Short Entry Conditions
periodShort = input(13, minval=1, title="Enter Period for Short Moving Average")
periodLong = input(48, minval=1, title="Enter Period for Long Moving Average")
fastema=ema(close,periodShort)
slowema=ema(close,periodLong)
//Calculate PSAR
psarStart = input(title="PSAR Start", type=input.float, step=0.001, defval=0.02, group="PSAR")
psarIncrement = input(title="PSAR Increment", type=input.float, step=0.001, defval=0.02, group="PSAR")
psarMaximum = input(title="PSAR Maximum", type=input.float, step=0.01, defval=0.2, group="PSAR")
psar = sar(psarStart, psarIncrement, psarMaximum)
longema=crossover(ema(close, periodShort), ema(close, periodLong))
shortema=crossunder(ema(close, periodShort), ema(close, periodLong))
longCondition = (longema and slowema>psar and close>fastema) or (fastema>slowema and crossunder(psar,close) and close>fastema)
shortCondition = ( shortema and slowema<psar and close<fastema) or ( fastema<slowema and crossover(psar,close) and close<fastema)
longConditionexit=(shortema and close<fastema) or (crossover(psar,slowema) and close<fastema)
shortConditionexit= (longema and close>fastema) or ( crossunder(psar,slowema) and close>fastema) // or ( close>fastema and close>close[1] and close[1]>close[2] and close[2]>close[3] and close[3]>close[4])
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Stoploss will be in the size of difference between strategy average price and desired SMA
length =input(title="SMAlength",type=input.integer,minval=9,defval=60)
source1=sma(close,length)
smastoplosslong=valuewhen(longCondition,source1,0)
smastoplossshort=valuewhen(shortCondition,source1,0)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
float distancelong=0.00
float stoplosslong=0.00
float takeprofitlong=0.00
float distanceshort=0.00
float stoplossshort=0.00
float takeprofitshort=0.00
///////////////////////////////////////////LONG///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if longCondition and strategy.opentrades==0 and allowedToTrade>0
strategy.entry("Long", strategy.long)
distancelong := abs(strategy.position_avg_price-smastoplosslong)
// Stop size calculation
stoplosslong:=distancelong/syminfo.mintick
//Profit size calculation
takeprofitlong:=RiskRewardIndex*distancelong/syminfo.mintick
strategy.exit("exit", "Long", loss =stoplosslong ,profit=takeprofitlong,comment='ExitLongRR')
//Close Strategy
if longConditionexit
strategy.close( "Long",when=longConditionexit,comment='ExLoEMACross')
///////////////////////////////////////////SHORT///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if shortCondition and strategy.opentrades== 0 and allowedToTrade>0
strategy.entry("Short", strategy.short)
distanceshort :=abs(strategy.position_avg_price-smastoplossshort)
// Stop size calculation
stoplossshort:=distanceshort/syminfo.mintick
//Profit size calculation
takeprofitshort:=RiskRewardIndex*distanceshort/syminfo.mintick
strategy.exit("exit", "Short", loss = stoplossshort,profit=takeprofitshort,comment='ExitShortRR')
//Close Strategy
if shortConditionexit
strategy.close( "Short",when=shortConditionexit,comment='ExShEMACross')
/////////////////// Colors /////////////////////
collongsl = color.blue
collongtp = color.green
colshortsl = color.orange
colshorttp = color.red
///////////////////////////////////////////PLOT///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//LONG Position TP/SL Plot
p1=plot(strategy.position_size > 0 ? strategy.position_avg_price-stoplosslong*syminfo.mintick : na, color= longCondition ? collongsl : na, style=plot.style_linebr, title="SL_Long")
p2=plot(strategy.position_size > 0 ? strategy.position_avg_price+takeprofitlong*syminfo.mintick : na, color= longCondition ? collongtp : na , style=plot.style_linebr, title="TP_Long")
p3=plot(strategy.position_size > 0 ? strategy.position_avg_price : na, color=color.silver, style=plot.style_linebr, title="Entry_Long")
fill(p1, p3,color=color.new(color.red, transp = 90))
fill(p2, p3, color=color.new(color.aqua, transp = 90))
//SHORT Position TP/SL Plot
p4=plot(strategy.position_size < 0 ? strategy.position_avg_price+stoplossshort*syminfo.mintick : na, color= shortCondition ? colshortsl : na, style=plot.style_linebr, title="SL_Short")
p5=plot(strategy.position_size < 0 ? strategy.position_avg_price-takeprofitshort*syminfo.mintick: na, color= shortCondition ? colshorttp : na, style=plot.style_linebr, title="TP_Short")
p6=plot(strategy.position_size < 0 ? strategy.position_avg_price : na, color=color.silver, style=plot.style_linebr, title="Entry_Short")
fill(p4, p6, color=color.new(color.red, transp = 90))
fill(p5, p6, color=color.new(color.aqua, transp = 90))
我正在尝试根据 RR (RiskRewardIndex
) 的期望值来定义我的策略,并且风险回报率随货币和时间范围而变化。我使用 sma50
和 strategy.position_avg_price
之间的距离来设置 RR 值。问题是策略在RR=RiskRewardIndex
之前退出(例如:当RR=2
时退出在1.43
等),退出点与RR比率不完全匹配。我更喜欢使用报价而不是价格变化。
//@version=4
strategy("Strategy Profit Loss RiskReaward", overlay=true)
smastoploss=sma(close,50)
plot(smastoploss,color=color.blue)
longCondition = crossover(sma(close, 14), sma(close, 28)) and close>smastoploss
shortCondition = crossunder(sma(close, 14), sma(close, 28)) and close<smastoploss
//Risk to Rewars Ration
RiskRewardIndex=input(2, "Risk Reward Index", type=input.float)
//Stoploss will be in the low (Long position) /high(Sort position) of ENTRY CANDLE
float distancelong=0.00
float stoplosslong=0.00
float takeprofitlong=0.00
float distanceshort=0.00
float stoplossshort=0.00
float takeprofitshort=0.00
//var float smastoploss=na
if (longCondition)
strategy.entry("Long", strategy.long)
distancelong := strategy.position_avg_price-smastoploss
stoplosslong:=distancelong/syminfo.mintick
takeprofitlong:=RiskRewardIndex*distancelong/syminfo.mintick
tpl=(takeprofitlong*syminfo.mintick)/2.0+ strategy.position_avg_price
plot(tpl,color=color.black)
strategy.exit("exit", "Long", loss =stoplosslong ,profit=takeprofitlong,comment='ExitLongRR')
if (shortCondition)
strategy.entry("Short", strategy.short)
distanceshort :=smastoploss-strategy.position_avg_price
stoplossshort:=distanceshort/syminfo.mintick
takeprofitshort:=RiskRewardIndex*distanceshort/syminfo.mintick
strategy.exit("exit", "Short", loss = stoplossshort,profit=takeprofitshort,comment='ExitShortRR')
我用pine-script中的valuewhen(condition, source, occurrence) → series[float]
函数解决了这个问题,这个函数returns当condition
为真时source
的第n个值。另外,我忽略了前 200 根蜡烛来计算移动平均线。
//@version=4
strategy("Strategy Profit Loss RiskReaward",overlay=true,calc_on_every_tick=true, pyramiding = 0, default_qty_type=strategy.percent_of_equity, default_qty_value=20,
initial_capital=5000, calc_on_order_fills=false,commission_type=strategy.commission.percent, commission_value=0.1)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Risk to Rewars Ration
RiskRewardIndex=input(4, "Risk Reward Index", type=input.float)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//We should ignore someof first candles to check our strategy
lastBarsFilterInput = input(100, "Bars Count:",type=input.integer)
// Here, we store the 'last_bar_index' value that is known from the beginning of the script's calculation.
// The 'last_bar_index' will change when new real-time bars appear, so we declare 'lastbar' with the 'var' keyword.
//var int lastbar=0
lastbar = 4900
// Check if the current bar_index is 'lastBarsFilterInput' removed from the last bar on the chart, or the chart is traded in real-time.
int allowedToTrade=0 //Which candles we cand trade
if (lastbar-bar_index >=lastbar-lastBarsFilterInput) or barstate.isrealtime
allowedToTrade:=0
else
allowedToTrade:=bar_index
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//CalculateLong/Short Entry Conditions
periodShort = input(13, minval=1, title="Enter Period for Short Moving Average")
periodLong = input(48, minval=1, title="Enter Period for Long Moving Average")
fastema=ema(close,periodShort)
slowema=ema(close,periodLong)
//Calculate PSAR
psarStart = input(title="PSAR Start", type=input.float, step=0.001, defval=0.02, group="PSAR")
psarIncrement = input(title="PSAR Increment", type=input.float, step=0.001, defval=0.02, group="PSAR")
psarMaximum = input(title="PSAR Maximum", type=input.float, step=0.01, defval=0.2, group="PSAR")
psar = sar(psarStart, psarIncrement, psarMaximum)
longema=crossover(ema(close, periodShort), ema(close, periodLong))
shortema=crossunder(ema(close, periodShort), ema(close, periodLong))
longCondition = (longema and slowema>psar and close>fastema) or (fastema>slowema and crossunder(psar,close) and close>fastema)
shortCondition = ( shortema and slowema<psar and close<fastema) or ( fastema<slowema and crossover(psar,close) and close<fastema)
longConditionexit=(shortema and close<fastema) or (crossover(psar,slowema) and close<fastema)
shortConditionexit= (longema and close>fastema) or ( crossunder(psar,slowema) and close>fastema) // or ( close>fastema and close>close[1] and close[1]>close[2] and close[2]>close[3] and close[3]>close[4])
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Stoploss will be in the size of difference between strategy average price and desired SMA
length =input(title="SMAlength",type=input.integer,minval=9,defval=60)
source1=sma(close,length)
smastoplosslong=valuewhen(longCondition,source1,0)
smastoplossshort=valuewhen(shortCondition,source1,0)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
float distancelong=0.00
float stoplosslong=0.00
float takeprofitlong=0.00
float distanceshort=0.00
float stoplossshort=0.00
float takeprofitshort=0.00
///////////////////////////////////////////LONG///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if longCondition and strategy.opentrades==0 and allowedToTrade>0
strategy.entry("Long", strategy.long)
distancelong := abs(strategy.position_avg_price-smastoplosslong)
// Stop size calculation
stoplosslong:=distancelong/syminfo.mintick
//Profit size calculation
takeprofitlong:=RiskRewardIndex*distancelong/syminfo.mintick
strategy.exit("exit", "Long", loss =stoplosslong ,profit=takeprofitlong,comment='ExitLongRR')
//Close Strategy
if longConditionexit
strategy.close( "Long",when=longConditionexit,comment='ExLoEMACross')
///////////////////////////////////////////SHORT///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if shortCondition and strategy.opentrades== 0 and allowedToTrade>0
strategy.entry("Short", strategy.short)
distanceshort :=abs(strategy.position_avg_price-smastoplossshort)
// Stop size calculation
stoplossshort:=distanceshort/syminfo.mintick
//Profit size calculation
takeprofitshort:=RiskRewardIndex*distanceshort/syminfo.mintick
strategy.exit("exit", "Short", loss = stoplossshort,profit=takeprofitshort,comment='ExitShortRR')
//Close Strategy
if shortConditionexit
strategy.close( "Short",when=shortConditionexit,comment='ExShEMACross')
/////////////////// Colors /////////////////////
collongsl = color.blue
collongtp = color.green
colshortsl = color.orange
colshorttp = color.red
///////////////////////////////////////////PLOT///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//LONG Position TP/SL Plot
p1=plot(strategy.position_size > 0 ? strategy.position_avg_price-stoplosslong*syminfo.mintick : na, color= longCondition ? collongsl : na, style=plot.style_linebr, title="SL_Long")
p2=plot(strategy.position_size > 0 ? strategy.position_avg_price+takeprofitlong*syminfo.mintick : na, color= longCondition ? collongtp : na , style=plot.style_linebr, title="TP_Long")
p3=plot(strategy.position_size > 0 ? strategy.position_avg_price : na, color=color.silver, style=plot.style_linebr, title="Entry_Long")
fill(p1, p3,color=color.new(color.red, transp = 90))
fill(p2, p3, color=color.new(color.aqua, transp = 90))
//SHORT Position TP/SL Plot
p4=plot(strategy.position_size < 0 ? strategy.position_avg_price+stoplossshort*syminfo.mintick : na, color= shortCondition ? colshortsl : na, style=plot.style_linebr, title="SL_Short")
p5=plot(strategy.position_size < 0 ? strategy.position_avg_price-takeprofitshort*syminfo.mintick: na, color= shortCondition ? colshorttp : na, style=plot.style_linebr, title="TP_Short")
p6=plot(strategy.position_size < 0 ? strategy.position_avg_price : na, color=color.silver, style=plot.style_linebr, title="Entry_Short")
fill(p4, p6, color=color.new(color.red, transp = 90))
fill(p5, p6, color=color.new(color.aqua, transp = 90))