Python - issue with plotting CandleStick plotly chart

Viewed 158

I am having trouble plotting values on Candlestick plotly chart. For some reason, I get all green bars instead of green/red bars. Green for when values are up and red when the values go down.

Here is my code that you can run as is if you just install cbpro:

# Import Dependencies
import time
import cbpro
import pandas as pd
import json
import plotly.graph_objects as go
from datetime import datetime

# Establish Public Client
public_client = cbpro.PublicClient()

# Create start and end time variable
stime = public_client.get_time()
etime = list(public_client.get_time().values())[0]
# Get past transaction for ETH
past_transactions = pd.DataFrame(public_client.get_product_historic_rates(product_id='ETH-USD'))
# Rename the columsn
past_transactions.columns= ["Date","Open","High","Low","Close","Volume"]
# Format the date
past_transactions['Date'] = pd.to_datetime(past_transactions['Date'], unit='s')
# Set the date as index
past_transactions.set_index('Date', inplace=True)
# Sorting values by date
past_transactions.sort_values(by='Date', ascending=True, inplace=True)

# Plot the values on CandleStick plotly chart
fig = go.Figure(data=[go.Candlestick(x= past_transactions.index,
                                            open = past_transactions['Open'],
                                            high = past_transactions['High'],
                                            low = past_transactions['Low'],
                                            close = past_transactions['Close']
                                            )])

fig.show()
past_transactions

What am I doing wrong here?

2 Answers

This is going to sound a bit strange, but please hear me out. And I might be wrong here, but to me this seems to be a broblem with your dataset. Either the data is strange all the way, or the following references are wrong:

open = past_transactions['Open'],
high = past_transactions['High'],
low = past_transactions['Low'],
close = past_transactions['Close']

By that I mean that lows have been mixed up with close or something to that effect. And what on earth can I mean by the data is strange all the way? I look at candle charts from a variety of markets all day long, and what we're looking at in your figure just can not represent any "normal" market. In theory, there would be absolutely nothing wrong with a chart that looks like yours as lon as close is higher than open for each and every time interval.

enter image description here

And that's exactly what we're looking at in your case with all those "flat" candles, meaning that low = open for all of them. Or in candle chart terms: we're not seeing any Lower Shadow

enter image description here

And that just would not happen for that many observations in any liquid and well functioning market. I may be wrong about that though, but I'm not wrong in that your plotly figure is producing the correct visualization given your data specifications.

I tried running the code you have added. But, I do get red and green bars. But the reds are not that visible because they are fewer in number and smaller. The plot: Plot with red and Green Bars

Related