blankly-finance / blankly

🚀 💸 Easily build, backtest and deploy your algo in just a few lines of code. Trade stocks, cryptos, and forex across exchanges w/ one package.
https://package.blankly.finance
GNU Lesser General Public License v3.0
2.16k stars 270 forks source link

Error Downloading History #221

Open iguy0 opened 1 year ago

iguy0 commented 1 year ago

Description

I've modified the default rsi scanner, using symbols 'AAPL' and 'SGAPY' from Alpaca. The values for downloading history might be incorrect or history is missing on the broker. How does blankly handle inexistent history for a symbol ?

bot.py

import blankly
from blankly import Screener, Alpaca, ScreenerState
from blankly.indicators import rsi

tickers = ['AAPL', 'SGAPY']  # any stocks that you may want

# This function is our evaluator and runs per stock
def is_stock_buy(symbol, state: ScreenerState):
    # This runs per stock
    prices = state.interface.history(symbol, 40, resolution='1d',
                                     return_as='list')  # get past 40 data points
    price = state.interface.get_price(symbol)
    rsi_values = rsi(prices['close'], 14)
    return {'is_oversold': bool(rsi_values[-1] < 30), 'price': price, 'symbol': symbol}

def formatter(results, state: ScreenerState):
    # results is a dictionary on a per-symbol basis
    result_string = 'These are all the stocks that are currently oversold: \n'
    for symbol in results:
        if results[symbol]['is_oversold']:
            result_string += '{} is currently oversold at a price of {}\n\n'.format(symbol, results[symbol]['price'])
    return result_string

if __name__ == '__main__':
    exchange = Alpaca()  # initialize our exchange    
    screener = Screener(exchange, is_stock_buy, symbols=tickers, formatter=formatter)  # find oversold

    print(screener.formatted_results)

settings.json

(.venv) v2@linuxtrader:~/blankly$ cat settings.json
{
    "settings": {
        "use_sandbox_websockets": true,
        "websocket_buffer_size": 10000,
        "test_connectivity_on_auth": true,
        "auto_truncate": false,
        "global_shorting": false,
        "simulate_margin": false,
        "coinbase_pro": {
            "cash": "USD"
        },
        "binance": {
            "cash": "USDT",
            "binance_tld": "com"
        },
        "binance_futures": {
            "cash": "USDT",
            "margin_type": "USDT-M"
        },
        "alpaca": {
            "websocket_stream": "iex",
            "cash": "USD",
            "enable_shorting": true,
            "use_yfinance": false
        },
        "oanda": {
            "cash": "USD"
        },
        "okx": {
            "cash": "USDT"
        },
        "keyless": {
            "cash": "USD"
        },
        "kucoin": {
            "cash": "USDT"
        },
        "ftx": {
            "cash": "USD",
            "ftx_tld": "com"
        },
        "ftx_futures": {
            "cash": "USD",
            "ftx_tld": "com"
        },
        "paper": {
            "price_source": "api"
        }
    }
}

backtest.json (if applicable)

{
    "price_data": {
        "assets": []
    },
    "settings": {
        "use_price": "close",
        "smooth_prices": false,
        "GUI_output": true,
        "show_tickers_with_zero_delta": false,
        "save_initial_account_value": true,
        "show_progress_during_backtest": true,
        "cache_location": "./price_caches",
        "continuous_caching": true,
        "resample_account_value_for_metrics": "1d",
        "quote_account_value_in": "USD",
        "ignore_user_exceptions": true,
        "risk_free_return_rate": 0.0,
        "benchmark_symbol": null
    }
}

Error

(.venv) v2@linuxtrader:~/blankly$ python bot.py
INFO: No portfolio name to load specified, defaulting to the first in the file: (AlpacaTrader). This is fine if there is only one portfolio in use.
INFO: Starting screener...the next screener run will be started in 59073.51 seconds or at 2023-02-06 09:30:00
/home/v2/blankly/.venv/lib/python3.9/site-packages/blankly/exchanges/interfaces/alpaca/alpaca_interface.py:466: FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
  frames[i]['time'] = pd.Series(series).values
/home/v2/blankly/.venv/lib/python3.9/site-packages/blankly/exchanges/interfaces/alpaca/alpaca_interface.py:466: FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
  frames[i]['time'] = pd.Series(series).values
/home/v2/blankly/.venv/lib/python3.9/site-packages/blankly/exchanges/interfaces/alpaca/alpaca_interface.py:466: FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
  frames[i]['time'] = pd.Series(series).values
/home/v2/blankly/.venv/lib/python3.9/site-packages/blankly/exchanges/interfaces/alpaca/alpaca_interface.py:466: FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
  frames[i]['time'] = pd.Series(series).values
/home/v2/blankly/.venv/lib/python3.9/site-packages/blankly/exchanges/interfaces/alpaca/alpaca_interface.py:466: FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
  frames[i]['time'] = pd.Series(series).values
/home/v2/blankly/.venv/lib/python3.9/site-packages/blankly/exchanges/interfaces/alpaca/alpaca_interface.py:466: FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
  frames[i]['time'] = pd.Series(series).values
/home/v2/blankly/.venv/lib/python3.9/site-packages/blankly/exchanges/interfaces/alpaca/alpaca_interface.py:466: FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
  frames[i]['time'] = pd.Series(series).values
/home/v2/blankly/.venv/lib/python3.9/site-packages/blankly/exchanges/interfaces/alpaca/alpaca_interface.py:466: FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
  frames[i]['time'] = pd.Series(series).values
/home/v2/blankly/.venv/lib/python3.9/site-packages/blankly/exchanges/interfaces/alpaca/alpaca_interface.py:466: FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
  frames[i]['time'] = pd.Series(series).values
/home/v2/blankly/.venv/lib/python3.9/site-packages/blankly/exchanges/interfaces/alpaca/alpaca_interface.py:466: FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
  frames[i]['time'] = pd.Series(series).values
/home/v2/blankly/.venv/lib/python3.9/site-packages/blankly/exchanges/interfaces/alpaca/alpaca_interface.py:466: FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
  frames[i]['time'] = pd.Series(series).values
/home/v2/blankly/.venv/lib/python3.9/site-packages/blankly/exchanges/interfaces/alpaca/alpaca_interface.py:466: FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
  frames[i]['time'] = pd.Series(series).values
/home/v2/blankly/.venv/lib/python3.9/site-packages/blankly/exchanges/interfaces/alpaca/alpaca_interface.py:466: FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
  frames[i]['time'] = pd.Series(series).values
/home/v2/blankly/.venv/lib/python3.9/site-packages/blankly/exchanges/interfaces/alpaca/alpaca_interface.py:466: FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
  frames[i]['time'] = pd.Series(series).values
/home/v2/blankly/.venv/lib/python3.9/site-packages/blankly/exchanges/interfaces/alpaca/alpaca_interface.py:466: FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
  frames[i]['time'] = pd.Series(series).values
/home/v2/blankly/.venv/lib/python3.9/site-packages/blankly/exchanges/interfaces/alpaca/alpaca_interface.py:466: FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
  frames[i]['time'] = pd.Series(series).values
/home/v2/blankly/.venv/lib/python3.9/site-packages/blankly/exchanges/interfaces/alpaca/alpaca_interface.py:466: FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
  frames[i]['time'] = pd.Series(series).values
/home/v2/blankly/.venv/lib/python3.9/site-packages/blankly/exchanges/interfaces/alpaca/alpaca_interface.py:466: FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
  frames[i]['time'] = pd.Series(series).values
/home/v2/blankly/.venv/lib/python3.9/site-packages/blankly/exchanges/interfaces/alpaca/alpaca_interface.py:466: FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
  frames[i]['time'] = pd.Series(series).values
Traceback (most recent call last):
  File "/home/v2/blankly/bot.py", line 46, in <module>
    screener = Screener(exchange, is_stock_buy, symbols=tickers, formatter=formatter)  # find oversold
  File "/home/v2/blankly/.venv/lib/python3.9/site-packages/blankly/frameworks/screener/screener.py", line 91, in __init__
    self.__run()
  File "/home/v2/blankly/.venv/lib/python3.9/site-packages/blankly/frameworks/screener/screener.py", line 119, in __run
    result = evaluator(i, self.screener_state)
  File "/home/v2/blankly/bot.py", line 10, in is_stock_buy
    prices = state.interface.history(symbol, 40, resolution='1d',
  File "/home/v2/blankly/.venv/lib/python3.9/site-packages/blankly/exchanges/interfaces/abc_base_exchange_interface.py", line 56, in history
    response = self.overridden_history(symbol, start, stop, res_seconds, to=to,)
  File "/home/v2/blankly/.venv/lib/python3.9/site-packages/blankly/exchanges/interfaces/alpaca/alpaca_interface.py", line 482, in overridden_history
    response = pd.concat([response, find_last_n_points(batched_ranges[0], batched_ranges[1])])
  File "/home/v2/blankly/.venv/lib/python3.9/site-packages/blankly/exchanges/interfaces/alpaca/alpaca_interface.py", line 459, in find_last_n_points
    start=utils.iso8601_from_epoch(epoch_start_)).df)
  File "/home/v2/blankly/.venv/lib/python3.9/site-packages/blankly/utils/utils.py", line 278, in iso8601_from_epoch
    return dt.utcfromtimestamp(epoch).isoformat() + 'Z'
ValueError: year -848 is out of range

Platform Info

Additional context Working fine for the majority of symbols, but not others.

EmersonDove commented 1 year ago

That is definitely a new one, I wonder if alpaca changed their date format. I'll put this on my list of things to check on blankly.