以风险比率定义策略的问题

发布于 2025-01-23 23:04:18 字数 1900 浏览 3 评论 0原文

我试图根据RR的所需值(风险indIndex)来定义我的策略,并通过货币和时间范围的变化风险RO奖励比率变化。我使用sma50stragity.position_avg_price之间的距离来设置RR值。问题在于策略在RR = stight> RiskRewardIndex之前退出位置(例如:rr = 2时,它在1.43等中退出),出口点与RR比不完全匹配。我更喜欢使用tick而不是价格更改。

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


I am trying to define my strategy based on desired values of RR (RiskRewardIndex), and risk ro reward ratio changes by currency and timeframe. I use the distance between sma50 and strategy.position_avg_price to set RR values. The problem is that strategy exits the positions before RR= RiskRewardIndex (for example: when RR=2, it exits at 1.43 and etc) , the exit point does not matches exactly with RR ratio. I prefer to use ticks instead of price changes.

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


enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天生の放荡 2025-01-30 23:04:18

我通过使用value时解决了这个问题(条件,源,出现)→系列[float] pine-script中的函数,此函数返回source时的nth值。 >条件是正确的。另外,我忽略了前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))

I solved this question by using the valuewhen(condition, source, occurrence) → series[float] function in pine-script, this function returns nth value of source when condition is true. Also, I ignored first 200 candles to calculate moving averages.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文