1

I have data from three stock market trades in a DataFrame e.g.

df2 = pd.DataFrame(np.array([
    [10:00, 11:00, 12:00], 
    [10:03, 11:07, 12:09], 
    [67093, 67010, 66000], 
    [67095, 67012, 66010]]),
    columns=[
        'time_opened', 
        'time_closed', 
        'level_opened', 
        'level_closed'
    ]
)

I have a chart with time on the x axis, level (or stock price) on the y axis. I would like to draw a line/shape that represents each trade from each time_opened/level_opened to time_closed/level_closed. i.e. From 67093 at 10:00 to 67095 at 10:03

I'm trying to do something like this:

    fig.add_shape(
        dict(
            type='line',
            x0=positions['time_opened'],
            y0=positions['level_opened'],
            x1=positions['time_closed'],
            y1=positions['level_closed'],
            line=dict(color='Black', width=3),
        ),
    )
    fig.update_shapes(xaxis='x1')

Any ideas what I'm doing wrong?

1 Answer 1

1
import plotly.graph_objects as go
import pandas as pd

df = pd.DataFrame({'time_opened': ['10:00', '11:00', '12:00'],
                   'time_closed': ['10:03', '11:07', '12:09'],
                   'level_opened': [67093, 67010, 66000],
                   'level_closed': [67095, 67012, 66010]})

df['time_opened'] = pd.to_datetime(df['time_opened'])
df['time_closed'] = pd.to_datetime(df['time_closed'])

layout = dict(xaxis=dict(range=[df['time_opened'].min() - pd.Timedelta('30M'),
                                df['time_closed'].max() + pd.Timedelta('30M')],
                         type='date',
                         tickformat='%H:%M'))

data = []

for i in range(df.shape[0]):

    data.append(go.Scatter(x=[df['time_opened'][i], df['time_closed'][i]],
                           y=[df['level_opened'][i], df['level_closed'][i]],
                           name='trade ' + str(i + 1)))

fig = go.Figure(data=data, layout=layout)

fig.show()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.