Lumiwealth / lumibot

Backtesting and Trading Bots Made Easy for Crypto, Stocks, Options, Futures, FOREX and more
GNU General Public License v3.0
913 stars 170 forks source link

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

Open FengShengchuang opened 1 year ago

FengShengchuang commented 1 year ago

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 commented 9 months ago

Is there any plan this will be fixed?

grzesir commented 9 months ago

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 https://github.com/Lumiwealth/lumibot/issues/247#issuecomment-1912582136, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIYQKZ5QKPQGWAIOC3MTNDYQP73XAVCNFSM6AAAAAAXYDZGK6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMJSGU4DEMJTGY . You are receiving this because you are subscribed to this thread.Message ID: @.***>

FengShengchuang commented 9 months ago

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 commented 9 months ago

The issue is not the ip address, it is the clock

Robert Grzesik 347-635-3416

On Tue, Jan 30, 2024 at 11:37 AM FengShengchuang @.***> wrote:

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 … <#m2470193497041595711> 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) https://github.com/Lumiwealth/lumibot/issues/247#issuecomment-1912582136>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIYQKZ5QKPQGWAIOC3MTNDYQP73XAVCNFSM6AAAAAAXYDZGK6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMJSGU4DEMJTGY https://github.com/notifications/unsubscribe-auth/AAIYQKZ5QKPQGWAIOC3MTNDYQP73XAVCNFSM6AAAAAAXYDZGK6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMJSGU4DEMJTGY . You are receiving this because you are subscribed to this thread.Message ID: @.>

— Reply to this email directly, view it on GitHub https://github.com/Lumiwealth/lumibot/issues/247#issuecomment-1917443827, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIYQK4BDIOHN376Y2LBL3LYREOUFAVCNFSM6AAAAAAXYDZGK6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMJXGQ2DGOBSG4 . You are receiving this because you commented.Message ID: @.***>