#property  indicator_separate_window
#property  indicator_buffers 2
#property  indicator_color2  LimeGreen
#property  indicator_color1  OrangeRed
/*#property  indicator_color3  Orange
#property  indicator_color4  Aqua
#property  indicator_width1  2   */
#property  indicator_maximum 90.0
#property  indicator_minimum -90.0
//#property strict
//#include <MovingAverages.mqh>

input int InpSignalSMA=9;
double    Extchart3aBuffer[];
double    Extchart3bBuffer[];
/*double    Extchart3cBuffer[];
double    Extchart3dBuffer[]; */

bool  Flag,FirstRun,flag_2,flag_3=false;
int   PrevTime;
double O_Spread,Order_Spread;
string FileName;

bool RealSymbol(string Str) {
  return(MarketInfo(Str, MODE_BID) != 0); }

void OnInit() {
   IndicatorDigits(Digits+1);

   SetIndexStyle(0,DRAW_LINE);   //DRAW_HISTOGRAM
   SetIndexStyle(1,DRAW_LINE);
//   SetIndexDrawBegin(1,InpSignalSMA);

   SetIndexBuffer(1,Extchart3aBuffer);
   SetIndexBuffer(0,Extchart3bBuffer);
/*   SetIndexBuffer(2,Extchart3cBuffer);
   SetIndexBuffer(3,Extchart3dBuffer); */

   SetIndexLabel(1,"chart3-a");
   SetIndexLabel(0,"chart3-b");
/*   SetIndexLabel(2,"chart3-c");
   SetIndexLabel(3,"chart3-d"); */

  IndicatorSetString(INDICATOR_SHORTNAME,"Ind L. Objets (chart3)");
  EventSetMillisecondTimer(200);

  FirstRun = TRUE;
  Flag = RealSymbol(Symbol());
  FileName = Symbol() + Period() + "_o-Spread.dat";
  return; }

int OnCalculate(const int rates_total,const int prev_calculated,const datetime &time[],
                const double &open[],const double &high[],const double &low[],const double &close[],
                const long &tick_volume[],const long &volume[],const int &spread[]) {

   int i, limit=rates_total - prev_calculated;
   /* double Order_Spread;      Order_Price,    datetime Order_Time;
   if (GlobalVariableCheck("th_O_P")) Order_Price = GlobalVariableGet("th_O_P");
   if (GlobalVariableCheck("th_O_T")) Order_Time = GlobalVariableGet("th_O_T");

   if (prev_calculated > 0) limit ++;
   for(i=0; i<limit; i++)
      Extchart3aBuffer[i]=iMA(NULL,0,InpFastEMA,0,MODE_EMA,PRICE_CLOSE,i) - iMA(NULL,0,InpSlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
   SimpleMAOnBuffer(rates_total,prev_calculated,0,InpSignalSMA,Extchart3aBuffer,Extchart3bBuffer); //*/
//   for(i=0; i<limit; i++) Extchart3aBuffer[i] = iClose(NULL,0,i);    //Bars-

   Order_Spread = GlobalVariableGet("th_O_S") / Point;
   if (FirstRun) {
      for(i=0; i<limit; i++) Extchart3aBuffer[i] = Order_Spread > 0.0 ? Order_Spread : 1;
      for(i=0; i<limit; i++) Extchart3bBuffer[i] = Order_Spread < 0 ? Order_Spread : 0; }

   if (! Flag) { GetData();    return(rates_total); }

   if (FirstRun) { GetData();
      O_Spread = Order_Spread;
      PrevTime = Time[0];     FirstRun = FALSE;
      return(rates_total); }

   if (PrevTime != Time[0]) flag_2 = true;
   if (O_Spread != Order_Spread || (TimeCurrent() - Time[0] >= Period()*60 - 1 && flag_2)) { flag_2 = false;
      Print("** --> (TimeCurrent() - Time[0] >= Period()*60 - 1) ",(TimeCurrent() - Time[0] >= Period()*60 - 1),", ",(TimeCurrent() - Time[0]),", ",(Period()*60 - 1),", ",
         TimeToStr(TimeCurrent(),TIME_MINUTES),", ",TimeToStr(Time[0],TIME_MINUTES)," - flag_2 ",flag_2," - O_Spread ",O_Spread," - Order_Spread ",Order_Spread," **"); //*/
      if (O_Spread != Order_Spread) Print("** --> PrevTime ",PrevTime,", ",TimeToStr(PrevTime,TIME_MINUTES)," - Time[0] ",Time[0],", ",
         TimeToStr(Time[0],TIME_MINUTES)," - O_Spread ",O_Spread," - Order_Spread ",Order_Spread," **");
      if (Flag && (fabs(Order_Spread) < 150 || ! flag_3)) WriteOSpread();      PrevTime = Time[0];      O_Spread = Order_Spread; }

   if ((Extchart3aBuffer[0]==0.0 && Extchart3bBuffer[0]==0.0) || Extchart3aBuffer[0]!=Order_Spread || Extchart3bBuffer[0]!=Order_Spread) {
      Extchart3aBuffer[0] = Order_Spread > 0.0 ? Order_Spread : 1;     Extchart3bBuffer[0] = Order_Spread < 0 ? Order_Spread : 0; }
/*   Extchart3aBuffer[2] = Order_Spread > 0 ? Order_Spread : 0;   Extchart3bBuffer[2] = Order_Spread < 0 ? Order_Spread : 0;

/*   for(i=1; i<limit; i++) if (Extchart3aBuffer[i] == 0.0 && Order_Spread > 0) Extchart3aBuffer[i] = Order_Spread;
   for(i=1; i<limit; i++) if (Extchart3bBuffer[i] == 0.0 && Order_Spread < 0) Extchart3bBuffer[i] = Order_Spread; //*/

//   IndicatorSetInteger(INDICATOR_LEVELCOLOR, Aqua);
  return(rates_total); }

void deinit() {
  if (Flag && (fabs(Order_Spread) < 150 || ! flag_3)) WriteOSpread();
  return; }

void GetData() {
  int T, i, O_Spread_=0; //, total, total_, total2, total3;
  int handle = FileOpen(FileName, FILE_BIN|FILE_READ);
  
  if (handle < 0) return;
  while (! FileIsEnding(handle)) {
    T = FileReadInteger(handle);
    O_Spread_ = flag_3 && (fabs(O_Spread_ - O_Spread)>150 || fabs(O_Spread)>150)? O_Spread_: O_Spread;
    O_Spread = FileReadDouble(handle);

    i = iBarShift(Symbol(), Period(), T, TRUE);
//Print("*** (1) i ",i," O_Spread_ ",O_Spread_," O_Spread ",O_Spread," ***");
    if (i >= 0) if (flag_3 && (fabs(O_Spread_ - O_Spread) > 150 || fabs(O_Spread) > 150)) {
//Print("*** (2) i ",i," O_Spread_ ",O_Spread_," O_Spread ",O_Spread," ***");
         Extchart3aBuffer[i] = O_Spread_ > 0.0 ? O_Spread_: 1;      Extchart3bBuffer[i] = O_Spread_ < 0 ? O_Spread_: 0; }
       else { Extchart3aBuffer[i] = O_Spread > 0.0 ? O_Spread : 1;   Extchart3bBuffer[i] = O_Spread < 0 ? O_Spread : 0; }
    }

  FileClose(handle);
  return; }

void WriteOSpread() {
  int handle = FileOpen(FileName, FILE_BIN|FILE_READ|FILE_WRITE);

  FileSeek(handle, 0, SEEK_END);
  FileWriteInteger(handle, PrevTime);
  FileWriteDouble(handle, Order_Spread);

  FileClose(handle);
  return; }
