Esempio n. 1
0
def calc_change_percent(sess, ticker, start_time, now, peak=True):
    if peak:
        start = Ticker.peak(sess, ticker, start_time=start_time, now=now)
    else:
        start = Ticker.current_ask(sess, ticker, start_time)
    current = Ticker.current_ask(sess, ticker, now)
    if current is None or start is None:
        log.debug(f"Could not get current/start for {ticker} at s={start_time} n={now}: c={current}, p={start}")
        return 0, current

    return round(100 * (current - start) / start, 2), current
Esempio n. 2
0
 def get_allowed_diff(self, sess, coin):
     if coin == 'BTC':
         return BTC_DIFF_THRESH
     now = datetime.datetime.utcnow()
     current = Ticker.current_ask(sess, coin, now)
     if current is None:
         current = 1
     return BTC_DIFF_THRESH / current  # in terms of the coin
Esempio n. 3
0
def account_value_btc(sess, account, now):
    # TODO: remove this duplicate
    btc = account.balance('BTC')
    for coin in account.coins:
        if coin == 'BTC':
            continue  # BTC is priced in USD, everything else in BTC
        units = account.balance(coin)
        unit_price = Ticker.current_ask(sess, coin, now)
        btc += units * unit_price
    return btc
Esempio n. 4
0
def close_alt_positions(sess, account, period):
    for coin in account.coins:
        if coin == 'BTC':
            continue
        price = Ticker.current_ask(sess, coin, period)
        assert price is not None, f"no price for {coin} at {period}"
        proceeds = account.trade(coin, -account.balance(coin), price, period)
        assert proceeds >= 0, f"got {proceeds} when selling {coin}"
        current = account.balance('BTC')
        assert current >= 0, f"negative BTC in account"
        current += proceeds
        account.balances['BTC'] = current
Esempio n. 5
0
 def value_btc(self, sess, now=None):
     btc = self.balance('BTC')
     self.values_in_btc['BTC'] = btc
     for coin in self.coins:
         if coin == 'BTC':
             continue  # BTC is priced in USD, everything else in BTC
         units = self.balance(coin)
         unit_price = Ticker.current_ask(sess, coin, now)
         if not unit_price:
             continue
         value = units * unit_price
         self.values_in_btc[coin] = value
         btc += value
     return btc
Esempio n. 6
0
    def calculate_strengths(self, now, ticker, allow_missing=False):
        if self.prices.get(ticker) is None:
            self.fetch_data(ticker, now)

        hour_avgs = self.avg_by_hour(now, ticker, allow_missing)
        if hour_avgs is None:
            return None
        log.debug("Averages by hour: {}".format(hour_avgs))

        current_price = Ticker.current_ask(self.sess, ticker, now)

        if current_price is None or hour_avgs.get(1) is None:
            log.debug("No price for {} @ {}".format(ticker, now))
            return None

        return current_price, [hour_avgs[hour] / current_price
                               for hour in self.HOURS[1:]]
Esempio n. 7
0
def buy_and_hold(interval, coins, db_loc, step, balances):
    start, stop = interval
    account = Account(balances)
    # one share of each alt, one share of BTC
    btc_per_coin = account.balance('BTC') / (len(coins) + 1)
    with_fees = btc_per_coin - (btc_per_coin * 0.0025)

    db = create_db(db_loc)
    sess = new_session(db)

    for coin in coins:
        price = Ticker.current_ask(sess, coin, now=start)
        if not price:
            continue
        to_buy = with_fees / price
        cost = account.trade(coin, to_buy, price, start)
        account.update('BTC', cost, start)

    start_value = account_value_btc(sess, account, now=start)
    finish_value = account_value_btc(sess, account, stop)
    low = min(start_value, finish_value)
    high = max(start_value, finish_value)

    results = BacktestResult(start,
                             stop,
                             step,
                             start_value,
                             finish_value,
                             account.fees,
                             account.txns,
                             gain_txns=[],
                             loss_txns=[],
                             out_of_btc=0,
                             hit_coin_limit=0,
                             high=high,
                             low=low)
    log.debug("\nBuy and hold\n")
    results.print_results()

    return results