//=================================================================================================
//
//
//
//=================================================================================================
#property copyright "DNK"
#property link      "slysov@yandex.ru"
#include <WinUser32.mqh>


#property indicator_chart_window
#property indicator_buffers 5
#property indicator_color1 Lime
#property indicator_color2 DodgerBlue
#property indicator_color3 Red
#property indicator_color4 Brown
#property indicator_color5 Lime

//+-----------------------------------------------------------------------------------------------+
//
//+-----------------------------------------------------------------------------------------------+
extern    int basket       = 8;
extern string Currency     = "##RING##";
extern   bool UseWeighting = false;
extern    int MaxBars      = 1000;
extern    int TF           = 0;
extern string Symbols      = "";
extern    int Yoffset      = 0;
  bool Visual = false;
string Pair_suffix = "";
string shortname;

//---- buffers
double BUF_0[];
double BUF_1[];
double BUF_2[];
double BUF_3[];
double BUF_4[];
//---
int length;
string Pair[14];
   int StartBar;

static int ExtHandle=-1;
static int tf = 0;
//+-----------------------------------------------------------------------------------------------+
//
//+-----------------------------------------------------------------------------------------------+
int init() 
{
   shortname = "RING Price v6 (" + basket + ")";
  IndicatorShortName(shortname);

   if (Visual) 
     SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
   else        
     SetIndexStyle(0,DRAW_NONE,STYLE_DOT);
   SetIndexLabel (0, "BuyAll");
   SetIndexBuffer(0,BUF_0);
   
   if (Visual) 
     SetIndexStyle(1,DRAW_LINE,STYLE_DOT);
   else        
     SetIndexStyle(1,DRAW_NONE,STYLE_DOT);
   SetIndexLabel (1, "BuyAll_H");
   SetIndexBuffer(1,BUF_1);
   
   if (Visual) 
     SetIndexStyle(2,DRAW_LINE,STYLE_DOT);
   else        
     SetIndexStyle(2,DRAW_NONE,STYLE_DOT);
   SetIndexLabel (2, "BuyAll_L");
   SetIndexBuffer(2,BUF_2);

   SetIndexStyle (3,DRAW_NONE,STYLE_DOT);
   SetIndexBuffer(3,BUF_3);

   SetIndexStyle (4,DRAW_NONE,STYLE_DOT);
   SetIndexBuffer(4,BUF_4);
//---
   int pos=0;
   if (basket==0 && StringLen(Symbols)>0) 
   {
     while (true) 
     {
       Pair[basket]=StringSubstr(Symbols,pos,6);
       pos+=7;
       basket++;
       if (pos>=StringLen(Symbols)) break;
     }
   } 
   else 
   {
 
   //basket ---------------------------------------------------------
  
     switch (basket) 
     {
       case 1: Pair[0] = "GBPJPY"+Pair_suffix; Pair[7]  = "";
               Pair[1] = "";                   Pair[8]  = "";
               Pair[2] = "";                   Pair[9]  = "";
               Pair[3] = "";                   Pair[10] = "";
               Pair[4] = "";                   Pair[11] = "";
               Pair[5] = "";                   Pair[12] = "";
               Pair[6] = "";                   Pair[13] = "";
            break;
     
       case 2: Pair[0] = "GBPUSD"+Pair_suffix; Pair[7]  = "";
               Pair[1] = "EURJPY"+Pair_suffix; Pair[8]  = "";
               Pair[2] = "";                   Pair[9]  = "";
               Pair[3] = "";                   Pair[10] = "";
               Pair[4] = "";                   Pair[11] = "";
               Pair[5] = "";                   Pair[12] = "";
               Pair[6] = "";                   Pair[13] = "";
            break;
     
       case 4: Pair[0] = "GBPUSD"+Pair_suffix; Pair[7]  = "";
               Pair[1] = "EURJPY"+Pair_suffix; Pair[8]  = "";
               Pair[2] = "EURUSD"+Pair_suffix; Pair[9]  = "";
               Pair[3] = "GBPJPY"+Pair_suffix; Pair[10] = "";
               Pair[4] = "";                   Pair[11] = "";
               Pair[5] = "";                   Pair[12] = "";
               Pair[6] = "";                   Pair[13] = "";
            break;
     
       case 8: Pair[0] = "GBPUSD"+Pair_suffix; Pair[7]  = "AUDJPY"+Pair_suffix;
               Pair[1] = "EURJPY"+Pair_suffix; Pair[8]  = "";
               Pair[2] = "AUDUSD"+Pair_suffix; Pair[9]  = "";
               Pair[3] = "NZDJPY"+Pair_suffix; Pair[10] = "";
               Pair[4] = "EURUSD"+Pair_suffix; Pair[11] = "";
               Pair[5] = "GBPJPY"+Pair_suffix; Pair[12] = "";
               Pair[6] = "NZDUSD"+Pair_suffix; Pair[13] = "";
            break;
     
       case 14:Pair[0] = "GBPUSD"+Pair_suffix; Pair[7]  = "EURUSD"+Pair_suffix;
               Pair[1] = "EURGBP"+Pair_suffix; Pair[8]  = "USDJPY"+Pair_suffix;
               Pair[2] = "GBPJPY"+Pair_suffix; Pair[9]  = "AUDUSD"+Pair_suffix;
               Pair[3] = "USDCHF"+Pair_suffix; Pair[10] = "NZDJPY"+Pair_suffix;
               Pair[4] = "NZDUSD"+Pair_suffix; Pair[11] = "GBPCHF"+Pair_suffix;
               Pair[5] = "AUDJPY"+Pair_suffix; Pair[12] = "CHFJPY"+Pair_suffix;
               Pair[6] = "EURJPY"+Pair_suffix; Pair[13] = "EURCHF"+Pair_suffix;
            break;
       default: break;
     }
   }
   length = basket;

   if (TF==0) 
     tf = Period();
   else       
     tf = TF;
   
//Create initial file
   int version = 400;
   string c_copyright;
   string c_symbol = Currency;
   int i_period = tf;
   int i_digits = MarketInfo (Symbol(),MODE_DIGITS);
   int i_unused[13];

//DNK - indentify
   i_unused[0] = 3; i_unused[1] = 3; i_unused[2] = 3;
//----  
   ExtHandle = FileOpenHistory(c_symbol+i_period+".hst", FILE_BIN|FILE_WRITE);
   if (ExtHandle<0) return(-1);
//---- write history file header
       c_copyright="(C)opyright 2010, DNK";
      FileWriteInteger(ExtHandle, version, LONG_VALUE);
      FileWriteString (ExtHandle, c_copyright, 64);
      FileWriteString (ExtHandle, c_symbol, 12);
      FileWriteInteger(ExtHandle, i_period, LONG_VALUE);
      FileWriteInteger(ExtHandle, i_digits, LONG_VALUE);
      FileWriteInteger(ExtHandle, 0, LONG_VALUE);       //timesign
      FileWriteInteger(ExtHandle, 0, LONG_VALUE);       //last_sync
        FileWriteArray(ExtHandle, i_unused, 0, 13);
             FileFlush(ExtHandle);

//Init message ObjectCreate

   if (ObjectFind (Currency+shortname+tf)==-1) 
   {
      ObjectCreate(Currency+shortname+tf,OBJ_LABEL,0,0,0);
        ObjectSet (Currency+shortname+tf,OBJPROP_CORNER,3);
        ObjectSet (Currency+shortname+tf,OBJPROP_XDISTANCE, 20);
        ObjectSet (Currency+shortname+tf,OBJPROP_YDISTANCE, Yoffset+20);
   } 
   return(0);
}
//---- 
int deinit() 
{
   FileFlush(ExtHandle);
   FileClose(ExtHandle);
   ObjectDelete (Currency+shortname+tf);
   return(0);
}
//+-----------------------------------------------------------------------------------------------+
//
//+-----------------------------------------------------------------------------------------------+
static datetime lastbar   = 0;
static      int last_fpos = 0;
static      int hwnd;
static datetime last_time = 0;

int start() 
{
   if (ExtHandle==-1) Print ("Error");
  
   int counted_bars=IndicatorCounted();
   //---- check for possible errors
   if (counted_bars<0) return(-1);      
   
   StartBar = MaxBars;
   
   if (counted_bars>0) StartBar = Bars-counted_bars;
   
   //---- main loop
   for (int i=StartBar; i>=0; i--) 
   {        
     if (lastbar<iTime(Symbol(),tf,i)) 
     {
         last_fpos = FileTell(ExtHandle);
         lastbar   = iTime(Symbol(),tf,i);
     } 
     else 
       FileSeek(ExtHandle,last_fpos,SEEK_SET);
     
     datetime timebar = iTime(Symbol(),tf,i);
     
     if (UseWeighting) 
     { 
       double weight_calc = 0;
       for (int j=0; j<length; j++)
       { 
         weight_calc += GetTickValue (Pair[j],i);
       }
     } 
     
     double sumP_0=0, sumP_1=0, sumP_2=0, sumP_3=0, sumP_4=0; //1 = High, 2 = Low, 3 = Open, 4 = Volume
     
     for (j=0; j<length; j++) 
     {
       int shift = iBarShift(Pair[j],tf,timebar);
       if (iTime(Pair[j],tf,i)>iTime(Pair[j],tf,shift)) 
       { 
         shift++; 
       } 
       
       if (StringSubstr( Pair[j],3,3)=="JPY") 
         double kK = 0.01;
       else                                          
         kK = 1;
       
       if (UseWeighting) 
       { 
         double weight = GetTickValue(Pair[j],shift)/weight_calc; 
         int Len = 1; 
       }
       else              
       {        
          weight = 1; Len = length; }
          sumP_0+=  iClose(Pair[j],tf,shift)*kK;
          sumP_1+=   iHigh(Pair[j],tf,shift)*kK;
          sumP_2+=    iLow(Pair[j],tf,shift)*kK;
          sumP_3+=   iOpen(Pair[j],tf,shift)*kK;
          sumP_4+= iVolume(Pair[j],tf,shift)*kK;
     }
     BUF_0[i] = sumP_0*weight/Len; 
     BUF_1[i] = sumP_1*weight/Len; 
     BUF_2[i] = sumP_2*weight/Len; 
     BUF_3[i] = sumP_3*weight/Len; 
     BUF_4[i] = sumP_4*weight/Len;

     FileWriteInteger(ExtHandle, lastbar, LONG_VALUE);
     FileWriteDouble(ExtHandle, BUF_3[i], DOUBLE_VALUE);
     FileWriteDouble(ExtHandle, BUF_2[i], DOUBLE_VALUE);
     FileWriteDouble(ExtHandle, BUF_1[i], DOUBLE_VALUE);
     FileWriteDouble(ExtHandle, BUF_0[i], DOUBLE_VALUE);
     FileWriteDouble(ExtHandle, BUF_4[i], DOUBLE_VALUE);
           FileFlush(ExtHandle);
   }
   
   int cur_time = LocalTime();
   if (hwnd==0) 
   { 
     hwnd = WindowHandle(Currency,tf);
     if (hwnd!=0) Print("Chart window detected");
   }
   
   if (hwnd!=0 && cur_time-last_time>=2) 
   {         //---- refresh window not frequently than 1 time in 2 seconds
      PostMessageA(hwnd,WM_COMMAND,33324,0);
      last_time = cur_time;
   }

   if (ObjectFind (Currency+shortname+tf)==-1) 
   {
      ObjectCreate(Currency+shortname+tf,OBJ_LABEL,0,0,0);
        ObjectSet (Currency+shortname+tf,OBJPROP_CORNER,3);
        ObjectSet (Currency+shortname+tf,OBJPROP_XDISTANCE, 20);
        ObjectSet (Currency+shortname+tf,OBJPROP_YDISTANCE, Yoffset+20);
   }
   ObjectSetText (Currency+shortname+tf,Currency+","+tf+"-RUNNING",8,"",Green);
   return(0);
}
//+-----------------------------------------------------------------------------------------------+
//
//+-----------------------------------------------------------------------------------------------+
double GetTickValue(string Symb, int offset) 
{
   string Str, ProfitCurrency, SymbolPrefix;
   double Res, PriceExchage;
   string BaseCurrency   = AccountCurrency();
            ProfitCurrency = StringSubstr(Symb,3,3);
            SymbolPrefix   = StringSubstr(Symb,6);
  if (ProfitCurrency==BaseCurrency)   
    Res = MarketInfo(Symb, MODE_LOTSIZE)*MarketInfo(Symb,MODE_TICKSIZE);
  else 
  {                              
    Str = BaseCurrency + ProfitCurrency + SymbolPrefix;
    if (RealSymbol(Str)) 
    { 
      double spread = MarketInfo (Str, MODE_SPREAD);
      PriceExchage  = iClose(Str,tf,offset)+spread*MarketInfo(Str,MODE_POINT);
      Res           = MarketInfo(Symb,MODE_LOTSIZE)*MarketInfo(Symb, MODE_TICKSIZE)/PriceExchage;    
    } 
    else 
    { 
      Str          = ProfitCurrency + BaseCurrency + SymbolPrefix;
      spread       = MarketInfo (Str, MODE_SPREAD);
      PriceExchage = iClose(Str,tf,offset)-spread*MarketInfo(Str,MODE_POINT);
      Res          = MarketInfo(Symb, MODE_LOTSIZE) * MarketInfo(Symb, MODE_TICKSIZE)*PriceExchage;
    }
  } return(Res);
}
//+-----------------------------------------------------------------------------------------------+
//
//+-----------------------------------------------------------------------------------------------+
bool RealSymbol(string Str) 
{
   return (MarketInfo(Str,MODE_BID)!=0);
}

