-
Notifications
You must be signed in to change notification settings - Fork 653
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Date mismatch when using external axes object #364
Comments
@Vittorio94 Here is how you should be doing what you are trying to accomplish above: ap = mpf.make_addplot(candles.open)
mpf.plot(candles, addplot=ap, style='yahoo') This accomplishes the same thing with half the amount of code! The reason you are seeing the apparent x-axis mismatch is because you are allowing Again, however, I discourage the use of external axes mode unless absolutely necessary since it requires much more code and it necessarily disables some features of mplfinance. If you explain what else you are trying to accomplish, I can let you know if there is a simple way to do it with mplfinance or if external axes are appropriate. If you have not already read it, I suggest "Adding Your Own Technical Studies to Plots." |
@DanielGoldfarb |
@Vittorio94 If you don't find it inconvenient to put the markers in separate arrays, you can reduce the number of fig, axlist = mpf.plot(data,...,returnfig=True)
axlist[0].scatter(...) # or whatever else you choose to do here to plot your markers
mpf.show() Of course, if you find it simpler to just use external axes mode directly, then of course do that. Thanks for you interest in mplfinance. Please, if you don't mind, post an image of your final result. I very much enjoy seeing the creative things that people are doing with mplfinance. All the best. --Daniel |
@DanielGoldfarb
fig, axlist = mpf.plot(candles, show_nontrading=True, returnfig=True, style="classic", type="candle")
for i in range(len(positions)):
if positions.iloc[i].closedPL > 0:
lineColor = "limegreen"
else:
lineColor = "crimson"
axlist[0].plot(
[positions.iloc[i].entryDateTime, positions.iloc[i].exitDateTime],
[positions.iloc[i].entryPrice, positions.iloc[i].exitPrice],
color=lineColor,
linewidth=0.5,
linestyle="dashed",
)
if positions.iloc[i].direction == "long":
entryColor = "royalblue"
exitColor = "crimson"
axlist[0].scatter(
[positions.iloc[i].entryDateTime, positions.iloc[i].exitDateTime],
[positions.iloc[i].entryPrice, positions.iloc[i].exitPrice],
c=[entryColor, exitColor],
marker="x"
)
else:
entryColor = "crimson"
exitColor = "royalblue"
axlist[0].scatter(
[positions.iloc[i].entryDateTime, positions.iloc[i].exitDateTime],
[positions.iloc[i].entryPrice, positions.iloc[i].exitPrice],
c=[entryColor, exitColor],
marker="x"
)
mpf.show() |
@Vittorio94 |
Here as a small example how it can look like if you use multiple scatter plots. Also directly on a point if you take the circles, my "donuts". |
@fxhuhn |
The source table contains the OHLC data and some signals. I have separated the steps so that I can still understand the code later as well. First step is to assign the signals with the price. In my case, it is the "Low" or "High", and create a new column with them (prefix 'signal_').
Second step is my dict of scatter signals. It contains my signals that i want to see in the chart ('signal' is the name of the colum and the other key/values are scatter plot related):
Annotation. Calling it signal was not my best idea for readable code. Now we come to the last step. Build the scatter plot.
With the following calculation, I can ensure the appropriate spacing between signals in each chart so that there is no unnecessary overlap. Small hint for the calculation of the offset: I hope this is all understandable so far. Otherwise just ask ;-) |
Hi Daniel, In the above reply: You state: Does this (show_nontrading=True) remain the only way to achieve the scatter plot overlay onto of a regular chart? I am asking as the gaps in the chart from the weekend days makes the moving average look really wonky. I am using external axes to overlay multiple scatter plots (signals) onto my daily chart. I have be able to do the same rather easily with the make_addplot() function, with the caveat that I was unable to add labels or a legend with this method, and when trying to track multiple signals, this feature is a must. |
@grdnryn
It's difficult for me to answer because you haven't completely described what you did to try to add labels or legends other than
That said, I can take somewhat of a guess, and/or at least provide some insight into the issues involved here. Certainly you have not missed something simple because there are definitely some complexities going on. Before I describe them, let me first point out that as a general rule, if you can do something without using external axes mode then you should. You save yourself a lot of work by allowing mplfinance to do much of the work for you. This does not mean that you can't access the Axes objects at all (for labeling, annotations, etc). On the contrary, using The complexity here comes into play (as you mentioned) when you want to avoid gaps in the plot due to non-trading periods. There is no simple way to skip over datetimes that have no data associated with them. Essentially the x-axis then becomes non-linear, and discontinuous, with respect to time. One way to implement such a non-linear, discontinuous time axis, is to plot the data according to the row number in the dataframe (row number being linear and, at least in integer space, continuous). Then maintain a mapping between actual datetimes and row numbers or fractions thereof (for plotting purposes). This is how it is implemented inside mplfinance. For the most part, this is transparent to the user. For example, when using kwarg I say "for the most part" because when a user wants to do something with the Axes objects (from I am currently working on an enhancement that will make this easier, exposing mapping transform functions which will allow users to work entirely in datetime space, which is clearly more intuitive in most situations. I hope that helps. Let me know if you have more questions or need more clarification. All the best. --Daniel |
Thanks Daniel, Since output speed is not a determining factor here, I was able to do a combination of both (make_addplot and external axes). |
I have the following dataframe that contains ohlc data:

candles.head()
I want to plot the data using an external axes object and then add additional stuff on the chart using matplotlib. The problem is that there seems to be a mismatch in the way mplfinance and matplotlib handle dates. For example, the following code:
should overlay a line connecting all the opens of the candles. However, the result is this:

Am I doing something wrong? Thank you
The text was updated successfully, but these errors were encountered: