newgrid_2009_11_15_floating_center_thorup_001

Author: Expert for tests
Price Data Components
Series array that contains the lowest prices of each barSeries array that contains the highest prices of each bar
Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reachedIt can change open orders parameters, due to possible stepping strategyIt Closes Orders by itself
0 Views
0 Downloads
0 Favorites
newgrid_2009_11_15_floating_center_thorup_001
#property copyright "Expert for tests"

extern int MagicNumber = 100001;
extern double Lots = 0.1 ;
extern bool MM=false;
extern double Balancefactor=4650;
extern bool microlots=false;
extern int max_trades = 30 ;
extern int grid_lines = 30 ;
extern double grid_separation = 0.005 ;
extern double TP_absolute = 0.005 ;
extern double AccountEquityPercent=100.01;
extern bool FIFO=false;
extern int LookBackDays = 90;
extern int slip = 3 ;
extern double pipvalue=0.1;
   
int BarCount;
int Current;
bool TickCheck = False;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init() {
   return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit() {
   GlobalVariableDel("next_level_"+Symbol());
   GlobalVariableDel("test_next_level__"+Symbol());
   return(0);
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start() {
 
   int Entertradebuy =0 ;
   int Entertradesell =0 ;
   double next_level_,test_next_level_,i ;
   double Ticket1, Ticket2;
   double pips_risked=0,money_risked=0;
   int iOrders=0;
   if(IsTesting()==true)
      {
      if(OrdersTotal()<max_trades)
         {
         for (i = 1; i<grid_lines; i++) 
            {
            if (Ask <= (RangeMid() - grid_separation*i)+0.0001 && Ask >= (RangeMid() - grid_separation*i)-0.0001)
               {
               Entertradebuy=1 ;
               }
            if (Bid <= (RangeMid() + grid_separation*i)+0.0001 && Bid >= (RangeMid() + grid_separation*i)-0.0001)
               {
               Entertradesell=1 ; 
               }
            }
         }
      iOrders = OrdersTotal()-1;
      for (i = iOrders; i>=0; i--)
         {
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if (((OrderOpenPrice() <= Ask+0.0003 && OrderOpenPrice() >= Ask-0.0003) || (OrderOpenPrice() <= Bid+0.0003 && OrderOpenPrice() >= Bid-0.0003))&&OrderMagicNumber()==MagicNumber)
            {
            Entertradebuy=0  ;
            Entertradesell=0 ;
            }
         } 
      if(FIFO==false&&MM==false)
         {
         if(Entertradebuy==1)
            {
            Ticket1 = OrderSend(Symbol(), OP_BUY, Lots, Ask, slip, 0, 0, "Buy(#" + 1 + ")", MagicNumber, 0, DodgerBlue) ;
            if(Ticket1!=1) Print("Error# "+GetLastError());         
            }
         if(Entertradesell==1)
            {
            Ticket2 = OrderSend(Symbol(), OP_SELL, Lots, Bid, slip, 0, 0, "Sell(#" + 1 + ")", MagicNumber, 0, DeepPink) ;
            if(Ticket2!=1) Print("Error# "+GetLastError());         
            }
         for(i=0;i<OrdersTotal();i++)
            {
            OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
            if(OrderType()<=OP_SELL&&OrderMagicNumber()==MagicNumber&&OrderTakeProfit()==0)
               {
               if(OrderType()==OP_BUY)
                  {
                  OrderModify(OrderTicket(),OrderOpenPrice(),0,OrderOpenPrice()+TP_absolute,0,CLR_NONE);
                  }
               else
                  {   
                  OrderModify(OrderTicket(),OrderOpenPrice(),0,OrderOpenPrice()-TP_absolute,0,CLR_NONE);
                  }
               }
            }      
         for (i = 1; i<grid_lines; i++)
            {
            if(Ask - (RangeMid() - grid_separation*i) < grid_separation)
            GlobalVariableSet("test_next_level_"+Symbol(), RangeMid() - grid_separation*i) ;
            if(RangeMid() + grid_separation*i - Bid < grid_separation)
            GlobalVariableSet("test_next_level_"+Symbol(), RangeMid() + grid_separation*i) ;
            }
         next_level_=GlobalVariableGet("test_next_level_"+Symbol()) ;
         for (i = 1; i<grid_lines-1; i++)
            {
            pips_risked = pips_risked+(((RangeMid()+grid_separation*grid_lines)-(RangeMid()-grid_separation*(grid_lines-i))));
            money_risked = (pips_risked*100000*Lots)/2 ;
            }
         if(OrdersTotal() < max_trades)
         Comment("I am trading a grid from ", RangeMid()-grid_separation*grid_lines, " to ", RangeMid()+grid_separation*grid_lines, "\n",
                 "closest grid line to enter trade is ", test_next_level_ , "\n", 
                 "distance between grid lines is ", grid_separation*10000, " pips", "\n",
                 "number of grid lines is ", grid_lines, "\n",
                 "maximum number of trades is ", max_trades, "\n",
                 "TP is ", TP_absolute, "\n",
                 "---------------------------------------------------",  "\n",
                 "You are risking a potential ", money_risked, "USD")  ;     
         if(OrdersTotal() >= max_trades)
         Comment("I am full, I have ", max_trades, " positions opened");
         return(0);
         }
      if(FIFO==false&&MM==true)
         {   
         if(Entertradebuy==1)
            {
            Ticket1 = OrderSend(Symbol(), OP_BUY, LotsOptimized(), Ask, slip, 0, 0, "Buy(#" + 1 + ")", MagicNumber, 0, DodgerBlue) ;         
            if(Ticket1!=1) Print("Error# "+GetLastError());         
            }
         if(Entertradesell==1)
            {
            Ticket2 = OrderSend(Symbol(), OP_SELL, LotsOptimized(), Bid, slip, 0, 0, "Sell(#" + 1 + ")", MagicNumber, 0, DeepPink) ;
            if(Ticket2!=1) Print("Error# "+GetLastError());         
            }
         for(i=0;i<OrdersTotal();i++)
            {
            OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
            if(OrderType()<=OP_SELL&&OrderMagicNumber()==MagicNumber&&OrderTakeProfit()==0)
               {
               if(OrderType()==OP_BUY)
                  {
                  OrderModify(OrderTicket(),OrderOpenPrice(),0,OrderOpenPrice()+TP_absolute,0,CLR_NONE);
                  }
               else
                  {   
                  OrderModify(OrderTicket(),OrderOpenPrice(),0,OrderOpenPrice()-TP_absolute,0,CLR_NONE);
                  }
               }
            }      
         for (i = 1; i<grid_lines; i++)
            {
            if(Ask - (RangeMid() - grid_separation*i) < grid_separation)
            GlobalVariableSet("test_next_level_"+Symbol(), RangeMid() - grid_separation*i) ;
            if(RangeMid() + grid_separation*i - Bid < grid_separation)
            GlobalVariableSet("test_next_level_"+Symbol(), RangeMid() + grid_separation*i) ;
            }
         next_level_=GlobalVariableGet("test_next_level_"+Symbol()) ;
         for (i = 1; i<grid_lines-1; i++)
            {
            pips_risked = pips_risked+(((RangeMid()+grid_separation*grid_lines)-(RangeMid()-grid_separation*(grid_lines-i))));
            money_risked = (pips_risked*100000*LotsOptimized())/2 ;
            }
         if(OrdersTotal() < max_trades)
         Comment("I am trading a grid from ", RangeMid()-grid_separation*grid_lines, " to ", RangeMid()+grid_separation*grid_lines, "\n",
                 "closest grid line to enter trade is ", test_next_level_ , "\n", 
                 "distance between grid lines is ", grid_separation*10000, " pips", "\n",
                 "number of grid lines is ", grid_lines, "\n",
                 "maximum number of trades is ", max_trades, "\n",
                 "TP is ", TP_absolute, "\n",
                 "---------------------------------------------------",  "\n",
                 "You are risking a potential ", money_risked, "USD")  ;     
         if(OrdersTotal() >= max_trades)
         Comment("I am full, I have ", max_trades, " positions opened");
         return(0);
         }
      if(FIFO==true&&MM==false)
         {   
         if(Entertradebuy==1)
            {
            Ticket1 = OrderSend(Symbol(), OP_BUY, Lots, Ask, slip, 0, 0, "Buy(#" + 1 + ")", MagicNumber, 0, DodgerBlue) ;         
            if(Ticket1!=1) Print("Error# "+GetLastError());         
            }
         if(Entertradesell==1)
            {
            Ticket2 = OrderSend(Symbol(), OP_SELL, Lots, Bid, slip, 0, 0, "Sell(#" + 1 + ")", MagicNumber, 0, DeepPink) ;
            if(Ticket2!=1) Print("Error# "+GetLastError());         
            }
         for (i = 1; i<grid_lines; i++)
            {
            if(Ask - (RangeMid() - grid_separation*i) < grid_separation)
            GlobalVariableSet("test_next_level_"+Symbol(), RangeMid() - grid_separation*i) ;
            if(RangeMid() + grid_separation*i - Bid < grid_separation)
            GlobalVariableSet("test_next_level_"+Symbol(), RangeMid() + grid_separation*i) ;
            }
         next_level_=GlobalVariableGet("test_next_level_"+Symbol()) ;
         for (i = 1; i<grid_lines-1; i++)
            {
            pips_risked = pips_risked+(((RangeMid()+grid_separation*grid_lines)-(RangeMid()-grid_separation*(grid_lines-i))));
            money_risked = (pips_risked*100000*Lots)/2 ;
            }
         if(OrdersTotal() < max_trades)
         Comment("I am trading a grid from ", RangeMid()-grid_separation*grid_lines, " to ", RangeMid()+grid_separation*grid_lines, "\n",
                 "closest grid line to enter trade is ", test_next_level_ , "\n", 
                 "distance between grid lines is ", grid_separation*10000, " pips", "\n",
                 "number of grid lines is ", grid_lines, "\n",
                 "maximum number of trades is ", max_trades, "\n",
                 "AccountEquityPercent is ", AccountEquity()/AccountBalance()*100, "\n",
                 "---------------------------------------------------",  "\n",
                 "You are risking a potential ", money_risked, "USD")  ;     
         if(OrdersTotal() >= max_trades)
         Comment("I am full, I have ", max_trades, " positions opened");
         if(AccountEquity()/AccountBalance()*100>=AccountEquityPercent)
            {
            for(i=0;i<OrdersTotal();i++)
               {
               OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
               if(OrderType() <= OP_SELL && OrderSymbol() == Symbol())
                  {
                  if(OrderType() == OP_BUY)
                     {
                     OrderClose(OrderTicket(),OrderLots(),Bid,slip,Yellow);
                     }
                  else
                     {
                     OrderClose(OrderTicket(),OrderLots(),Ask,slip,Yellow);
                     }
                  }
               i--;
               }
            }
         return(0);
         }
      if(FIFO==true&&MM==true)
         {   
         if(Entertradebuy==1)
            {
            Ticket1 = OrderSend(Symbol(), OP_BUY, LotsOptimized(), Ask, slip, 0, 0, "Buy(#" + 1 + ")", MagicNumber, 0, DodgerBlue) ;         
            if(Ticket1!=1) Print("Error# "+GetLastError());         
            }
         if(Entertradesell==1)
            {
            Ticket2 = OrderSend(Symbol(), OP_SELL, LotsOptimized(), Bid, slip, 0, 0, "Sell(#" + 1 + ")", MagicNumber, 0, DeepPink) ;
            if(Ticket2!=1) Print("Error# "+GetLastError());         
            }
         for (i = 1; i<grid_lines; i++)
            {
            if(Ask - (RangeMid() - grid_separation*i) < grid_separation)
            GlobalVariableSet("test_next_level_"+Symbol(), RangeMid() - grid_separation*i) ;
            if(RangeMid() + grid_separation*i - Bid < grid_separation)
            GlobalVariableSet("test_next_level_"+Symbol(), RangeMid() + grid_separation*i) ;
            }
         next_level_=GlobalVariableGet("test_next_level_"+Symbol()) ;
         for (i = 1; i<grid_lines-1; i++)
            {
            pips_risked = pips_risked+(((RangeMid()+grid_separation*grid_lines)-(RangeMid()-grid_separation*(grid_lines-i))));
            money_risked = (pips_risked*100000*LotsOptimized())/2 ;
            }
         if(OrdersTotal() < max_trades)
         Comment("I am trading a grid from ", RangeMid()-grid_separation*grid_lines, " to ", RangeMid()+grid_separation*grid_lines, "\n",
                 "closest grid line to enter trade is ", test_next_level_ , "\n", 
                 "distance between grid lines is ", grid_separation*10000, " pips", "\n",
                 "number of grid lines is ", grid_lines, "\n",
                 "maximum number of trades is ", max_trades, "\n",
                 "AccountEquityPercent is ", AccountEquity()/AccountBalance()*100, "\n",
                 "---------------------------------------------------",  "\n",
                 "You are risking a potential ", money_risked, "USD")  ;     
         if(OrdersTotal() >= max_trades)
         Comment("I am full, I have ", max_trades, " positions opened");
         if(AccountEquity()/AccountBalance()*100>=AccountEquityPercent)
            {
            for(i=0;i<OrdersTotal();i++)
               {
               OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
               if(OrderType() <= OP_SELL && OrderSymbol() == Symbol())
                  {
                  if(OrderType() == OP_BUY)
                     {
                     OrderClose(OrderTicket(),OrderLots(),Bid,slip,Yellow);
                     }
                  else
                     {
                     OrderClose(OrderTicket(),OrderLots(),Ask,slip,Yellow);
                     }
                  }
               i--;
               }
            }
         return(0);
         }
      }
   else
      {   
      if(OrdersTotal()<max_trades)
         {
         for (i = 1; i<grid_lines; i++) 
            {
            if (Ask == RangeMid() - grid_separation*i)
               {
               Entertradebuy=1 ;
               }
            if (Bid == RangeMid() + grid_separation*i)
               {
               Entertradesell=1 ; 
               }
            }
         }
      iOrders = OrdersTotal()-1;
      for (i = iOrders; i>=0; i--)
         {
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if ((OrderOpenPrice() == Ask || OrderOpenPrice() == Bid)&&OrderMagicNumber()==MagicNumber)
            {
            Entertradebuy=0  ;
            Entertradesell=0 ;
            }
         } 
      if(FIFO==false&&MM==false)
         {
         if(Entertradebuy==1)
            {
            Ticket1 = OrderSend(Symbol(), OP_BUY, Lots, Ask, slip, 0, 0, "Buy(#" + 1 + ")", MagicNumber, 0, DodgerBlue) ;         
            if(Ticket1!=1) Print("Error# "+GetLastError());         
            }
         if(Entertradesell==1)
            {
            Ticket2 = OrderSend(Symbol(), OP_SELL, Lots, Bid, slip, 0, 0, "Sell(#" + 1 + ")", MagicNumber, 0, DeepPink) ;
            if(Ticket2!=1) Print("Error# "+GetLastError());         
            }
         for(i=0;i<OrdersTotal();i++)
            {
            OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
            if(OrderType()<=OP_SELL&&OrderMagicNumber()==MagicNumber&&OrderTakeProfit()==0)
               {
               if(OrderType()==OP_BUY)
                  {
                  OrderModify(OrderTicket(),OrderOpenPrice(),0,OrderOpenPrice()+TP_absolute,0,CLR_NONE);
                  }
               else
                  {   
                  OrderModify(OrderTicket(),OrderOpenPrice(),0,OrderOpenPrice()-TP_absolute,0,CLR_NONE);
                  }
               }
            }      
         for (i = 1; i<grid_lines; i++)
            {
            if(Ask - (RangeMid() - grid_separation*i) < grid_separation)
            GlobalVariableSet("next_level_"+Symbol(), RangeMid() - grid_separation*i) ;
            if(RangeMid() + grid_separation*i - Bid < grid_separation)
            GlobalVariableSet("next_level_"+Symbol(), RangeMid() + grid_separation*i) ;
            }
         next_level_=GlobalVariableGet("next_level_"+Symbol()) ;
         for (i = 1; i<grid_lines-1; i++)
            {
            pips_risked = pips_risked+(((RangeMid()+grid_separation*grid_lines)-(RangeMid()-grid_separation*(grid_lines-i))));
            money_risked = (pips_risked*100000*Lots)/2 ;
            }
         if(OrdersTotal() < max_trades)
         Comment("I am trading a grid from ", RangeMid()-grid_separation*grid_lines, " to ", RangeMid()+grid_separation*grid_lines, "\n",
                 "closest grid line to enter trade is ", next_level_ , "\n", 
                 "distance between grid lines is ", grid_separation*10000, " pips", "\n",
                 "number of grid lines is ", grid_lines, "\n",
                 "maximum number of trades is ", max_trades, "\n",
                 "TP is ", TP_absolute, "\n",
                 "---------------------------------------------------",  "\n",
                 "You are risking a potential ", money_risked, "USD")  ;     
         if(OrdersTotal() >= max_trades)
         Comment("I am full, I have ", max_trades, " positions opened");
         return(0);
         }
      if(FIFO==false&&MM==true)
         {   
         if(Entertradebuy==1)
            {
            Ticket1 = OrderSend(Symbol(), OP_BUY, LotsOptimized(), Ask, slip, 0, 0, "Buy(#" + 1 + ")", MagicNumber, 0, DodgerBlue) ;         
            if(Ticket1!=1) Print("Error# "+GetLastError());         
            }
         if(Entertradesell==1)
            {
            Ticket2 = OrderSend(Symbol(), OP_SELL, LotsOptimized(), Bid, slip, 0, 0, "Sell(#" + 1 + ")", MagicNumber, 0, DeepPink) ;
            if(Ticket2!=1) Print("Error# "+GetLastError());         
            }
         for(i=0;i<OrdersTotal();i++)
            {
            OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
            if(OrderType()<=OP_SELL&&OrderMagicNumber()==MagicNumber&&OrderTakeProfit()==0)
               {
               if(OrderType()==OP_BUY)
                  {
                  OrderModify(OrderTicket(),OrderOpenPrice(),0,OrderOpenPrice()+TP_absolute,0,CLR_NONE);
                  }
               else
                  {   
                  OrderModify(OrderTicket(),OrderOpenPrice(),0,OrderOpenPrice()-TP_absolute,0,CLR_NONE);
                  }
               }
            }      
         for (i = 1; i<grid_lines; i++)
            {
            if(Ask - (RangeMid() - grid_separation*i) < grid_separation)
            GlobalVariableSet("next_level_"+Symbol(), RangeMid() - grid_separation*i) ;
            if(RangeMid() + grid_separation*i - Bid < grid_separation)
            GlobalVariableSet("next_level_"+Symbol(), RangeMid() + grid_separation*i) ;
            }
         next_level_=GlobalVariableGet("next_level_"+Symbol()) ;
         for (i = 1; i<grid_lines-1; i++)
            {
            pips_risked = pips_risked+(((RangeMid()+grid_separation*grid_lines)-(RangeMid()-grid_separation*(grid_lines-i))));
            money_risked = (pips_risked*100000*LotsOptimized())/2 ;
            }
         if(OrdersTotal() < max_trades)
         Comment("I am trading a grid from ", RangeMid()-grid_separation*grid_lines, " to ", RangeMid()+grid_separation*grid_lines, "\n",
                 "closest grid line to enter trade is ", next_level_ , "\n", 
                 "distance between grid lines is ", grid_separation*10000, " pips", "\n",
                 "number of grid lines is ", grid_lines, "\n",
                 "maximum number of trades is ", max_trades, "\n",
                 "TP is ", TP_absolute, "\n",
                 "---------------------------------------------------",  "\n",
                 "You are risking a potential ", money_risked, "USD")  ;     
         if(OrdersTotal() >= max_trades)
         Comment("I am full, I have ", max_trades, " positions opened");
         return(0);
         }
      if(FIFO==true&&MM==false)
         {   
         if(Entertradebuy==1)
            {
            Ticket1 = OrderSend(Symbol(), OP_BUY, Lots, Ask, slip, 0, 0, "Buy(#" + 1 + ")", MagicNumber, 0, DodgerBlue) ;         
            if(Ticket1!=1) Print("Error# "+GetLastError());         
            }
         if(Entertradesell==1)
            {
            Ticket2 = OrderSend(Symbol(), OP_SELL, Lots, Bid, slip, 0, 0, "Sell(#" + 1 + ")", MagicNumber, 0, DeepPink) ;
            if(Ticket2!=1) Print("Error# "+GetLastError());         
            }
         for (i = 1; i<grid_lines; i++)
            {
            if(Ask - (RangeMid() - grid_separation*i) < grid_separation)
            GlobalVariableSet("next_level_"+Symbol(), RangeMid() - grid_separation*i) ;
            if(RangeMid() + grid_separation*i - Bid < grid_separation)
            GlobalVariableSet("next_level_"+Symbol(), RangeMid() + grid_separation*i) ;
            }
         next_level_=GlobalVariableGet("next_level_"+Symbol()) ;
         for (i = 1; i<grid_lines-1; i++)
            {
            pips_risked = pips_risked+(((RangeMid()+grid_separation*grid_lines)-(RangeMid()-grid_separation*(grid_lines-i))));
            money_risked = (pips_risked*100000*Lots)/2 ;
            }
         if(OrdersTotal() < max_trades)
         Comment("I am trading a grid from ", RangeMid()-grid_separation*grid_lines, " to ", RangeMid()+grid_separation*grid_lines, "\n",
                 "closest grid line to enter trade is ", next_level_ , "\n", 
                 "distance between grid lines is ", grid_separation*10000, " pips", "\n",
                 "number of grid lines is ", grid_lines, "\n",
                 "maximum number of trades is ", max_trades, "\n",
                 "AccountEquityPercent is ", AccountEquity()/AccountBalance()*100, "\n",
                 "---------------------------------------------------",  "\n",
                 "You are risking a potential ", money_risked, "USD")  ;     
         if(OrdersTotal() >= max_trades)
         Comment("I am full, I have ", max_trades, " positions opened");
         if(AccountEquity()/AccountBalance()*100>=AccountEquityPercent)
            {
            for(i=0;i<OrdersTotal();i++)
               {
               OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
               if(OrderType() <= OP_SELL && OrderSymbol() == Symbol())
                  {
                  if(OrderType() == OP_BUY)
                     {
                     OrderClose(OrderTicket(),OrderLots(),Bid,slip,Yellow);
                     }
                  else
                     {
                     OrderClose(OrderTicket(),OrderLots(),Ask,slip,Yellow);
                     }
                  }
               i--;
               }
            }
         return(0);
         }
      if(FIFO==true&&MM==true)
         {   
         if(Entertradebuy==1)
            {
            Ticket1 = OrderSend(Symbol(), OP_BUY, LotsOptimized(), Ask, slip, 0, 0, "Buy(#" + 1 + ")", MagicNumber, 0, DodgerBlue) ;         
            if(Ticket1!=1) Print("Error# "+GetLastError());         
            }
         if(Entertradesell==1)
            {
            Ticket2 = OrderSend(Symbol(), OP_SELL, LotsOptimized(), Bid, slip, 0, 0, "Sell(#" + 1 + ")", MagicNumber, 0, DeepPink) ;
            if(Ticket2!=1) Print("Error# "+GetLastError());         
            }
         for (i = 1; i<grid_lines; i++)
            {
            if(Ask - (RangeMid() - grid_separation*i) < grid_separation)
            GlobalVariableSet("next_level_"+Symbol(), RangeMid() - grid_separation*i) ;
            if(RangeMid() + grid_separation*i - Bid < grid_separation)
            GlobalVariableSet("next_level_"+Symbol(), RangeMid() + grid_separation*i) ;
            }
         next_level_=GlobalVariableGet("next_level_"+Symbol()) ;
         for (i = 1; i<grid_lines-1; i++)
            {
            pips_risked = pips_risked+(((RangeMid()+grid_separation*grid_lines)-(RangeMid()-grid_separation*(grid_lines-i))));
            money_risked = (pips_risked*100000*LotsOptimized())/2 ;
            }
         if(OrdersTotal() < max_trades)
         Comment("I am trading a grid from ", RangeMid()-grid_separation*grid_lines, " to ", RangeMid()+grid_separation*grid_lines, "\n",
                 "closest grid line to enter trade is ", next_level_ , "\n", 
                 "distance between grid lines is ", grid_separation*10000, " pips", "\n",
                 "number of grid lines is ", grid_lines, "\n",
                 "maximum number of trades is ", max_trades, "\n",
                 "AccountEquityPercent is ", AccountEquity()/AccountBalance()*100, "\n",
                 "---------------------------------------------------",  "\n",
                 "You are risking a potential ", money_risked, "USD")  ;     
         if(OrdersTotal() >= max_trades)
         Comment("I am full, I have ", max_trades, " positions opened");
         if(AccountEquity()/AccountBalance()*100>=AccountEquityPercent)
            {
            for(i=0;i<OrdersTotal();i++)
               {
               OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
               if(OrderType() <= OP_SELL && OrderSymbol() == Symbol())
                  {
                  if(OrderType() == OP_BUY)
                     {
                     OrderClose(OrderTicket(),OrderLots(),Bid,slip,Yellow);
                     }
                  else
                     {
                     OrderClose(OrderTicket(),OrderLots(),Ask,slip,Yellow);
                     }
                  }
               i--;
               }
            }
         return(0);
         }
      }
   }   
double LotsOptimized()
   {
   double lot = Lots;
   if(microlots==true)
      {
      lot = NormalizeDouble(MathFloor(AccountBalance()/Balancefactor)*0.01-0.005,2);
      if(lot < 0.01) lot = 0.01;
      return(lot);
      }
   else
      {    
      lot = NormalizeDouble(MathFloor(AccountBalance()/Balancefactor)*0.01-0.05, 1);
      if(lot < 0.1) lot = 0.1;
      return(lot);
      }
   }
double RangeMid()
   {
   double ilow = 0.0,rangemid = 0;
   ilow = iLow(Symbol(), PERIOD_D1, 0);
   for (int i = 0; i <= LookBackDays; i++)
      if (iLow(Symbol(), PERIOD_D1, i) < ilow) ilow = iLow(Symbol(), PERIOD_D1, i);
   double ihigh = 0.0;
   ihigh = iHigh(Symbol(), PERIOD_D1, 0);
   for (i = 0; i <= LookBackDays; i++)
      if (iHigh(Symbol(), PERIOD_D1, i) < ihigh) ilow = iHigh(Symbol(), PERIOD_D1, i);
   ihigh = NormalizeDouble(ihigh, Digits);
   ilow = NormalizeDouble(ilow, Digits);
   rangemid = (ihigh + ilow) / 2.0;
   rangemid = NormalizeDouble(rangemid, Digits);
   return(rangemid);
   }

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

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