Best way to make the robot only operate on the open market, outside of auctions, etc.

 

Hello everything is fine? I need to make changes so that my robot operates only within the open market, so that it does not operate in auctions and after the closing call, I saw that there was nothing native for this, so I developed the following methods that run every tick:

bool IsMarketOpen()
{
    MqlDateTime currentTime;
    TimeToStruct(TimeLocal(), currentTime);

    int hour = currentTime.hour;
    int minute = currentTime.min;
    
    if ((hour > 9 || (hour == 9 && minute >= 0)) && (hour < 17 || (hour == 17 && minute < 55)))
        return true;
    return false;
}

void CloseAllAt18h()
{
    MqlDateTime currentTime;
    TimeToStruct(TimeLocal(), currentTime);

    int hour = currentTime.hour;
    int minute = currentTime.min;

    if (hour == 18 && minute == 0)
    {
        for (int i = PositionsTotal() - 1; i >= 0; i--)
        {
            if (PositionSelect(PositionGetSymbol(i)))
            {
                string symbol = PositionGetString(POSITION_SYMBOL);
                double volume = PositionGetDouble(POSITION_VOLUME);

                if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
                {
                    trade.Sell(volume, symbol);
                }
                else if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
                {
                    trade.Buy(volume, symbol);
                }
            }
        }
    }
}

void OnTick()
{
    if (IsBought() || IsSold())
        CloseAllAt18h();

    if (!IsMarketOpen())
        return;
}

Is this approach correct, and is it the best one to follow? Thanks in advance!

 
How about the functions SymbolInfoSessionQuote and SymbolInfoSessionTrade?
 
Stanislav Korotky #:
How about the functions SymbolInfoSessionQuote and SymbolInfoSessionTrade?

Hello, is everything ok? I decided not to use these methods, I developed the method below to check if the asset is up for auction, do you agree that it will work?


bool IsAuction(string symbol)
{
    double bid = SymbolInfoDouble(symbol, SYMBOL_BID);
    double ask = SymbolInfoDouble(symbol, SYMBOL_ASK);

    bool isSpreadNegative = bid > ask;

    bool isNoVolume = true;
    MqlTick ticks[];
    int count = CopyTicks(symbol, ticks, COPY_TICKS_ALL, 0, 1);

    if (count > 0)
        isNoVolume = false;

    if (isSpreadNegative || isNoVolume)
        return true;

    return false;
}
 
Jvmelo #:

Hello, is everything ok? I decided not to use these methods, I developed the method below to check if the asset is up for auction, do you agree that it will work?


Generally speaking, I don't know. AFAICT, the given form of CopyTicks will always return latest available tick, so count will be 1. Also I'm not sure that spread should be negative during auction, it's more often to see very large but positive spread, so probably checking against SYMBOL_BIDLOW/SYMBOL_ASKHIGH/SYMBOL_SESSION_PRICE_LIMIT_MIN/SYMBOL_SESSION_PRICE_LIMIT_MAX could be helpful as well.