-2

Why does pine script do this?the two plots are from this code I have been testing on

here is the code I have been messing on:

//@version=4
// Copyright (c) 2019-present, Alex Orekhov (everget)
// Chandelier Exit script may be freely distributed under the terms of the GPL-3.0 license.
study("Chandelier Exit", shorttitle="CE", overlay=true)

length = input(title="ATR Period", type=input.integer, defval=1)
mult = input(title="ATR Multiplier", type=input.float, step=0.1, defval=2.0)
showLabels = input(title="Show Buy/Sell Labels ?", type=input.bool, defval=true)
useClose = input(title="Use Close Price for Extremums ?", type=input.bool, defval=true)
highlightState = input(title="Highlight State ?", type=input.bool, defval=true)

atr = mult * atr(length)
//plot(atr)

longStop = (useClose ? highest(close, length) : highest(length)) - atr
plot(longStop, color = color.rgb(255, 190, 51))
//plot(highest(close, length))
longStopPrev = nz(longStop[1], longStop) 
plot(longStopPrev)
longStop := close[1] > longStopPrev ? max(longStop, longStopPrev) : longStop
//plot(close[1])
//test = max(longStop, longStopPrev)
//plot (test)
//plot(longStop, color = color.rgb(255, 190, 51))

shortStop = (useClose ? lowest(close, length) : lowest(length)) + atr
shortStopPrev = nz(shortStop[1], shortStop)
//plot(lowest(close, length))
//plot(shortStopPrev)
//plot(shortStop)
shortStop := close[1] < shortStopPrev ? min(shortStop, shortStopPrev) : shortStop
//plot(shortStop, color = color.rgb(240, 108, 47))

var int dir = 1
dir := close > shortStopPrev ? 1 : close < longStopPrev ? -1 : dir

var color longColor = color.green
var color shortColor = color.red

//longStopPlot = plot(dir == 1 ? longStop : na, title="Long Stop", style=plot.style_linebr, linewidth=2, color=longColor)
buySignal = dir == 1 and dir[1] == -1
//plotshape(buySignal ? longStop : na, title="Long Stop Start", location=location.absolute, style=shape.circle, size=size.tiny, color=longColor, transp=0)
//plotshape(buySignal and showLabels ? longStop : na, title="Buy Label", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=longColor, textcolor=color.white, transp=0)

//shortStopPlot = plot(dir == 1 ? na : shortStop, title="Short Stop", style=plot.style_linebr, linewidth=2, color=shortColor)
sellSignal = dir == -1 and dir[1] == 1
//plotshape(sellSignal ? shortStop : na, title="Short Stop Start", location=location.absolute, style=shape.circle, size=size.tiny, color=shortColor, transp=0)
//plotshape(sellSignal and showLabels ? shortStop : na, title="Sell Label", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=shortColor, textcolor=color.white, transp=0)

midPricePlot = plot(ohlc4, title="", style=plot.style_circles, linewidth=0, display=display.none, editable=false)

longFillColor = highlightState ? (dir == 1 ? longColor : na) : na
shortFillColor = highlightState ? (dir == -1 ? shortColor : na) : na
//fill(midPricePlot, longStopPlot, title="Long State Filling", color=longFillColor)
//fill(midPricePlot, shortStopPlot, title="Short State Filling", color=shortFillColor)

changeCond = dir != dir[1]
alertcondition(changeCond, title="Alert: CE Direction Change", message="Chandelier Exit has changed direction!")
alertcondition(buySignal, title="Alert: CE Buy", message="Chandelier Exit Buy!")
alertcondition(sellSignal, title="Alert: CE Sell", message="Chandelier Exit Sell!")

I want to implement this behavior in python.

Thanks for anyone who can help me with this.

    # sort data
    high_prices = np.array([entry['high'] for entry in kline])
    low_prices = np.array([entry['low'] for entry in kline])
    close_prices = np.array([entry['close'] for entry in kline])

    # carculate atr
    result = talib.ATR(high_prices, low_prices, close_prices, timeperiod=length)
    result = np.round(result, decimals=1)         #round atr values
    atr = result*mult

    # long stop
    long_stop_1 = []
    for i in range(length, (len(kline)+1)):
        if useClose:
            highest_value = np.max(close_prices[-i:][:length])

        else:
            highest_value = np.max(high_prices[-i:][:length])

        test = highest_value - atr[-i]
        long_stop_1.insert(-len(long_stop_1), test)

    long_stop_prev = []
    for i in range(len(long_stop_1)-1):
        i = i+2
        long_stop_prev.insert(-len(long_stop_prev), long_stop_1[-i])

I thought that this is correct but long_stop_prev in totally wrong the long_stop_1 is totaly acurate to the longStop = (useClose ? highest(close, length) : highest(length)) - atr but i can not seem to get the correct long_stop_prev value

8meau
  • 1
  • 1

0 Answers0