def update(self): """Returns: void Replace the candles with the most recent ones available. Algorithm for minimizing number of updated candles: Get the time difference from chart end to now. If the time difference is greater than the width of the chart, request <chart size> candles. else, request candles from end of chart to now. """ new_history = None if self.get_lag() > self.get_time_span(): # replace all candles new_history = Broker.get_instrument_history( instrument=self._instrument, granularity=self._granularity, count=self.get_size(), to=datetime.datetime.utcnow() ) else: # request new candles starting from end of chart # TODO verify candleFormat is same as existing chart new_history_ = broker.get_instrument_history( instrument=self._instrument, granularity=self._granularity, from_time=self.get_end_timestamp ) if new_history == None: Log.write('chart.py update(): Failed to get new candles.') raise Exception else: # Got new candles. Stow them. new_candles = new_history['candles'] # Iterate forwards from last candle. The last candle is probably # non-complete, so overwrite it. This thereby fills in the missing # gap between (end of chart) and (now). If the gap is smaller # than the size of the chart, only the beginning of the chart is # overwritten. If the gap is bigger than the chart, all candles # get overwritten. for i in range(0, len(self._candles)): # TODO assuming bid/ask candles new_candle = new_candles[i] self._candles[self._get_end_index()].timestamp = util_date.string_to_date(new_candle['time']) self._candles[self._get_end_index()].volume = float(new_candle['volume']) self._candles[self._get_end_index()].complete = bool(new_candle['complete']) self._candles[self._get_end_index()].open_bid = float(new_candle['bid']['o']) self._candles[self._get_end_index()].open_ask = float(new_candle['ask']['o']) self._candles[self._get_end_index()].high_bid = float(new_candle['bid']['h']) self._candles[self._get_end_index()].high_ask = float(new_candle['ask']['h']) self._candles[self._get_end_index()].low_bid = float(new_candle['bid']['l']) self._candles[self._get_end_index()].low_ask = float(new_candle['ask']['l']) self._candles[self._get_end_index()].close_bid = float(new_candle['bid']['c']) self._candles[self._get_end_index()].close_ask = float(new_candle['ask']['c']) if i < len(self._candles) - 1: self._increment_start_index() # increments end index too
def __init__( self, in_instrument, # <Instrument> granularity='S5', # string - See Oanda's documentation count=None, # int - number of candles start=None, # datetime - UTC end=None, # datetime - UTC price='MBA', # string include_first=None, # bool daily_alignment=None, # int alignment_timezone=None, # string - timezone weekly_alignment=None ): self._candles = [] # verify instance of <Instrument> by accessing a member. if in_instrument.get_id() == 0: pass if not count in [0]: # do send None # get candles from broker instrument_history = Broker.get_instrument_history( instrument=in_instrument, granularity=granularity, count=count, from_time=start, to=end, price=price, include_first=include_first, daily_alignment=daily_alignment, alignment_timezone=alignment_timezone, weekly_alignment=weekly_alignment ) if instrument_history == None: Log.write('chart.py __init__(): Failed to get instrument history.') raise Exception else: candles_raw = instrument_history['candles'] for c_r in candles_raw: new_candle = Candle( timestamp=util_date.string_to_date(c_r['time']), volume=float(c_r['volume']), complete=bool(c_r['complete']), open_bid=float(c_r['bid']['o']), high_bid=float(c_r['bid']['h']), low_bid=float(c_r['bid']['l']), close_bid=float(c_r['bid']['c']), open_ask=float(c_r['ask']['o']), high_ask=float(c_r['ask']['h']), low_ask=float(c_r['ask']['l']), close_ask=float(c_r['ask']['c']) ) self._candles.append(new_candle) self._instrument = in_instrument self._granularity = granularity self._start_index = 0 # start self._price = price self.include_first = include_first self.daily_alignment = daily_alignment self._alignment_timezone = alignment_timezone self.weekly_alignment = weekly_alignment