//+------------------------------------------------------------------+
//|                                              Bob'sMarketInfo.mq4 |
//|                      Copyright 2013, Deltabron - Paul Geirnaerdt |
//|                                          http://www.deltabron.nl |
//+------------------------------------------------------------------+
// 
#property copyright "Copyright 2013, Deltabron - Paul Geirnaerdt"
#property link      "http://www.deltabron.nl"

#property indicator_chart_window

#define version            "v3.1"

#import "user32.dll"
int GetWindowRect(int hWnd, int &rect[4]); 
#import
//+------------------------------------------------------------------+
//| Release Notes                                                    |
//+------------------------------------------------------------------+
// v1.0.0, 2/6/13
// * Initial release

// v1.0.1 , 15 nov 203 (Skyline) : Added Swap long/short 

// external variables
extern string  ppf               = "----Pip factors----";
extern string  pipFactor100      = "JPY,XAG,SILVER,BRENT,WTI";
extern string  pipFactor10       = "XAU,GOLD,SP500";
extern string  pipFactor1        = "UK100,WS30,DAX30,NAS100,CAC400";
extern string  fon               = "----Font inputs----";
extern string  fontName          = "Tahoma";
extern int     fontSize          = 10;
extern int     fontSizeLabel     = 8;
extern string  col               = "----Colors----";
extern color   colorPrice        = Yellow;
extern color   colorLabel        = WhiteSmoke;
extern color   colorTitle        = Blue;


// global variables
string         objectPrefix     = "BMI_";
int            pipFactor        = 10000;

// display variables
int            sizeX            = 800;
int            sizeY            = 600;
int            windowDimension[4];
int            horOffsetCorner0 = -170;
int            horOffsetCorner1 = 8;
int            verOffset        = 6;
int            verShift         = 24;
int            verOffsetLabel   = 10;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
  pipFactor = getPipFactor();

  return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
  for ( int i = ObjectsTotal() - 1; i >= 0; i-- )
  {
    if ( StringSubstrOld( ObjectName( i ), 0, StringLen( objectPrefix ) ) == objectPrefix ) ObjectDelete( ObjectName ( i ) );
  }
  return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
  if ( IsDllsAllowed() ) 
  {
    GetWindowRect( WindowHandle( Symbol(), Period() ), windowDimension );
    sizeX = windowDimension[2] - windowDimension[0];
    sizeY = windowDimension[3] - windowDimension[1];
  }
  int verCorr = 0;
  //
  // Price
  //
  string objectName = objectPrefix + "_obj_" + "price" + "_title";
  if ( ObjectFind ( objectName ) == -1 )
  {
     if ( ObjectCreate ( objectName, OBJ_LABEL, 0, 0, 0 ) )
     {
        ObjectSet ( objectName, OBJPROP_CORNER, 0 );
        ObjectSetText ( objectName, "ACTION", fontSizeLabel, fontName, colorTitle );
     }
  }
  ObjectSet ( objectName, OBJPROP_XDISTANCE, sizeX + horOffsetCorner0 );
  ObjectSet ( objectName, OBJPROP_YDISTANCE, verOffset + ( verShift * 0 ) + verOffsetLabel + verCorr );
  verCorr = -6;
  objectName = objectPrefix + "_obj_" + "price" + "_lbl";
  if ( ObjectFind ( objectName ) == -1 )
  {
     if ( ObjectCreate ( objectName, OBJ_LABEL, 0, 0, 0 ) )
     {
        ObjectSet ( objectName, OBJPROP_CORNER, 0 );
        ObjectSetText ( objectName, "price: ", fontSizeLabel, fontName, colorLabel );
     }
  }
  ObjectSet ( objectName, OBJPROP_XDISTANCE, sizeX + horOffsetCorner0 );
  ObjectSet ( objectName, OBJPROP_YDISTANCE, verOffset + ( verShift * 1 ) + verOffsetLabel + verCorr );
  objectName = objectPrefix + "_obj_" + "price";
  if ( ObjectFind ( objectName ) == -1 )
  {
     if ( ObjectCreate ( objectName, OBJ_LABEL, 0, 0, 0 ) )
     {
        ObjectSet ( objectName, OBJPROP_CORNER, 1 );
        ObjectSet ( objectName, OBJPROP_XDISTANCE, horOffsetCorner1 );
        ObjectSet ( objectName, OBJPROP_YDISTANCE, verOffset + ( verShift * 1 ) + verCorr );
     }
  }

  // Show object
  string showText = DoubleToStr( Bid, 4 );
  ObjectSetText ( objectName, showText, fontSize, fontName, colorPrice );

  //
  // Distance
  //
  objectName = objectPrefix + "_obj_" + "distance" + "_title";
  if ( ObjectFind ( objectName ) == -1 )
  {
     if ( ObjectCreate ( objectName, OBJ_LABEL, 0, 0, 0 ) )
     {
        ObjectSet ( objectName, OBJPROP_CORNER, 0 );
        ObjectSetText ( objectName, "240MA 4H", fontSizeLabel, fontName, colorTitle );
     }
  }
  ObjectSet ( objectName, OBJPROP_XDISTANCE, sizeX + horOffsetCorner0 );
  ObjectSet ( objectName, OBJPROP_YDISTANCE, verOffset + ( verShift * 2 ) + verOffsetLabel + verCorr );
  verCorr -= 6;
  objectName = objectPrefix + "_obj_" + "distance" + "_lbl";
  if ( ObjectFind ( objectName ) == -1 )
  {
     if ( ObjectCreate ( objectName, OBJ_LABEL, 0, 0, 0 ) )
     {
        ObjectSet ( objectName, OBJPROP_CORNER, 0 );
        ObjectSetText ( objectName, "pips distance: ", fontSizeLabel, fontName, colorLabel );
     }
  }
  ObjectSet ( objectName, OBJPROP_XDISTANCE, sizeX + horOffsetCorner0 );
  ObjectSet ( objectName, OBJPROP_YDISTANCE, verOffset + ( verShift * 3 ) + verOffsetLabel + verCorr );
  objectName = objectPrefix + "_obj_" + "distance";
  if ( ObjectFind ( objectName ) == -1 )
  {
     if ( ObjectCreate ( objectName, OBJ_LABEL, 0, 0, 0 ) )
     {
        ObjectSet ( objectName, OBJPROP_CORNER, 1 );
        ObjectSet ( objectName, OBJPROP_XDISTANCE, horOffsetCorner1 + 0 );
        ObjectSet ( objectName, OBJPROP_YDISTANCE, verOffset + ( verShift * 3 ) + verCorr );
     }
  }

  // Show object
  double ma240 = iMA( NULL, PERIOD_H4, 240, 0, MODE_LWMA, PRICE_OPEN, 0 );
  double distance = ( Bid - ma240 ) * pipFactor;
  showText = DoubleToStr( distance, 1 );
  ObjectSetText ( objectName, showText, fontSize, fontName, colorPrice );

  //
  // Exposure
  //
  objectName = objectPrefix + "_obj_" + "exposure" + "_title";
  if ( ObjectFind ( objectName ) == -1 )
  {
    // if ( ObjectCreate ( objectName, OBJ_LABEL, 0, 0, 0 ) )
    // {
        ObjectSet ( objectName, OBJPROP_CORNER, 0 );
     //   ObjectSetText ( objectName, "Cents/1000 Dollars", fontSizeLabel, fontName, colorTitle );
    // }
  }
  ObjectSet ( objectName, OBJPROP_XDISTANCE, sizeX + horOffsetCorner0 );
  ObjectSet ( objectName, OBJPROP_YDISTANCE, verOffset + ( verShift * 4 ) + verOffsetLabel + verCorr );
  verCorr -= 6;
  objectName = objectPrefix + "_obj_" + "exposure0.5" + "_lbl";
 /*
 // if ( ObjectFind ( objectName ) == -1 )
  {
    // if ( ObjectCreate ( objectName, OBJ_LABEL, 0, 0, 0 ) )
     {
     //   ObjectSet ( objectName, OBJPROP_CORNER, 0 );
     //   ObjectSetText ( objectName, "exposure 0.5%: ", fontSizeLabel, fontName, colorLabel );
     }
  }
  */
 // ObjectSet ( objectName, OBJPROP_XDISTANCE, sizeX + horOffsetCorner0 );
//  ObjectSet ( objectName, OBJPROP_YDISTANCE, verOffset + ( verShift * 5 ) + verOffsetLabel + verCorr );
  objectName = objectPrefix + "_obj_" + "exposure0.5";
 /*
 // if ( ObjectFind ( objectName ) == -1 )
  {
  //   if ( ObjectCreate ( objectName, OBJ_LABEL, 0, 0, 0 ) )
     {
  //      ObjectSet ( objectName, OBJPROP_CORNER, 1 );
  //      ObjectSet ( objectName, OBJPROP_XDISTANCE, horOffsetCorner1 + 0 );
  //      ObjectSet ( objectName, OBJPROP_YDISTANCE, verOffset + ( verShift * 5 ) + verCorr );
     }
  }
  */
   // Show object
	double lotStep = 0;
	double exposure = 0;

   double mTickValue = MarketInfo(Symbol(), MODE_TICKVALUE);
   double mLotStep = MarketInfo(Symbol(), MODE_LOTSTEP);
   if((mLotStep == 0) || (mTickValue == 0))
   {
      ObjectSetText(objectName, "...", fontSize, fontName, colorPrice);
   }
   else
   {
      lotStep = mLotStep;
      exposure = distance * mTickValue;
      
      double allowedExposure = AccountBalance() * 0.22 / 100;
      int totalSteps = (int)MathRound(((allowedExposure / exposure ) / lotStep));
      double lotSize = totalSteps * lotStep;
      showText = DoubleToStr(lotSize, 2);
      ObjectSetText(objectName, showText, fontSize, fontName, colorPrice);
   }
   objectName = objectPrefix + "_obj_" + "exposure1.0" + "_lbl";
  /*
 // if ( ObjectFind ( objectName ) == -1 )
  {
   //  if ( ObjectCreate ( objectName, OBJ_LABEL, 0, 0, 0 ) )
     {
  //      ObjectSet ( objectName, OBJPROP_CORNER, 0 );
    //    ObjectSetText ( objectName, "exposure 1.0%: ", fontSizeLabel, fontName, colorLabel );
     }
  }
  //ObjectSet ( objectName, OBJPROP_XDISTANCE, sizeX + horOffsetCorner0 );
 // ObjectSet ( objectName, OBJPROP_YDISTANCE, verOffset + ( verShift * 6 ) + verOffsetLabel + verCorr );
 // objectName = objectPrefix + "_obj_" + "exposure1.0";
  //if ( ObjectFind ( objectName ) == -1 )
  {
   //  if ( ObjectCreate ( objectName, OBJ_LABEL, 0, 0, 0 ) )
     {
    //    ObjectSet ( objectName, OBJPROP_CORNER, 1 );
     //   ObjectSet ( objectName, OBJPROP_XDISTANCE, horOffsetCorner1 + 0 );
   //     ObjectSet ( objectName, OBJPROP_YDISTANCE, verOffset + ( verShift * 6 ) + verCorr );
     }
  }
*/
   // Show object
   if((mLotStep > 0) && (mTickValue > 0))
   {
      allowedExposure = AccountBalance() * 0.44 / 100;
      totalSteps = (int)MathRound(((allowedExposure / exposure) / lotStep));
      lotSize = totalSteps * lotStep;
      showText = DoubleToStr(lotSize, 2);
      // ObjectSetText ( objectName, showText, fontSize, fontName, colorPrice );
   }
 // objectName = objectPrefix + "_obj_" + "exposure2.0" + "_lbl";
 // if ( ObjectFind ( objectName ) == -1 )
/*  {
   //  if ( ObjectCreate ( objectName, OBJ_LABEL, 0, 0, 0 ) )
     {
   //     ObjectSet ( objectName, OBJPROP_CORNER, 0 );
    //    ObjectSetText ( objectName, "exposure 2.0%: ", fontSizeLabel, fontName, colorLabel );
     }
  }
  //ObjectSet ( objectName, OBJPROP_XDISTANCE, sizeX + horOffsetCorner0 );
//  ObjectSet ( objectName, OBJPROP_YDISTANCE, verOffset + ( verShift * 7 ) + verOffsetLabel + verCorr );
 // objectName = objectPrefix + "_obj_" + "exposure2.0";
 // if ( ObjectFind ( objectName ) == -1 )
  {
 //    if ( ObjectCreate ( objectName, OBJ_LABEL, 0, 0, 0 ) )
     {
   //     ObjectSet ( objectName, OBJPROP_CORNER, 1 );
   //     ObjectSet ( objectName, OBJPROP_XDISTANCE, horOffsetCorner1 + 0 );
    //    ObjectSet ( objectName, OBJPROP_YDISTANCE, verOffset + ( verShift * 7 ) + verCorr );
     }
  }

  // Show object
 // allowedExposure = AccountBalance() * 0.88 / 100;
 // totalSteps = MathRound( ( ( allowedExposure / exposure ) / lotStep ) );
 // lotSize = totalSteps * lotStep;
 // showText = DoubleToStr( lotSize, 2 );
 // ObjectSetText ( objectName, showText, fontSize, fontName, colorPrice );
*/
  /*
  objectName = objectPrefix + "_obj_" + "size";
  if ( ObjectFind ( objectName ) == -1 )
  {
     if ( ObjectCreate ( objectName, OBJ_LABEL, 0, 0, 0 ) )
     {
        ObjectSet ( objectName, OBJPROP_CORNER, 0 );
        ObjectSet ( objectName, OBJPROP_XDISTANCE, sizeX + horOffsetCorner0 + 0 );
        ObjectSet ( objectName, OBJPROP_YDISTANCE, verOffset + 120 );
     }
  }

  // Show object
  showText = "X: " + sizeX + ", Y: " + sizeY;
  ObjectSetText ( objectName, showText, fontSize, fontName, colorPrice );
  */
  
  //
  // Swap
  //
  objectName = objectPrefix + "_obj_" + "swap" + "_title";
  if ( ObjectFind ( objectName ) == -1 )
  {
     if ( ObjectCreate ( objectName, OBJ_LABEL, 0, 0, 0 ) )
     {
        string swaptype = ""; // 0 - in points; 1 - in the symbol base currency; 2 - by interest; 3 - in the margin currency.
        int st = (int)MarketInfo(Symbol(),MODE_SWAPTYPE);
        switch(st)
        {
          case 0 : swaptype = " (in points)";
          break;
          case 1 : swaptype = " (in the symbol base currency)";
          break;
          case 2 : swaptype = " (by interest)";
          break;          
          case 3 : swaptype = " (CURRENT)";//margin currency
          break;          
        } 
        ObjectSet ( objectName, OBJPROP_CORNER, 0 );
        ObjectSetText ( objectName, "SWAP"+swaptype, fontSizeLabel, fontName, colorTitle );
     }
  }
  ObjectSet ( objectName, OBJPROP_XDISTANCE, sizeX + horOffsetCorner0 );
  ObjectSet ( objectName, OBJPROP_YDISTANCE, verOffset + ( verShift * 4.5 ) + verOffsetLabel + verCorr );
  verCorr = -6;
  objectName = objectPrefix + "_obj_" + "swaplong" + "_lbl";
  if ( ObjectFind ( objectName ) == -1 )
  {
     if ( ObjectCreate ( objectName, OBJ_LABEL, 0, 0, 0 ) )
     {
        ObjectSet ( objectName, OBJPROP_CORNER, 0 );
        ObjectSetText ( objectName, "swap long: ", fontSizeLabel, fontName, colorLabel );
     }
  }
  ObjectSet ( objectName, OBJPROP_XDISTANCE, sizeX + horOffsetCorner0 );
  ObjectSet ( objectName, OBJPROP_YDISTANCE, verOffset + ( verShift * 5 ) + verOffsetLabel + verCorr );
  objectName = objectPrefix + "_obj_" + "swaplong";
  if ( ObjectFind ( objectName ) == -1 )
  {
     if ( ObjectCreate ( objectName, OBJ_LABEL, 0, 0, 0 ) )
     {
        ObjectSet ( objectName, OBJPROP_CORNER, 1 );
        ObjectSet ( objectName, OBJPROP_XDISTANCE, horOffsetCorner1 );
        ObjectSet ( objectName, OBJPROP_YDISTANCE, verOffset + ( verShift * 5 ) + verCorr );
     }
  }

  // Show object
  showText = DoubleToStr( MarketInfo(Symbol(),MODE_SWAPLONG), 2 );
  ObjectSetText ( objectName, showText, fontSize, fontName, colorPrice );

  verCorr = -6;
  objectName = objectPrefix + "_obj_" + "swapshort" + "_lbl";
  if ( ObjectFind ( objectName ) == -1 )
  {
     if ( ObjectCreate ( objectName, OBJ_LABEL, 0, 0, 0 ) )
     {
        ObjectSet ( objectName, OBJPROP_CORNER, 0 );
        ObjectSetText ( objectName, "swap short: ", fontSizeLabel, fontName, colorLabel );
     }
  }
  ObjectSet ( objectName, OBJPROP_XDISTANCE, sizeX + horOffsetCorner0 );
  ObjectSet ( objectName, OBJPROP_YDISTANCE, verOffset + ( verShift * 6 ) + verOffsetLabel + verCorr );
  objectName = objectPrefix + "_obj_" + "swapshort";
  if ( ObjectFind ( objectName ) == -1 )
  {
     if ( ObjectCreate ( objectName, OBJ_LABEL, 0, 0, 0 ) )
     {
        ObjectSet ( objectName, OBJPROP_CORNER, 1 );
        ObjectSet ( objectName, OBJPROP_XDISTANCE, horOffsetCorner1 );
        ObjectSet ( objectName, OBJPROP_YDISTANCE, verOffset + ( verShift * 6 ) + verCorr );
     }
  }

  // Show object
  showText = DoubleToStr( MarketInfo(Symbol(),MODE_SWAPSHORT), 2 );
  ObjectSetText ( objectName, showText, fontSize, fontName, colorPrice );

  return(0);
 
}

//+------------------------------------------------------------------+
//| initStringArrayFromInput()                                       |
//+------------------------------------------------------------------+
void initStringArrayFromInput( string& strings[], string sInput )
{
   int i;
   ArrayResize( strings, 0 );

   sInput = StringTrimLeft(sInput);
   sInput = StringTrimRight(sInput);

   if (StringSubstrOld(sInput, StringLen(sInput) - 1) != ",")
   {
      sInput = StringConcatenate(sInput, ",");   
   }   

   i = StringFind(sInput, ","); 
   while (i != -1)
   {
      int size = ArraySize(strings);
      ArrayResize(strings, size + 1);
      strings[size] = StringSubstrOld(sInput, 0, i);
      sInput = StringSubstrOld(sInput, i + 1);
      i = StringFind(sInput, ","); 
   }
}
//+------------------------------------------------------------------+
//| getPipFactor()                                                   |
//+------------------------------------------------------------------+
int getPipFactor()
{
  string factor100[];
  initStringArrayFromInput( factor100, pipFactor100 );
  string factor10[];
  initStringArrayFromInput( factor10, pipFactor10 );
  string factor1[];
  initStringArrayFromInput( factor1, pipFactor1 );
   
  int factor = 10000;       // correct factor for most pairs
  for ( int j = 0; j < ArraySize( factor100 ); j++ )
  {
     if ( StringFind( Symbol(), factor100[j] ) != -1 ) factor = 100;
  }   
  for ( j = 0; j < ArraySize( factor10 ); j++ )
  {
     if ( StringFind( Symbol(), factor10[j] ) != -1 ) factor = 10;
  }   
  for ( j = 0; j < ArraySize( factor1 ); j++ )
  {
     if ( StringFind( Symbol(), factor1[j] ) != -1 ) factor = 1;
  }
  
  return ( factor );
}
//+------------------------------------------------------------------+
string StringSubstrOld(string x,int a,int b=-1)
  {
   if(a<0) a=0; // Stop odd behaviour
   if(b<=0) b=-1; // new MQL4 EOL flag
   return StringSubstr(x,a,b);
  }
//+------------------------------------------------------------------+
