I just realized that I only created an indicator, but no strategy and they are two totally different things lol. All clear
Best posts made by marsario
-
RE: AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Indicat' object has no attribute '_addobserver'
-
RE: Custom indicator, how datas work in init
@run-out Amazing! Thanks a lot! All clear!
Latest posts made by marsario
-
RE: Store data from Indicators for later use
@run-out Hi! I could show the code if it really helps, but I'm looking for a way of working rather than help with a specific piece of code.
[The Backtrader flow is:
- Initializing cerebro, downloading stocks data from yahoo finance or csv and creating datas
- Calculating the indicators (usually in init)
- Doing the buy and sell (usually in next)
I'm looking for a way to avoid doing steps 1. and 2. each time, so I can only change step 3. in my trials.]
I haven't looked into preprocessing in pandas and importing. It might be what I need, but I am not sure I understand. Would you mind expanding?
Thanks a lot!
-
Store data from Indicators for later use
Hi!
I am running tests to adjust the weight of some indicators, but the code takes almost a day to run each time so it becomes very time-consuming. However, 99% of the time goes into calculating the indicators values whereas the strategy itself is relatively quick (and the strategy is the only part of the code that should change in each test, in fact). I am wondering if there could be a way to store the value of the indicators somewhere on my hard drive so they can be quickly retrieved rather than calculating them again each time.
Do you have suggestions on how I could do that?
Thanks a lot!
-
RE: math.log with linebuffer._LineDelay object
@ab_trader Is there a way still to find the logarithm inside an indicator? The solution that I thought I found is a mess and it won't work.
-
RE: math.log with linebuffer._LineDelay object
After some unsuccessful attempts, i came back to the strategy you recommended. I defined log like this:
from backtrader.functions import MultiLogic class Log(MultiLogic): flogic = math.log
Also, added one more line to the code you recommended so that I passed the mean to the line object:
class weightedlogmeanInd(bt.Indicator): lines = ('mean',) # output line (array) params = ( ('speed', 4), ('tau',0.01), ) def next(self): self.close_log[0] = Log(self.datas[0].close[0]) self.change[0] = self.close_log[0] - self.close_log[-1] self.weighted_change[0] = self.change[0] * self.weights[0] self.change_sum[0] = self.change_sum[-1] + self.weighted_change[0] self.lines.mean[0]= bt.Sum(self.change_sum)/len(self.weights)
This doesn't send an error, but doesn't behave as I wished (ideally, it should create a Mean line for each day of the datafeed. I realize that I don't understand how to use Indicators' Next method (I have created indicators but always inside Init). I wish I could find more resources to learn – Platform concepts and Indicators Development don't help me enough apparently. Do you have recommendations of how I could learn to build this indicator?
-
RE: math.log with linebuffer._LineDelay object
I'm trying to implement the indicator with a different logic. And I don't understand what happens.
class weightedlogmeanInd(bt.Indicator): lines = ('mean',) # output line (array) params = ( ('speed', 4), ('tau',0.01), ) weights = [] def __init__(self): self.weights = findweights(self.p.speed,self.p.tau) today=self.data.get(ago=0,size=len(self.weights)) yesterday=self.data.get(ago=-1,size=len(self.weights)) print("len(today):",len(today)) print("len(yesterday):",len(yesterday)) print("What is happening in today?", (today) ) print("What is happening in yesterday?",(yesterday) ) growth = np.log(today)-np.log(yesterday) print("data close:",self.data.close(0)) print("What is happening in growth?", growth )
The printout and error message:
(base) Simones-Air:Learning Backtrader simoneromeo$ python 26\ onestock\ strategy.py Launching the strategy Starting logmean strategy len(today): 0 len(yesterday): 27 What is happening in today? array('d') What is happening in yesterday? array('d', [133.74, 137.18, 136.76, 136.91, 136.01, 135.39, 135.13, 135.37, 133.19, 130.84, 129.71, 129.87, 126.0, 125.86, 125.35, 120.99, 121.26, 127.79, 125.12, 122.06, 120.13, 121.42, 116.36, 121.09, 119.98, 121.96, 121.03]) Traceback (most recent call last): File "26 onestock strategy.py", line 150, in <module> cerebro.run() ... File "26 onestock strategy.py", line 46, in __init__ growth = np.log(today)-np.log(yesterday) ValueError: operands could not be broadcast together with shapes (0,) (27,)
I don't undestand: why does
self.data.get(ago=0,size=len(self.weights)
return an empty array andself.data.get(ago=-1,size=len(self.weights)
return an array with the right length? -
RE: math.log with linebuffer._LineDelay object
@run-out said in math.log with linebuffer._LineDelay object:
def next(self):
self.close_log[0] = bt.Log(self.datas[0].close[0])
self.change[0] = self.close_log[0] - self.close_log[-1]
self.weighted_change[0] = self.change[0] * self.weights[0]
self.change_sum[0] = self.change_sum[-1] + self.weighted_change[0]Hi, thanks for your suggestion!
I'm getting AttributeError that backtrader doesn't have a Log method. That's suppose to be an indicator, isn't it?
If so I should be able to recreate it.
-
math.log with linebuffer._LineDelay object
Hello!
This is my indicator:
class weightedlogmeanInd(bt.Indicator): lines = ('mean',) # output line (array) params = ( ('speed', 4), ('tau',0.01), ) def __init__(self): weights = findweights(self.p.speed,self.p.tau) #this is an array change_sum = 0 period = len(weights) for day in range(0,-period,-1): change = self.data.close(0-day) - ( self.data.close(-1-day)) # change = math.log(self.data.close(0-day)) - math.log( self.data.close(-1-day)) weighted_change = change * weights[-day] change_sum = change_sum + weighted_change self.lines.mean = change_sum / period
I would like to change this line:
change = self.data.close(0-day) - ( self.data.close(-1-day))
into the line below, which is now commented out:
change = math.log(self.data.close(0-day)) - math.log( self.data.close(-1-day))
When I do it, I get the following error message:
TypeError: must be real number, not _LineDelay
Basically, I would like to use the logarithm to get the price change from yesterday to today. What would be the best way to do it?
Thanks a lot!
-
RE: Multiple datafeeds with different start dates
Hi! was checking this post again, and I have to admit I didn't understand what you meant. Did you recommend downloading historical data with yfinance rather than Backtrader's YahooFinanceData method? Why?
-
RE: YahooFinanceCSVData & "NVDA": not working
As you see, backtrader loads the datafeed properly but the valueerror comes as soon as run() starts.
-
RE: YahooFinanceCSVData & "NVDA": not working
Right! I forgot to include the error message. Here you go:
Traceback (most recent call last): File "22 smooth CSV.py", line 178, in <module> main() File "22 smooth CSV.py", line 27, in main cerebro.run() File "/Users/simoneromeo/opt/anaconda3/lib/python3.8/site-packages/backtrader/cerebro.py", line 1127, in run runstrat = self.runstrategies(iterstrat) File "/Users/simoneromeo/opt/anaconda3/lib/python3.8/site-packages/backtrader/cerebro.py", line 1212, in runstrategies data.preload() File "/Users/simoneromeo/opt/anaconda3/lib/python3.8/site-packages/backtrader/feed.py", line 688, in preload while self.load(): File "/Users/simoneromeo/opt/anaconda3/lib/python3.8/site-packages/backtrader/feed.py", line 479, in load _loadret = self._load() File "/Users/simoneromeo/opt/anaconda3/lib/python3.8/site-packages/backtrader/feed.py", line 710, in _load return self._loadline(linetokens) File "/Users/simoneromeo/opt/anaconda3/lib/python3.8/site-packages/backtrader/feeds/yahoo.py", line 133, in _loadline o = float(linetokens[next(i)]) ValueError: could not convert string to float: ''