I am not completely new to Pinescript but facing an issue. I understand this error indicates infinite loop but I dont think I have any or maybe I am failing to recognize even after 100th attempt.
I strongly believe the root cause might be related to garbage value or incorrect data in the variable or assignment.
Can you please help?
- Function
calcTrailingStopLoss(entryType = "None") =>
var float slMult = 0
var float stopLossValueTSL = na
var float slPointsTSL = na
var float entryValueTSL = na
var bool enableLongTSL = false
var bool enableShortTSL = false
var int rRTSL = na
var float prevStopLossValue = na
rRTSL := str.endswith(strategy.opentrades.entry_id(0),"SL") ? 2 : varRRTSL
if entryType == "Long"
stopLossValueTSL := stopLossValueLong
slPointsTSL := slPointsLong
entryValueTSL := entryValueLong
else if entryType == "Short"
stopLossValueTSL := stopLossValueShort
slPointsTSL := slPointsShort
entryValueTSL := entryValueShort
slMult := math.floor(math.abs(close-stopLossValueTSL)/slPointsTSL)
prevStopLossValue := stopLossValueTSL
//[currentCandleHealthPer, currentCandleBody] = getCandleHealthPer(0)
if bar_index >= debugBarIndex-50 and bar_index <=debugBarIndex+50
log.warning("calcTrailingStopLoss - slMult Before:{0}",slMult)
//Checking if system current candle is elligible for trailling
if isGreen and entryType == "Long" and (candleHealthPer >= varCandleHealthPer) and ((close > (entryValueTSL+(rRTSL*slPointsTSL)) and (stopLossValueTSL < close) and (strategy.position_size[1] > 0 and strategy.position_size > 0)) or (math.abs(close - entryValueTSL) >= (entryValueTSL*0.005) or dailyPoints >= entryValueTSL*0.005))
enableLongTSL := true
else if isRed and entryType == "Short" and (candleHealthPer >= varCandleHealthPer) and ((close < (entryValueTSL-(rRTSL*slPointsTSL)) and stopLossValueTSL < close and isRed and (strategy.position_size[1] < 0 and strategy.position_size < 0)) or (math.abs(close - entryValueTSL) >= entryValueTSL*0.005 or dailyPoints >= entryValueTSL*0.005))
enableShortTSL := true
//Trailing logic
//Trailing logic
if enableLongTSL or enableShortTSL
//for i=0 to slMult//-1
while slMult>=1 and ((entryType == "Long" and stopLossValueTSL+(slPointsTSL) <= low) or (entryType == "Short" and stopLossValueTSL-(slPointsTSL) >= high))
if entryType == "Long"
stopLossValueTSL := slMult ==1 ? (stopLossValueTSL + slPointsTSL) - tSLMarginPer : stopLossValueTSL + slPointsTSL
else if entryType == "Short"
stopLossValueTSL := slMult ==1 ? (stopLossValueTSL - slPointsTSL) + tSLMarginPer : stopLossValueTSL - slPointsTSL
slMult := slMult-1
//if bar_index >= debugBarIndex-50 and bar_index <=debugBarIndex+50
// log.warning("calcTrailingStopLoss - slMult After:{0}",slMult)
if entryType == "Long" and (prevStopLossValue < stopLossValueTSL)
//log.info("calcTrailingStopLoss - inside label printing")
label.new(bar_index, high, text="SL="+ str.tostring(stopLossValueTSL)+"|slP="+str.tostring(slPointsTSL), color=color.new(color.green, 80))
else if entryType == "Short" and (prevStopLossValue > stopLossValueTSL)
label.new(bar_index, low, text="SL="+ str.tostring(stopLossValueTSL)+"|slP="+str.tostring(slPointsTSL), color=color.new(color.red, 80),style = label.style_label_up)
//log.info("calcTrailingStopLoss - enableLongTSL:{0}|enableShortTSL:{1}|prevStopLossValue:{2}|stopLossValueTSL:{3}|slMult:{4}",enableLongTSL,enableShortTSL,prevStopLossValue,stopLossValueTSL,slMult)
enableLongTSL := false
enableShortTSL := false
[stopLossValueTSL, enableLongTSL, enableShortTSL]
I get error on line (slMult := slMult-1)
at different candles, on specific conditions, not always. Can you advise what is the exact problem here?
I tried putting logs and manual calculations to understand why it is failing but no luck. There is no way my calculations can take this long time, there has to be some leaks or garbage value that is cause this issue.
Vishnu Kant is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1