I have a little project with an Expert Advisor coded for Metatrader 4. It is supposed to take a buy trade when the price closes at least 5 points above the daily price at 6 am and a sell trade when the price closes at least 5 points below the daily price. Trading time is set to 7.45 am to 11.30 am. It is not allowed to take two buy trades or two sell trades in a row. I believe I tried everything, but it just won´t take trades when I backtest it. Hope someone can see the problem.
// Declare global variable to store daily opening price
double dailyOpen = 0.0;
// Define global variables for trading start and end times
datetime tradingStartTime = D'1970.01.01 07:45:00';
datetime tradingEndTime = D'1970.01.01 11:30:00';
// Define global variables for the buy and sell orders
int orderTicket = 0;
bool orderPlaced = false;
int orderType = 0; // 0: No trade, 1: Buy, -1: Sell
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Calculate daily opening price on EA initialization
dailyOpen = iOpen(Symbol(), PERIOD_D1, 0);
// Your other initialization code goes here
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
// Check if the current time is within the trading window
datetime currentTime = TimeCurrent();
if (currentTime < tradingStartTime || currentTime > tradingEndTime)
{
// Outside of trading hours, do not execute trading logic
return;
}
// Get the 5-minute closing price
double closePrice = iClose(Symbol(), PERIOD_M5, 0);
// Check if a trade is already placed and open
if (orderPlaced && OrderSelect(orderTicket, SELECT_BY_TICKET) && OrderSymbol() == Symbol())
{
// Check if the trade is closed
if (OrderCloseTime() > 0)
{
// Trade is closed, reset orderPlaced and determine the next order type
orderPlaced = false;
orderType *= -1; // Alternate between buy (1) and sell (-1)
}
// No further logic needed if the trade is still open
return;
}
// Buy condition: Price closes 5 points above the daily opening price at 6 AM
if (TimeHour(currentTime) == 6 && closePrice > dailyOpen + 5 && !orderPlaced && orderType != -1)
{
// Place a buy order with take profit and stop loss
orderTicket = OrderSend(Symbol(), OP_BUY, 1.0, Ask, 3, 0, 0, "Buy Order", 0, 10, Green);
if (orderTicket > 0)
{
orderPlaced = true;
orderType = 1; // Set order type to Buy
}
}
// Sell condition: Price closes 5 points below the daily opening price at 6 AM
if (TimeHour(currentTime) == 6 && closePrice < dailyOpen - 5 && !orderPlaced && orderType != 1)
{
// Place a sell order with take profit and stop loss
orderTicket = OrderSend(Symbol(), OP_SELL, 1.0, Bid, 3, 0, 0, "Sell Order", 0, 10, Red);
if (orderTicket > 0)
{
orderPlaced = true;
orderType = -1; // Set order type to Sell
}
}
}
I am hoping someone can help me finish up my EA so I can backtest it.