-
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) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hello, saving @kernc some time here. I'll try to answer your questions.
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.)
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
Regarding your using the SMA example from the quickstart guide. That example (listed below) goes long and short. 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.
-
Very useful! |
Beta Was this translation helpful? Give feedback.
Hello, saving @kernc some time here. I'll try to answer your questions.
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…