AntiFragile EA_v1.0

Author: Copyright 2013, Mt-Coder
Orders Execution
It automatically opens orders when conditions are reachedChecks for the total of open ordersIt can change open orders parameters, due to possible stepping strategy
2 Views
0 Downloads
0 Favorites

Profitability Reports

GBP/CAD Oct 2024 - Jan 2025
40.00 %
Total Trades 25
Won Trades 0
Lost trades 0
Win Rate 0.00 %
Expected payoff -5.77
Gross Profit 94.80
Gross Loss -239.06
Total Net Profit -144.26
-100%
-50%
0%
50%
100%
GBP/USD Oct 2024 - Jan 2025
64.00 %
Total Trades 13
Won Trades 8
Lost trades 5
Win Rate 0.62 %
Expected payoff -4.28
Gross Profit 100.34
Gross Loss -156.00
Total Net Profit -55.66
-100%
-50%
0%
50%
100%
AntiFragile EA_v1.0
//+------------------------------------------------------------------+
//|                                               AntiFragile EA.mq4 |
//|                                         Copyright 2013, Mt-Coder |
//|                                             mt-coderØhotmail.com |
//|                                                                  |
//|                 For your programming projects                    |
//|          Don't hesitate to contact me Ø                          |
//|                            mt-coderØhotmail.com                  |
//+------------------------------------------------------------------+

#property copyright "Copyright 2013, Mt-Coder"
#property link      "mailto:mt-coder@hotmail.com"

extern double     StartingLot = 0.1;
extern double     IncreasePercentage = 1;
extern int        SpaceBetweenTrades = 700;
extern int        NumberOfTrades = 50;
extern int        StopLoss = 300;
extern int        TrailingStop = 100;
extern bool        TradeLong = true;
extern bool        TradeShort = true;
extern int         Magic = 11235;

//---------
int     POS_n_BUY;
int     POS_n_SELL;
int     POS_n_BUYLIMIT;
int     POS_n_SELLLIMIT;
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();
   //-------
   if(POS_n_BUYLIMIT == 0){DeleteSellLimit();}
   if(POS_n_SELLLIMIT == 0){DeleteBuyLimit();}
   //-------
   // place orders
   //-------
   
   if(POS_n_BUYLIMIT + POS_n_SELLLIMIT == 0)
   {

      for(i=1;i<=NumberOfTrades;i++)
      {
      RefreshRates();
      
      TradedLot = NormalizeDouble(StartingLot*(1+((i-1)*(IncreasePercentage/100))),2);
      TradedBLevel = NormalizeDouble(Bid - ((SpaceBetweenTrades * i)*Point),Digits);
      TradedSLevel = NormalizeDouble(Ask + ((SpaceBetweenTrades * i)*Point),Digits);
            
         if(TradeLong) 
         {
         ticketB=OrderSend(Symbol(),OP_BUYLIMIT,TradedLot,TradedBLevel,1,TradedBLevel-StopLoss*Point,0,"AF EA",Magic,0,Green);
            if(ticketB>0)
           {
            if(OrderSelect(ticketB,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY LIMIT order sent : ",OrderOpenPrice());
           }
            else 
            {
             Print("Error sending BUY LIMIT order : ",GetLastError()); 
            }
         }
        
        //---------------
         if(TradeShort) 
         {
     
         ticketS=OrderSend(Symbol(),OP_SELLLIMIT,TradedLot,TradedSLevel,1,TradedSLevel+StopLoss*Point,0,"AF EA",Magic,0,Red);
            if(ticketS>0)
           {
            if(OrderSelect(ticketS,SELECT_BY_TICKET,MODE_TRADES)) Print("SELLLIMIT order sent : ",OrderOpenPrice());
           }
            else 
            {
             Print("Error sending SELLLIMIT 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_BUYLIMIT = 0;
    POS_n_SELLLIMIT = 0;
    
    for( int i = 0 ; i < OrdersTotal() ; i++ ){
        if( OrderSelect( i, SELECT_BY_POS, MODE_TRADES ) == false || 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_BUYLIMIT  && OrderSymbol() == Symbol() && OrderMagicNumber()==Magic){
            POS_n_BUYLIMIT++;
        }
        else if( OrderType() == OP_SELLLIMIT  && OrderSymbol() == Symbol() && OrderMagicNumber()==Magic){
            POS_n_SELLLIMIT++;
        }
        
    }
    POS_n_total = POS_n_BUY + POS_n_SELL + POS_n_BUYLIMIT + POS_n_SELLLIMIT;
}
//-------
void DeleteBuyLimit()
{
int cnt, total;
total=OrdersTotal();
  for(cnt=total-1;cnt>=0;cnt--)
     {
       OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
       if(OrderMagicNumber() != Magic || OrderType()!=OP_BUYLIMIT) break;
      
      if( OrderSymbol() == Symbol())  OrderDelete(OrderTicket());
      
     }
}
//-------
void DeleteSellLimit()
{
int cnt, total;
total=OrdersTotal();
  for(cnt=total-1;cnt>=0;cnt--)
     {
       OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
       if(OrderMagicNumber() != Magic || OrderType()!=OP_SELLLIMIT) break;
      
if( OrderSymbol() == Symbol())  OrderDelete(OrderTicket());      
     }
}
//+------------------------------------------------------------------+

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