Beispiel #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)
Beispiel #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)
Beispiel #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 
Beispiel #4
0
    def getCompletedTrades(self, weeks=4):
        """ Example response:
        {u'timestamp': Decimal('1386924359.0'),
         u'price': Decimal('906.19'),
         u'type': u'Buy',
         u'amount': Decimal('0.6605'),
         u'exchange': u'bitstamp'}
         """
        now = datetime.utcnow()
        start = dt_to_ut(now - timedelta(weeks=weeks))
        payload = {'symbol': self.base+self.alt, 'timestamp': start}
        response = self.api.past_trades(payload)

        # check response
        if response is None:
            return
        elif 'message' in response:
            self.messages.append(response['message'])
            return
        else:
            self.my_trades = response[::-1]
            return True
Beispiel #5
0
    def getCompletedTrades(self, weeks=4):
        """ Example response:
        {u'timestamp': Decimal('1386924359.0'),
         u'price': Decimal('906.19'),
         u'type': u'Buy',
         u'amount': Decimal('0.6605'),
         u'exchange': u'bitstamp'}
         """
        now = datetime.utcnow()
        start = dt_to_ut(now - timedelta(weeks=weeks))
        payload = {'symbol': self.base + self.alt, 'timestamp': start}
        response = self.api.past_trades(payload)

        # check response
        if response is None:
            return
        elif 'message' in response:
            self.messages.append(response['message'])
            return
        else:
            self.my_trades = response[::-1]
            return True
Beispiel #6
0
 def get_candles_from(self, timestamp):
     timestamp = dt_to_ut(ut_to_dt(timestamp) - self.step)
     g = itertools.dropwhile(
         lambda c: c[0] < timestamp, self.closed_candles)
     return list(g)
Beispiel #7
0
 def get_candles_from(self, timestamp):
     timestamp = dt_to_ut(ut_to_dt(timestamp) - self.step)
     g = itertools.dropwhile(lambda c: c[0] < timestamp,
                             self.closed_candles)
     return list(g)
Beispiel #8
0
 def get_candles_from(self, timestamp): # candles containing timestamp and beginning after timestamp
     timestamp = dt_to_ut(ut_to_dt(timestamp) - self.step)
     g = itertools.dropwhile(lambda c: c[0] < timestamp, self.closed_candles)
     return list(g)