@mtucker502 I believe your question is well documented here. Use compression.
-
RE: Resample data to timeframe other than bt.TimeFrame
-
RE: 'list' object is not callable : Error in Custom Indicator
We would like to help you but your question is not very informative. Please Include all of your code. Please also include the full log print out that has the error. Also please follow the instructions at the very top of this page for using three back ticks to wrap around your code.
For example:
put code here.
-
RE: How to extract historical account value from backtrader or its results
@t3chap Here is a working example: Make sure you have install
openpyxl
import backtrader as bt import pandas as pd class CashMarket(bt.analyzers.Analyzer): """ Analyzer returning cash and market values """ def create_analysis(self): self.rets = {} self.vals = 0.0 def notify_cashvalue(self, cash, value): self.vals = ( self.strategy.datetime.datetime().strftime("%Y-%m-%d"), cash, value, ) self.rets[len(self)] = self.vals def get_analysis(self): return self.rets class Strategy(bt.Strategy): def __init__(self): self.rsi = bt.ind.RSI(period=14) def next(self): if not self.position: if self.rsi <= 35: self.buy() elif self.rsi >= 65: self.close() if __name__ == "__main__": cerebro = bt.Cerebro() data = bt.feeds.GenericCSVData( dataname="data/2006-day-001.txt", dtformat=("%Y-%m-%d"), timeframe=bt.TimeFrame.Days, compression=1, ) cerebro.adddata(data) cerebro.addstrategy(Strategy) cerebro.addanalyzer(CashMarket, _name="cash_market") # Execute results = cerebro.run() # Dictionary dictionary = results[0].analyzers.getbyname("cash_market").get_analysis() df = pd.DataFrame(dictionary).T df.columns = ["Date", "Cash", "Value"] df.to_excel("Your file name here.xlsx", engine="openpyxl", index=False)
-
RE: How to make it quick start without waiting for indicators ready?
@goldcar Did you get a chance to look at backfilling?
-
RE: Sell order not executing from notify order
@vypy1 Your logs are not informative enough. Try using some or all of the following and then you'll get better data as to what's happening with your trades.
def notify_order(self, order): """ Triggered upon changes to orders. """ # Suppress notification if it is just a submitted order. if order.status == order.Submitted: return # Print out the date, security name, order number and status. dt, dn = self.datetime.date(), order.data._name type = "Buy" if order.isbuy() else "Sell" self.log( f"{order.data._name:<6} Order: {order.ref:3d}\tType: {type:<5}\tStatus" f" {order.getstatusname():<8} \t" f"Size: {order.created.size:9.4f} Price: {order.created.price:9.4f} " f"Position: {self.getposition(order.data).size}" ) if order.status == order.Margin: return # Check if an order has been completed if order.status in [order.Completed]: self.log( f"{order.data._name:<6} {('BUY' if order.isbuy() else 'SELL'):<5} " # f"EXECUTED for: {dn} " f"Price: {order.executed.price:6.2f} " f"Cost: {order.executed.value:6.2f} " f"Comm: {order.executed.comm:4.2f} " f"Size: {order.created.size:9.4f} " )
-
RE: Add fields of type String from csv
@jplaza This is a bit hacky, but you could also load the data in the
__init__
of your strategy. It's not highly recommended, and you would need to be diligent about your dates and forward looking bias. -
RE: Sell order not executing from notify order
@vypy1 Hi can you include your logs showing the trades? Thanks.
-
RE: Add fields of type String from csv
@jplaza Seems like backtrader is expecting floats:
he code expects all fields to be in place and be convertible to floats, except for the datetime which has a fixed YYYY-MM-DD format and can be parsed without using datetime.datetime.strptime.
I tried myself running text into backtrader and backtrader is definitely expecting floats (or convertable to floats) values.
I cannot see your data, but I wonder if there's a way to categorize your data into numbers and preprosses it before loading?
-
RE: Adding multiple data feeds in a for loop gives error
@willt Can you please elaborate on your error and start a new thread? Thanks.