jamesmawm / High-Frequency-Trading-Model-with-IB

A high-frequency trading model using Interactive Brokers API with pairs and mean-reversion in Python
MIT License
2.44k stars 657 forks source link

How to get pre-market-price from tws by using IB. #19

Open lovetrading10 opened 4 years ago

lovetrading10 commented 4 years ago

I would like to get the pre-market-price -> the price between 8 -9.30. By the way, I don't know how to get the price. Would you please inform me about it? I will wait for your favorable reply. Thanks.

twopirllc commented 4 years ago

Hi @SunHxiao,

You will need to disable RTH (Regular Trading Hours) in the script and possibly change in your IB Gateway settings. How can I trade stocks during the pre-market or post market?

But first, I would try and set useRTH=False (line #249) in hft_model_1.py and see if that works. If you are not doing HFT, then also adjust durationStr and barSizeSetting.

https://github.com/jamesmawm/High-Frequency-Trading-Model-with-IB/blob/a19d2a62c2c73ad9e5053bf305a9c89306fbf193/models/hft_model_1.py#L240-L254

Hope this helps, KJ

lovetrading10 commented 4 years ago

Hi, there. I appreciate your help. I would like to run your script(HFT). By the way, I can't run it. Would you please inform me how to run it? After downloading your script, how to install the environment, and how to run it. Thank you very much.

twopirllc commented 4 years ago

@SunHxiao,

My script? Or the owner's, Jame's, HFT script?

To run it, you need to make sure you have IB Gateway installed and Paper Trade Account setup because you certainly do not want to run it on a funded Live Account without fulling understanding what the algorithm does. In fact, make sure any Algo is ran on a Paper Trading Account and does as expected for some time before running it on a Live Account.

As for installation, there is a green 'Clone or Download' button at the Repository main page. I also recommend installing a Python 3 Virtual Environment or use Anaconda to setup the environment. I would look for videos on how to set up the Python environment if you are not familiar doing it.

Hope this helps. Good luck. KJ

lovetrading10 commented 4 years ago

Hi @twopirllc. Thanks for your quick reply. I have following issue. "Only 50 simultaneous API historical data requests allowed." For fixing this error, I have used ib.cancelHistoricalData function. So I fixed the error. But I am getting this message. image I don't want to get this message. Can you help me? Thanks for your kind.

twopirllc commented 4 years ago

Hey @SunHxiao,

If you are limited to 50 simultaneous API historical data requests, then you need to throttle or stagger your requests to stay under 50. Here are the IB API Message Codes. Unfortunately Error 162 has no Additional Notes about it.

Play around with your code some more and you'll figure it out.

Good luck, KJ