you could write them to csv, or if you are using pycharm you can have the console output to csv
did you look at the backtrader.analyzers.Transactions?
Best posts made by Brad Lloyd
-
RE: Reach _trades list
Latest posts made by Brad Lloyd
-
RE: Check the order status of Multiple Stocks
I was just saying I did the loop different using 'for' instead of enumerate
but I do something like thisparams = ('portfolio', ['AAPL', 'GOOG', 'SPY', 'IWM', 'EEM']) def next(self): for symbol in self.p.portfolio: d = self.getdatabyname(symbol) if self.order[symbol]: continue if self.signal: self.order[symbol] = self.buy(d, size=self.trade_size[symbol])
-
RE: Check the order status of Multiple Stocks
@cwse
I create the self.order dict in init and don't have a problem
you would have to run it in debug and figure out what key it fails onthe only thing I could see is that my loop is different as I pass all my symbols as param called portfolio and then just do loop that is
for symbol in self.p.portfolio:
-
RE: Check the order status of Multiple Stocks
@cwse
I use the following for multiple symbolsin your init where you define self.order = None, make it a dict , self.order = {}
def __init__(self): self.order = {} for i, d in enumerate(d for d in self.datas if len(d)): self.order[d._name] = None def notify(self, order): # standard code for notification self.order[order.data._name] = None def next(self): for i, d in enumerate(d for d in self.datas if len(d)): if not self.order[d._name]: continue
-
RE: create a CSV of the result of optstrategy
try this:
cerebro.addwriter(bt.WriterFile, csv = True, out='your_strategy_results') -
RE: Reach _trades list
if you are using IB, you should be able to put each system in a sub account and manage the positions this way.
you can also access the IB transaction log and I think you can retrieve things by order id, but I know this was somewhat of an issue for us, IB was not all that helpful. on a daily basis you should probably reconcile what your system thinks your position is vs what the broker has, the brokers make mistakes more often than you would think
we had a similar issue with needing persistent state variables for live trading and ended up having to store them in a database, but I would think you could do the same in a single csv file and just read the last lines in at startup.
-
RE: resampling Pandas dataframe
note we solved it by creating a new cerebro class that lets us do the following
cerebro.add_feeds(startdate, enddate, portfolio_symbols, conflations)
-
RE: resampling Pandas dataframe
not sure how to get the formatted code to paste
startdate = datetime.datetime(2012, 1, 1) enddate = datetime.datetime(2013, 1, 3) def get_mstar_data(sym, startdate, enddate): s = startdate.year e = enddate.year df = None for i in range(s, e + 1): for filename in glob.glob("%s/%d/%s*_1min.gz" % (m_star_cbot_path, i, sym)): filedf = _create_df(filename) filedf = filedf.ix[startdate:enddate] print (len(filedf.index)) df = filedf if df is None else df.append(filedf) return mstar( fromdate=startdate, todate=enddate, dataname=df, name=sym, datetime=None ) data = get_mstar_data('ZC0Y', startdate, enddate): cerebro.adddata(data) cerebro.resampledata(data, name=ZC0Y15m, timeframe=bt.Timeframe.Minutes, compression=15)
-
resampling Pandas dataframe
not sure if anyone else has had trouble with loading data from a pandas dataframe and then using cerebro.resample, but we did. The dataframe for minute data loads find and then when I go to pass the dataframte to cerebro.resample I get nan values. if I use a csv it loads and resamples with no problems
so we ended up using pandas own resample feature which works just fine
-
RE: Problem with resampling of intraday data
your daily resampled data will have the previous days price because you don't know todays values
you might want to try and replay the data if you are wanting the daily values to update through out the day -
RE: Submit order 30 minutes before market close
@Ed-Bartosh
the bulk of the time you have 16:00 as you close, 16:15 for some of the index ETFs,
you can download the exchange calendars and then put the holidays in where you early closeif you have some timezone issues then try using pytz to manage that, I think there is some functionality in backtrader for it
another option would be to count minutes from the first trade of the day, but you still have to manage the holidays