Bar Series Question

Dustin_LangDustin_Lang Posts: 25
edited March 2017 in FAQs

Is there a way to iterate only once over a bar series and get several data items from the same bar ? For example, if I want to make a ordered list of open/close prices : pseudo code list = [] bars = function to get list of bars for bar in bars: list.append(bar.open) list.append(bar.close) instead of iterate over bars.open once and on bars.close once ?

Comments

  • shayneshayne Posts: 70
    edited August 2017

    I'm sure there are other approaches, using itertools works

    from cloudquant.interfaces import Strategy
    import itertools
    
    class ZipBars(Strategy):
        @staticmethod
        def in_symbol_universe(symbol, md, service):
            return symbol == 'TQQQ'
    
    
        def on_start(self, md, order, service, account):
            bar = md.bar.daily(start=-100)
    
    
            ## itertools zip iterates over two equal length arrays
            for open, close in itertools.izip(bar.open,bar.close):
                print( '{:.2f}\t{:.2f}\t{:.2f}'.format(open, close, open-close) )
    
    
            service.terminate()
    
Sign In or Register to comment.