Pending_order_grid

Profit factor:
2.01

Here's a breakdown of what this script does, explained simply for someone who doesn't code:

Overall Purpose:

This script is designed for the MetaTrader platform (likely MetaTrader 4). It's an automated trading system, often called an "Expert Advisor" (EA), that aims to place a series of buy and sell orders based on price movements. It's attempting a grid-like trading strategy.

Key Settings (External Variables):

  • StartingLot: The initial size of the trades it will place (e.g., 0.01 lots). Think of this as how much "stock" it's buying or selling in each individual trade.

  • IncreasePercentage: A small percentage (e.g., 0.001) that it will increase the lot size with each subsequent trade in the series. If it places multiple trades, each one will be slightly larger than the last.

  • DistanceFromPrice: A small distance (e.g., 0.0010) away from the current price where it will start placing its orders. It avoids placing orders exactly at the current market price.

  • SpaceBetweenTrades: The distance, measured in "points," between each of the buy or sell orders it places. This determines how far apart the grid lines are. A point is a standard unit of measure for price movement in trading.

  • NumberOfTrades: The total number of buy and sell orders that the script will attempt to place.

  • Takeprofit: This is how far the price needs to move in your favor ("points") for a trade to automatically close and take a profit.

  • StopLoss: How far the price can move against you ("points") before a trade automatically closes to limit losses. Set to 99999, the script effectively disables stop loss by making it too far to execute.

  • TrailingStop: If a trade goes in your favor, the stop-loss order will "trail" behind the price, locking in profits. This setting determines how closely the stop loss follows the price ("points").

  • TradeLong: A true/false setting that determines whether the script is allowed to place buy orders ("long" positions).

  • TradeShort: A true/false setting that determines whether the script is allowed to place sell orders ("short" positions).

  • Magic: A unique identification number. This helps the script manage only its own trades and avoid interfering with other EAs or manual trades.

How it Works (Simplified):

  1. Initialization: When the script starts, it prepares itself. It doesn't do much other than get ready.

  2. Main Loop (start() function): This is the core of the script. It runs repeatedly while the script is active.

    • Order Placement: The script checks how many orders it has currently placed. If the number is less than NumberOfTrades, it attempts to place more.

      • It calculates the lot size for the next trade, increasing it slightly based on the IncreasePercentage.
      • It calculates the price levels at which to place the buy and sell orders, based on the current price, DistanceFromPrice, and SpaceBetweenTrades.
      • It sends "Buy Stop" and "Sell Stop" orders to the trading platform. These are pending orders that will only activate if the price reaches the specified levels.
      • The script monitors for errors during order placement and prints messages if something goes wrong.
    • Trailing Stop Management:

      • The script constantly checks the currently opened trades.
      • If the script find a trade and that trade goes in a favorable direction by a certain number of "points", the stop loss orders are moved to lock in profits.

Important Considerations:

  • Risk: Grid trading strategies can be risky. If the price moves strongly in one direction, the script can open many losing trades, potentially leading to significant losses.
  • Market Conditions: This type of strategy may work better in ranging or sideways markets than in strongly trending markets.
  • Customization: The many settings allow for some customization, but understanding how these settings interact is crucial for successful use.
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 strategy
11 Views
0 Downloads
0 Favorites
Pending_order_grid
ÿþ

extern double     StartingLot = 0.01;

extern double     IncreasePercentage = 0.001;

extern double     DistanceFromPrice = 0.0010;

extern int        SpaceBetweenTrades = 150;

extern int        NumberOfTrades = 200;

extern int        Takeprofit = 200;

extern int        StopLoss = 9999;

extern int        TrailingStop = 150;

extern bool        TradeLong = true;

extern bool        TradeShort = true;

extern int         Magic = 1;



//---------

int     POS_n_BUY;

int     POS_n_SELL;

int     POS_n_BUYSTOP;

int     POS_n_SELLSTOP;

int     POS_n_total;



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

//| expert initialization function                                   |

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

int init()

  {

//----

   

//----

   return(0);

  }

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

//| expert deinitialization function                                 |

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

int deinit()

  {

//----

   

//----

   return(0);

  }

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

//| expert start function                                            |

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

int start()

{

//----



   string DisplayText = "\n" +  "______ AntiFragile EA ______\n" + 

   "Coded By: MT-Coder\n" + 

   "** MT-CoderØhotmail.com **\n" ;

   

   Comment(DisplayText);



   int i ;

   double TradedLot;

   double TradedBLevel;

   double TradedSLevel;

   int ticketB;

   int ticketS;

   int total;

   int cnt;

   

   //-------

   count_position();

   //-------

   //-------

   // place orders

   //-------

   

   if(OrdersTotal()<=NumberOfTrades)

   {



      for(i=1;i<=NumberOfTrades;i++)

      {

      RefreshRates();

      

      TradedLot = NormalizeDouble(StartingLot*(1+((i-1)*(IncreasePercentage/100))),2);

      TradedBLevel = NormalizeDouble((Bid + DistanceFromPrice) + ((SpaceBetweenTrades * i)*Point),Digits);

      TradedSLevel = NormalizeDouble((Ask - DistanceFromPrice) -((SpaceBetweenTrades * i)*Point),Digits);

            

         if(TradeLong) 

         {

         ticketB=OrderSend(Symbol(),OP_BUYSTOP,TradedLot,TradedBLevel,1,TradedBLevel-StopLoss*Point,TradedBLevel+Takeprofit*Point,"AF EA",Magic,0,Green);

            if(ticketB>0)

           {

            if(OrderSelect(ticketB,SELECT_BY_TICKET,MODE_TRADES)) Print("BUYSTOP order sent : ",OrderOpenPrice());

           }

            else 

            {

             Print("Error sending BUYSTOP order : ",GetLastError()); 

            }

         }

        

        //---------------

         if(TradeShort) 

         {

     

         ticketS=OrderSend(Symbol(),OP_SELLSTOP,TradedLot,TradedSLevel,1,TradedSLevel+StopLoss*Point,TradedSLevel-Takeprofit*Point,"AF EA",Magic,0,Red);

            if(ticketS>0)

           {

            if(OrderSelect(ticketS,SELECT_BY_TICKET,MODE_TRADES)) Print("SELLSTOP order sent : ",OrderOpenPrice());

           }

            else 

            {

             Print("Error sending SELLSTOP order : ",GetLastError()); 

            }

         }      

      }

 }     

      //trailing stop

      

       total=OrdersTotal();

   for(cnt=0;cnt<total;cnt++)

     {

      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

      if(OrderType()<=OP_SELL &&   // check for opened position 

         OrderSymbol()==Symbol())  // check for symbol

        {

         if(OrderType()==OP_BUY)   // long position is opened

           {

            // check for trailing stop

            if(TrailingStop>0)  

              {                 

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

                 {

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

                    {

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

                     return(0);

                    }

                 }

              }

           }

         else // go to short position

           {

            // check for trailing stop

            if(TrailingStop>0)  

              {                 

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

                 {

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

                    {

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

                     return(0);

                    }

                 }

              }

           }

        }

       }

      

 

   return(0);

}



//-----

void count_position()

{

    POS_n_BUY  = 0;

    POS_n_SELL = 0;

    

    POS_n_BUYSTOP = 0;

    POS_n_SELLSTOP = 0;

    

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

        if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == true || OrderMagicNumber() != Magic){

            break;

        }

        if( OrderType() == OP_BUY  && OrderSymbol() == Symbol() && OrderMagicNumber()==Magic){

            POS_n_BUY++;

        }

        else if( OrderType() == OP_SELL  && OrderSymbol() == Symbol() && OrderMagicNumber()==Magic){

            POS_n_SELL++;

        }

        else if( OrderType() == OP_BUYSTOP  && OrderSymbol() == Symbol() && OrderMagicNumber()==Magic){

            POS_n_BUYSTOP++;

        }

        else if( OrderType() == OP_SELLSTOP  && OrderSymbol() == Symbol() && OrderMagicNumber()==Magic){

            POS_n_SELLSTOP++;

        }

        

    }

    POS_n_total = POS_n_BUY + POS_n_SELL + POS_n_BUYSTOP + POS_n_SELLSTOP;

}

//-------



Profitability Reports

USD/JPY Jul 2025 - Sep 2025
1.12
Total Trades 54
Won Trades 52
Lost trades 2
Win Rate 96.30 %
Expected payoff 0.12
Gross Profit 61.09
Gross Loss -54.74
Total Net Profit 6.35
-100%
-50%
0%
50%
100%
USD/CHF Jul 2025 - Sep 2025
0.59
Total Trades 21
Won Trades 19
Lost trades 2
Win Rate 90.48 %
Expected payoff -0.83
Gross Profit 24.77
Gross Loss -42.28
Total Net Profit -17.51
-100%
-50%
0%
50%
100%
USD/CAD Jul 2025 - Sep 2025
0.90
Total Trades 22
Won Trades 20
Lost trades 2
Win Rate 90.91 %
Expected payoff -0.12
Gross Profit 22.79
Gross Loss -25.37
Total Net Profit -2.58
-100%
-50%
0%
50%
100%
NZD/USD Jul 2025 - Sep 2025
0.77
Total Trades 20
Won Trades 18
Lost trades 2
Win Rate 90.00 %
Expected payoff -0.36
Gross Profit 24.72
Gross Loss -32.00
Total Net Profit -7.28
-100%
-50%
0%
50%
100%
GBP/USD Jul 2025 - Sep 2025
0.93
Total Trades 41
Won Trades 39
Lost trades 2
Win Rate 95.12 %
Expected payoff -0.11
Gross Profit 58.92
Gross Loss -63.50
Total Net Profit -4.58
-100%
-50%
0%
50%
100%
GBP/CAD Jul 2025 - Sep 2025
0.87
Total Trades 39
Won Trades 37
Lost trades 2
Win Rate 94.87 %
Expected payoff -0.14
Gross Profit 38.27
Gross Loss -43.76
Total Net Profit -5.49
-100%
-50%
0%
50%
100%
GBP/AUD Jul 2025 - Sep 2025
1.04
Total Trades 42
Won Trades 40
Lost trades 2
Win Rate 95.24 %
Expected payoff 0.04
Gross Profit 44.52
Gross Loss -42.83
Total Net Profit 1.69
-100%
-50%
0%
50%
100%
EUR/USD Jul 2025 - Sep 2025
0.89
Total Trades 393
Won Trades 153
Lost trades 240
Win Rate 38.93 %
Expected payoff -6.54
Gross Profit 21366.37
Gross Loss -23935.88
Total Net Profit -2569.51
-100%
-50%
0%
50%
100%
AUD/USD Jul 2025 - Sep 2025
0.89
Total Trades 17
Won Trades 15
Lost trades 2
Win Rate 88.24 %
Expected payoff -0.18
Gross Profit 24.39
Gross Loss -27.50
Total Net Profit -3.11
-100%
-50%
0%
50%
100%
USD/JPY Jan 2025 - Jul 2025
1.39
Total Trades 126
Won Trades 124
Lost trades 2
Win Rate 98.41 %
Expected payoff 0.29
Gross Profit 132.78
Gross Loss -95.61
Total Net Profit 37.17
-100%
-50%
0%
50%
100%

Comments