Buy Order doesn't close or sell order execute

CEO

Trader
Sep 1, 2023
11
0
7
23
MQL5:
//+------------------------------------------------------------------+
//|                                                       Malick.mq5 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
 
// Expert Advisor settings
input double lotSize = 0.1; // Trading lot size
input int consecutiveTicks = 3; // Number of consecutive ticks required for entry
input int oppositeTicks = 2; // Number of ticks in opposite direction to close the trade
 
// Define trading constants
#define OP_BUY 0
#define OP_SELL 1
 
int ticket = 0;
 
// Define variables for Ask and Bid
double askPrice, bidPrice;
 
// Define the trading symbol (you can use _Symbol)
string symbol = "EURUSD";
 
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    static int consecutiveUpTicks = 0;
    static int consecutiveDownTicks = 0;
 
    // Calculate Ask and Bid prices
   // double askPrice, bidPrice;
    if (!SymbolInfoDouble(symbol, SYMBOL_BID, bidPrice) ||
        !SymbolInfoDouble(symbol, SYMBOL_ASK, askPrice)) {
        Print("Error getting market info. Error code: ", GetLastError());
        return;
    }
 
    // Get the current tick direction (0 for unchanged, 1 for up, -1 for down)
    int currentTickDirection = 0;
 
    if (askPrice > bidPrice) {
        currentTickDirection = 1; // Up tick
    } else if (askPrice < bidPrice) {
        currentTickDirection = -1; // Down tick
    }
 
    if (currentTickDirection == 1) {
        consecutiveUpTicks++;
        consecutiveDownTicks = 0;
    } else if (currentTickDirection == -1) {
        consecutiveDownTicks++;
        consecutiveUpTicks = 0;
    } else {
        consecutiveUpTicks = 0;
        consecutiveDownTicks = 0;
    }
 
    if (consecutiveUpTicks >= consecutiveTicks) {
        // Buy signal - Place a buy order
        ticket = OrderSend(symbol, OP_BUY, lotSize, askPrice, 2, 0, 0, "", 0, clrNONE);
        if (ticket > 0) {
            consecutiveUpTicks = 0;
            consecutiveDownTicks = 0;
        }
    } else if (consecutiveDownTicks >= consecutiveTicks) {
        // Sell signal - Place a sell order
        ticket = OrderSend(symbol, OP_SELL, lotSize, bidPrice, 2, 0, 0, "", 0, clrNONE);
        if (ticket > 0) {
            consecutiveUpTicks = 0;
            consecutiveDownTicks = 0;
        }
    }
 
    // Check for opposite ticks on the 3rd tick
    if ((consecutiveUpTicks == 3 && currentTickDirection == -1) ||
        (consecutiveDownTicks == 3 && currentTickDirection == 1)) {
        // Close the trade on opposite ticks
        bool result = OrderClose(ticket, OrderLots(), bidPrice, 2, clrNONE);
        if (result) {
            // Handle successful trade closure if needed
        } else {
            Print("Error closing position. Error code: ", GetLastError());
        }
    }
}
//+------------------------------------------------------------------+
Post automatically merged:

Updated to the one below

MQL5:
//+------------------------------------------------------------------+
//|                                                       Malick.mq5 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include <Trade\Trade.mqh>
 
// Expert Advisor settings
input double lotSize = 0.1; // Trading lot size
input int consecutiveTicks = 3; // Number of consecutive ticks required for entry
input int oppositeTicks = 2; // Number of ticks in opposite direction to close the trade
 
// Define trading constants
#define OP_BUY 0
#define OP_SELL 1
CTrade ExtTrade;
int ticket = 0;
 
// Define variables for Ask and Bid
double askPrice, bidPrice;
 
 
// Define variables for symbol and lot size
string symbol = _Symbol;
 
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    static int consecutiveUpTicks = 0;
    static int consecutiveDownTicks = 0;
 
    // Calculate Ask and Bid prices
    double askPrice, bidPrice;
    if (!SymbolInfoDouble(symbol, SYMBOL_BID, bidPrice) ||
        !SymbolInfoDouble(symbol, SYMBOL_ASK, askPrice)) {
        Print("Error getting market info. Error code: ", GetLastError());
        return;
    }
 
    // Get the current tick direction (0 for unchanged, 1 for up, -1 for down)
    int currentTickDirection = 0;
 
    if (askPrice > bidPrice) {
        currentTickDirection = 1; // Up tick
    } else if (askPrice < bidPrice) {
        currentTickDirection = -1; // Down tick
    }
 
    if (currentTickDirection == 1) {
        consecutiveUpTicks++;
        consecutiveDownTicks = 0;
    } else if (currentTickDirection == -1) {
        consecutiveDownTicks++;
        consecutiveUpTicks = 0;
    } else {
        consecutiveUpTicks = 0;
        consecutiveDownTicks = 0;
    }
 
    if (consecutiveUpTicks >= consecutiveTicks) {
        // Buy signal - Place a buy order
        ticket = ExtTrade.PositionOpen(symbol, OP_BUY, lotSize, askPrice, 0, 0);
        if (ticket > 0) {
            consecutiveUpTicks = 0;
            consecutiveDownTicks = 0;
        }
    } else if (consecutiveDownTicks >= consecutiveTicks) {
        // Sell signal - Place a sell order
        ticket = ExtTrade.PositionOpen(symbol, OP_SELL, lotSize, bidPrice, 0, 0);
        if (ticket > 0) {
            consecutiveUpTicks = 0;
            consecutiveDownTicks = 0;
        }
    }
 
    // Check for opposite ticks on the 3rd tick
    if ((consecutiveUpTicks == 3 && currentTickDirection == -1) ||
        (consecutiveDownTicks == 3 && currentTickDirection == 1)) {
        // Close the trade on opposite ticks
        bool result = ExtTrade.PositionClose(symbol, ticket); // Use PositionClose to close the trade
        if (result) {
            // Handle successful trade closure if needed
        } else {
            Print("Error closing position. Error code: ", GetLastError());
        }
    }
}
//+------------------------------------------------------------------+