Alastair King
Alastair King

Reputation: 1

Code not plotting - Function should be called on each calculation for consistency. It is recommended to extract the call from this scope

I am getting this as an orange error - not sure what I have done wrong or whether this error is the reason that the code doesn't plot.

Code not plotting - Function should be called on each calculation for consistency. It is recommended to extract the call from this scope

//@version=5
indicator("PCY TEST4")

//Get User Inputs
fast_length = input(title="Fast Length", defval=12)
slow_length = input(title="Slow Length", defval=26)
src = input(title="Source", defval=close)
signal_length = input.int(title="Signal Smoothing",  minval = 1, maxval = 50, defval = 9)
sma_source = input.string(title="Oscillator MA Type",  defval="EMA", options=["SMA", "EMA"])
sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"])

//Variables 
a = 0.33
PCY = 0.0
PCYi = 0.0

// Calculating
fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macd = fast_ma - slow_ma

minMacd=0.0
for i = 0 to (signal_length[1])
    minMacd := ta.min(macd)
    
maxMacd=0.0
for i = 0 to (signal_length[1])
    maxMacd := ta.max(macd)

//maxMacd = ta.min(macd)
//minMacd = ta.max(macd)
//signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
//hist = macd - signal
delta = maxMacd-(fast_ma - slow_ma)/(maxMacd -minMacd)

for i = 0 to (signal_length)
    PCYi := a*((delta - PCYi[1])+PCYi[1])
    PCYi

//Plotting
plot(PCYi)

Upvotes: 0

Views: 65

Answers (1)

Dave
Dave

Reputation: 865

I think it's because you're calculating the min/max in a for loop Those functions should (not mandatory but strongly recommended) be calculated at every candle instead

for i = 0 to (signal_length[1])
    minMacd := ta.min(macd)
    
maxMacd=0.0
for i = 0 to (signal_length[1])
    maxMacd := ta.max(macd)

If you want to calculate the min/max over a X period of time, maybe use the ta.highest/ta.lowest functions instead :) And of course, those 2 should be called at every candle (i.e. not in a if, for,... statement)

Upvotes: 1

Related Questions