For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
How can I plot the broker value vs a stock?
-
I have been able to plot the AAPL stock and the value on different charts. I would like to have them on the same chart so I can compare them. How can I do that? I don't know if I can add a value indicator to the data or what. This is what I have.
import datetime # For datetime objects import os.path # To manage paths import sys # To find out the script name (in argv[0]) # Import the backtrader platform import backtrader as bt from _extensions.data_definitions import DOHLCVData AAPL_PATH = "./../../data/datasets/02_processed/eod/AAPL.csv" class LongAAPL(bt.Strategy): def next(self): self.order_target_percent(self.data, 1) if __name__ == '__main__': # Create a cerebro entity cerebro = bt.Cerebro(stdstats=False) # Create a Data Feed data = DOHLCVData( dataname=AAPL_PATH, headers=True, fromdate=datetime.datetime(2020, 1, 1), todate=datetime.datetime(2020, 12, 31), # month was in 12 reverse=False ) cerebro.adddata(data, name="AAPL") cerebro.broker.setcash(100000.0) cerebro.addobserver(bt.observers.Value) cerebro.addstrategy(LongAAPL) print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue()) # Run over everything cerebro.run() # Print out the final result print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue()) cerebro.plot()
-
Of course right now the lines would be the same since I'm buying AAPL 100% but I want so I can see the results after modifying the strategy. Thanks!
-
Check out benchmark observer -
Articles - Benchmarking
Docs - Observers - Benchmarking