For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Summing And Splicing Data



  • Hi Community,

    I'm new to programming but have a finance background.
    I'm trying to sum a backward looking range (ex take the highest value from the pervious 55 days). Not sure what I am doing wrong, any help would be greatly appreciated.

    Specifically:

            if self.dataclose[0] > max(self.data.get(0, -self.params.period)):
                # BUY, BUY, BUY!!! (with all possible default parameters)
                self.log('BUY CREATE, %.2f' % self.dataclose[0])
                self.buy()
    
    import pandas as pd
    import math
    import backtrader as bt
    
    class MyFirstStrategy(bt.Strategy):
        params = dict(period55 = 55)
        def log(self, txt, dt=None):
            ''' Logging function fot this strategy'''
            dt = dt or self.datas[0].datetime.date(0)
            print('%s, %s' % (dt.isoformat(), txt))
    
        def __init__(self):
            # Keep a reference to the "close" line in the data[0] dataseries
            self.dataclose = self.datas[0].close
    
        def next(self):
            # Simply log the closing price of the series from the reference
            self.log('Close, %.2f' % self.dataclose[0])
    
            if self.dataclose[0] > max(self.data.get(0, -self.params.period)):
                # BUY, BUY, BUY!!! (with all possible default parameters)
                self.log('BUY CREATE, %.2f' % self.dataclose[0])
                self.buy()
    


  • Best to create your highest indicator in the init.
    See example here and reference here.

    def __init__():
        self.highest_value = bt.ind.Highest(self.data.high, period=self.p.period)
    def next():
        if self.dataclose[0] > self.highest_value[0]:
            etc...
    


  • @run-out thank you so much. Really appreciate you helping me out.


Log in to reply
 

});