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

IndexError in is_overbought_or_oversold() #14

Open
mbronckers opened this issue Aug 9, 2019 · 3 comments
Open

IndexError in is_overbought_or_oversold() #14

mbronckers opened this issue Aug 9, 2019 · 3 comments

Comments

@mbronckers
Copy link

mbronckers commented Aug 9, 2019

I get an index error while trying to run the main.py related to the last_prices_a & last_prices_b:

High-Frequency-Trading-Model-with-IB/test/models/hft_model_1.py", line 208, in is_overbought_or_oversold last_price_b = self.df_hist[symbol_b].dropna().values[-1] IndexError: index -1 is out of bounds for axis 0 with size 0

related to line 208:

def is_overbought_or_oversold(self):
		[symbol_a, symbol_b] = self.symbols

		last_price_a = self.df_hist[symbol_a].dropna().values[-1]
		last_price_b = self.df_hist[symbol_b].dropna().values[-1]

		expected_last_price_a = last_price_b * self.beta

		is_overbought = last_price_a < expected_last_price_a  # Cheaper than expected
		is_oversold = last_price_a > expected_last_price_a  # Higher than expected

Any thoughts or help would be appreciated. Thanks.

@chicago-joe
Copy link
Contributor

chicago-joe commented Aug 15, 2019

@mbronckers I think this might be occurring because of the contract type.

In your main.py, are you trading Forex contracts? Try using stock contracts instead:

main.py


if __name__ == '__main__':
	TWS_HOST = os.environ.get('TWS_HOST', '127.0.0.1')
	TWS_PORT = os.environ.get('TWS_PORT', 4002)

	print('Connecting on host:', TWS_HOST, 'port:', TWS_PORT)

	model = HftModel1(
		host=TWS_HOST,
		port=TWS_PORT,
		client_id=1,)

	to_trade = [
		('SPY', Stock('SPY','SMART','USD')),
		('QQQ', Stock('QQQ','SMART','USD')),]

	model.run(to_trade=to_trade, trade_qty=100) 

I can't be 100% certain, but I'm not getting your index error anymore. I still can't run the code because I removed all my market data subscriptions, but the error I have now is just:

Error 162, reqId 321: Historical Market Data Service error message: No market data permissions for AMEX STK, contract: Stock(symbol='SPY', exchange='SMART', currency='USD')

This is to be expected. If I had a market data sub, this code should run correctly.

@chicago-joe
Copy link
Contributor

chicago-joe commented Aug 15, 2019

ALTERNATIVE SOLUTION (if you're still receiving the out-of-bounds indexing error):

The easiest way to fix the problem is to change the array. Your code is trying to get the last price (i.e. T-1) but the array position [-1] does not exist.

I found a workaround in an earlier release, but it has been months since I've touched that code.

Take a look at lines 276 - 358 of this file.

The mathematics might seem a little complex, but you should be able to understand how the array of stock prices is indexed and referenced.

Feel free to reach out if you need help!

@SC4RECOIN
Copy link

Same as #18

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

3 participants