Specification
HI
i want some one to make the following MQL5 code work for me. the code has errors.
#include <Trade\Trade.mqh> // Include the trade library
// Define input parameters
input double lotSize = 0.1; // Lot size for trading
input int stopLoss = 50; // Stop Loss in pips
input int takeProfit = 100; // Take Profit in pips
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Subscribe to OnTrade() event
EventSetMillisecondTimer(60); // Set a timer to check conditions every 60 milliseconds
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
// Deinitialization function (if needed)
EventKillTimer(); // Kill the timer when deinitializing
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Nothing to do here for conditions check, as we use OnTrade()
}
//+------------------------------------------------------------------+
//| Expert trade event handler |
//+------------------------------------------------------------------+
void OnTrade()
{
// Calculate the open and close prices of the previous day
double prevDayOpen = iOpen(Symbol(), PERIOD_D1, 1);
double prevDayClose = iClose(Symbol(), PERIOD_D1, 1);
// Check for Rule 1: Previous day was an uptrend
if (prevDayClose > prevDayOpen)
{
// Check if the current open price is between the previous day's open and close prices
double currentOpen = iOpen(Symbol(), PERIOD_D1, 0);
if (currentOpen > prevDayOpen && currentOpen < prevDayClose)
{
// Buy if the price breaks above the previous day's close price
double currentHigh = iHigh(Symbol(), PERIOD_D1, 0);
if (currentHigh > prevDayClose)
{
// Place a Buy order
double openPrice = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
double stopLossPrice = openPrice - stopLoss * _Point;
double takeProfitPrice = openPrice + takeProfit * _Point;
int ticket = OrderSend(_Symbol, OP_BUY, lotSize, openPrice, 3, 0, 0, "", 0, clrNONE, 0, clrNONE);
if (ticket > 0)
{
// Order placed successfully - set Stop Loss and Take Profit
OrderSend(_Symbol, OP_SELL, lotSize, openPrice, 0, stopLossPrice, takeProfitPrice, "", 0, clrNONE, 0, clrNONE);
}
}
}
}
// Check for Rule 2: Opposite of Rule 1
if (prevDayClose < prevDayOpen)
{
// Check if the current open price is between the previous day's open and close prices
double currentOpen = iOpen(Symbol(), PERIOD_D1, 0);
if (currentOpen > prevDayOpen && currentOpen < prevDayClose)
{
// Sell if the price breaks below the previous day's close price
double currentLow = iLow(Symbol(), PERIOD_D1, 0);
if (currentLow < prevDayClose)
{
// Place a Sell order
double openPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);
double stopLossPrice = openPrice + stopLoss * _Point;
double takeProfitPrice = openPrice - takeProfit * _Point;
int ticket = OrderSend(_Symbol, OP_SELL, lotSize, openPrice, 3, 0, 0, "", 0, clrNONE, 0, clrNONE);
if (ticket > 0)
{
// Order placed successfully - set Stop Loss and Take Profit
OrderSend(_Symbol, OP_BUY, lotSize, openPrice, 0, stopLossPrice, takeProfitPrice, "", 0, clrNONE, 0, clrNONE);
}
}
}
}
}