#include <stdlib.mqh> 

#property show_inputs
 
extern double RiskPercent       =           1.5;
extern double StopLoss          =            30;
extern double TakeProfit1       =            10;
extern double TakeProfit2       =           100;


int start()
{ 
   double dLots = 0.01;
   double dPipValue;
   
   
   if(Digits==5||Digits==3)
      dPipValue=Point*10;
   else
      dPipValue=Point;
   
   
   dLots = HTCalculateLotSize(RiskPercent, Ask, Bid - (StopLoss * dPipValue));
   
   if(dLots > 0.0)
   {
      SetGlobalVariable("HT_LotSize",     dLots);
      SetGlobalVariable("HT_StopLoss",    StopLoss);
      SetGlobalVariable("HT_TakeProfit1", TakeProfit1);
      SetGlobalVariable("HT_TakeProfit2", TakeProfit2);
   }
   
   return(0);
}

void SetGlobalVariable(string sName, double dVal)
{
   int nErrCode;
   
   if( GlobalVariableSet(sName, dVal) == 0 )
   {
      nErrCode = GetLastError();
      
      Alert("Error setting variable ", sName, ". (", nErrCode, ") ", ErrorDescription(nErrCode), ".");
   }
   else
      Print(sName, " set to: ", dVal);
}

//+------------------------------------------------------------------+
//| CalculateLotSize                                                 |
//| Calculates the Lot Size based on the specified Risk percentage   |
//| and the amount of the Stop Loss Pips                             |
//+------------------------------------------------------------------+
double HTCalculateLotSize(double dRisk, double dPrice, double dSLPrice)
{
   double dRecommendedLotSize, dLotSize, dAccountSize, dMinLot, dMaxLot;
   
   dAccountSize = AccountFreeMargin();
         
   dRecommendedLotSize = RiskToLot(dRisk, dPrice, dSLPrice);
   
   dMinLot = MarketInfo(OrderSymbol(), MODE_MINLOT);
   
   dMaxLot = MarketInfo(OrderSymbol(), MODE_MAXLOT);
   
   if (dRecommendedLotSize < dMinLot)
      dLotSize = dMinLot;      
   else if(dRecommendedLotSize > dMaxLot)
      dLotSize = dMaxLot;
   else
      dLotSize = NormalizeDouble(dRecommendedLotSize, 2);
      
   Print("Calculating LotSize:   AccountSize=", dAccountSize, "  AccountFreeMargin=", AccountFreeMargin(), "  AccountBalance=", AccountBalance(), "  Aceptable Risk ", dRisk, "% =(", (dRisk * dAccountSize / 100), ") SLPips=", MathAbs(dPrice-dSLPrice), " Recommended Lot Size=", DoubleToStr(dRecommendedLotSize, Digits), "  Actual Lot Size=", DoubleToStr(dLotSize, 2));
   
   return (dLotSize);
}

double RiskToLot(double risk, double price, double sl)
{
	if(price==sl) return(0);
	double lot = 0;
	double delta = MathAbs(price-sl);
	
	risk = risk/100*AccountBalance();
	
	lot = risk/delta/MarketInfo(Symbol(),MODE_LOTSIZE);
	string baseSymbol = StringSubstr(Symbol(),3,3);
	string faceSymbol = StringSubstr(Symbol(),0,3);
	string suffix = "";
	if(StringLen(Symbol())>6) suffix = StringSubstr(Symbol(),6,StringLen(Symbol())-6);
	if(baseSymbol=="USD") {
		return(lot);
	}

	if(faceSymbol=="USD") {
		//Print(Symbol());
		return(lot*sl);
	}
	
	//USDXXX
	string symbol = "USD"+baseSymbol+suffix;
	if(MarketInfo(symbol,MODE_ASK)>0) return(lot*MarketInfo(symbol,MODE_ASK));
	symbol = baseSymbol+"USD"+suffix;
	if(MarketInfo(symbol,MODE_BID)>0) {
		//Print("Symbol XXXUSD: ",symbol);
		return(lot/MarketInfo(symbol,MODE_BID));	
	}
	return(lot);
}