def __init__(self, symbol): super(_CoinPriceHistory, self).__init__() self._symbol = symbol.upper() with sql.GetCursor() as cursor: cursor.execute('SELECT timestamp, price FROM coinhistory ' 'where symbol = "%s"' % self.GetSymbol()) for t in cursor.fetchall(): self[t[0]] = t[1]
def Sell(self, symbol, amount, timestamp=None): timestamp = int(timestamp if timestamp else time.time()) with sql.GetCursor() as cursor: cursor.execute( 'INSERT INTO transactions (user_id, type, timestamp, out_symbol, out_amount) ' 'values (%s, "%s", %s, "%s", %s)' % (self._user_id, "SELL", timestamp, symbol.upper(), amount)) transaction = Transaction(type="SELL", timestamp=timestamp, out_symbol=symbol.upper(), out_amount=amount) self._transactions.insert(self._transactions.bisect(transaction), transaction) self.InitFromTransactions()
def Init(self, tuples, timestamp=None): """Takes a list of tuples of (symbol, amount).""" timestamp = int(timestamp if timestamp else time.time()) with sql.GetCursor() as cursor: cursor.execute( 'INSERT INTO transactions (user_id, type, timestamp) ' 'values (%s, "%s", %s)' % (self._user_id, "INIT", timestamp)) transaction = Transaction(type="INIT", timestamp=timestamp) self._transactions.insert(self._transactions.bisect(transaction), transaction) for t in tuples: self.Buy(t[0], t[1], timestamp, init=False) self.InitFromTransactions()
def __init__(self, user_id): super(PortfolioHistory, self).__init__() self._user_id = user_id with sql.GetCursor() as cursor: cursor.execute( 'SELECT type, timestamp, in_symbol, in_amount, out_symbol, out_amount ' 'FROM transactions where user_id = %s' % user_id) self._transactions = SortedList([ Transaction(type=t[0], timestamp=t[1], in_symbol=t[2], in_amount=t[3], out_symbol=t[4], out_amount=t[5]) for t in cursor.fetchall() ]) self.InitFromTransactions()
def _DownloadNewDataPoint(): market = coinmarketcap.Market() cmc_dict = market.ticker(limit=0) with sql.GetCursor() as cursor: for coin in sorted(cmc_dict, key=lambda d: int(d["rank"])): if coin["price_usd"]: try: cursor.execute( 'insert into coinhistory (symbol, price, timestamp) values ' '("%s", %s, %s)' % (coin["symbol"].upper(), float(coin["price_usd"].replace( ',', '')), int(time.time()))) except Exception as e: if not "Duplicate entry" in str(e): raise
def ClearRemote(self): with sql.GetCursor() as cursor: cursor.execute('DELETE FROM transactions where user_id = %s' % self._user_id) self.clear()