mplfinance - plotting the trades of an automated strategy against price candlesticks - having issues with "duplicate labels" in index?

99 views Asked by At

I have price data for a ticker which I have inserted into a dataframe with the following columns.

["timestamp", "open", "high", "low", "close"]

I am trying to display 15-second candles and this portion of my code works correctly. When I create the dataframe, I set the index to be "timestamp" .

candles_df.set_index("timestamp", inplace=True)

I also have a crappy automated strategy (work in progress) which simulates trades that I want to plot on the chart. These trades are in a separate dataframe with columns

{"timestamp", "price"}

The timestamps for each trade is not at the start of a candle. When I tried to simply "make_addplot" with the original timestamps, I had issues when trying to reindex the timestamps of the trades to the candle timestamps. In order to get around this, I decided to round the timestamps down to the nearest 15-second mark.

Given that I'm new to pandas and dataframes, I made the timestamp the index for the trades as well.

The problem I'm running into now is that my strategy executed two trades within 1 second of each other. That means that after rounding the timestamps, two trades have the same timestamp which leads to a duplicate index value.

How can I get two trades on the same candle in mplfinance?

Is the mistake that I'm indexing the trades by "timestamp" after rounding?

Am I making a mistake by rounding the timestamp of each trade to the closest 15s candle?

Thank you guys in advance!

0

There are 0 answers