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

Bug in the function "is_market_open()" and possibly other functions #247

Open
FengShengchuang opened this issue May 6, 2023 · 4 comments

Comments

@FengShengchuang
Copy link

Hi, I'm a new Lumibot user in Singapore. I found that I can't submit market order after midnight (12:00 am) local time, and in my Spyder console, it shows: Sleeping until the market opens.

After some checking, I found that in the file broker.py, there is a bug in the function is_market_open().
In this function, this line:
open_time = self.utc_to_local(self.market_hours(close=False))
gives the open time. It works as intended before midnight Singapore time say on Friday. However, when I run this line after 12 am Singapore time, it actually shows the open time of next Monday. Although the market is still open because it is at noon New York time, the variable current_time is earlier than the open time of next Monday, so this line:
return (current_time >= open_time) and (close_time >= current_time)
will return "false".

Here are some lines I run in my console and their outputs for your reference:

alpaca.is_market_open()
Out[2]: False

alpaca.api.get_clock()
Out[3]: 
Clock({   'is_open': True,
    'next_close': '2023-05-05T16:00:00-04:00',
    'next_open': '2023-05-08T09:30:00-04:00',
    'timestamp': '2023-05-05T15:18:19.101183152-04:00'})
alpaca.utc_to_local(alpaca.market_hours(close=False))
Out[8]: Timestamp('2023-05-08 21:30:00+0800', tz='tzlocal()')

alpaca.utc_to_local(alpaca.market_hours(close=True))
Out[9]: Timestamp('2023-05-09 04:00:00+0800', tz='tzlocal()')

datetime.now().astimezone(tz=tz.tzlocal())
Out[10]: datetime.datetime(2023, 5, 6, 3, 33, 53, 871817, tzinfo=tzlocal())

open_time=alpaca.utc_to_local(alpaca.market_hours(close=False))

close_time=alpaca.utc_to_local(alpaca.market_hours(close=True))

current_time = datetime.now().astimezone(tz=tz.tzlocal())

current_time >= open_time
Out[14]: False

close_time >= current_time
Out[15]: True

As I mentioned earlier, when I try to submit a market order after midnight, it shows: Sleeping until the market opens. This may mean that there is a similar issue in the function trader.run_all() or other functions it calls.

Hopefully this bug can be fixed soon.
Thanks!
Shengchuang

@timo-klarshift
Copy link

Is there any plan this will be fixed?

@grzesir
Copy link
Contributor

grzesir commented Jan 29, 2024 via email

@FengShengchuang
Copy link
Author

I think you can use an VPN to change your IP address to another region.

Hey Timo, This is a difficult thing for me to fix since I cannot replicate it (I’m on the eastern timezone). If you submit a pull request fixing it I can merge it into lumibot Robert Grzesik 347-635-3416

On Fri, Jan 26, 2024 at 2:30 PM Timo Friedl @.> wrote: Is there any plan this will be fixed? — Reply to this email directly, view it on GitHub <#247 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIYQKZ5QKPQGWAIOC3MTNDYQP73XAVCNFSM6AAAAAAXYDZGK6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMJSGU4DEMJTGY . You are receiving this because you are subscribed to this thread.Message ID: @.>

@grzesir
Copy link
Contributor

grzesir commented Jan 30, 2024 via email

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