//+------------------------------------------------------------------+
//|                                                     simplefx.mq4 |
//|                             Copyright © 2007, GLENNBACON.COM LLC |
//|                                  http://www.GetForexSoftware.com |
//+------------------------------------------------------------------+
// Shift of moving average
#define Shift 1
// Trend Detection function
#define TREND_BUY 11
#define TREND_SELL 22
#define TREND_CLOSE 33
// Input variables
extern double  Lots              = 0.10;
extern int     Slippage          = 3;
extern string  Order_Comment     = "EL FODEURO";
extern color   Order_Arrow_Color = Green;
extern double PercentMargin = 0.005;
extern int NOGOVolatility = 200;
extern int SMALLGOVolatility = 100;
extern int SSP = 34; // bars - calculating period
extern int SSK = 29; // tolerance of second line
// Global variables
int Total_Open_Orders = 1; // we only want to open one order at a time and only manage the one order
int cnt = 0;               // counter variable, used in for() loops
bool init_variables;       // init variable when program starts
datetime PreviousBar;      // record the candle/bar time
int NowTrend = TREND_CLOSE;
int Magic = 1;
//Indicators
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double Wal1[];
double Wal2[];
double ExtMapBuffer4[];
int i, i1,  i2, shift;
double SsMax, SsMin, SsMax05, SsMin05, Rsmin, Rsmax, Tsmin, Tsmax;
int val1, val2, AvgRange;
bool uptrend, old;
double iClose0,iClose1,iIchi,iLine1,iLine2;
/*
int loadOrders() {
   int dim,handle;
   dim  = -1;
   
   handle=FileOpen(Symbol()+".txt",FILE_CSV|FILE_READ,';');
      
      
      Print(">>>>>>>>>>"+ Symbol()+".txt");
      string str1;
      
      int year,month,day ;
      
      
      
      ArrayInitialize(ordens,0);
      
      
      
      int pos = 0 ;
      
      while(!FileIsEnding( handle ))
         {
         
            
         
            str1 = FileReadString(handle);
      
      
      if ( pos % 7 == 0 ) {
               dim++;
               pos = 0;
          //    Print(str1);
            
            }
            if (pos != 3 ) {
            
                ordens[dim][pos] = StrToDouble(str1);
            }
            else tiposOrdens[dim] = str1;
          
           
         //   Print("pos =" + pos + " Value" + str1);
            pos++;
            
         }
      FileClose(handle);
return (dim);
}
*/
int init()
  {
   init_variables = true;  // Allows us to init variables in the start function because 
                           // we cannot do this in the init function because the variables 
                           // use values from the chart 
                           
      
      //Print("existem " + dim +  "ordens" );
   return(0);
  }
int deinit()
  {
   // clear chart when EA is removed
   ObjectsDeleteAll();
   return(0);
  }
int start()
  {          
   // make sure trader has set Lots to at least the minimum lot size of the broker and 
   // we will normalize the Lots variable so we can properly open an order
   
   
   
   if(MarketInfo(Symbol(),MODE_MINLOT) == 0.01)
   {
      Lots = NormalizeDouble(Lots,2);
      if(Lots < 0.01)
      {
         Comment("The variable Lots must be 0.01 or greater to open an order. ");
         return(0);
      }
   }
   if(MarketInfo(Symbol(),MODE_MINLOT) == 0.1)
   {
      Lots = NormalizeDouble(Lots,1);
      if(Lots < 0.1)
      {
         Comment("The variable Lots must be 0.1 or greater to open an order. ");
         return(0);
      }
   }
   if(MarketInfo(Symbol(),MODE_MINLOT) == 1)
   {
      Lots = NormalizeDouble(Lots,0);
      if(Lots < 1)
      {
         Comment("The variable Lots must be 1 or greater to open an order. ");
         return(0);
      }
   }
         
   // init variables when the expert advisor first starts running
   if(init_variables == true)
   {
      PreviousBar = Time[0];     // record the current canle/bar open time
      
      // place code here that you only wnat to run one time
      MathSrand(TimeLocal());
      //Magic = MathRand();  
           
      init_variables = false;    // change to false so we only init variable once
   }
   
   // perform analysis and open orders on new candle/bar 
   if(NewBar() == true) {
 
   double ordens[100][6];
   string tiposOrdens[];
     int dim,handle;
      dim  = -1;
   
      handle=FileOpen(Symbol()+".txt",FILE_CSV|FILE_READ,';');
      
      
      //Print("fodasse >>>>>>>>>>"+ Symbol()+".txt");
      string str1;
      
      int year,month,day ;
      
      
      
      ArrayInitialize(ordens,0);
      
      
      
      int pos = 0 ;
      
      while(!FileIsEnding( handle ))
         {
         
            
         
            str1 = FileReadString(handle);
      
      
      if ( pos % 7 == 0 ) {
               dim++;
               pos = 0;
          //    Print(str1);
            
            }
            if (pos != 3 ) {
            
                ordens[dim][pos] = StrToDouble(str1);
                
             //   Print( " dim=" + dim +" pos =" + pos +" Value=" + ordens[dim][pos]);
            }
            else {
             tiposOrdens[dim] = str1;
             
               // Print("pos =" + pos + " Value" + tiposOrdens[dim]);
             }
            pos++;
            
      }
      FileClose(handle);
      
      
    
      if(TotalOpenOrders() == Total_Open_Orders && SelectTheOrder() == true)
      {
            Print(" estou aqui dentro ha uma ordem activa1 " + OrderProfit() );   
      /*
         if((OrderType()) == OP_BUY && ((NowTrend == TREND_SELL) || (NowTrend == TREND_CLOSE)))
         {
            OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Order_Arrow_Color);
         }
         if((OrderType() == OP_SELL) && ((NowTrend == TREND_BUY) || (NowTrend == TREND_CLOSE)))
         {
            OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Order_Arrow_Color);
         }
         
        */    
      }
      
  
      
      // only perform analysis and open new order if we have not reached our Total_Open_Orders max
      if(TotalOpenOrders() < Total_Open_Orders)
      {
      
      
           // Print(" prin teste " + ordens[0][0] + " " + ordens[0][1] +" " +  ordens[0][2] + " "  );
            int count=ArraySize(ordens);
            int xx = 0 ;
               while ( xx < dim ) {
                  
                     // some calculations.
                    // Print("count " + dim + " " + xx +"ordem para o dia "+ ordens[xx][1] +" " +  ordens[xx][2] + " "  );
                     if ( Year() == ordens[xx][0] && Month() == ordens[xx][1] && Day() == ordens[xx][2] 
                        && Hour() > 19 && Minute() >= 0  ) {
                      
                           
                           if ( tiposOrdens[i] == "buy" ) {
                           
                                          Print("encontrei a ordem "+ tiposOrdens[i] + " ao preco " + ordens[i][4] + " preco actual " + Ask);
                                          OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,Order_Comment,Magic,0,Order_Arrow_Color);                           
                                          
                                          
                           }
                           else {
                                 OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,Order_Comment,Magic,0,Order_Arrow_Color);                           
                           }
                      
                      
                           
                        
                     }
                        xx= xx+1;
                  }  
               
               
      
      
      /*
         // open buy
         if(NowTrend == TREND_BUY)
         {
            // open order
            OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,Order_Comment,Magic,0,Order_Arrow_Color);
         }
         
         // open sell
         else if(NowTrend == TREND_SELL)
         {
            // open order 
            OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0,0,Order_Comment,Magic,0,Order_Arrow_Color);
         }
         */
      }
/*   
          Print("Date: ",Month(),"-",Day(),"-",Year()," Server Time: ",Hour(),":",Minute(),":",Seconds(),
            " volatility:", AvgRange,
            " iClose0: ",iClose0,     
            " iClose1: ",iClose1,
            " iIchi: ",iIchi,
            " iLine1: ",iLine1,
            " iLine2: ",iLine2,
            " Trend: ",NowTrend);  
*/
         Display_Info();
   
   }
   return(0);
  }
/////////////////////////////////////////////////////////////////////////
// Common functions                                                   //
///////////////////////////////////////////////////////////////////////
// This function returns the total amount of orders the expert advisor has open  
int TotalOpenOrders()
{
   cnt=OrdersTotal();
   int TotalOpenOrders = 0;
   
   if(cnt==0)
   {
      return(0);
   }
   else
   {
      for(;cnt>=0;cnt--)
      {
         RefreshRates();
         OrderSelect(cnt,SELECT_BY_POS);
         if(OrderMagicNumber()==Magic && OrderSymbol() == Symbol() )
         {
            TotalOpenOrders++;
         }
      }
   }
   return(TotalOpenOrders);
}
// This function finds the open order and selects it 
int SelectTheOrder()
{
   cnt=OrdersTotal();
      
   if(cnt==0)
   {
      return(false);
   }
   else
   {
      for(;cnt>=0;cnt--)
      {
         RefreshRates();
         OrderSelect(cnt,SELECT_BY_POS);
         if(OrderMagicNumber()==Magic)
         {
            return(true);
         }
      }
   }
   return(false);
}
// This function return the value true if the current bar/candle was just formed
bool NewBar()
{
   if(PreviousBar<Time[0])
   {
      PreviousBar = Time[0];
      return(true);
   }
   else
   {
      return(false);
   }
   return(false);    // in case if - else statement is not executed
}
// is trend up/bullish or is it down/bearish
int TrendDetection()
{
   double topLine,bottomLine;
   
   if (iClose1 == 0)
      iClose1 = iClose(NULL,0,10);
   else
      iClose1 = iClose0;
      
   iClose0 = iClose(NULL,0,0);
   AIchimoku();
   if (iLine1 > iLine2)
   {
      topLine = iLine1;
      bottomLine = iLine2;
   }
   else
   {
      topLine = iLine2;
      bottomLine = iLine1;   
   }
   if ((iClose0 > iIchi) && (iClose1 < iIchi))
      return(TREND_BUY);
   if ((iClose0 < iIchi) && (iClose1 > iIchi))
      return(TREND_SELL);
   if ((iClose0 < topLine ) && (iClose0 > bottomLine))
      return(TREND_CLOSE);         
   if (AvgRange < NOGOVolatility )
   {
      if ((iClose0 > topLine) && (iClose0 > iClose1))
            if (((iClose0-topLine)/iClose0)<PercentMargin)
               return(TREND_BUY);
   
      if ((iClose0 < bottomLine) && (iClose0 < iClose1))
            if (((bottomLine-iClose0)/bottomLine)<PercentMargin)
               return(TREND_SELL);
   }
   
   // flat no trend return 0
   return(0);
}
void Display_Info()
{
   Comment("Price:  ",NormalizeDouble(Bid,4),"\n",
            "Date: ",Month(),"-",Day(),"-",Year()," Server Time: ",Hour(),":",Minute(),":",Seconds(),"\n",
            "volatility: ", AvgRange,"\n",
            "iClose0: ",DoubleToStr(iClose0,4),"\n",     
            "iClose1: ",DoubleToStr(iClose1,4),"\n",
            "iIchi: ",DoubleToStr(iIchi,4),"\n",
            "iLine1: ",DoubleToStr(iLine1,4),"\n",
            "iLine2: ",DoubleToStr(iLine2,4),"\n");          
   return(0);
}
void AIchimoku()
{
       // maximum of previous SSP bars period
       SsMax = High[iHighest(NULL, 0, MODE_HIGH, SSP, 0)];      
       // minimum of previous SSP bars period
       SsMin = Low[iLowest(NULL, 0, MODE_LOW, SSP, 0)];         
       // maximum of SSP bars period for SSK bars from begin
       SsMax05 = High[iHighest(NULL, 0, MODE_HIGH, SSP, 0 + SSK)];
       // maximum of SSP bars period for SSK bars from begin
       SsMin05 = Low[iLowest(NULL, 0, MODE_LOW, SSP, 0 + SSK)];   
       iLine1 = (SsMax + SsMin) / 2;
       iLine2 = (SsMax05+SsMin05) / 2;
       val1 = ExtMapBuffer1[1] / Point;
       val2 = ExtMapBuffer2[1] / Point;
       //----
       Rsmax = High[iHighest(NULL, 0, MODE_HIGH, SSP*2, 0)]; 
       Rsmin = Low[iLowest(NULL, 0, MODE_LOW, SSP*2, 0)];  
       AvgRange = (Rsmax / Point) - (Rsmin / Point);
       //----
       Tsmax = High[iHighest(NULL, 0, MODE_HIGH, SSP*1.62, 0)];     
       Tsmin = Low[iLowest(NULL, 0, MODE_LOW, SSP*1.62, 0)];    
       iIchi = (Tsmax + Tsmin) / 2;
}
             
            
Comments