#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
input string inputSymbol = "EURUSD";
input double inputLots = 0.1;
input int inputStopLoss = 50;
input int inputTakeProfit1 = 100;
input int inputTakeProfit2 = 200;
int ticket;
int OnInit()
{
return(INIT_SUCCEEDED);
}
void OnTick()
{
// Get the Ask and Bid prices
double Ask = SymbolInfoDouble(inputSymbol, SYMBOL_ASK);
double Bid = SymbolInfoDouble(inputSymbol, SYMBOL_BID);
if (ticket == 0) // If no trade is open, open one
{
ticket = OrderSend(inputSymbol, OP_BUY, inputLots, Ask, 3, Ask - inputStopLoss * Point, Ask + inputTakeProfit1 * Point);
if (ticket < 0)
{
Print("Error opening order: ", GetLastError());
}
}
else // If a trade is open
{
// Check if the trade has reached the second take profit level
if (Bid >= OrderOpenPrice() + inputTakeProfit2 * Point)
{
// Move the stop loss to the entry price
if (!OrderModify(ticket, OrderOpenPrice(), OrderOpenPrice(), 0, CLR_NONE))
{
Print("Error modifying order: ", GetLastError());
}
}
}
}
void OnDeinit(const int reason)
{
if (reason == REASON_CHARTCHANGE || reason == REASON_REMOVE)
{
if (ticket != 0) // Close the trade if it's still open
{
if (!OrderClose(ticket, inputLots, SymbolInfoDouble(inputSymbol, SYMBOL_BID), 3, clrNONE))
{
Print("Error closing order: ", GetLastError());
}
}
}
}
You can copy and paste this code into a new file in the MetaEditor and save it with the ".mq4" extension. Remember that this is a simple example, and in real life, you need to thoroughly test any trading algorithm before using it on a live account.