//+------------------------------------------------------------------+
//|                                                      HedgeEA.mq4 |
//|                                 Copyright © 2006, ForexForums.org|
//+------------------------------------------------------------------+




#property copyright "Copyright © 2006, ForexForums.org"
#property link      "http://www.forexforums.org/"

#include <stdlib.mqh>
#include <WinInet.mqh>

//---- input parameters
extern string     eaname           = "[DH_Perky]";                       // Expert Name and first part of comment line
extern int        Magic            = 1001;                          // Magic Number ( 0 - for All positions)
extern bool       Autotrade        = true;                          // Set to false to prevent an entry after an exit
extern bool     HedgeGBPUSD=True;
extern bool     HedgeEURUSD=True;
extern bool     HedgeUSDCHF=True;
extern bool     HedgeUSDJPY=True;



extern string     Lotsizes         = "Set Ratio to 1 to use equal";
extern double     Lots             = 0.1;                           // Lots for first pair if MM is turned off
extern bool       StopManageAcc    = false;                         // Stop of Manage Account switch(Close All Trades)
extern double     MaxLoss          = 0;                             // Maximum total loss in pips or USD 
extern bool       AccountIsMicro   = true;                          // Set true if you use a micro account
extern double     ProfitTarget     = 50;                            // Profit target in pips or USD  
extern bool       UsePips          = true;
extern bool       MoneyManagement  = true;
extern double     Risk             = 20;                            // Risk
extern bool       AutoProfit       = true;                          // When the price of Bolliner pair passes the Upper Bollinger close all trades


string	myUrl        = "http://www.altertrader.com/EURUSDclo.html";
string 	Outputfile       =   "altertrade.txt";
int beginning=1;
string Symbol1, Symbol2;
bool Symbol1isLong, Symbol2isLong;
double Ratio;
bool UseCorrelation;
bool UseBollinger;
bool UseSwap;
double Correlation;
double Bands;
double Bollinger_Symbol;
double valueswap;


string comment = "";
string eBody = "";
string eSubject = "";
string TradeSymbol ="";
int totalPips=0;
double  totalProfits=0;
int ticket1=0
   ,ticket2=0
   ,Symbol1SP
   ,Symbol2SP
   ,Order1=0
   ,Order2=0
   ,c1=0
   ,c2=0
   ,Symbol1OP
   ,Symbol2OP
   ,numords=0
   ;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//---- 
  }


int gogetwebpage (string xURL)
  {
   
   int handle;
   int finalend;
   int end;
   int i,xx;
   string sData;
   string xdate,xprice,xpct;
   double Pricez[];
   int size;

   int endcheck;


	GrabWeb(myUrl, sData);
	finalend=StringLen(sData);
	


	// let's parse it now and write to csv file
	handle = FileOpen(Outputfile,FILE_CSV|FILE_WRITE,",");

   // Handle DATE just once 
   beginning = StringFind(sData,"<BR>",beginning)+4;
   end = StringFind(sData,"<",beginning);
   //Print("1 B="+beginning+" E="+end);
   if ((end-beginning)>0)
     {
      xdate = StringSubstr(sData,beginning,end-beginning);
     }

	FileWrite(handle,"Price","Percent ",xdate);
	
	while (beginning < finalend)
	  {
     
      // Handle PRICE 
      beginning = StringFind(sData,"align=\"center\">",beginning)+15;
      end = StringFind(sData,"<",beginning);
      //Print("1 B="+beginning+" E="+end);
      if ((end-beginning)>0)
        {
         xprice = StringSubstr(sData,beginning,end-beginning);
         //pricez[xx]=xprice;
        }
         
      // Handle PERCENT
      beginning = StringFind(sData,"align=\"center\">",beginning)+15;
      end = StringFind(sData,"<",beginning);
      //Print("2 B="+beginning+" E="+end);
      if ((end-beginning)>0)
        {
         xpct = StringSubstr(sData,beginning,end-beginning);
        }
         

      FileWrite(handle,xprice+","+xpct);
      xx=xx+1;  
      // are we out of data?
      endcheck = StringFind(sData,"<tr>", beginning);
      if(endcheck<0) break;
      
      beginning=endcheck;

	}

	FileClose(handle);
 
  }




// ---- Scan Open Trades
int ScanOpenTrades()
{   
           
   Order1=0;Order2=0;  
   int total = OrdersTotal();
   int numords = 0;
   for(int cnt=0; cnt<=total-1; cnt++)
   {
    OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
    //if(OrderMagicNumber()==Magic)
    if(Magic > 0) if(OrderMagicNumber() == Magic) numords++;
    {
    if(OrderType()==Symbol1OP && OrderMagicNumber() == Magic && OrderSymbol()==Symbol1)Order1  += 1;
    if(OrderType()==Symbol2OP && OrderMagicNumber() == Magic && OrderSymbol()==Symbol2)Order2  += 1;
    }    
   }
}


// Generate Comment on OrderSend 
string GenerateComment(string eaname, int Magic)
{
   return (StringConcatenate(eaname, "-", Magic));
}




void ChartComment()
{
   string sComment   = "";
   string sp         = "****************************\n";
   string NL         = "\n";

   sComment = sp;
   sComment = sComment + "Open Positions      = " + ScanOpenTrades() + NL;
   sComment = sComment + "Current Ratio       = " + DoubleToStr(Ratio,2) + NL;
   
   if (UsePips) {
     sComment = sComment + "Current Profit(Pip)= " + totalPips + NL;
   } else {
     sComment = sComment + "Current Profit(USD) = " + DoubleToStr(totalProfits,2) + NL + NL; 
   }
   if(UseCorrelation){sComment = sComment + "Correlation              = " + DoubleToStr(Correlation,3) + NL + NL;}
   if(UseBollinger){sComment = sComment + "Bollinger Middle           = " + DoubleToStr(Bands,4) + NL;} 
   if(UseBollinger){sComment = sComment + "Bollinger Pair Price       = " + DoubleToStr(MarketInfo(Bollinger_Symbol,MODE_ASK),4) + NL + NL;}
   if (UsePips) {
     sComment = sComment + "SWAP Value (Pip)   = " + DoubleToStr(valueswap,2) + NL;
   } else {
     sComment = sComment + "SWAP Value (USD)   = " + DoubleToStr(valueswap,2) + NL;
   }
   if(UseSwap){
          sComment = sComment + "SWAP Enabled" + NL;
       } else {
          sComment = sComment + "SWAP Disabled" + NL;
       }
   if (UsePips) {
     sComment = NL + sComment + "Net Value (Pip)      = " + DoubleToStr(totalPips+valueswap,2) + NL;
   } else {
     sComment = NL + sComment + "Net Value (USD)      = " + DoubleToStr(totalProfits+valueswap,2) + NL;
   }
   //sComment = sComment + "Account Leverage 1:" + AccountLeverage() + NL;
   sComment = sComment + NL + sp;

   eSubject = "Hedge Profit:" + DoubleToStr(totalProfits+valueswap,2);
   eBody    = "HedgeEA Report" + NL + sComment;
   
   Comment(sComment);
}	  


// added MM v3
double LotSize()
{
     double lotMM = MathCeil(AccountFreeMargin() *  Risk / 1000) / AccountLeverage() / 2;
	  
	  if(AccountIsMicro==false)                          //normal account
	  {
	     if(lotMM < 0.1)                    lotMM = 0.1;
	     if((lotMM >= 0.1) && (lotMM < 0.2)) lotMM = 0.2;
	     if((lotMM >= 0.2) && (lotMM < 0.3)) lotMM = 0.3;
	     if((lotMM >= 0.3) && (lotMM < 0.4)) lotMM = 0.4;
	     if((lotMM >= 0.4) && (lotMM < 1))   lotMM = 0.5;  
	     if(lotMM >= 1.0)                    lotMM = MathCeil(lotMM);
	     if(lotMM >= 100)                    lotMM = 100;
	  }
	  else                                               //micro account
	  {
	     if(lotMM < 0.01)                 lotMM = 0.01; 
	     if((lotMM >= 0.01) && (lotMM < 0.02)) lotMM = 0.02;
	     if((lotMM >= 0.02) && (lotMM < 0.03)) lotMM = 0.03;
	     if((lotMM >= 0.03) && (lotMM < 0.04)) lotMM = 0.04;
	     if((lotMM >= 0.05) && (lotMM < 0.06)) lotMM = 0.05; 
	     if((lotMM >= 0.06) && (lotMM < 0.07)) lotMM = 0.06; 
	     if((lotMM >= 0.07) && (lotMM < 0.08)) lotMM = 0.08; 
	     if((lotMM >= 0.08) && (lotMM < 0.09)) lotMM = 0.09;
	     if((lotMM >= 0.09) && (lotMM < 0.10)) lotMM = 0.1;  
	     if((lotMM >= 0.1) && (lotMM < 0.2)) lotMM = 0.2;
	     if((lotMM >= 0.2) && (lotMM < 0.3)) lotMM = 0.3;
	     if((lotMM >= 0.3) && (lotMM < 0.4)) lotMM = 0.4;
	     if((lotMM >= 0.4) && (lotMM < 1))   lotMM = 0.5; 	   
	     if(lotMM >= 1.0)                  lotMM = MathCeil(lotMM);
	     if(lotMM >= 100)                  lotMM = 100;
	  }
  
//	  Print(lotMM);
	  
	  return (lotMM);
}

 
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
   
{
  
 
   ChartComment();

   

    
    

  
// Prepare Comment line for the trades

     if(Symbol1isLong) {
     comment = Symbol1 + "_L/"; 
     } else {
     comment = Symbol1 + "_S/";
     }
     if(Symbol2isLong) {
     comment = comment + "L_" + Symbol2;
     } else {
     comment = comment +"S_" + Symbol2;
     }
     
     comment = comment + " " + GenerateComment(eaname, Magic);
     
// Micro or Mini

double AccountSize;

     if(AccountIsMicro)  {
     
     AccountSize=2;
     
     } else {
     
     AccountSize=1;
     
     }        

// added MM statement


double OrderLots1,OrderLots2;


     if(MoneyManagement) {
     
     OrderLots1 = NormalizeDouble(LotSize(),AccountSize); //Adjust the lot size
     OrderLots2 = NormalizeDouble(LotSize() * Ratio,AccountSize); // change 2 to 1 for mini account
     
     } else {
     
     OrderLots1 = Lots;
     OrderLots2 = NormalizeDouble(Lots * Ratio,2); //change 2 to 1 for mini account
   
     }
      
     

     

     
     
// Long/Short   open trades//+------------------------------------------------------------------+







     
     
           
      
      OrderSend(Symbol1,OP_BUY,OrderLots1,MarketInfo(Symbol1,MODE_ASK),3,0,0,comment,Magic,0,Blue);
      if (GetLastError()==0) {Order1=1;}
     
   
      OrderSend(Symbol2,OP_SELL,OrderLots2,MarketInfo(Symbol2,MODE_BID),3,0,0,comment,Magic,0,Red);
      if (GetLastError()==0) {Order2=1;}
      
      
      

      

    
      
              
 return(0);
}//int start
//+------------------------------------------------------------------+



