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)
(.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
Python version: Python 3.9.16 (main, Dec 7 2022, 01:11:51)
Platform: 20.04.1-Ubuntu
Additional context
Working fine for the majority of symbols, but not others.
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
settings.json
backtest.json (if applicable)
Error
Platform Info
Additional context Working fine for the majority of symbols, but not others.