-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
-
Hi! I have a few questions about what I am looking at when analyzing the bt.plot. When looking at the plot below what exactly am I looking at? Can I see trade enter and exit? I see the red and green triangles, but those relate to the P/L when exiting a trade correct? Also what is the blue and yellow area on the top. And with the train tracks at the bottom that are supposed to be trades, why are some red and green? I dont think Im taking any trades short (Im just using the example sma algo with large n values to try and de-clutter the chart)
https://kernc.github.io/backtesting.py/doc/backtesting/backtesting.html#backtesting.backtesting.Backtest.plot
I looked through the documentation here, but it appears to just be parameters I can pass into the plot function. If there is documentation that I am missing that explains all this please feel free to link that! Any help would be greatly appreciated! Thank you so much!
Capture
Beta Was this translation helpful? Give feedback.
All reactions
Hello, saving @kernc some time here. I'll try to answer your questions.
Can I see trade enter and exit? I see the red and green triangles, but those relate to the P/L when exiting a trade correct?
You are correct about the green and red triangles. These refer to the exits not the entries. The size of the triangle represents the relative quantity of the the asset traded. In other words, the largest triangle on the entire plot would be the most bitcoin you ever bot/sold in a single trade. As you see in your plot, the triangles are large in the beginning, because the price of bitcoin was relatively small when you started your backtest relative to your account size. Then as the value of bit...
Replies: 2 comments 1 reply
-
Hello, saving @kernc some time here. I'll try to answer your questions.
Can I see trade enter and exit? I see the red and green triangles, but those relate to the P/L when exiting a trade correct?
You are correct about the green and red triangles. These refer to the exits not the entries. The size of the triangle represents the relative quantity of the the asset traded. In other words, the largest triangle on the entire plot would be the most bitcoin you ever bot/sold in a single trade. As you see in your plot, the triangles are large in the beginning, because the price of bitcoin was relatively small when you started your backtest relative to your account size. Then as the value of bitcoin rose, the trade quantities got smaller over time. The color of the triangle represents gain or loss, see how your first 5 trades end up losing money. The height of the triangle (Y-axis) is the size of the gain or loss. The up or down arrow of the triangle indicates whether or not it was a long position or a short position. Your 6th trade was closing out a long position for a profit (triangle green pointing up), and your 7th trade was closing out a short position for a profit (triangle green but down.)
I dont think Im taking any trades short (Im just using the example sma algo with large n values to try and de-clutter the chart)
You were taking both long and short trades. This should answer your "train tracks" question as well. Below, I'll show you how to change your strategy so that it is a long only strategy. But to clarify, the "train tracks" show the entry of a trade and exit. The green train tracks show a profitable trade and red for a loss.
The upper plot relates to your equity value. Imagine you started your account with 10,000ドル how much money did you make or lose over time. Represented as a %. (If you want to start the plot at zero instead so the last point on the plot represents the cumulative return instead of the "growth of 100" when you call plot()
use plot(plot_return=True)
Also what is the blue and yellow area on the top.
They represent the time period that you are in a "drawdown" from your previous peak asset value. Once your equity value crosses through another high it will start again. The red dot represents the bottom of the worst drawdown you ever would have experienced.
Regarding your using the SMA example from the quickstart guide. That example (listed below) goes long and short.
https://kernc.github.io/backtesting.py/doc/examples/Quick%20Start%20User%20Guide.html
from backtesting import Strategy from backtesting.lib import crossover class SmaCross(Strategy): # Define the two MA lags as *class variables* # for later optimization n1 = 10 n2 = 20 def init(self): # Precompute the two moving averages self.sma1 = self.I(SMA, self.data.Close, self.n1) self.sma2 = self.I(SMA, self.data.Close, self.n2) def next(self): # If sma1 crosses above sma2, close any existing # short trades, and buy the asset if crossover(self.sma1, self.sma2): self.position.close() self.buy() # Else, if sma1 crosses below sma2, close any existing # long trades, and sell the asset elif crossover(self.sma2, self.sma1): self.position.close() self.sell()
To change it to a long only strategy do this: (MY COMMENTS ARE IN ALL CAPS)
from backtesting import Strategy from backtesting.lib import crossover class SmaCross(Strategy): # Define the two MA lags as *class variables* # for later optimization n1 = 10 n2 = 20 def init(self): # Precompute the two moving averages self.sma1 = self.I(SMA, self.data.Close, self.n1) self.sma2 = self.I(SMA, self.data.Close, self.n2) def next(self): # If sma1 crosses above sma2, close any existing # short trades, and buy the asset if crossover(self.sma1, self.sma2): # self.position.close() # BASED ON THE BELOW, THIS WON'T BE NECESSARY self.buy() # Else, if sma1 crosses below sma2, close any existing # long trades, and sell the asset # WE AREN'T GOING TO SELL AFTER WE CLOSE elif crossover(self.sma2, self.sma1): self.position.close() # THIS IS THE CLOSING SELL ORDER, NO NEED TO PLACE IT AGAIN BELOW # self.sell() # COMMENT THIS LINE OUT NO NEED TO SELL SHORT
Hopefully this was helpful. Feel free to ask other questions here. Also don't hesitate to search the repo. @kernc has answered a ton of questions in the "issues" section that are now resolved. I learned a lot doing that when I first got started.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 6 -
❤️ 7
-
Thanks.
A big step for me after reading this! Maybe it should be pasted here: https://kernc.github.io/backtesting.py/doc/backtesting/backtesting.html#backtesting.backtesting.Backtest.plot
Beta Was this translation helpful? Give feedback.
All reactions
-
Very useful!
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 2