Exemple #1
0
    def update(self):
        """ Checks for new trades and updates the candle data. """

        new_trades = self.tradesource.new_trades
        if new_trades:
            self.active_trades += [{
                'timestamp': int(trade['timestamp']),
                'price': Decimal(trade['price']),
                'amount': Decimal(trade['amount'])
            } for trade in new_trades]

            if not self.next_start:
                first = ut_to_dt(self.active_trades[0]['timestamp'])
                start = datetime(year=first.year,
                                 month=first.month,
                                 day=first.day)
                while start + self.step < first:
                    start += self.step
                self.next_start = start + self.step
                self.active_candle = get_candle(dt_to_ut(start),
                                                self.active_trades)
                self.last_closed_known = False

            # dump older trades if active candle has closed,
            # accounting for possible gaps
            while ut_to_dt(
                    self.active_trades[-1]['timestamp']) > self.next_start:
                self.dump()

            # update active candle
            new_candle = get_candle(self.active_candle[0], new_trades)
            self.active_candle = self.update_candle(self.active_candle,
                                                    new_candle)
Exemple #2
0
    def update(self):
        """ Checks for new trades and updates the candle data. """

        new_trades = self.tradesource.new_trades
        if new_trades:
            self.active_trades += [{'timestamp': int(trade['timestamp']),
                                    'price': Decimal(trade['price']),
                                    'amount': Decimal(trade['amount'])}
                                   for trade in new_trades]

            if not self.next_start:
                first = ut_to_dt(self.active_trades[0]['timestamp'])
                start = datetime(
                    year=first.year, month=first.month, day=first.day)
                while start + self.step < first:
                    start += self.step
                self.next_start = start + self.step
                self.active_candle = get_candle(
                    dt_to_ut(start), self.active_trades)
                self.last_closed_known = False

            # dump older trades if active candle has closed,
            # accounting for possible gaps
            while ut_to_dt(
                    self.active_trades[-1]['timestamp']) > self.next_start:
                self.dump()

            # update active candle
            new_candle = get_candle(self.active_candle[0], new_trades)
            self.active_candle = self.update_candle(
                self.active_candle, new_candle)
Exemple #3
0
    def dump(self):
        """ Run once the candle is completed, to close the candle and record 
        it to the candle data file. 
        """
        to_dump = [t for t in self.active_trades if ut_to_dt(t['timestamp']) < self.next_start]
        to_keep = [t for t in self.active_trades if ut_to_dt(t['timestamp']) >= self.next_start]

        if len(to_dump):
            if not self.quiet:
                print '{} {}{} candle closed at {} with {} trades'.format(self.exchange, self.p_value, self.p_unit, to_dump[-1]['price'], len(to_dump))
            
            dump_candle = get_candle(self.active_candle[0], to_dump)
            
            self.closed_candles.append(dump_candle)       
        
            if self.record_candles:
                # if last entry not closed, pop out the last entry before rewriting with update
                if not self.last_closed_known:
                    save_candlefile(self.closed_candles, self.period, self.candlefile, replace=True)
                else:
                    save_candlefile([dump_candle], self.period, self.candlefile, replace=False)
        
        self.active_trades = to_keep
        self.active_candle = get_candle(dt_to_ut(self.next_start), [to_keep[0]])
        
        self.next_start += self.step

        # only closed candles are saved, so last will always be closed on further updates
        self.last_closed_known = True