@backtrader thanks so much for the tip!
Latest posts made by Rstrong
-
RE: Calling Trade.pnl within next()
-
Calling Trade.pnl within next()
Hello,
I have a very beginner question here that I am trying to figure out.
I am trying to get a unrealized PnL per position each time next() goes through another line in its data feed. Can someone point out how I would call trade.pnl within next()?
Thanks in advance
-
RE: Reducing run time
I am revising this topic because I still havnt been able to find a solution and realized I might have asked the question incorrectly.
Yes I agree that two different samples of tick data could have varying run times because of the length of the data, what what I am saying is that when I take the same group of three tick data samples, the length of time to run them all together is significantly longer than the sum of time to run them all individually.
An example for further clarity is below.
Take the following three stocks with their respective run/processing times.
Stock A: 5 seconds
Stock B: 7 seconds
Stock C: 9 secondsIf we add the amount of time it takes to run them all individually it takes 21 seconds.
However when I have them as part of a list and run them all within the same script, it takes over a minute.
This is what I am trying to figure out and trouble shoot. Iv also tried feeding them all to backtrader by resetting cerebro each time a new data set is processed, but its still running very slow.
-
Buying the open of the first candle
Hello,
I am running filtered data through a backtrader script in which I am buying the open of the first candle of every data set I put it through.
Is there a way to have backtrader do this without using cheat on open? If I use it then I have problems when trying to simulate exiting the position.
-
RE: Reducing run time
@backtrader could you provide a bit more color on what you mean?
In this script I filter the data to only put 10 minutes worth of data into backtrader for each data feed.
How else would I filter the data?
-
RE: Reducing run time
@backtrader i meant to put this in the help section. Can you give me authorization to delete it so i can repost it there?
-
Reducing run time
Hello,
So I am running a very simple script that i am using to simply plot the tick data I have.
When I run one stock through it individually it takes 5 seconds. However when I have a list of two stocks run through it, it takes ~75 seconds.
The lists containing the stock names are in CSV form with the date in one column and the ticker name in the other.
Does anyone have an idea of why this might be happening?
I have the script below.
import datetime from datetime import timedelta from datetime import date import os.path import sys from pathlib import Path import pandas as pd import backtrader.feeds as btfeeds import pandas_datareader.data as web import backtrader as bt import backtrader.indicators as btind import backtrader.analyzers as btanalyzers import re import time import matplotlib import backtrader.utils.flushfile import subprocess import sys """ Charter """ class Inputs: path = 'C:\Python27\.vscode\Excel Files\Stock_lists\Names_dates_small.csv' Stocklist = [] data_columns = ['Date', "Ticker"] with open(path) as csvfile: data = pd.read_csv(csvfile, names = data_columns) for index,row in data.iterrows(): Stocklist.append([row['Ticker'],row['Date']]) params = { 'starting_cash' : 100000, 'Bar_compression' : 1, 'Bar_type' : bt.TimeFrame.Seconds, } class BuyySell(bt.observers.BuySell): plotlines = dict( buy=dict(markersize=6.0), sell=dict(markersize=6.0),) params = ( ("barplot",True), ("bardist" ,0.0003)) class Algo(bt.Strategy): def next(self): # trade variables for i, d in enumerate(self.datas): if (len(self.datas[i])>0): Open = self.datas[i].open High = self.datas[i].high Low = self.datas[i].low Close = self.datas[i].close Volume = self.datas[i].volume Symbol = self.datas[i]._name Time = self.datas[i].datetime.time Date = self.datas[i].datetime.datetime position = self.broker.getposition(data = d) class run: def runstrat(self): #Algo Cerebro start, add strat, slippage, multiobserver cerebro = bt.Cerebro() cerebro.addstrategy(Algo) cerebro.addobservermulti(BuyySell) for i in Inputs.Stocklist: ticker = i[0] day = datetime.datetime.strptime(i[1],"%m/%d/%Y") date = day.strftime("%Y%m%d") dayy = day.strftime("%Y-%m-%d") path = ("Q:/data/equity_prints_quotes/csv/%s/%s_trades.csv" %(date,ticker)) # path tab here mypath = Path(path) if mypath.exists(): data_columns = ["day", "time" , "ticker", "price", "volume"] with open(path) as csvfile: data = pd.read_csv(csvfile, names = data_columns) data= pd.DataFrame(data) data['Open']= data['price'] data['High']= data['price'] data['Low']= data['price'] data['Close']= data['price'] data['date']= data[["day","time"]].apply(lambda x : '{} {}'.format(x[0],x[1]), axis=1) data['time'] = pd.to_datetime(data['time']) data= data.set_index('time') end_time = data.index[0] + timedelta(minutes = 10) data = (data.loc[:(end_time)]) data['date'] = pd.to_datetime(data['date']) data['date']= data['date'].astype('datetime64[ns]') data= data.set_index('date') data = data[["Open", "High", "Low", "Close", "volume",]] data2 = btfeeds.PandasData(dataname = data, timeframe=bt.TimeFrame.Ticks,) # cerebro.adddata(data2, name = ticker) cerebro.resampledata(data2, name= ('{}, {}'.format(ticker,dayy)), timeframe =Inputs.params['Bar_type'], compression = Inputs.params['Bar_compression']) #Cerebro: PnL, calc, Inputs, Broker, Sizer, Plot cerebro.broker.setcash(Inputs.params['starting_cash']) cerebro.addsizer(bt.sizers.FixedSize, stake=300) cerebro.broker.setcommission(commission=0.0001) cerebro.run() cerebro.plot(style = 'Tick', bardown = 'black',fmt_x_data = ('%H:%M:%S'),volume = True) if __name__ == '__main__': strat = run() strat.runstrat()
-
RE: Indicators for datas with less periods than the indicator requires
The only way I can possibly think to work around this is to record the len() of my data before i put it into cerebro. I would record it into a dictionary and then write something like:
if lendic['BRY'] < 250: period = lendic['BRY']
This seems messy though and I prefer not to do it if I dont have to.
EDIT
but now that I think of it, this doesnt solve the rolling period issue. -
RE: Indicators for datas with less periods than the indicator requires
@ab_trader Thanks for the reply. I really appriciate it.
I have indeed made such indicators within next(), but it makes back testing go much much slower.
What specifics would you like so I can provide some more color? I think you have a good idea of what I am trying to do.
A specific example might be trying to get year highs for the ticker BRY. I am using daily data as the feed for the stock. The stock had its IPO on July 26. This is my indicator for year highs.
for i, elm in enumerate(self.datas): Symbol = self.datas[i]._name High = self.datas[i].high self.Year_highs[Symbol] = btind.Highest(High, period = abs(Inputs.params['Year_bars']),plot = False)
Since the stock has been around for less than 250 days(periods) the code throws an error. I could do what you suggested and make the indicator in the next() method, but when processing many stocks it can take a while to complete a backtest.
-
Indicators for datas with less periods than the indicator requires
I am trying to get an indicator for IPO stocks. The trouble is that the indicators require a certain amount of periods to have passed for the indicator to work and sometimes there are less periods in the whole data set than the indicator requires.
I would like to write something like:
if len(self.datas) < 14: period = len(self.datas)
The trouble is that I dont know how to get the length of the data within the def init_(self): of the bt.strategy class.
Any idea how to do this?
EDIT
Actually the period would have to be a rolling for some and static for others. If the indicator I wanted to make was all time highs, and its the 3rd period of a data then that number would have a rolling period always referencing the first day. Is this something that is possible to do with a custom indicator?
However if the indicator was ATR for example, I would want the ATR of the last 3 periods until it hits 14 periods and then be 14 after that.