AminHP / gym-mtsim

A general-purpose, flexible, and easy-to-use simulator alongside an OpenAI Gym trading environment for MetaTrader 5 trading platform (Approved by OpenAI Gym)
MIT License
428 stars 107 forks source link

Check if market is closed #37

Open mateusscheper opened 1 year ago

mateusscheper commented 1 year ago

Hi!

Any way to check if market is closed at a given date and time? Like holydays and weekends.

Thanks!

AminHP commented 1 year ago

Hi @mateusscheper , I'm not sure, but you might find some clues here. Please let me know if you found something.

wayneadams commented 1 year ago

@mateusscheper , on the object returned from symbol_info() there's a trade_mode member that seems to give this information. See docs for ENUM_SYMBOL_TRADE_MODE here. It only works real-time. Does that break your "given date and time" requirement?

mateusscheper commented 1 year ago

I'm not sure how MT5's API works. Currently I'm using this workaround to skip the price if it's in a time when the market is usually closed or if it's the same as the last price I got:

def get_state_sequence(sim, current_time):
    state_sequence = []
    i = 0
    time_multiplier = 0
    while i < 21:
        time = current_time - timedelta(minutes=15 * time_multiplier)
        time_multiplier += 1
        if is_not_weekday(time):
            continue

        state = get_state(sim, time)
        if state_sequence and state == state_sequence[-1]:
            continue
        state_sequence.append(state)
        i += 1
    state_sequence.reverse()
    state_sequence = np.reshape(state_sequence, (1, 21, 5))
    return state_sequence

def is_not_weekday(current_time):
    return current_time.weekday() > 4 \
        or (current_time.weekday() == 0 and current_time.hour < 7) \
        or (current_time.weekday() == 4 and current_time.hour > 23 and current_time.minute > 45)

It doesn't get 100% right but it helps. Still, it would be better to have a builtin function to check if the market is open/closed.