MACD PSAR 5 Min

Author: Copyright � 2007, AKA TradeForexFX
Price Data Components
Series array that contains close prices for each barSeries array that contains close prices for each bar
Orders Execution
Checks for the total of open ordersIt Closes Orders by itself It can change open orders parameters, due to possible stepping strategyIt automatically opens orders when conditions are reached
Indicators Used
MACD HistogramParabolic Stop and Reverse systemStochastic oscillator
Miscellaneous
It sends emails
0 Views
0 Downloads
0 Favorites

Profitability Reports

AUD/USD Oct 2024 - Jan 2025
56.00 %
Total Trades 226
Won Trades 0
Lost trades 0
Win Rate 0.00 %
Expected payoff -42.48
Gross Profit 12150.00
Gross Loss -21750.00
Total Net Profit -9600.00
-100%
-50%
0%
50%
100%
GBP/USD Oct 2024 - Jan 2025
77.00 %
Total Trades 409
Won Trades 176
Lost trades 233
Win Rate 0.43 %
Expected payoff -19.16
Gross Profit 25632.00
Gross Loss -33468.00
Total Net Profit -7836.00
-100%
-50%
0%
50%
100%
MACD PSAR 5 Min
//+------------------------------------------------------------------+
//|                       MACD PSAR 5 Min
//| 
//|          Trading Methodology Devised by TradeForex  
//|         
//|        Magic number and times to trade coding by Robert Hill         |
//|  
//|                  EA in progress and NOT YET COMPLETE
//|     In no event will author be liable for any damages whatsoever.|
//|                      Use at your own risk.                       |
//|                                                                  |
//+------------------- DO NOT REMOVE THIS HEADER --------------------+

#define SIGNAL_NONE 0
#define SIGNAL_BUY   1
#define SIGNAL_SELL  2
#define SIGNAL_CLOSEBUY 3
#define SIGNAL_CLOSESELL 4

#property copyright "Copyright © 2007, AKA TradeForexFX"
#property link      "http://www./"

extern string  Expert_Name    = "---- MACD PSAR by TF ----";


//***********************
// Modified to use base for calculation of MagicNumber
extern int MagicNumberBase = 10000;
int MagicNumber;  // This is now calculated based on Symbol and Period

extern bool SignalMail = False;
extern bool EachTickMode = True;
extern double Lots = 1.00;
extern int MaxSpread = 8;
extern int Slippage = 3;
extern bool StopLossMode = True;
extern int StopLoss = 150;
extern bool TakeProfitMode = True;
extern int TakeProfit = 150;
extern bool TrailingStopMode = True;
extern int TrailingStop = 150;
extern string Time_Period_ = "Use 15 or 5 ";
extern string Time_Period__ = "Recommended 5 ";
extern int Time_Period = 5;
extern string Lower_Time_Period_ = "Use 5 or 1 (Lower than that used above)";
extern int Lower_Time_Period = 1;



//******************************************
// Inputs for trading hours
extern string  sm0="--Trading Hours Filter--";
extern string  sm2="UseTradingHours - Enter 0 for false, 1 for true";
extern int    UseTradingHours = 0;
extern string  sm3="Trade Session 1 - Enter 0 for false, 1 for true";
extern int    TradeSession1 = 0;
extern int     Session1Start = 0;       // Start trades after time
extern int     Session1Stop = 1215;      // Stop trading after time
extern string  sm4="Trade Session 2 - Enter 0 for false, 1 for true";
extern int    TradeSession2 = 0;
extern int     Session2Start = 1215;       // Start trades after time
extern int     Session2Stop = 2000;      // Stop trading after time
extern string  sm5="Trade Session 3 - Enter 0 for false, 1 for true";
extern int    TradeSession3 = 0;
extern int     Session3Start = 700;       // Start trades after time
extern int     Session3Stop = 900;      // Stop trading after time
extern string  sm6="Trade Session 4 - Enter 0 for false, 1 for true";
extern int    TradeSession4 = 0;
extern int     Session4Start = 1330;       // Start trades after time
extern int     Session4Stop = 1500;      // Stop trading after time
extern string  sm7="Trade Session 5 - Enter 0 for false, 1 for true";
extern int    TradeSession5 = 0;
extern int     Session5Start = 1700;       // Start trades after time
extern int     Session5Stop = 1900;      // Stop trading after time

bool     YesStop;

// Trading Hours variables end here
//************************************

int BarCount;
int Current;
bool TickCheck = False;


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init() {
   BarCount = Bars;

   if (EachTickMode) Current = 0; else Current = 1;
   
//*******************************
// Calculate MagicNumber
	MagicNumber = MagicNumberBase + func_Symbol2Val(Symbol())*100 + func_TimeFrame_Const2Val(Period()); 

   return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit() {
   return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start() {
   int Order = SIGNAL_NONE;
   int Total, Ticket;
   double StopLossLevel, TakeProfitLevel;



   if (EachTickMode && Bars != BarCount) TickCheck = False;
   Total = OrdersTotal();
   Order = SIGNAL_NONE;
   
   
   
   

   //+------------------------------------------------------------------+
   //| Variable Begin                                                   |
   //+------------------------------------------------------------------+

//double iSAR( string symbol, int timeframe, double step, double maximum, int shift) 

double MACD_Current_Main =iMACD(NULL,Time_Period,12,26,9,PRICE_CLOSE,MODE_MAIN,0); 
double MACD_Current_Signal=iMACD(NULL,Time_Period,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0); 
double MACD_Previous_Main=iMACD(NULL,Time_Period,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
double MACD_Previous_Signal=iMACD(NULL,Time_Period,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);


double PSAR_Current = iSAR( NULL,Time_Period, 0.02, 0.2, 0); 
double PSAR_Previous = iSAR( NULL,Time_Period, 0.02, 0.2, 1); 
double PSAR_Lower_Time_Frame = iSAR( NULL,Lower_Time_Period, 0.02, 0.2, 1); 


double Stoch_Current_Main = iStochastic( NULL,Time_Period, 5, 3, 3, MODE_SMA, 0 , MODE_MAIN, 0); 
double Stoch_Previous_Main = iStochastic( NULL,Time_Period, 5, 3, 3, MODE_SMA, 0 , MODE_MAIN, 1); 

double Spread = MathAbs(Ask - Bid)*Point - (MaxSpread + 1);

//double iClose( string symbol, int timeframe, int shift) 

double Previous_Close = iClose( NULL,Time_Period, 1) ;

//double Buy

double Buy1_1 = MACD_Current_Main;
double Buy1_2 = MACD_Current_Signal;

//double Buy2_1 = MACD_Previous_Main;
//double Buy2_2 = MACD_Previous_Signal ;

double Buy3_1 = MACD_Current_Main;
double Buy3_2 = MACD_Previous_Signal ;

double Buy4_1 = MACD_Current_Main;
double Buy4_2 = MACD_Previous_Main;

double Buy5_1 = (Bid + Ask)/2;
double Buy5_2 = PSAR_Current;

double Buy6_1 = Previous_Close;
double Buy6_2 = PSAR_Previous;

double Buy7_1 = PSAR_Current;
double Buy7_2 = PSAR_Previous;


double Buy8_1 = MathAbs (PSAR_Current -(Bid + Ask)/2) ;
double Buy8_2 = 60;

double Buy9_1 = Stoch_Current_Main;
double Buy9_2 = Stoch_Previous_Main;

double Buy10_1 = (Bid + Ask)/2;
double Buy10_2 = PSAR_Lower_Time_Frame;


double Buy11_1 = Spread;
double Buy11_2 = 0;

double Buy12_1 = MACD_Current_Main;
double Buy12_2 = 0 ;


//double Sell

double Sell1_1 = MACD_Current_Main;
double Sell1_2 = MACD_Current_Signal;

//double Sell2_1 = MACD_Previous_Main;
//double Sell2_2 = MACD_Previous_Signal ;

double Sell3_1 = MACD_Current_Main;
double Sell3_2 = MACD_Previous_Signal ;

double Sell4_1 = MACD_Current_Main;
double Sell4_2 = MACD_Previous_Main;

double Sell5_1 = (Bid + Ask)/2;
double Sell5_2 = PSAR_Current;

double Sell6_1 = Previous_Close;
double Sell6_2 = PSAR_Previous;

double Sell7_1 = PSAR_Current;
double Sell7_2 = PSAR_Previous;

double Sell8_1 = MathAbs (PSAR_Current -(Bid + Ask)/2);
double Sell8_2 = 60;

double Sell9_1 = Stoch_Current_Main;
double Sell9_2 = Stoch_Previous_Main;

double Sell10_1 = (Bid + Ask)/2;
double Sell10_2 = PSAR_Lower_Time_Frame;


double Sell11_1 = Spread;
double Sell11_2 = 0;

double Sell12_1 = MACD_Current_Main;
double Sell12_2 = 0;

//double Close Buy

double CloseBuy1_1 = (Bid + Ask)/2;
double CloseBuy1_2 = PSAR_Current;

double CloseBuy2_1 = Previous_Close;
double CloseBuy2_2 = PSAR_Previous;

double CloseBuy3_1 = (Bid + Ask)/2;
double CloseBuy3_2 = PSAR_Lower_Time_Frame;


   
//double Close Sell


double CloseSell1_1 = (Bid + Ask)/2;
double CloseSell1_2 = PSAR_Current;

double CloseSell2_1 = Previous_Close;
double CloseSell2_2 = PSAR_Previous;

double CloseSell3_1 = (Bid + Ask)/2;
double CloseSell3_2 = PSAR_Lower_Time_Frame;

   
   
   //+------------------------------------------------------------------+
   //| Variable End                                                     |
   //+------------------------------------------------------------------+






     
   //Check position
   bool IsTrade = False;

   for (int i = 0; i < Total; i ++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
//*******************************************
// Important to have code check MagicNumber and Symbol

      if (OrderSymbol() != Symbol()) continue;
      if (OrderMagicNumber() != MagicNumber) continue;
//*******************************************
      
      if(OrderType() <= OP_SELL) {
         IsTrade = True;
         if(OrderType() == OP_BUY) {
            //Close

            //+------------------------------------------------------------------+
            //| Signal Begin(Exit Buy)                                           |
            //+------------------------------------------------------------------+

                     if (CloseBuy1_1 < CloseBuy1_2 && CloseBuy2_1 < CloseBuy2_2 && CloseBuy3_1 < CloseBuy3_2 ) Order = SIGNAL_CLOSEBUY;

//&& CloseBuy2_1 < CloseBuy2_2 
            //+------------------------------------------------------------------+
            //| Signal End(Exit Buy)                                             |
            //+------------------------------------------------------------------+

            if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
               OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);
               if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");
               if (!EachTickMode) BarCount = Bars;
               IsTrade = False;
               continue;
            }
            //Trailing stop
            if(TrailingStopMode && TrailingStop > 0) {                 
               if(Bid - OrderOpenPrice() > Point * TrailingStop) {
                  if(OrderStopLoss() < Bid - Point * TrailingStop) {
                     OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);
                     if (!EachTickMode) BarCount = Bars;
                     continue;
                  }
               }
            }
         } else {
            //Close

            //+------------------------------------------------------------------+
            //| Signal Begin(Exit Sell)                                          |
            //+------------------------------------------------------------------+
// && CloseSell2_1 > CloseSell2_2 
                     if (CloseSell1_1 > CloseSell1_2 && CloseSell2_1 > CloseSell2_2 && CloseSell3_1 > CloseSell3_2 ) Order = SIGNAL_CLOSESELL;


            //+------------------------------------------------------------------+
            //| Signal End(Exit Sell)                                            |
            //+------------------------------------------------------------------+

            if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
               OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);
               if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");
               if (!EachTickMode) BarCount = Bars;
               IsTrade = False;
               continue;
            }
            //Trailing stop
            if(TrailingStopMode && TrailingStop > 0) {                 
               if((OrderOpenPrice() - Ask) > (Point * TrailingStop)) {
                  if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) {
                     OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);
                     if (!EachTickMode) BarCount = Bars;
                     continue;
                  }
               }
            } // Trailing Stop
         } // Close
      } // OrderType
   }// for

//*******************************************
// Check trading sessions

   int NumOrders = 0;
   
   NumOrders = CalculateCurrentOrders();
   if(NumOrders == 0) {
     YesStop = false;
      
     if (UseTradingHours == 1) {
        YesStop = CheckTradingTimes();
   
        if (YesStop == true) {
          Comment ("Trading has been stopped - wrong time of day");
        }
        else {
          Comment ("Trading has resumed - time is OK");
        }
     }

     if (YesStop == false) {

   //+------------------------------------------------------------------+
   //| Signal Begin(Entry)                                              |
   //+------------------------------------------------------------------+
/*


&& Sell2_1 < Sell2_2
&& Buy2_1 > Buy2_2
*/      
       if (Buy1_1 > Buy1_2  && Buy3_1 > Buy3_2  && Buy4_1 > Buy4_2  && Buy5_1 > Buy5_2  && Buy6_1 > Buy6_2 && Buy7_1 > Buy7_2 && Buy8_1 < Buy8_2 && Buy9_1 < Buy9_2  && Buy10_1 > Buy10_2  && Buy11_1 < Buy11_2 && Buy12_1 > Buy12_2   ) Order = SIGNAL_BUY;
 
       if (Sell1_1 < Sell1_2  && Sell3_1 < Sell3_2 && Sell4_1 < Sell4_2 && Sell5_1 < Sell5_2 && Sell6_1 < Sell6_2 && Sell7_1 < Sell7_2  && Sell8_1 < Sell8_2&& Sell9_1 > Sell9_2  && Sell10_1 < Sell10_2 && Sell11_1 < Sell11_2 && Sell12_1 < Sell12_2  ) Order = SIGNAL_SELL;


   //+------------------------------------------------------------------+
   //| Signal End                                                       |
   //+------------------------------------------------------------------+


   //Buy
       if (Order == SIGNAL_BUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
          if(!IsTrade) {
         //Check free margin
            if (AccountFreeMargin() < (500 * Lots)) {
              Print("We have no money. Free Margin = ", AccountFreeMargin());
              return(0);
            }

            if (StopLossMode) StopLossLevel = Ask - StopLoss * Point; else StopLossLevel = 0.0;
            if (TakeProfitMode) TakeProfitLevel = Ask + TakeProfit * Point; else TakeProfitLevel = 0.0;

            Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, StopLossLevel, TakeProfitLevel, "MACD PSAR Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);
            if(Ticket > 0) {
             if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
				    Print("BUY order opened : ", OrderOpenPrice());
                if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Open Buy");
	  		    } else {
				    Print("Error opening BUY order : ", GetLastError());
			    }
            }
            if (EachTickMode) TickCheck = True;
            if (!EachTickMode) BarCount = Bars;
            return(0);
          }
       }
  







 //Sell


  
       if (Order == SIGNAL_SELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
          if(!IsTrade) {
         //Check free margin
             if (AccountFreeMargin() < (500 * Lots)) {
                Print("We have no money. Free Margin = ", AccountFreeMargin());
                return(0);
             }

             if (StopLossMode) StopLossLevel = Bid + StopLoss * Point; else StopLossLevel = 0.0;
             if (TakeProfitMode) TakeProfitLevel = Bid - TakeProfit * Point; else TakeProfitLevel = 0.0;

             Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, StopLossLevel, TakeProfitLevel, "MACD PSAR Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink);
             if(Ticket > 0) {
                if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {
				      Print("SELL order opened : ", OrderOpenPrice());
                  if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Open Sell");
			       } else {
				      Print("Error opening SELL order : ", GetLastError());
			       }
             }
             if (EachTickMode) TickCheck = True;
             if (!EachTickMode) BarCount = Bars;
             return(0);
          }
       }

     } // YesStop
   } // NumOrders

   if (!EachTickMode) BarCount = Bars;

   return(0);
}

//+------------------------------------------------------------------+
// Return true if time is not in session for trading
bool CheckOutsideSession(int SessionStart, int SessionStop, int ct)
{

   if (SessionStart < SessionStop)
   {
     if (ct >= SessionStart && ct <= SessionStop) return (false); else return(true);
   }
   else
   {
      if (ct > SessionStop && ct < SessionStart) return (true); else return(false);
   }
}

bool CheckTradingTimes()
{
   bool StopTrading;
   int ct;
   ct = Hour() * 100 + Minute();
   
     StopTrading = true;

     if (TradeSession1 == 1)
     {
        StopTrading = CheckOutsideSession(Session1Start, Session1Stop, ct);
     }
     if (StopTrading == true)
     {

       if (TradeSession2 == 1)
       {
         StopTrading = CheckOutsideSession(Session2Start, Session2Stop, ct);
       }
     }
     if (StopTrading == true)
     {

       if (TradeSession3 == 1)
       {
         StopTrading = CheckOutsideSession(Session3Start, Session3Stop, ct);
       }
     }
     if (StopTrading == true)
     {

       if (TradeSession4 == 1)
       {
         StopTrading = CheckOutsideSession(Session4Start, Session4Stop, ct);
       }
     }
     if (StopTrading == true)
     {

       if (TradeSession5 == 1)
       {
         StopTrading = CheckOutsideSession(Session5Start, Session5Stop, ct);
       }
     }
     
     return(StopTrading);
}

int CalculateCurrentOrders()
  {
   int buys = 0, sells = 0, num = 0;
   for(int i=0;i<OrdersTotal();i++)
     {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()!= Symbol()) continue;
      if (OrderMagicNumber()!= MagicNumber) continue;
      if(OrderType() == OP_BUY)  buys++;
      if(OrderType() == OP_SELL) sells++;
     }
     
   num = buys + sells;
   return(num);
  }


// Functions for MagicNumber Calculations
int func_Symbol2Val(string symbol)
 {
   string mySymbol = StringSubstr(symbol,0,6);
   
	if(mySymbol=="AUDCAD") return(1);
	if(mySymbol=="AUDJPY") return(2);
	if(mySymbol=="AUDNZD") return(3);
	if(mySymbol=="AUDUSD") return(4);
	if(mySymbol=="CHFJPY") return(5);
	if(mySymbol=="EURAUD") return(6);
	if(mySymbol=="EURCAD") return(7);
	if(mySymbol=="EURCHF") return(8);
	if(mySymbol=="EURGBP") return(9);
	if(mySymbol=="EURJPY") return(10);
	if(mySymbol=="EURUSD") return(11);
	if(mySymbol=="GBPCHF") return(12);
	if(mySymbol=="GBPJPY") return(13);
	if(mySymbol=="GBPUSD") return(14);
	if(mySymbol=="NZDJPY") return(15);
	if(mySymbol=="NZDUSD") return(16);
	if(mySymbol=="USDCAD") return(17);
	if(mySymbol=="USDCHF") return(18);
	if(mySymbol=="USDJPY") return(19);
   Comment("unexpected Symbol");
	return(999);
}

//+------------------------------------------------------------------+
//| Time frame interval appropriation  function                      |
//+------------------------------------------------------------------+

int func_TimeFrame_Const2Val(int Constant ) {
   switch(Constant) {
      case 1:  // M1
         return(1);
      case 5:  // M5
         return(2);
      case 15:
         return(3);
      case 30:
         return(4);
      case 60:
         return(5);
      case 240:
         return(6);
      case 1440:
         return(7);
      case 10080:
         return(8);
      case 43200:
         return(9);
   }
}


Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---