
//+------------------------------------------------------------------+
//|                                               SuperScalper_00.mq4|
//|                           Copyright © 2006, Done by investor_me. |
//|                                            investor.me@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Done by investor_me. (EURUSD M1 chart)"
#property link      "investor.me@gmail.com"
extern bool Usefilewrite=false;
int handle=0;
extern double StopLoss=13;
extern double TakeProfit=16;
int max_trades=2;

extern int EMA1=9;

extern int EMA2=42;

int EMA3=60;

extern double min1=0.8;

extern double min2=0.2;

extern double min3=0;

double min_close1=-3;

double min_close2=3;

double min_close3=-3;

extern int secure_pips=5;

extern int minutes_between_trades=0;

extern double risk=1;

extern bool auto_risk=false;

extern double Lots=0.1;

extern int MAGIC=123451;
extern double MaxLots=100;

double last_profit=0, last_lots, last_diff, C, EMA;
int last_order=0, ClosedTrades, buys_met, sells_met, MA[4], gap=1;
datetime last_order_time;

int init()  
  { 
   last_lots=Lots;  last_order_time=0;
   MA[1]=EMA1; MA[2]=EMA2; MA[3]=EMA3;
   minutes_between_trades=minutes_between_trades*60;
  }

int deinit(){ return(0); }

bool condition1(int type)
 { int total=0,i;
   if (type==1)
    {
      if (iClose(Symbol(),0,0)>M(EMA1,0)) 
       { 
         total++;
         for (i=1; i<=max_trades; i++) if (M(MA[i-1],0)>M(MA[i],0)&& M(MA[i],0)>M(MA[i],1)) total++;
       }    
    }
   else if (type==2)
    {
      if (iClose(Symbol(),0,0)<M(EMA1,0)) 
       { 
         total++;
         for (i=1; i<=max_trades; i++) if (M(MA[i-1],0)<M(MA[i],0)&& M(MA[i],0)<M(MA[i],1)) total++;
       }    
    } 
   if (total==max_trades) return (true);
   return (false); 
 }

bool condition2(int type)
 {
   if (type==1)
    {
      if (M(EMA1,0)-M(EMA1,1)>min1*Point && M(EMA1,gap)-M(EMA1,gap+1)<min2*Point) return (true); 
    }
   else if (type==2)
    {
      if (M(EMA1,1)-M(EMA1,0)>min1*Point && M(EMA1,gap+1)-M(EMA1,gap)<min2*Point) return (true); 
    } 
   return (false); 
 }

bool condition3(int type)
 {
   if (type==1)
    {
      if ((M(EMA1,0)-M(MA[max_trades],0))-(M(EMA1,gap+1)-M(MA[max_trades],gap))>min3*Point) return (true); 
    }
   else if (type==2)
    {
      if ((M(MA[max_trades],0)-M(EMA1,0))-(M(MA[max_trades],gap)-M(EMA1,gap+1))>min3*Point) return (true); 
    } 
   return (false); 
 }


bool condition_close1(int type)
 { int total=0,i;
   if (type==1)
    {
      if (iClose(Symbol(),0,0)>M(EMA1,0)) 
       { 
         total++;
         for (i=1; i<=max_trades; i++) if (M(MA[i-1],0)>M(MA[i],0)&& M(MA[i],0)>M(MA[i],1)) total++;
       }    
    }
   else if (type==2)
    {
      if (iClose(Symbol(),0,0)<M(EMA1,0)) 
       { 
         total++;
         for (i=1; i<=max_trades; i++) if (M(MA[i-1],0)<M(MA[i],0)&& M(MA[i],0)<M(MA[i],1)) total++;
       }    
    } 
   if (total==max_trades)
      {
      if (Usefilewrite)
         {
         if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES)==true)
            {
             handle = FileOpen("SuperScalper.txt",FILE_READ|FILE_WRITE);
             if(handle!=-1)
               {
               FileSeek(handle,0,SEEK_END);
               FileWrite(handle,"close_condition1 OrderTicket: ",OrderTicket() );
               //FileWrite(handle,"  ");

               FileFlush(handle);
               FileClose(handle);
               }
             }//select 
         }//usefilewrite
      return (true);
      } 
   return (false); 
 }

bool condition_close2(int type)
 {
   if (type==1)
    {
      if (M(EMA1,0)-M(EMA1,1)>min_close1*Point && M(EMA1,gap)-M(EMA1,gap+1)<min_close2*Point) 
      {
      if (Usefilewrite)
         {
         if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES)==true)
            {
             handle = FileOpen("SuperScalper.txt",FILE_READ|FILE_WRITE);
             if(handle!=-1)
               {
               FileSeek(handle,0,SEEK_END);
               FileWrite(handle,"close_condition2 type 1 OrderTicket: ",OrderTicket() );
               //FileWrite(handle,"  ");

               FileFlush(handle);
               FileClose(handle);
               }
             }//select 
         }//usefilewrite
      return (true);
      }  
    }
   else if (type==2)
    {
      if (M(EMA1,1)-M(EMA1,0)>min_close1*Point && M(EMA1,gap+1)-M(EMA1,gap)<min_close2*Point) 
      {
      if (Usefilewrite)
         {
         if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES)==true)
            {
             handle = FileOpen("SuperScalper.txt",FILE_READ|FILE_WRITE);
             if(handle!=-1)
               {
               FileSeek(handle,0,SEEK_END);
               FileWrite(handle,"close_condition2 type 2 OrderTicket: ",OrderTicket() );
               //FileWrite(handle,"  ");

               FileFlush(handle);
               FileClose(handle);
               }
             }//select 
         }//usefilewrite
      return (true);
      } 
    } 
   return (false); 
 }

bool condition_close3(int type)
 {
   if (type==1)
    {
      if ((M(EMA1,0)-M(MA[max_trades],0))-(M(EMA1,gap+1)-M(MA[max_trades],gap))>min_close3*Point) 
      {
      if (Usefilewrite)
         {
         if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES)==true)
            {
             handle = FileOpen("SuperScalper.txt",FILE_READ|FILE_WRITE);
             if(handle!=-1)
               {
               FileSeek(handle,0,SEEK_END);
               FileWrite(handle,"close_condition3 type 1 OrderTicket: ",OrderTicket() );
               //FileWrite(handle,"  ");

               FileFlush(handle);
               FileClose(handle);
               }
             }//select 
         }//usefilewrite
      
      return (true); 
      }
    }
   else if (type==2)
    {
      if ((M(MA[max_trades],0)-M(EMA1,0))-(M(MA[max_trades],gap)-M(EMA1,gap+1))>min_close3*Point) 
      {
      if (Usefilewrite)
         {
         if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES)==true)
            {
             handle = FileOpen("SuperScalper.txt",FILE_READ|FILE_WRITE);
             if(handle!=-1)
               {
               FileSeek(handle,0,SEEK_END);
               FileWrite(handle,"close_condition3 type 2 OrderTicket: ",OrderTicket() );
               //FileWrite(handle,"  ");

               FileFlush(handle);
               FileClose(handle);
               }
             }//select 
         }//usefilewrite
      
      return (true); 
      }
    } 
   return (false); 
 }


bool close_condition(int type)
 {
   if (type==1)
    {
      if ((M(MA[max_trades],0)-M(EMA1,0))-(M(MA[max_trades],gap)-M(EMA1,gap+1))>0 && M(EMA1,0)<M(EMA1,1)) 
      {
      if (Usefilewrite)
         {
         if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES)==true)
            {
             handle = FileOpen("SuperScalper.txt",FILE_READ|FILE_WRITE);
             if(handle!=-1)
               {
               FileSeek(handle,0,SEEK_END);
               FileWrite(handle,"close_condition type 1 OrderTicket: ",OrderTicket() );
               //FileWrite(handle,"  ");

               FileFlush(handle);
               FileClose(handle);
               }
             }//select 
         }//usefilewrite
      
      
      return (true);
      } 
    }
   else if (type==2)
    {
      if ((M(EMA1,0)-M(MA[max_trades],0))-(M(EMA1,gap+1)-M(MA[max_trades],gap))>0 && M(EMA1,0)>M(EMA1,1)) 
      {
       if (Usefilewrite)
         {
         if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES)==true)
            {
             handle = FileOpen("SuperScalper.txt",FILE_READ|FILE_WRITE);
             if(handle!=-1)
               {
               FileSeek(handle,0,SEEK_END);
               FileWrite(handle,"close_condition type 2 OrderTicket: ",OrderTicket() );
               //FileWrite(handle,"  ");

               FileFlush(handle);
               FileClose(handle);
               }
             }//select 
         }//usefilewrite
      
      return (true); 
      }
    } 
   return (false); 
 }

void ClosePending()
 { 
     if(OrderType()<=OP_SELL && OrderSymbol()==Symbol() && OrderComment()=="Super Scalper" && OrderMagicNumber()==MAGIC) 
        {
          if(OrderType()==OP_BUY)
             {  
               OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); 
             }  
          else if (OrderType()==OP_SELL)
             {
               OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); 
             }  
        }
 }

double M(int value, int shift) { return (iMA(Symbol(),0,value,0,MODE_EMA,PRICE_CLOSE,shift)); }

int order_condition()
 {
   int total=0,i;
   if (iClose(Symbol(),0,0)>M(EMA1,0) /*&& Close[0]>Close[1]*/) 
    { 
     total++;
     for (i=1; i<=max_trades; i++) if (M(MA[i-1],0)>M(MA[i],0)&& M(MA[i],0)>M(MA[i],1)) OpenOrder(1);
    }    
   if (iClose(Symbol(),0,0)<M(EMA2,0) /*&& Close[0]<Close[1]*/) 
    { 
     total--;
     for (i=1; i<=max_trades; i++) if (M(MA[i-1],0)<M(MA[i],0)&& M(MA[i],0)<M(MA[i],1)) OpenOrder(2);
    }    
   return (total);
 }

int start()
  {
   int i;

   if(!IsTesting() && !IsDemo()) return (0);

   if (PendingOrders()>0) return (0);
   if (condition1(1) && condition2(1) && condition3(1)) OpenOrder(2);//sell
   else if (condition1(2) && condition2(2) && condition3(2)) OpenOrder(1);//buy
  }

void OpenOrder(int type) 
  {
   double r=risk;
   if (Time[0]-last_order_time <minutes_between_trades) return(0);
   if (auto_risk) r=risk+MathAbs((M(MA[1],0)-M(MA[1],1))+(M(MA[2],0)-M(MA[2],1)))*100;
   if (type==1)
     OrderSend(Symbol(),OP_BUY,GetLots(),Ask,2,Ask-Point*StopLoss,Ask+(TakeProfit*(1+r))*Point,"Super Scalper",MAGIC,0,Green); 
   else if (type==2)
     OrderSend(Symbol(),OP_SELL,GetLots(),Bid,2,Bid+Point*StopLoss,Bid-(TakeProfit*(1+r))*Point,"Super Scalper",MAGIC,0,Red); 
   last_order_time=Time[0];  
  }

int PendingOrders()  
  {
   int pending=0;
   for(int i=i;i<=OrdersTotal(); i++)  
      {
         OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         if(OrderSymbol()==Symbol() && OrderComment()=="Super Scalper" && OrderMagicNumber()==MAGIC) 
            { last_profit=OrderProfit(); last_lots=OrderLots(); 
              if (OrderType()==OP_BUY && close_condition(1) && OrderProfit()>=secure_pips*10*last_lots) { ClosePending(); pending=0; break; }
              else if (OrderType()==OP_SELL && close_condition(2) && OrderProfit()>=secure_pips*10*last_lots) { ClosePending(); pending=0; break; }
              if (OrderType()==OP_BUY && condition_close1(1)&&condition_close2(1)&&condition_close3(1)) { ClosePending(); pending=0; break; }
              else if (OrderType()==OP_SELL && condition_close1(2)&&condition_close2(2)&&condition_close3(2)) { ClosePending(); pending=0; break; }
              pending++; 
            }
      }   
   return (pending);
   }

double ProfitMade()
  {
    int i, hstTotal=HistoryTotal();
    for(i=hstTotal-1;i<hstTotal;i++)
        { 
           if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
               {
                Print("Access to history failed with error (",GetLastError(),")");
                break;
               }
           if((OrderType()==OP_BUY || OrderType()==OP_SELL) && OrderMagicNumber()==MAGIC)
                  return (OrderProfit());
        }
   return (0);
  }
  
double GetLots()
  { 
   double prof=ProfitMade();
   double current_lots=Lots;
   double r=risk;
   if (auto_risk) r=risk+MathAbs((M(MA[1],0)-M(MA[1],1))+(M(MA[2],0)-M(MA[2],1)))*100;
   if (last_profit>prof && prof>0) prof=last_profit;
   if (prof>0) 
     { 
      current_lots=last_lots*(1+r);
      RefreshRates();
      if (current_lots*(1000*Ask)>AccountBalance()) 
        { current_lots=AccountBalance()/(1000*Ask); current_lots=MathFloor(current_lots*10)/10; } 
      if (current_lots>=MaxLots) current_lots=MaxLots; 
     } 
   return (current_lots);
  }   