//+------------------------------------------------------------------+
//| ProfitTakeDemoEA.mq4 |
//| Copyright 2023, Your Name |
//| your.website.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, Your Name"
#property link "your.website.com"
#property version "1.00"
#property strict
//--- Input Parameters
input int FastMAPeriod = 10; // Fast MA Period
input int SlowMAPeriod = 20; // Slow MA Period
input double LotSize = 0.01; // Lot Size
input int TakeProfitPips = 50; // Take Profit in Pips
input int StopLossPips = 25; // Stop Loss in Pips
input int MagicNumber = 67890; // Magic Number for orders
input string OrderComment = "ProfitTakeDemo";
//--- Global variables
int slippage = 3; // Acceptable slippage in points
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit() {
Print("ProfitTakeDemoEA Initialized. TP Pips: ", TakeProfitPips, ", SL Pips: ",
StopLossPips);
if (TakeProfitPips <= 0) {
Print("Warning: TakeProfitPips is zero or negative. Profit taking might not
occur as expected.");
// Depending on strategy, you might want to prevent trading:
return(INIT_FAILED);
}
if (StopLossPips <= 0) {
Print("Warning: StopLossPips is zero or negative. Trades will not have a Stop
Loss.");
// Depending on strategy, you might want to prevent trading:
return(INIT_FAILED);
}
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
Print("ProfitTakeDemoEA Deinitialized. Reason: ", reason);
Comment(""); // Clear chart comment
}
//+------------------------------------------------------------------+
//| Expert tick function (called on every new tick) |
//+------------------------------------------------------------------+
void OnTick() {
//--- Check if trading is allowed
if (Bars < 100 || !IsTradeAllowed()) {
return;
}
//--- Prevent trading on every tick, only once per new bar
static datetime prevBarTime = 0;
if (prevBarTime == Time[0]) {
return;
}
prevBarTime = Time[0];
//--- Calculate Moving Averages
double fastMA_current = iMA(Symbol(), Period(), FastMAPeriod, 0, MODE_SMA,
PRICE_CLOSE, 0);
double fastMA_prev = iMA(Symbol(), Period(), FastMAPeriod, 0, MODE_SMA,
PRICE_CLOSE, 1);
double slowMA_current = iMA(Symbol(), Period(), SlowMAPeriod, 0, MODE_SMA,
PRICE_CLOSE, 0);
double slowMA_prev = iMA(Symbol(), Period(), SlowMAPeriod, 0, MODE_SMA,
PRICE_CLOSE, 1);
//--- Check if we already have an open trade for this symbol/magic
if (CountOpenTradesByMagic(Symbol(), MagicNumber) > 0) {
return; // Only one trade at a time for this simple EA
}
//--- Define point size for SL/TP calculation
double point = Point;
if (_Digits == 3 || _Digits == 5) { // Adjust for 3/5 digit brokers
point *= 10;
}
//--- Trading Logic
// Buy Signal: Fast MA crossed above Slow MA
if (fastMA_prev < slowMA_prev && fastMA_current > slowMA_current) {
double askPrice = NormalizeDouble(Ask, _Digits);
double tpPrice = 0;
double slPrice = 0;
if (TakeProfitPips > 0) {
tpPrice = NormalizeDouble(askPrice + TakeProfitPips * point, _Digits);
}
if (StopLossPips > 0) {
slPrice = NormalizeDouble(askPrice - StopLossPips * point, _Digits);
}
int ticket = OrderSend(Symbol(), OP_BUY, LotSize, askPrice, slippage, slPrice,
tpPrice, OrderComment, MagicNumber, 0, clrGreen);
if (ticket > 0) {
Print("BUY Order Sent: #", ticket, " at ", askPrice, " SL: ", slPrice, " TP:
", tpPrice);
} else {
Print("Error Sending BUY Order: ", GetLastError());
}
}
// Sell Signal: Fast MA crossed below Slow MA
else if (fastMA_prev > slowMA_prev && fastMA_current < slowMA_current) {
double bidPrice = NormalizeDouble(Bid, _Digits);
double tpPrice = 0;
double slPrice = 0;
if (TakeProfitPips > 0) {
tpPrice = NormalizeDouble(bidPrice - TakeProfitPips * point, _Digits);
}
if (StopLossPips > 0) {
slPrice = NormalizeDouble(bidPrice + StopLossPips * point, _Digits);
}
int ticket = OrderSend(Symbol(), OP_SELL, LotSize, bidPrice, slippage, slPrice,
tpPrice, OrderComment, MagicNumber, 0, clrRed);
if (ticket > 0) {
Print("SELL Order Sent: #", ticket, " at ", bidPrice, " SL: ", slPrice, " TP:
", tpPrice);
} else {
Print("Error Sending SELL Order: ", GetLastError());
}
}
}
//+------------------------------------------------------------------+
//| Helper function to count open trades by magic number |
//+------------------------------------------------------------------+
int CountOpenTradesByMagic(string symbol, int magic) {
int count = 0;
for (int i = OrdersTotal() - 1; i >= 0; i--) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
if (OrderSymbol() == symbol && OrderMagicNumber() == magic) {
if (OrderType() == OP_BUY || OrderType() == OP_SELL) {
count++;
}
}
}
}
return count;
}
//+------------------------------------------------------------------+