Fastest way of getting current price

 

Hi!

Which is the fastest way of getting the current price?

Option 1:

double close[]; 

if (CopyClose(_Symbol,PERIOD_CURRENT,0,1,close) > 0)

Option 2:

if (SymbolInfoDouble(_Symbol,SYMBOL_LAST))

Option 3:

if (SymbolInfoTick)

Some other option 4


I would bet in Option 2, but I'm asking just to be sure. :) 

Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
  • www.mql5.com
To obtain the current market information there are several functions: SymbolInfoInteger(), SymbolInfoDouble() and SymbolInfoString(). The first parameter is the symbol name, the values of the second function parameter can be one of the identifiers of ENUM_SYMBOL_INFO_INTEGER, ENUM_SYMBOL_INFO_DOUBLE and ENUM_SYMBOL_INFO_STRING. Some symbols...
 
SymbolInfoTick().
 
Martin Bittencourt:

Hi!

Which is the fastest way of getting the current price?

Option 1:

Option 2:

Option 3:

Some other option 4


I would bet in Option 2, but I'm asking just to be sure. :) 

priceCurrent() by CSymbolInfo class... no? Or, if you have a selected position position.PriceCurrent() (by CPositionInfo class).

 
Martin Bittencourt:

Hi!

Which is the fastest way of getting the current price?

Option 1:

Option 2:

Option 3:

Some other option 4


I would bet in Option 2, but I'm asking just to be sure. :) 

to know this you can check yourself by using 

ulong  GetMicrosecondCount();

and check which is fastest

 

Use Benchmark macro to check which is the fastest

https://www.mql5.com/en/code/43910

Benchmark
Benchmark
  • www.mql5.com
A set of macros to benchmark small code snippets for their execution speeds.
 

I have a problem...how to close a trade at the opening price of the next red candle

def execute_sell_trade(df, symbol, lot_size=0.2):
    """
    Executes the RSI sell trade based on the given strategy conditions.

    Parameters:
        df (pd.DataFrame): DataFrame containing historical data, including RSI values.
        symbol (str): The trading symbol to execute the sell trade.
        lot_size (float, optional): The lot size for the sell trade. Default is 0.2.

    Returns:
        None

    Explanation:
        The `execute_sell_trade` function implements the sell part of the RSI (Relative Strength Index) 
        strategy based on specific conditions. The strategy aims to capture potential overbought market 
        conditions using the RSI indicator. The sell trade is taken when the RSI value is above 70, and a 
        particular candle pattern is observed.

        Sell Conditions:
        1. RSI is above 70, indicating an overbought market.
        2. The candle two periods ago (confirmation candle) is green (close > open).
        3. The previous candle (immediate previous to the current candle) is red (close < open).
        4. The current candle (most recent) is also red (close < open).

        Execution:
        Once all the sell conditions are met, a sell trade is executed at the current market price. The 
        trade is taken on the next red candle after the green confirmation candle. This means that when all 
        conditions are satisfied, the function opens a sell trade at the open price of the next red candle. 

        Exit:
        The trade remains open until the close of the second red candle after the confirmation candle. The 
        exit time and exit price are recorded based on the close price of the second red candle.

        Note:
        In a real trading scenario, additional risk management and stop-loss mechanisms should be 
        implemented to mitigate potential losses.

    """
    
    current_bar = df.iloc[-1]
    previous_bar = df.iloc[-2]
    confirmation_bar = df.iloc[-3]

    # Check for sell conditions
    if current_bar["rsi"] > 70 and confirmation_bar["close"] > confirmation_bar["open"] \
            and previous_bar["close"] < previous_bar["open"] \
            and current_bar["close"] < current_bar["open"]:

        if not mt5.initialize():
            print("initialize() failed ☢️")
            mt5.shutdown()
            return

        # Execute the trade on the next red candle (Candle 3 in the description)
        request = {
            "action": mt5.TRADE_ACTION_DEAL,
            "symbol": symbol,
            "volume": lot_size,
            "type": mt5.ORDER_TYPE_SELL,
            "price": mt5.symbol_info_tick(symbol).bid,
            "deviation": 10,
            "magic": 0,
            "comment": "RSI Sell Strategy",
            "type_filling": find_filling_mode(symbol),
            "type_time": mt5.ORDER_TIME_GTC
        }

        result = mt5.order_send(request)

        mt5.shutdown()

        if result.comment == "Accepted":
            print("Sell executed")
            # Wait for the trade to close (at the close of the second red candle)
            while True:
                new_bar = get_historical_data(symbol, mt5.TIMEFRAME_M1, 1)
                if new_bar is not None:
                    if new_bar.iloc[0]["close"] < new_bar.iloc[0]["open"]:
                        break
                    time.sleep(1)
            print("Trade closed")
        else:
            print("Error executing the trade")

This is the part of the function responsible for closing the trade

        if result.comment == "Accepted":
            print("Sell executed")
            # Wait for the trade to close (at the close of the second red candle)
            while True:
                new_bar = get_historical_data(symbol, mt5.TIMEFRAME_M1, 1)
                if new_bar is not None:
                    if new_bar.iloc[0]["close"] < new_bar.iloc[0]["open"]:
                        break
                    time.sleep(1)
            print("Trade closed")
        else:
            print("Error executing the trade")

Thank you in advance for your help

Reason: