Buy & Sell GBPUSD EA

Orders Execution
It automatically opens orders when conditions are reachedIt can change open orders parameters, due to possible stepping strategyChecks for the total of open ordersChecks for the total of closed orders
0 Views
0 Downloads
0 Favorites
Buy & Sell GBPUSD EA
ÿþ//+------------------------------------------------------------------+

//|                                         Buy & Sell GBPUSD EA.mq4 |

//|                                               2021, Ricardojefs. |

//|                                              http://www.mql4.com |

//+------------------------------------------------------------------+

#property copyright   "Copyright 2021, Ricardojefs."                                   // copyright

#property version     "1.00"                                                  // Version

#property strict

#property description "This Expert Advisor is based on Moving Average, Bulls Power,Parabolic Sar and RSI."           // Description (line 1)         

#property description "The input parameters are optimized for GBPUSD H1."



extern int MagicNumber=10001; 

extern double Lots =0.01;       //Use the lots that you want

extern double StopLoss=0.28;    //Dont change this

extern double TakeProfit=0.37;  //Dont change this

extern int TrailingStop=12;     //Dont change this

extern int Slippage=3;

//+------------------------------------------------------------------+

//    expert start function

//+------------------------------------------------------------------+

int start()

  {

   double MyPoint=Point;

   if(Digits==3 || Digits==5)

      MyPoint=Point*10;



   double TheStopLoss=0;

   double TheTakeProfit=0;

   if(TotalOrdersCount()==0)

     {

      int result=0;

      if((iSAR(NULL,0,0.10,0.2,0)<Close[0])||(iBullsPower(NULL,0,100,PRICE_CLOSE,0)>0)||(iMA(NULL,0,40,0,MODE_EMA,PRICE_LOW,0)<Close[0])||(iMA(NULL,0,200,0,MODE_EMA,PRICE_LOW,0)<Close[0])||(iRSI(NULL,0,14,PRICE_CLOSE,0)<30)) // Here is your open buy rule

        {

         result=OrderSend(Symbol(),OP_BUY,AdvancedMM(),Ask,Slippage,0,0,"EA By Ricardojefs",MagicNumber,0,Blue);

         if(result>0)

           {

            TheStopLoss=0;

            TheTakeProfit=0;

            if(TakeProfit>0)

               TheTakeProfit=Ask+TakeProfit*MyPoint;

            if(StopLoss>0)

               TheStopLoss=Ask-StopLoss*MyPoint;

            OrderSelect(result,SELECT_BY_TICKET);

            OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);

           }

         return(0);

        }

      if((iSAR(NULL,0,0.10,0.2,0)>Close[0])||(iBullsPower(NULL,0,100,PRICE_CLOSE,0)<0)||(iMA(NULL,0,40,0,MODE_EMA,PRICE_LOW,0)>Close[0])||(iMA(NULL,0,200,0,MODE_EMA,PRICE_HIGH,0)>Close[0])||(iRSI(NULL,0,14,PRICE_CLOSE,0)>70)) // Here is your open Sell rule

        {

         result=OrderSend(Symbol(),OP_SELL,AdvancedMM(),Bid,Slippage,0,0,"EA",MagicNumber,0,Red);

         if(result>0)

           {

            TheStopLoss=0;

            TheTakeProfit=0;

            if(TakeProfit>0)

               TheTakeProfit=Bid-TakeProfit*MyPoint;

            if(StopLoss>0)

               TheStopLoss=Bid+StopLoss*MyPoint;

            OrderSelect(result,SELECT_BY_TICKET);

            OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);

           }

         return(0);

        }

     }



   for(int cnt=0; cnt<OrdersTotal(); cnt++)

     {

      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

      if(OrderType()<=OP_SELL &&

         OrderSymbol()==Symbol() &&

         OrderMagicNumber()==MagicNumber

        )

        {

         if(OrderType()==OP_BUY)

           {

            if(TrailingStop>0)

              {

               if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)

                 {

                  if(OrderStopLoss()<Bid-MyPoint*TrailingStop)

                    {

                     OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MyPoint,OrderTakeProfit(),0,Green);

                     return(0);

                    }

                 }

              }

           }

         else

           {

            if(TrailingStop>0)

              {

               if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))

                 {

                  if((OrderStopLoss()>(Ask+MyPoint*TrailingStop)) || (OrderStopLoss()==0))

                    {

                     OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MyPoint*TrailingStop,OrderTakeProfit(),0,Red);

                     return(0);

                    }

                 }

              }

           }

        }

     }

   return(0);

  }



//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

int TotalOrdersCount()

  {

   int result=0;

   for(int i=0; i<OrdersTotal(); i++)

     {

      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

      if(OrderMagicNumber()==MagicNumber)

         result++;



     }

   return (result);

  }

//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

double AdvancedMM()

  {

   int i;

   double AdvancedMMLots = 0;

   bool profit1=false;

   int SystemHistoryOrders=0;

   for(i=0; i<OrdersHistoryTotal(); i++)

     {

      OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);

      if(OrderMagicNumber()==MagicNumber)

         SystemHistoryOrders++;

     }

   bool profit2=false;

   int LO=0;

   if(SystemHistoryOrders<2)

      return(Lots);

   for(i=OrdersHistoryTotal()-1; i>=0; i--)

     {

      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))

         if(OrderMagicNumber()==MagicNumber)

           {

            if(OrderProfit()>=0 && profit1)

               return(Lots);

            if(LO==0)

              {

               if(OrderProfit()>=0)

                  profit1=true;

               if(OrderProfit()<0)

                  return(OrderLots());

               LO=1;

              }

            if(OrderProfit()>=0 && profit2)

               return(AdvancedMMLots);

            if(OrderProfit()>=0)

               profit2=true;

            if(OrderProfit()<0)

              {

               profit1=false;

               profit2=false;

               AdvancedMMLots+=OrderLots();

              }

           }

     }

   return(AdvancedMMLots);

  }

//+------------------------------------------------------------------+

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 ---