Grid Trading EA (Let Profits Run)

 
//+------------------------------------------------------------------+
//| Let Profits Run EA (Buy Only) v3.20
//+------------------------------------------------------------------+

#property copyright "Aladdin Abdulkareem"
#property link      ""

extern int MagicNumeber;
extern double GridStep        = 10;
extern double Lot             = 0.01;
extern int Slippage           = 0;
extern string Comments        = "LPR_v3.20_Buy";

double step = GridStep*Point*10;
double gridstart = iLow(0,PERIOD_MN1,iLowest(0,PERIOD_MN1,MODE_LOW,WHOLE_ARRAY,0));
double gridend = iHigh(0,PERIOD_MN1,iHighest(0,PERIOD_MN1,MODE_LOW,WHOLE_ARRAY,0));
double traderange = (gridend-gridstart)/Point*10; 
int gridlines = round((gridend-gridstart)/Point*10)/GridStep;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
   return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
{
   ObjectsDeleteAll(0);
   return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
   Comment("Magic Number: ",MagicNumeber,"\n",
           "Grid Step: ", GridStep, " pip", "\n",
           "Money Management: ",UseMoneyManagement,"\n",
           "Lot Size: ",lot,"\n",
           "Slippage: ",Slippage);
   
   int buy = 0;
   {
   for(int i = 1; i <= gridlines; i++) 
   {
   if(Bid == gridstart + i*step)
   {
   buy=1;
   }
   }
   }   
   int iOrderbuy = OrdersTotal();
   for (i = iOrderbuy; i >= 0; i--)
   {
   bool selectbuy = OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
   if(OrderOpenPrice() == Ask)
   {
   buy=0;
   }
   }      
   if(buy==1)
   {
   double Ticketbuy = OrderSend(Symbol(),OP_BUY,Lot,Ask,Slippage,Ask-step,0,Comments,MagicNumeber,0);       
   }
   return(0);
}

Hello Traders and Coders,

I'd like to share with you this EA which depends on Grid Trading strategy. It's very simple as the EA will open trades when price reaches any of the grid levels which is predefined by the trader with predefined step between each grid level.

The ea will cut the losses (by closing the loosing trades) and keep the winning trades open.

The attached EA is Buy Only, I'm beginner in coding and couldn't combine both Buy and Sell in one EA. If anyone can combine the Buy and Sell in one EA, and add to the code Close All Trades when AccountEquity() reaches above the AccountBalance() by some Profit Factor, I believe this EA will have very good and safe results.


Chirs,

 
double gridstart  = iLow(0,PERIOD_MN1,iLowest(0,PERIOD_MN1,MODE_LOW,WHOLE_ARRAY,0));
double gridend    = iHigh(0,PERIOD_MN1,iHighest(0,PERIOD_MN1,MODE_LOW,WHOLE_ARRAY,0));
double traderange = (gridend-gridstart)/Point*10; 
int gridlines     = round((gridend-gridstart)/Point*10)/GridStep;
Global and static variables work exactly the same way in MT4/MT5/C/C++.
  1. They are initialized once on program load.
  2. They don't update unless you assign to them.
  3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5 (or MT4 with strict which you should always use.)

    MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
  4. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary. Think symbol/TF change.
              external static variable - Inflation - MQL4 programming forum

And don't capitilize "let profit run" (as in your thread title,) as that is a commercial company and you'll confuse people.

 
Aladdin Abdulkareem:

Do not double post!

I have deleted your duplicate topic.

Reason: