@hamid-zargar Just solved it!
Posts made by Hamid Zargar
What's wrong with my VWAP indicator?
Hi, I've tried to find out what's wrong with my indicator for 5 hours now and I can't seem to find what's wrong with it. I think I have a clue but I'm not sure how to solve it. When I print the the cumulative values of the lists for my calculations, they are wrong but I can't see where I went wrong. Also, when I print the volume variable, i get <backtrader.linebuffer.LineBuffer object at 0x0000027FA2DF3080> as the output which is an object but if i print the price variable I get the right price (float variable). I'm not sure why these are different since the data is being imported properly. My volume data is at the correct index in my .csv file. Also, I've noticed the when I initiate my VWAP indicator in my initializing function for my strategy, it alters the output of my trades and results although I havn't incorporated the VWAP in my trading logic yet. Also, I'm not sure if I'm I'm using the plotting function to plot my indicator properly or it's because of the errors I'm describing above.
Any help would be greatly appreciated. Thanks.
I'm initializing using this:
self.vwap = bt.indicators.VWAP(period=5)
And my vwap function is:
''' # There are five steps in calculating VWAP: # # Calculate the Typical Price for the period. [(High + Low + Close)/3)] # # Multiply the Typical Price by the period Volume (Typical Price x Volume) # # Create a Cumulative Total of Typical Price. Cumulative(Typical Price x Volume) # # Create a Cumulative Total of Volume. Cumulative(Volume) # # Divide the Cumulative Totals. # # VWAP = Cumulative(Typical Price x Volume) / Cumulative(Volume) ''' class VolumeWeightedAveragePrice(Indicator): alias = ('VWAP', 'VolumeWeightedAveragePrice',) lines = ('vwap',) params = (('period', 5),) plotinfo = dict(subplot=False) def __init__(self): self.addminperiod(self.p.period) self.cum_price_by_Volume = list() self.cum_volume = list() def _plotlabel(self): # This method returns a list of labels that will be displayed # behind the name of the indicator on the plot # The period must always be there plabels = [self.p.period] plabels += [self.lines.vwap] return plabels def next(self): price = (self.data.close + self.data.high + self.data.low) / 3 #check price print(price) volume = self.data.volume #check price print(volume) price_by_Volume = price * volume self.cum_price_by_Volume.append(price_by_Volume) self.cum_volume.append(volume) if len(self.cum_price_by_Volume) and len(self.cum_volume) >= self.p.period: #Check cumulative values for accuracy print(sum(self.cum_price_by_Volume[-1*(self.p.period):])) print(sum(self.cum_volume[-1*(self.p.period):])) # get last n'th (period) values in list and perform calculations for VWAP. This is because # the latest values go at the end of the list due to the way data is imported (earliest to latest) self.lines.vwap = sum(self.cum_price_by_Volume[-1*(self.p.period):]) / sum(self.cum_volume[-1*(self.p.period):])
RE: How do i check an order type?
here is the code, I need to replace if self.buy and if self.sell with the appropriate check
else: #TO CLOSE A LONG POSITION if self.buy() and (self.ma1 < self.ma2): # SELL, SELL, SELL!!! (with all possible default parameters) self.log('SELL CREATE, %.2f' % self.dataclose) # Keep track of the created order to avoid a 2nd order self.order = self.sell() #TO CLOSE A SHORT POSITION elif self.sell() and (self.ma1 > self.ma2): # BUY!!! BUY!!! BUY!!! (with all possible default parameters) self.log('BUY CREATE, %.2f' % self.dataclose) # Keep track of the created order to avoid a 2nd order self.order = self.buy()
How do i check an order type?
I'm new to algo trading/programming and I'm trying to get my head around all this.
I'm making a simple moving average crossover strategy for learning purposes and I was wondering how do I check the position type (whether its a long or short position) so I can exit my positions? I can't seem to find anything in the docs for this and I bet it's a simple line or attribute in one of the classes that I'm missing. If someone could post the line of code for checking an position type I would greatly appreciate it.