psemdel / py-trading-bot

Trading-bot in python using django, vertorbt lib and interactive-brokers
MIT License
166 stars 38 forks source link

Getting real time data #27

Closed ben1628 closed 1 year ago

ben1628 commented 1 year ago

I have a chance to look at the codes.

It seems that reqMktdata is controlled by TIME_INTERVAL_CHECK (set to 10 minutes, I set to 1 minute), and the call will make reqMktData and then cancel it afterwards. sleep for 0.01 in between. Making a req and then cxl is quite expensive IMO.

Since we're using ib_insync, it would be much easier and efficient and capture the data in the data event like bars.updateEvent += onBarUpdate

In my case, I like to get data in 1 minute or even 5 second interval and then resample it to whatever I need, generate entry/signal according to the strategies. Then Trade based on those signal.

What do you think?

psemdel commented 1 year ago

I think I got that from here: https://groups.io/g/insync/topic/22588896 You can make a change and a PR. I have nothing against it.

Concerning the strategy. Right now, I have only strategies that run on daily basis implemented. But there is no problem setting a strategy that run much more often.

ben1628 commented 1 year ago

Concerning the strategy. Right now, I have only strategies that run on daily basis implemented. But there is no problem setting a strategy that run much more often<<

what is controlling the timing? oh, on daily_report

psemdel commented 1 year ago

The daily report is performed once a day, see telegram.py

self.do_weekday(time(17,15,tzinfo=tz_Paris),self.daily_report_17h)

The algorithm applying the strategy is in reporting/models.py

def daily_report(self,actions,exchange,use_IB,**kwargs): [...]

If you want to run your strategy every 10 minutes, you just have to do

self.manager.every(10, 'minutes').do(self.intraday_report)

And define a function intraday_report in the same style as daily_report.

Moreover in ib.py retrieve_data:

res=IBData.fetch( all_symbols, period=period, missing_index='drop', timeframe="1 day", #see also interval_YF_to_ib exchanges=exchanges, indexes=indexes)

and

res=vbt.YFData.fetch(all_symbols, period=period,missing_index='drop',**kwargs)

must be adapted with the correct timeframe/interval. The period may also need to be changed.