FractalArtificialIntelligence





//+------------------------------------------------------------------+
//|                                FractalArtificialIntelligence.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "mich99@o2.pl inspired by Yury V. Reshetov ICQ:282715499 http://reshetov.xnet.uz/"
#property link      ""
//---- input parameters

extern int    x1 = 100;
extern int    x2 = 100;
extern int    x3 = 100;
extern int    x4 = 100;


extern int    s1 = 3;
extern int    s2 = 3;
extern int    s3 = 3;
extern int    s4 = 3;



// StopLoss level



extern double sl = 550; // for four digits =55
extern double profittrail=2;
extern double lots = 0.1;
extern int MagicNumber = 777888;
static int prevtime = 0;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   if(Time[0] == prevtime) 
       return(0);
   prevtime = Time[0];
   int spread = 3;
//----
   if(IsTradeAllowed()) 
     {
       RefreshRates();
       spread = MarketInfo(Symbol(), MODE_SPREAD);
     } 
   else 
     {
       prevtime = Time[1];
       return(0);
     }
   int ticket = -1;
// check for opened position
   int total = OrdersTotal();   
//----





   for(int i = 0; i < total; i++) 
     {
       OrderSelect(i, SELECT_BY_POS, MODE_TRADES); 
       // check for symbol & magic number
       if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) 
         {
           int prevticket = OrderTicket();
           // long position is opened
           if(OrderType() == OP_BUY) 
             {
               // check profit 
               if(Bid > (OrderStopLoss() + (sl * profittrail  + spread) * Point)) 
                 {               
                   if(Crossed()<0 )  
                     { // reverse
                       ticket = OrderSend(Symbol(), OP_SELL, lots * 2, Bid, 3, 
                                          Ask + sl * Point, 0, WindowExpertName(), MagicNumber, 0, Red); 
                       Sleep(30000);
                       //----
                       if(ticket < 0) 
                           prevtime = Time[1];
                       else 
                           OrderCloseBy(ticket, prevticket, Blue);   
                     } 
                   if(Crossed()>0 ) 
                     { // trailing stop
                       if(!OrderModify(OrderTicket(), OrderOpenPrice(), Bid - sl * Point, 
                          0, 0, Blue)) 
                         {
                           Sleep(30000);
                           prevtime = Time[1];
                         }
                     }
                 }  
               // short position is opened
             } 
           else 
             {
               // check profit 
               if(Ask < (OrderStopLoss() - (sl * profittrail + spread) * Point)) 
                 {
                   if(Crossed()>0 )  
                     { // reverse
                       ticket = OrderSend(Symbol(), OP_BUY, lots * 2, Ask, 3, 
                                          Bid - sl * Point, 0, WindowExpertName(), MagicNumber, 0, Blue); 
                       Sleep(30000);
                       //----
                       if(ticket < 0) 
                           prevtime = Time[1];
                       else 
                           OrderCloseBy(ticket, prevticket, Blue);   
                     } 
                   if(Crossed()<0 ) 
                     { // trailing stop
                       if(!OrderModify(OrderTicket(), OrderOpenPrice(), Ask + sl * Point, 
                          0, 0, Blue)) 
                         {
                           Sleep(30000);
                           prevtime = Time[1];
                         }
                     }
                 }  
             }
           // exit
           return(0);
         }
     }
// check for long or short position possibility
   if(Crossed()>0 ) 
     { //long
       ticket = OrderSend(Symbol(), OP_BUY, lots, Ask, 3, Bid - sl * Point, 0, WindowExpertName(), 
                          MagicNumber, 0, Blue); 
       //----
       if(ticket < 0) 
         {
           Sleep(30000);
           prevtime = Time[1];
         }
     } 
   if(Crossed()<0 ) 
     { // short
       ticket = OrderSend(Symbol(), OP_SELL, lots, Bid, 3, Ask + sl * Point, 0, WindowExpertName(), 
                          MagicNumber, 0, Red); 
       if(ticket < 0) 
         {
           Sleep(30000);
           prevtime = Time[1];
         }
     }
//--- exit
   return(0);
  }
//+------------------------------------------------------------------+
//| The PERCEPTRON - a perceiving and recognizing function           |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
  
  
  
   double Cro1(int period)
   {
    
   double frup = iFractals(NULL, 5, MODE_UPPER, period);
   double frdw = iFractals(NULL, 5, MODE_LOWER, period);
   
      
   double mid = Close[1];//iMA(NULL,0,period,0,MODE_SMA, PRICE_CLOSE, 1);
	double masell = iMA(NULL,5,50,0,MODE_SMA, PRICE_CLOSE, 1)+ 2 * iATR(NULL, 5, 50, 1);
   double mabuy = iMA(NULL,5,50,0,MODE_SMA, PRICE_CLOSE, 1)- 2 * iATR(NULL, 5, 50, 1);

    if ( mid > frup && frup!=0 && mid < masell) return (-1); 
    if ( mid < frdw && frdw!=0  && mid > mabuy ) return (1); 
    
     return (0); 
   }
   
   
       
   double Cro2(int period) 
   {
    double mid = Close[1];//iMA(NULL,0,period,0,MODE_SMA, PRICE_CLOSE, 1);
	double frup = iFractals(NULL, 15, MODE_UPPER, period);
   double frdw = iFractals(NULL, 15, MODE_LOWER, period);
   
   double masell = iMA(NULL,15,50,0,MODE_SMA, PRICE_CLOSE, 1)+ 2 * iATR(NULL, 15, 50, 1);
   double mabuy = iMA(NULL,15,50,0,MODE_SMA, PRICE_CLOSE, 1)- 2 * iATR(NULL, 15, 50, 1);

    if ( mid > frup && frup!=0 && mid < masell) return (-1); 
    if ( mid < frdw && frdw!=0  && mid > mabuy ) return (1);
     
     return (0); //elsewhere
   }    
     
     
     double Cro3(int period)   
   {
   double frup = iFractals(NULL, 60, MODE_UPPER, period);
   double frdw = iFractals(NULL, 60, MODE_LOWER, period);
   
    
   double mid = Close[1];//iMA(NULL,0,period,0,MODE_SMA, PRICE_CLOSE, 1);
	double masell = iMA(NULL,60,50,0,MODE_SMA, PRICE_CLOSE, 1)+ 2 * iATR(NULL, 60, 50, 1);
   double mabuy = iMA(NULL,60,50,0,MODE_SMA, PRICE_CLOSE, 1)- 2 * iATR(NULL, 60, 50, 1);

    if ( mid > frup && frup!=0 && mid < masell) return (-1); 
    if ( mid < frdw && frdw!=0  && mid > mabuy ) return (1); 
      
     return (0); //elsewhere
   }    
    
    
    
     
     double Cro4(int period)
   {
   double frup = iFractals(NULL, 240, MODE_UPPER, period);
   double frdw = iFractals(NULL, 240, MODE_LOWER, period);
   
      
   double mid = Close[1];//iMA(NULL,0,period,0,MODE_SMA, PRICE_CLOSE, 1);
	double masell = iMA(NULL,240,50,0,MODE_SMA, PRICE_CLOSE, 1)+ 2 * iATR(NULL, 240, 50, 1);
   double mabuy = iMA(NULL,240,50,0,MODE_SMA, PRICE_CLOSE, 1)- 2 * iATR(NULL, 240, 50, 1);

    if ( mid > frup && frup!=0 && mid < masell) return (-1); //up trend
    if ( mid < frdw && frdw!=0  && mid > mabuy ) return (1); //down trend
     
    
    
    
     return (0); //elsewhere
   }    
             
             
     double Crossed() 
  {
   double w1 = x1 - 100.0;
   double w2 = x2 - 100.0;
   double w3 = x3 - 100.0;
   double w4 = x4 - 100.0;
  
   double a1 = Cro1(s1);
   double a2 = Cro2(s2);
   double a3 = Cro3(s3);
   double a4 = Cro4(s4);
 
   return(w1 * a1 + w2 * a2 + w3 * a3 + w4 * a4);
  }
  
  
 



Sample





Analysis



Market Information Used:

Series array that contains open time of each bar
Series array that contains close prices for each bar


Indicator Curves created:


Indicators Used:

Fractals
Indicator of the average true range
Moving average indicator


Custom Indicators Used:

Order Management characteristics:
Checks for the total of open orders

It can change open orders parameters, due to possible stepping strategy

Other Features: