louisnw01 / lightweight-charts-python

Python framework for TradingView's Lightweight Charts JavaScript library.
MIT License
1.16k stars 215 forks source link

Horizontal line not showing in subchart [BUG] #324

Closed alloc7260 closed 7 months ago

alloc7260 commented 7 months ago

Expected Behavior

I have added a horizontal line in subchart with macd lines.

Current Behaviour

But the horizontal line doesn't showed up. macd lines are showing only.

Reproducible Example

chart = Chart(width=1400, height=800, title="BANKNIFTY", inner_height=0.7)
chart2 = chart.create_subchart(height=0.3, width=1, sync=True)

chart.set(df1)

sma_data = calculate_sma(df1, 20)
line = chart.create_line(name="sma")
line.set(sma_data)

chart2.horizontal_line(43200, color="white")

macd_data = calculate_macd(df1)
macd_line = chart2.create_line(name="macd", color="purple")
signal_line = chart2.create_line(name="signal", color="yellow")
macd_hist = chart2.create_histogram(name="histogram")
macd_line.set(macd_data)
signal_line.set(macd_data)
macd_hist.set(macd_data)

chart.show(block=True)

Environment

- OS: ubuntu 22.04
- Library: lightweight-charts  1.0.20
alloc7260 commented 7 months ago

image

alloc7260 commented 7 months ago

@louisnw01 and also histogram is not aligned with y-axis it is floating

watch this video

louisnw01 commented 7 months ago

Hey @alloc7260 ,

You are setting the horizontal line value on chart2 to a value of 43,200, whereas the MACD value only goes up to around 40-50.

Louis

alloc7260 commented 7 months ago

@louisnw01 it not showing the line, tried this also. chart2.horizontal_line(0, color="white")

alloc7260 commented 7 months ago

Hey @alloc7260 ,

You are setting the horizontal line value on chart2 to a value of 43,200, whereas the MACD value only goes up to around 40-50.

Louis

43200 is just example, not working any of value

louisnw01 commented 7 months ago

Ah I see the issue now; you want to use the horizontal_line method on the macd line object.

Let me know if you still have any issues!

Louis

alloc7260 commented 7 months ago

@louisnw01 yes louis, it is not showing the horizontal line on subcharts, i tried a lot. please help me.

louisnw01 commented 7 months ago

@louisnw01 yes louis,

it is not showing the horizontal line on subcharts, i tried a lot.

please help me.

Can you show me the current code you are using? Should be something like macd.horizontal_line(...)

alloc7260 commented 7 months ago

@louisnw01 yeah, sure

chart = Chart(width=1400, height=800, title="BANKNIFTY", inner_height=0.7)
chart2 = chart.create_subchart(height=0.3, width=1, sync=True)

chart.legend(True)
chart2.legend(True)

sma_data = calculate_sma(df1, 20)
line = chart.create_line(name="sma")
line.set(sma_data)

chart.set(df1)

macd_data = calculate_macd(df1)
# macd_ndj = ta.macd(df1.close, 12, 26, 9)
macd_line = chart2.create_line(name="macd", color="purple")
signal_line = chart2.create_line(name="signal", color="yellow")
macd_hist = chart2.create_histogram(name="histogram")
macd_line.set(macd_data)
signal_line.set(macd_data)
macd_hist.set(macd_data)

chart2.horizontal_line(0, color="white")

chart.show(block=True)
def calculate_macd(
    data: pd.DataFrame,
    short_period: int = 12,
    long_period: int = 26,
    signal_period: int = 9,
    source: str = "close",  # open / high / low / close
):
    short_ema = data[source].ewm(span=short_period, min_periods=1).mean()
    long_ema = data[source].ewm(span=long_period, min_periods=1).mean()
    macd_line = short_ema - long_ema
    signal_line = macd_line.ewm(span=signal_period, min_periods=1).mean()
    macd_histogram = macd_line - signal_line
    macd_data = pd.DataFrame(
        {
            "time": data["time"],
            "macd": macd_line,
            "signal": signal_line,
            "histogram": macd_histogram,
        }
    )

    return macd_data
louisnw01 commented 7 months ago

You're still putting the horizontal line on the chart object, not the macd object.

alloc7260 commented 7 months ago

@louisnw01 can you give me the corrected code.