//+------------------------------------------------------------------+
//| Prabhu Sar +MACD+ MA200 EA.mq5 |
//| Copyright 2024, Richard |
//| https://www.forexaisignal.com |
//+------------------------------------------------------------------+
#property copyright "MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
input int FastEMA=12; // Fast EMA period
input int SlowEMA=26; // Slow EMA period
input int SignalSMA=9; // Signal SMA period
input int AppliedPrice=PRICE_CLOSE; // Applied price
input int Shift=0; // Horizontal shift of the indicator in bars
input int StopLoss=200; // Stop loss in points
input int TrailingStop=50; // Trailing stop in points
double ExtFastMa[];
double ExtSlowMa[];
double ExtSignalMa[];
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
double macdMain, macdSignal, macdHist;
int ma200 = iMA(NULL, 0, 200, 0, MODE_SMA, PRICE_CLOSE, 0);
int psar = iSAR(NULL, 0, 0.02, 0.2, 0);
// MACD calculation
ArraySetAsSeries(ExtFastMa,true);
ArraySetAsSeries(ExtSlowMa,true);
ArraySetAsSeries(ExtSignalMa,true);
macdMain=iMACD(NULL,0,FastEMA,SlowEMA,SignalSMA,AppliedPrice,MODE_MAIN,Shift);
macdSignal=iMACD(NULL,0,FastEMA,SlowEMA,SignalSMA,AppliedPrice,MODE_SIGNAL,Shift);
macdHist=macdMain-macdSignal;
// Check conditions for sell trade
if(psar < Close[1] && macdMain > 0 && ma200 > 0)
{
// Place sell trade
int ticket = OrderSend(Symbol(), OP_SELL, 1, Ask, 3, Ask + StopLoss * Point, Ask - TakeProfit * Point, "My order", 16384, 0, clrRed);
if(ticket > 0)
{
if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
Print("SELL order opened : ",OrderOpenPrice());
}
else
Print("Error opening SELL order : ",GetLastError());
}
// Check conditions for buy trade
if(psar > Close[1] && macdMain < 0 && ma200 < 0)
{
// Place buy trade
int ticket = OrderSend(Symbol(), OP_BUY, 1, Bid, 3, Bid - StopLoss * Point, Bid + TakeProfit * Point, "My order", 16384, 0, clrBlue);
if(ticket > 0)
{
if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
Print("BUY order opened : ",OrderOpenPrice());
}
else
Print("Error opening BUY order : ",GetLastError());
}
// Trailing Stop for all orders
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,clrBlue);
}
}
}
else
{
if(OrderOpenPrice()-Ask>Point*TrailingStop)
{
if(OrderStopLoss()<Ask+Point*TrailingStop || OrderStopLoss()==0)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,clrRed);
}
}
}
}
}
}
}
//+------------------------------------------------------------------+