newgrid_2009_11_15_thorup_001

Author: Expert for tests
Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reachedIt Closes Orders by itself
0 Views
0 Downloads
0 Favorites

Profitability Reports

GBP/USD Oct 2024 - Jan 2025
0.00 %
Total Trades 0
Won Trades 0
Lost trades 0
Win Rate 0.0 %
Expected payoff 0.00
Gross Profit 0.00
Gross Loss 0.00
Total Net Profit 0.00
-100%
-50%
0%
50%
100%
newgrid_2009_11_15_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 RangeMid = 1.5465 ;
extern double grid_separation = 0.005 ;
extern double TP_absolute = 0.005 ;
extern double AccountEquityPercent=100.01;
extern bool FIFO=false;
extern int slip = 3 ;
   
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)
               {
               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, Ask+TP_absolute, "Buy(#" + 1 + ")", MagicNumber, 0, DodgerBlue) ;
            if(Ticket1!=1) Print("Error# "+GetLastError());         
            }
         if(Entertradesell==1)
            {
            Ticket2 = OrderSend(Symbol(), OP_SELL, Lots, Bid, slip, 0, Bid-TP_absolute, "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",
                 "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, Ask+TP_absolute, "Buy(#" + 1 + ")", MagicNumber, 0, DodgerBlue) ;         
            if(Ticket1!=1) Print("Error# "+GetLastError());         
            }
         if(Entertradesell==1)
            {
            Ticket2 = OrderSend(Symbol(), OP_SELL, LotsOptimized(), Bid, slip, 0, Bid-TP_absolute, "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",
                 "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, Ask+TP_absolute, "Buy(#" + 1 + ")", MagicNumber, 0, DodgerBlue) ;         
            if(Ticket1!=1) Print("Error# "+GetLastError());         
            }
         if(Entertradesell==1)
            {
            Ticket2 = OrderSend(Symbol(), OP_SELL, Lots, Bid, slip, 0, Bid-TP_absolute, "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",
                 "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, Ask+TP_absolute, "Buy(#" + 1 + ")", MagicNumber, 0, DodgerBlue) ;         
            if(Ticket1!=1) Print("Error# "+GetLastError());         
            }
         if(Entertradesell==1)
            {
            Ticket2 = OrderSend(Symbol(), OP_SELL, LotsOptimized(), Bid, slip, 0, Bid-TP_absolute, "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",
                 "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);
      }
   }
   //+------------------------------------------------------------------+

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