def __init__(self, period, *args, **kwargs): self.period = period self.indicators = [] self.candleGenerator = CandleGenerator(period) self.times = [] self.opens = [] self.highs = [] self.lows = [] self.closes = [] self.volumes = [] logger.info("initializing timeframe") if 'indicators' in kwargs: if not type(kwargs['indicators']) is list: logger.debug( self + "; indicators passed via keywords is not a list as required." ) else: self.indicators = kwargs['indicators']
def __init__(self, period, *args, **kwargs): self.period = period self.indicators = [] self.candleGenerator = CandleGenerator(period) self.times = [] self.opens = [] self.highs = [] self.lows = [] self.closes = [] self.volumes = [] logger.info("initializing timeframe") if kwargs.has_key('indicators'): if not type(kwargs['indicators']) is list: logger.debug(self + "; indicators passed via keywords is not a list as required.") else: self.indicators = kwargs['indicators']
class TimeFrame: def __init__(self, period, *args, **kwargs): self.period = period self.indicators = [] self.candleGenerator = CandleGenerator(period) self.times = [] self.opens = [] self.highs = [] self.lows = [] self.closes = [] self.volumes = [] logger.info("initializing timeframe") if kwargs.has_key('indicators'): if not type(kwargs['indicators']) is list: logger.debug(self + "; indicators passed via keywords is not a list as required.") else: self.indicators = kwargs['indicators'] def append(self, value): # check if valid input if not self._validate(value): return False self.virtualCheck(value) self.updateLists(value) for ind in self.indicators: ind.append(value) def handleTick(self, time, last): if not self._validateTick(time, last): # raise Error return candles = self.candleGenerator.processTick(time, last) if candles: for candle in candles: self.append(candle) def _validate(self, value): if type(value) is tuple: if len(value) != 6: logger.error('invalid input: tuple length should be 6; input: %s' % (value,)) return False elif not type(value[0]) is datetime.datetime: logger.error('invalid input: tuple element [0] should be a datetime; input: %s' % (value[0],)) return False else: for i in range(1,6): if type(value[i]) is int or type(value[i]) is float: continue else: logger.error('invalid input: tuple element [%s] is not int or float; input: %s' % (i,value[i])) return False if len(self.times) > 0 and value[0] < self.times[-1]: logger.error('invalid input: tuple element [0] (datetime) should be greater than previous: %s; input: %s' % (self.times[-1]), value[0]) return False return True else: logger.error('invalid input: should be a tuple (d, o, h, l, c, v); input: %s' % (value,)) return False def _validateTick(self, time, last): if not time: logger.error('invalid input: time is not supplied') return False if not last: logger.error('invalid input: last is not supplied') return False if not type(time) is datetime.datetime: logger.error('invalid input: time should be a datetime; input: %s' % (time,)) return False if not (type(last) is int or type(last) is float): logger.error('invalid input: last should be an int or float; input: %s' % (last,)) return False return True def virtualCheck(self, value): if len(self.times) > 0 and self.times[-1] == value[0]: # now revert back to previous state, remove previous virtual candle self.times = self.times[:-1] self.opens = self.opens[:-1] self.highs = self.highs[:-1] self.lows = self.lows[:-1] self.closes = self.closes[:-1] self.volumes = self.volumes[:-1] def updateLists(self, value): self.times.append(value[0]) # datetime self.opens.append(value[1]) # open self.highs.append(value[2]) # high self.lows.append(value[3]) # low self.closes.append(value[4]) # close self.volumes.append(value[5]) # volume # overloads def __str__(self): string = '' #TODO return 'TimeFrame(%s):\n' % self.period def __repr__(self): return 'TimeFrame(%s)' % self.period def __len__(self): return len(self.times) def __getitem__(self, offset): return (self.times[offset], self.opens[offset], self.highs[offset], self.lows[offset], self.closes[offset], self.volumes[offset]) def __getslice__(self, low, high): #TODO return
class TimeFrame: def __init__(self, period, *args, **kwargs): self.period = period self.indicators = [] self.candleGenerator = CandleGenerator(period) self.times = [] self.opens = [] self.highs = [] self.lows = [] self.closes = [] self.volumes = [] logger.info("initializing timeframe") if 'indicators' in kwargs: if not type(kwargs['indicators']) is list: logger.debug( self + "; indicators passed via keywords is not a list as required." ) else: self.indicators = kwargs['indicators'] def append(self, value): # check if valid input if not self._validate(value): return False self.virtualCheck(value) self.updateLists(value) for ind in self.indicators: ind.append(value) def handleTick(self, time, last): if not self._validateTick(time, last): # raise Error return candles = self.candleGenerator.processTick(time, last) if candles: for candle in candles: self.append(candle) def _validate(self, value): if type(value) is tuple: if len(value) != 6: logger.error( 'invalid input: tuple length should be 6; input: %s' % (value, )) return False elif not type(value[0]) is datetime.datetime: logger.error( 'invalid input: tuple element [0] should be a datetime; input: %s' % (value[0], )) return False else: for i in range(1, 6): if type(value[i]) is int or type(value[i]) is float: continue else: logger.error( 'invalid input: tuple element [%s] is not int or float; input: %s' % (i, value[i])) return False if len(self.times) > 0 and value[0] < self.times[-1]: logger.error( 'invalid input: tuple element [0] (datetime) should be greater than previous: %s; input: %s' % (self.times[-1]), value[0]) return False return True else: logger.error( 'invalid input: should be a tuple (d, o, h, l, c, v); input: %s' % (value, )) return False def _validateTick(self, time, last): if not time: logger.error('invalid input: time is not supplied') return False if not last: logger.error('invalid input: last is not supplied') return False if not type(time) is datetime.datetime: logger.error( 'invalid input: time should be a datetime; input: %s' % (time, )) return False if not (type(last) is int or type(last) is float): logger.error( 'invalid input: last should be an int or float; input: %s' % (last, )) return False return True def virtualCheck(self, value): if len(self.times) > 0 and self.times[-1] == value[0]: # now revert back to previous state, remove previous virtual candle self.times = self.times[:-1] self.opens = self.opens[:-1] self.highs = self.highs[:-1] self.lows = self.lows[:-1] self.closes = self.closes[:-1] self.volumes = self.volumes[:-1] def updateLists(self, value): self.times.append(value[0]) # datetime self.opens.append(value[1]) # open self.highs.append(value[2]) # high self.lows.append(value[3]) # low self.closes.append(value[4]) # close self.volumes.append(value[5]) # volume # overloads def __str__(self): string = '' #TODO return 'TimeFrame(%s):\n' % self.period def __repr__(self): return 'TimeFrame(%s)' % self.period def __len__(self): return len(self.times) def __getitem__(self, offset): return (self.times[offset], self.opens[offset], self.highs[offset], self.lows[offset], self.closes[offset], self.volumes[offset]) def __getslice__(self, low, high): #TODO return