Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

symbol not found #46

Closed
alitekdemir opened this issue Aug 30, 2021 · 11 comments
Closed

symbol not found #46

alitekdemir opened this issue Aug 30, 2021 · 11 comments
Assignees

Comments

@alitekdemir
Copy link

Description

whatever I do, I can't get past the "symbol not found" error.

Standard RSI strategy

import blankly
from blankly import StrategyState


def price_event(price, symbol, state: StrategyState):
    """ This function will give an updated price every 15 seconds from our definition below """
    state.variables['history'].append(price)
    rsi = blankly.indicators.rsi(state.variables['history'])
    if rsi[-1] < 30:
        # Dollar cost average buy
        state.interface.market_order(symbol, side='buy', funds=10)
    elif rsi[-1] > 70:
        # Dollar cost average sell
        state.interface.market_order(symbol, side='sell', funds=10)


def init(symbol, state: StrategyState):
    # Download price data to give context to the algo
    state.variables['history'] = state.interface.history(symbol, start_date = "2021-08-01", end_date = "2021-08-28", return_as='list')['close']


if __name__ == "__main__":
    binance = blankly.Binance(portfolio_name="ATD Binance")
    binance_strategy = blankly.Strategy(binance)
    binance_strategy.add_price_event(price_event, symbol='ETH-USDT', resolution='1h', init=init)

    # Start the strategy. This will begin each of the price event ticks
    # binance_strategy.start()
    # Or backtest using this
    binance_strategy.backtest(start_date = "2021-08-01", end_date = "2021-08-28",
                              initial_values={'USDT': 100})

settings.json

{
  "settings": {
    "account_update_time": 5000,
    "use_sandbox": false,
    "use_sandbox_websockets": false,
    "websocket_buffer_size": 10000,

    "coinbase_pro": {
      "cash": "USD"
    },
    "binance": {
      "cash": "USDT",
      "binance_tld": "com"
    },
    "alpaca": {
      "websocket_stream": "iex",
      "cash": "USD"
    }
  }
}

backtest.json

{
  "price_data": {
    "assets": [
      [ "ETH-USDT", 1546290000, 1630270800, 300.0 ],
      [ "ETH-USDT", 1546290000, 1630270800, 900.0 ],
      [ "ETH-USDT", 1546290000, 1630270800, 1800.0 ],
      [ "ETH-USDT", 1546290000, 1630270800, 3600.0 ],
      [ "ETH-USDT", 1546290000, 1630270800, 14400.0 ],
      [ "ETH-USDT", 1546290000, 1630270800, 21600.0 ],
      [ "ETH-USDT", 1546290000, 1630270800, 86400.0 ]
    ]
  },
  "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",
    "resample_account_value_for_metrics": "1d",
    "quote_account_value_in": "USDT",
    "ignore_user_exceptions": false
  }
}

Error in Pyzo Editor

Including: ETH-USDT,1546290000,1630270800,300.0.csv in backtest.
Including: ETH-USDT,1546290000,1630270800,900.0.csv in backtest.
Including: ETH-USDT,1546290000,1630270800,1800.0.csv in backtest.
Including: ETH-USDT,1546290000,1630270800,3600.0.csv in backtest.
Including: ETH-USDT,1546290000,1630270800,14400.0.csv in backtest.
Including: ETH-USDT,1546290000,1630270800,21600.0.csv in backtest.
Including: ETH-USDT,1546290000,1630270800,86400.0.csv in backtest.
Including: ETH-USDT,1627776000.0,1630108800.0,3600.0.csv in backtest.
Including: ETH-USDT,1598774943.8658986,1630310943.8658986,3600.0.csv in backtest.
No exact cache exists for ETH-USDT from 1630310996.5534158 to 1630311056.5534158 at 3600.0s resolution. Downloading...
already identified

Initializing...
1630098000.0
Traceback (most recent call last):
  File "C:\Blankly\RSI-strategy.py", line 30, in <module>
    binance_strategy.backtest(start_date = "2021-08-01", end_date = "2021-08-28",
  File "c:\python39\lib\site-packages\blankly\strategy\strategy_base.py", line 473, in backtest
    results = self.backtesting_controller.run()
  File "c:\python39\lib\site-packages\blankly\exchanges\interfaces\paper_trade\backtest_controller.py", line 397, in run
    available_dict, no_trade_dict = self.format_account_data(self.initial_time)
  File "c:\python39\lib\site-packages\blankly\exchanges\interfaces\paper_trade\backtest_controller.py", line 221, in format_account_data
    true_account[i] = self.interface.get_account(i)
  File "c:\python39\lib\site-packages\blankly\exchanges\interfaces\paper_trade\paper_trade_interface.py", line 298, in get_account
    raise KeyError("Symbol not found.")
KeyError: 'Symbol not found.'

Error in CMD

C:\Blankly>RSI-strategy.py
Including: ETH-USDT,1546290000,1630270800,300.0.csv in backtest.
Including: ETH-USDT,1546290000,1630270800,900.0.csv in backtest.
Including: ETH-USDT,1546290000,1630270800,1800.0.csv in backtest.
Including: ETH-USDT,1546290000,1630270800,3600.0.csv in backtest.
Including: ETH-USDT,1546290000,1630270800,14400.0.csv in backtest.
Including: ETH-USDT,1546290000,1630270800,21600.0.csv in backtest.
Including: ETH-USDT,1546290000,1630270800,86400.0.csv in backtest.
Including: ETH-USDT,1627776000.0,1630108800.0,3600.0.csv in backtest.

Initializing...
1630098000.0
Traceback (most recent call last):
  File "C:\Python39\lib\site-packages\blankly\exchanges\interfaces\paper_trade\paper_trade_interface.py", line 296, in get_account
    return trade_local.get_account(symbol)
  File "C:\Python39\lib\site-packages\blankly\exchanges\interfaces\paper_trade\local_account\trade_local.py", line 125, in get_account
    return copy.deepcopy(utils.AttributeDict(local_account.account[asset_id]))
KeyError: 'USD'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Blankly\RSI-strategy.py", line 30, in <module>
    binance_strategy.backtest(start_date = "2021-08-01", end_date = "2021-08-28",
  File "C:\Python39\lib\site-packages\blankly\strategy\strategy_base.py", line 473, in backtest
    results = self.backtesting_controller.run()
  File "C:\Python39\lib\site-packages\blankly\exchanges\interfaces\paper_trade\backtest_controller.py", line 397, in run
    available_dict, no_trade_dict = self.format_account_data(self.initial_time)
  File "C:\Python39\lib\site-packages\blankly\exchanges\interfaces\paper_trade\backtest_controller.py", line 221, in format_account_data
    true_account[i] = self.interface.get_account(i)
  File "C:\Python39\lib\site-packages\blankly\exchanges\interfaces\paper_trade\paper_trade_interface.py", line 298, in get_account
    raise KeyError("Symbol not found.")
KeyError: 'Symbol not found.'

Platform Info

  • Python version: 3.9.6
  • Platform: Win10
@EmersonDove
Copy link
Member

EmersonDove commented Aug 30, 2021

This is an interesting error. I am testing your code locally and it performs the backtest correctly. I'm checking if this is because of platform or python version (using 3.7 on MacOS). It could also be an error with the account quoting system under pairs outside of -USD or generic currency endings but it will require a bit more analysis.

These types of bugs are extremely hard to identify by us, so thanks for creating an issue!

@EmersonDove EmersonDove self-assigned this Aug 30, 2021
@EmersonDove
Copy link
Member

EmersonDove commented Aug 31, 2021

I believe the issue is that there is no generic USD account allowed under the binance com ending. I can't create an international account so there is no way to verify this.

I have pushed a new version - v1.6.1-beta to pypi which doesn't make an implicit assumption that a USD account exists in the backtest. This may fix your issue. You can run pip install blankly --upgrade.

@alitekdemir
Copy link
Author

You are a very hardworking team. You are making great progress. Thank you very much for your reply and interest.
I have not yet achieved sufficient results to complete my strategy. I know I've come a long way. I am looking forward to getting better results and to meet and communicate with you.

By the way, unfortunately the backtest did not work and I will continue my experiments by installing a linux.
The graphic produced was very nice :)

chrome_npvatrOifF

Including: ETH-USDT,1546290000,1630270800,300.0.csv in backtest.
Including: ETH-USDT,1546290000,1630270800,900.0.csv in backtest.
Including: ETH-USDT,1546290000,1630270800,1800.0.csv in backtest.
Including: ETH-USDT,1546290000,1630270800,3600.0.csv in backtest.
Including: ETH-USDT,1546290000,1630270800,14400.0.csv in backtest.
Including: ETH-USDT,1546290000,1630270800,21600.0.csv in backtest.
Including: ETH-USDT,1546290000,1630270800,86400.0.csv in backtest.
Including: ETH-USDT,1627776000.0,1630108800.0,3600.0.csv in backtest.

Initializing...
1630098000.0

Backtesting...
Progress: [----------] 0.02% Traceback (most recent call last):
  File "c:\python39\lib\site-packages\blankly\exchanges\interfaces\paper_trade\backtest_controller.py", line 452, in run
    raise e
  File "c:\python39\lib\site-packages\blankly\exchanges\interfaces\paper_trade\backtest_controller.py", line 445, in run
    self.price_events[0]['function'](self.interface.get_price(self.price_events[0]['asset_id']),
  File "C:\Blankly\RSI-strategy.py", line 11, in price_event
    state.interface.market_order(symbol, side='buy', funds=10)
  File "c:\python39\lib\site-packages\blankly\exchanges\interfaces\paper_trade\paper_trade_interface.py", line 341, in market_order
    trade_local.test_trade(symbol, side, qty, price, quote_decimals, quantity_decimals)
  File "c:\python39\lib\site-packages\blankly\exchanges\interfaces\paper_trade\local_account\trade_local.py", line 87, in test_trade
    raise InvalidOrder("Insufficient funds. Available:" +
blankly.utils.exceptions.InvalidOrder: Insufficient funds. Available:0.0 hold: 0 requested: 0.0749.
c:\python39\lib\site-packages\blankly\metrics\portfolio.py:29: RuntimeWarning: divide by zero encountered in double_scalars
  return (end_value / start_value) ** (1 / years) - 1

@EmersonDove
Copy link
Member

Hi! I'm glad you like our graphical output!

I believe all the output you showed is expected behavior. The insufficient funds error is caused by running out of USDT. The loss in account value is caused by ETH devaluing over that period of time.

There is a warning caused by the portfolio valuation because the timeframe is over less than a year (so its a divide by zero). I will work to remove this warning but I want to give speedy responses.

If you want to continue the backtest even with errors you can toggle ignore_user_exceptions to true in backtest.json. A live strategy will also persist through user errors so in some ways this increases accuracy.

You can also check your cash value by doing interface.cash which can be used to prevent errors from potential overdraft purchases.

@bfan1256 bfan1256 closed this as completed Sep 7, 2021
@rohsat
Copy link

rohsat commented Apr 15, 2022

Hi, I am still getting this error when I try to backtest Binance. Was the fix reverted?

@EmersonDove
Copy link
Member

This should be patched, can you show your console output @rohsat?

@rohsat
Copy link

rohsat commented Apr 15, 2022

Hi Emerson,

I am running through Google Colab. Running the bot.py for Binance.

Code

import blankly

def price_event(price, symbol, state: blankly.StrategyState):
""" This function will give an updated price every 15 seconds from our definition below """
state.variables['history'].append(price)
rsi = blankly.indicators.rsi(state.variables['history'])
if rsi[-1] < 30 and not state.variables['owns_position']:
# Dollar cost average buy
buy = blankly.trunc(state.interface.cash/price, 2)
state.interface.market_order(symbol, side='buy', size=buy)
state.variables['owns_position'] = True
elif rsi[-1] > 70 and state.variables['owns_position']:
# Dollar cost average sell
curr_value = blankly.trunc(state.interface.account[state.base_asset].available, 2)
state.interface.market_order(symbol, side='sell', size=curr_value)
state.variables['owns_position'] = False

def init(symbol, state: blankly.StrategyState):
# Download price data to give context to the algo
state.variables['history'] = state.interface.history(symbol, to=150, return_as='deque',
resolution=state.resolution)['close']
state.variables['owns_position'] = False

if name == "main":
# Authenticate coinbase pro strategy
exchange = blankly.Binance()

# Use our strategy helper on coinbase pro
strategy = blankly.Strategy(exchange)

# Run the price event function every time we check for a new price - by default that is 15 seconds
strategy.add_price_event(price_event, symbol='BTC-USDT', resolution='1d', init=init)

# Start the strategy. This will begin each of the price event ticks
# strategy.start()
# Or backtest using this
results = strategy.backtest(to='1y', initial_values={'USDT': 10000})
print(results)

@rohsat
Copy link

rohsat commented Apr 15, 2022

Error:

INFO: No portfolio name to load specified, defaulting to the first in the file: (another cool portfolio). This is fine if there is only one portfolio in use.

Initializing...

KeyError Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/blankly/exchanges/interfaces/paper_trade/paper_trade_interface.py in get_account(self, symbol)
337 try:
--> 338 return self.local_account.get_account(symbol)
339 except KeyError:

6 frames
/usr/local/lib/python3.7/dist-packages/blankly/exchanges/interfaces/paper_trade/local_account/trade_local.py in get_account(self, asset_id)
210 """
--> 211 return copy.deepcopy(utils.AttributeDict(self.local_account[asset_id]))
212

KeyError: 'USD'

During handling of the above exception, another exception occurred:

KeyError Traceback (most recent call last)
in ()
38 # strategy.start()
39 # Or backtest using this
---> 40 results = strategy.backtest(to='1y', initial_values={'USDT': 10000})
41 print(results)

/usr/local/lib/python3.7/dist-packages/blankly/frameworks/strategy/strategy.py in backtest(self, to, initial_values, start_date, end_date, save, settings_path, callbacks, **kwargs)
168
169 # Run the backtest & return results
--> 170 results = self.backtesting_controller.run()
171
172 blankly.reporter.export_backtest_result(results)

/usr/local/lib/python3.7/dist-packages/blankly/exchanges/interfaces/paper_trade/backtest_controller.py in run(self)
716 # Add an initial account row here
717 if self.preferences['settings']['save_initial_account_value']:
--> 718 available_dict, no_trade_dict = self.format_account_data(self.initial_time)
719 price_data.append(available_dict)
720 no_trade.append(no_trade_dict)

/usr/local/lib/python3.7/dist-packages/blankly/exchanges/interfaces/paper_trade/backtest_controller.py in format_account_data(self, local_time)
499 for i in assets:
500 # Grab the account status
--> 501 true_account[i] = self.interface.get_account(i)
502
503 # Create an account total value

/usr/local/lib/python3.7/dist-packages/blankly/utils/utils.py in wrapper(self, symbol)
788 if symbol is not None:
789 symbol = get_base_asset(symbol)
--> 790 return func(self, symbol=symbol)
791 return wrapper
792

/usr/local/lib/python3.7/dist-packages/blankly/exchanges/interfaces/paper_trade/paper_trade_interface.py in get_account(self, symbol)
339 except KeyError:
340 if self.backtesting:
--> 341 raise KeyError("Symbol not found. This can be caused by an invalid quote currency "
342 "in backtest.json.")
343 else:

KeyError: 'Symbol not found. This can be caused by an invalid quote currency in backtest.json.'

@rohsat
Copy link

rohsat commented Apr 15, 2022

I have set USDT in backtest.json.

{
"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": "USDT",
"ignore_user_exceptions": true,
"risk_free_return_rate": 0.0,
"benchmark_symbol": null
}
}

@rohsat
Copy link

rohsat commented Apr 15, 2022

Additional details - My Binance tld is .com but I have created the account from India.

@rohsat
Copy link

rohsat commented Apr 16, 2022

I was able to make it work by modifying "quote_account_value_in": "USDT" in utils.py.

default_backtest_settings = {
    "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": "USDT",
        "ignore_user_exceptions": False,
        "risk_free_return_rate": 0.0,
        "benchmark_symbol": None
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants