Exemplo n.º 1
0
    def __init__(self, tradesource, period, record_candles=True, start=None, quiet=False):
        self.tradesource = tradesource
        self.p_value, self.p_unit = parse_period(period)
        self.period = period
        self.step = pdelta(self.p_value, self.p_unit)
        self.exchange, self.base, self.alt = tradesource.exchange, tradesource.base, tradesource.alt
        self.candlefile = CANDLES + '{}_{}_{}_{}{}'.format(self.exchange, self.base, self.alt, self.p_value, self.p_unit)
        self.record_candles = record_candles
        self.quiet = quiet

        # check for candle directory
        if not os.path.exists(CANDLES):
            build_data_directories()

        # check for candle file
        if os.path.exists(self.candlefile):
            with open(self.candlefile, 'rb') as readfile:
                reader = csv.reader(readfile, delimiter=',')
                if start:
                    self.closed_candles = [[int(candle[0])] + [Decimal(x) for x in candle[1:]] for candle in reader if ut_to_dt(candle[0]) < start]
                else:
                    self.closed_candles = [[int(candle[0])] + [Decimal(x) for x in candle[1:]] for candle in reader]
            self.active_candle = self.closed_candles.pop() 

        # if no candle file, check for trades in tradesource
        elif self.tradesource.trades:
            if not self.quiet: print 'No candlefile found; generating from tradesource...'
            if start:
                self.closed_candles = [[int(candle[0])] + [Decimal(x) for x in candle[1:]] 
                                        for candle in trades_to_candles(self.tradesource.trades, period)
                                        if ut_to_dt(candle[0]) < start]
            else:
                self.closed_candles = [[int(candle[0])] + [Decimal(x) for x in candle[1:]] 
                                        for candle in trades_to_candles(self.tradesource.trades, period)]
            # assume the last candle is still active
            self.active_candle = self.closed_candles.pop()
            
        # if no candles or trades
        else:
            if not self.quiet: print 'No candlefile found; no tradefile found; waiting for new trades...'
            self.closed_candles = []
            self.active_candle = []
            self.active_trades = []
            self.next_start = None
            
        if self.active_candle: # at least one candle was found
            self.next_start = ut_to_dt(self.active_candle[0]) + self.step       
        
            # assume last candle is not closed yet (check in update)
            self.last_closed_known = False
        
            # get trade data from most recent candle
            self.active_trades = [trade for trade in self.tradesource.trades 
                                    if trade['timestamp'] >= self.active_candle[0]]
Exemplo n.º 2
0
    def update(self):
        self.new_trades = []
        response = self.api.trades({}, self.symbol)
        if response:
            trades = sorted(response, key=lambda x: int(x['timestamp']))

            new_trades = [{
                'timestamp': int(t['timestamp']),
                'price': Decimal(t['price']),
                'amount': Decimal(t['amount'])
            } for t in trades
                          if t['timestamp'] > self.last_trade()['timestamp']
                          and t['exchange'] == self.exchange]

            if new_trades:
                self.new_trades = new_trades
                # print each new trade, and add it to the
                # trade file if record_trades==True
                for trade in new_trades:
                    if not self.quiet:
                        print "{}   {}   {} {}   {} {}".format(
                            ut_to_dt(trade['timestamp']), self.exchange,
                            trade['price'], self.alt, trade['amount'],
                            self.base)

                    self.trades.append({
                        'timestamp': int(trade['timestamp']),
                        'price': Decimal(trade['price']),
                        'amount': Decimal(trade['amount'])
                    })
                    self.price = self.trades[-1]['price']

                    # write new trades to tradefile
                    if self.record_trades:
                        tradefile = TRADES + '{}_{}_{}'.format(
                            self.exchange, self.base, self.alt)
                        if not os.path.exists(TRADES):
                            build_data_directories()

                        with open(tradefile, 'a') as writefile:
                            writer = csv.writer(writefile, delimiter=',')
                            writer.writerow([
                                trade['timestamp'], trade['price'],
                                trade['amount']
                            ])

        return self.new_trades
Exemplo n.º 3
0
    def update(self):
        self.new_trades = []
        response = self.api.trades({}, self.symbol)
        if response:
            trades = sorted(response, key=lambda x: int(x['timestamp']))

            new_trades = [{'timestamp': int(t['timestamp']),
                           'price': Decimal(t['price']),
                           'amount': Decimal(t['amount'])}
                          for t in trades
                          if t['timestamp'] > self.last_trade()['timestamp']
                          and t['exchange'] == self.exchange]

            if new_trades:
                self.new_trades = new_trades
                # print each new trade, and add it to the
                # trade file if record_trades==True
                for trade in new_trades:
                    if not self.quiet:
                        print "{}   {}   {} {}   {} {}".format(
                            ut_to_dt(trade['timestamp']), self.exchange,
                            trade['price'], self.alt, trade['amount'],
                            self.base)

                    self.trades.append({'timestamp': int(trade['timestamp']),
                                        'price': Decimal(trade['price']),
                                        'amount': Decimal(trade['amount'])})
                    self.price = self.trades[-1]['price']

                    # write new trades to tradefile
                    if self.record_trades:
                        tradefile = TRADES+'{}_{}_{}'.format(
                            self.exchange, self.base, self.alt)
                        if not os.path.exists(TRADES):
                            build_data_directories()

                        with open(tradefile, 'a') as writefile:
                            writer = csv.writer(writefile, delimiter=',')
                            writer.writerow([trade['timestamp'],
                                            trade['price'], trade['amount']])

        return self.new_trades