Exemple #1
0
def importFundamentals(db, ticker, start_year=2010):
  # in db: row for each quarterly with all the necessary corresponding columns
  table_name = getTableName('simfin', ticker, 'fund')
  current_year = int(datetime.now().strftime('%Y'))
  quarters = ['q1', 'q2', 'q3', 'q4']
  for year in range(start_year, current_year):
    for q in quarters:
      stmt = simfin.getQuarterlyStatement(quarter=q, fyear=year, ticker=ticker)
      if 'error' in stmt:
        return stmt
      data = {
        'quarter': str(year) + '_' + q,
        'report_date': getVal(stmt, 'Report Date'),
        'publish_date': getVal(stmt, 'Publish Date'),
        'earnings_per_share': getVal(stmt, 'Earnings Per Share, Basic'),
        'equity_per_share': getVal(stmt, 'Equity Per Share'),
        'total_debt': getVal(stmt, 'Total Debt'),
        'cash_cash_eq_short_inv': getVal(stmt, 'Cash, Cash Equivalents & Short Term Investments'),
        'minority_interest': getVal(stmt, 'Minority Interest'),
        'preferred_equity': getVal(stmt, 'Preferred Equity'),
        'ebitda': getVal(stmt, 'EBITDA'),
        'free_cash_flow': getVal(stmt, 'Free Cash Flow'),
        'net_income': getVal(stmt, 'Net Income'),
        'dividends_paid': getVal(stmt, 'Dividends Paid'),
        'total_equity':  getVal(stmt, 'Total Equity')
      }
      print(ticker, year, q, 'Report Date:', data['report_date'])
      #print('Report Date:', data['report_date'])
      if data['report_date']:
        createFundamentalTableIfNotExists(db, table_name, data)
        insertFundamentalStmt(db, table_name, data)
Exemple #2
0
def getData(provider, symbol, candle_size, start_date, end_date):
    db = sqliteDB()
    db.connect()
    table_name = getTableName(provider, symbol, candle_size)
    candleDf = db.getCandles(table_name, start_date, end_date)
    db.disconnect()
    return candleDf
Exemple #3
0
def updateDayCandleData():
    def mergeCandlesAndPriceTarget(candles, priceTargetHist):
        targets = {}
        last_know_pt = None
        if not priceTargetHist:
            return candles
        for target in priceTargetHist:
            targets[target['date']] = target['value']
        for candle in candles:
            if candle['date'] in targets:
                candle['price_target'] = targets[candle['date']]
                last_know_pt = targets[candle['date']]
            else:
                candle['price_target'] = last_know_pt
        return candles

    tickers = getUsExchangeTickers()
    db = sqliteDB()
    db.connect()
    for ticker in tickers:
        table_name = getTableName('koyfin', ticker['symbol'], '1D')
        koyfinId = getSymbolId(ticker['symbol'])
        num_analysts = getNumAnalysts(koyfinId)
        print('Processing', ticker['symbol'], koyfinId, 'num_analysts:',
              num_analysts)
        if num_analysts != None and num_analysts > 15:
            if not db.candleTableExists(table_name):
                candles = getAllDayCandlesForTicker(koyfinId)
                priceTargetHist = getCompletePriceTargetHistory(koyfinId)
                new_candles = mergeCandlesAndPriceTarget(
                    candles, priceTargetHist
                ) if priceTargetHist and candles else candles
                if new_candles:
                    db.createCandleTableIfNotExists(table_name)
                    for candle in new_candles:
                        price_target = candle.get('price_target')
                        db.insertCandle(
                            table_name,
                            datetime.strptime(candle['date'],
                                              '%Y-%m-%d'), candle['open'],
                            candle['high'], candle['low'], candle['close'],
                            candle['volume'], price_target, num_analysts)
            else:
                next_candle_to_get = db.getLastCandleTime(
                    table_name) + timedelta(days=1)
                yesterday = datetime.today() - timedelta(days=1)
                candles = getCandles(
                    koyfinId, datetime.strftime(next_candle_to_get,
                                                '%Y-%m-%d'),
                    datetime.strftime(yesterday, '%Y-%m-%d'))
                priceTargetHist = getTargetPriceHistory(
                    koyfinId, next_candle_to_get, yesterday)
                new_candles = mergeCandlesAndPriceTarget(
                    candles, priceTargetHist
                ) if priceTargetHist and candles else candles
                if new_candles:
                    for candle in new_candles:
                        price_target = candle[
                            'price_target'] if 'price_target' in candle else None
                        db.insertCandle(
                            table_name,
                            datetime.strptime(candle['date'],
                                              '%Y-%m-%d'), candle['open'],
                            candle['high'], candle['low'], candle['close'],
                            candle['volume'], price_target, num_analysts)
    db.disconnect()
Exemple #4
0
def backtestRunner(provider, candle_size, start_date, end_date, init_balance,
                   stratOpts):
    delta = {
        '1m': timedelta(minutes=1),
        '5m': timedelta(minutes=5),
        '15m': timedelta(minutes=15),
        '1h': timedelta(hours=1),
        '4h': timedelta(hours=4),
        '1D': timedelta(days=1)
    }[candle_size]
    current_datetime = start_date
    db = sqliteDB()
    db.connect()

    # Setup. Get warm up candles:
    data = {}
    trader = FakeTrader(init_balance)
    data['price_target'] = {}
    data['price_target']['candles'] = pd.DataFrame(data,
                                                   columns=[
                                                       'date', 'open', 'high',
                                                       'low', 'close',
                                                       'volume', 'price_target'
                                                   ])
    trader.trades['price_target'] = []

    plotter = Plotter(['price_target'])
    #plotter.createLine('price_target', 'price_target', 'green')

    pos_size = stratOpts['pos_size']

    symbols = db.getKoyfinTickers()
    # strategy = Strategy(stratOpts, trader, plotter, symbols)
    while current_datetime < end_date:
        print(f'{current_datetime.strftime("%Y-%m-%d")}')
        for symbol in symbols:
            table_name = getTableName(provider, symbol, '1D')
            candle = db.getCandle(table_name, current_datetime)
            data['price_target']['candles'] = data['price_target'][
                'candles'].append(candle, ignore_index=True)
            #plotter.addPtToLine('price_target', 'price_target', candle['date'], candle['price_target'])
            if candle == None or candle['price_target'] == None:
                # No enough data for this day
                pass
            else:
                # if
                price_dif_pct = (candle['price_target'] -
                                 candle['close']) / candle['close'] * 100
                if price_dif_pct > 30 and not trader.isPosOpen(symbol):
                    order = {
                        'symbol': symbol,
                        'quantity': pos_size / candle['close'],
                        'mkt_price': candle['close'],
                        'placed': candle['date']
                    }
                    trader.openPosition(order, current_datetime)
                    print('buy', symbol, 'price:', candle['close'], 'target:',
                          candle['price_target'], 'price dif:', price_dif_pct)
                for order in trader.orders:
                    if order['status'] == 'active' and order[
                            'symbol'] == symbol and price_dif_pct < 20:
                        trader.closePosition(order, candle['close'],
                                             current_datetime)
                        print('sell', symbol, 'price dif:', price_dif_pct)
        current_datetime += delta

    reporter = Reporter(['price_target'], plotter, init_balance)

    reporter.setCandles('price_target', data['price_target']['candles'])
    reporter.setTrades('price_target', trader.trades['price_target'],
                       trader.balance, trader.num_trades, trader.num_wins,
                       trader.total_r)

    db.disconnect()

    # profit = trader.balance - init_balance
    # profitPct = profit / init_balance * 100
    # winRate = trader.num_wins / trader.num_trades * 100
    # print('\nFinal balance: {:.2f} Profit: {:.2f} Profit%: {:.2f}%'.format(trader.balance, profit, profitPct))
    # print('Num trades: {} Num wins: {} Win rate: {:.2f}%\n'.format(trader.num_trades, trader.num_wins, winRate))

    # TODO: Return a reporter class instance that includes functions such as trades_to_excel, show_chart, chart_to_html, print_performance, etc.
    # The strategy needs access to the plotter class in order to add lines and dots
    # The dots that correspond to trades should be added automatically
    return reporter
Exemple #5
0
 def getTable(symbol):
     return getTableName(provider, symbol, candle_size)