High-Frequency-Trading-Model-with-IB
High-Frequency-Trading-Model-with-IB copied to clipboard
IndexError in is_overbought_or_oversold()
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.
@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.
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!
Same as https://github.com/jamesmawm/High-Frequency-Trading-Model-with-IB/issues/18