IND_Recycle

Price Data Components
Series array that contains close prices for each bar
Miscellaneous
Uses files from the file systemIt writes information to fileImplements a curve of type %1It reads information from a fileIt reads information from a fileIt writes information to fileIt writes information to fileIt reads information from a fileIt writes information to file
0 Views
0 Downloads
0 Favorites
IND_Recycle
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red
#property indicator_width1 2

#define MAX_AMOUNTSYMBOLS 15
#define MAX_POINTS 100000

extern string SymbolsStr = "AUDUSD, EURUSD, GBPUSD, USDCHF, USDJPY, USDCAD, NZDUSD, SILVER, GOLD, USDSEK, USDSGD, USDMXN, USDNOK";
extern bool SymbolKoef = TRUE;
extern bool Variance = TRUE;
extern bool Correlation = TRUE;
extern int Depth = 288;
extern int Iterations = 10;
extern int Height = 300;
extern int BarStep = 36;
extern color ColorText = Gray;
extern color ColorPlusBar = Yellow;
extern color ColorMinusBar = Magenta;
extern color ColorDigits = Blue;
extern color ColorAxis = Green;
extern color ColorArrow = Blue;
extern color ColorLine = Blue;

string UName, IndName, NameVar;
int SymbolPos;
double Buffer[];

string Symbols[MAX_AMOUNTSYMBOLS];
double BaseMatrix[][MAX_POINTS], MOMatrix[][MAX_POINTS], Vectors[][MAX_POINTS], Divers[MAX_POINTS], Recycles[MAX_POINTS];
double CvarMatrix[][MAX_AMOUNTSYMBOLS];
double Means[], Divs[];
int Times[MAX_POINTS];
int AmountSymbols, CurrTime, MatrixRows, CurrPos;
double V[];

string FontName = "Arial";

string StrDelSpaces( string Str )
{
  int Pos, Length;

  Str = StringTrimLeft(Str);
  Str = StringTrimRight(Str);

  Length = StringLen(Str) - 1;
  Pos = 1;

  while (Pos < Length)
    if (StringGetChar(Str, Pos) == ' ')
    {
      Str = StringSubstr(Str, 0, Pos) + StringSubstr(Str, Pos + 1, 0);
      Length--;
    }
    else
      Pos++;

  return(Str);
}

int StrToStringS( string Str, string Razdelitel, string &Output[] )
{
  int Pos, LengthSh;
  int Count = 0;

  Str = StrDelSpaces(Str);
  Razdelitel = StrDelSpaces(Razdelitel);

  LengthSh = StringLen(Razdelitel);

  while (TRUE)
  {
    Pos = StringFind(Str, Razdelitel);
    Output[Count] = StringSubstr(Str, 0, Pos);
    Count++;

    if (Pos == -1)
      break;

    Pos += LengthSh;
    Str = StringSubstr(Str, Pos);
  }

  return(Count);
}

int BooleanToInteger( bool Value )
{
  if (Value)
    return(1);

  return(0);
}

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

void FilterSymbols()
{
  int i, j;

  for (i = 0; i < AmountSymbols; i++)
    if (!RealSymbol(Symbols[i]))
    {
      for (j = i; j < AmountSymbols - 1; j++)
        Symbols[j] = Symbols[j + 1];

      AmountSymbols--;
    }

  return;
}

void SaveConfig( string FileName )
{
  string Str = Symbols[0];
  int handle = FileOpen(FileName, FILE_CSV|FILE_WRITE);

  for (int i = 1; i < AmountSymbols; i++)
    Str = Str + ", " + Symbols[i];

  FileWrite(handle, Str);
  FileWrite(handle, BooleanToInteger(Correlation));
  FileWrite(handle, Depth);
  FileWrite(handle, Iterations);
  FileWrite(handle, Height);

  FileClose(handle);

  return;
}

datetime GetStartTime( int Pos )
{
  datetime Tmp, StartTime;
  int i, PosAddon;

  PosAddon = iBarShift(Symbols[0], Period(), Time[0]);
  StartTime = iTime(Symbols[0], Period(), Pos + PosAddon);

  for (i = 1; i < AmountSymbols; i++)
  {
    PosAddon = iBarShift(Symbols[i], Period(), Time[0]);
    Tmp = iTime(Symbols[i], Period(), Pos + PosAddon);

    if (Tmp > StartTime)
      StartTime = Tmp;
  }

  return(StartTime);
}

double GetPrice( string Symb, int time )
{
  double Price;

  Price = iClose(Symb, Period(), iBarShift(Symb, Period(), time));

  return(Price);
}

int GetNextTime( int CurrTime )
{
  static int Pos[MAX_AMOUNTSYMBOLS];
  int i, MinTime, Tmp = -1;

  for (i = 0; i < AmountSymbols; i++)
  {
    Pos[i] = iBarShift(Symbols[i], Period(), CurrTime) - 1;

    if (Pos[i] >= 0)
      Tmp = i;
  }

  if (Tmp < 0)
    return(Time[0]);

  MinTime = iTime(Symbols[Tmp], Period(), Pos[Tmp]);

  i = Tmp - 1;

  while (i >= 0)
  {
    if (Pos[i] >= 0)
    {
      Tmp = iTime(Symbols[i], Period(), Pos[i]);

      if (Tmp < MinTime)
        MinTime = Tmp;
    }

    i--;
  }

  return(MinTime);
}

void GetBaseMatrix()
{
  int i, NextTime;

  NextTime = GetNextTime(CurrTime);

  while (NextTime < Time[0])
  {
    CurrTime = NextTime;

    for (i = 0; i < AmountSymbols; i++)
      BaseMatrix[i][MatrixRows] = 1000 * MathLog(GetPrice(Symbols[i], CurrTime));

    Times[MatrixRows] = CurrTime;

    MatrixRows++;

    NextTime = GetNextTime(CurrTime);
  }

  return;
}

void GetMeans( int Pos, int Len)
{
  int i, j;
  double Sum;

  for (i = 0; i < AmountSymbols;, i++)
  {
    Sum = 0;

    for (j = Pos; j > Pos - Len; j--)
      Sum += BaseMatrix[i][j];

    Means[i] = Sum / Len;
  }

  return;
}

void GetMOMatrix( int Pos, int Len)
{
  int i, j;
  double Sum;

  for (i = 0; i < AmountSymbols;, i++)
    for (j = Pos; j > Pos - Len; j--)
      MOMatrix[i][j] = BaseMatrix[i][j] - Means[i];

  return;
}

void GetCvarMatrix( int Pos, int Len )
{
  int i, j, k;
  double Cvar;

  GetMeans(Pos, Len);
  GetMOMatrix(Pos, Len);

  for (i = 0; i < AmountSymbols; i++)
  {
     Cvar = 0;

     for (k = Pos; k > Pos - Len; k--)
       Cvar += MOMatrix[i][k] * MOMatrix[i][k];

     Divs[i] = Cvar / Len;
     CvarMatrix[i][i] = Divs[i];

    for (j = i + 1; j < AmountSymbols; j++)
    {
      Cvar = 0;

     for (k = Pos; k > Pos - Len; k--)
        Cvar += MOMatrix[i][k] * MOMatrix[j][k];

      CvarMatrix[i][j] = Cvar / Len;
    }
  }
  return;
}

void FileToNull( string FileName )
{
  int handle = FileOpen(FileName, FILE_BIN|FILE_WRITE);

  FileClose(handle);

  return;
}

void SetIndName()
{
  if (SymbolPos < AmountSymbols)
  {
    if (Correlation)
      IndName = "Correlation Absolute Koef";
    else
      IndName = "Recycle Absolute Koef";
  }
  else if (Correlation)
  {
    IndName = "Correlation Koefs";

    if (Variance)
      IndName = IndName + " (Variance graph)";
    else
      IndName = IndName + " (Recycle graph)";
  }
  else
  {
    IndName = "Recycle Koefs";

    if (Variance)
      IndName = IndName + " (Variance graph)";
    else
      IndName = IndName + " (Recycle graph)";
  }

  IndicatorShortName(IndName);

  return;
}

int GetSymbolPos( string Symb )
{
  int Pos = 0;

  while (Pos < AmountSymbols)
  {
    if (Symb == Symbols[Pos])
      break;

    Pos++;
  }

  return(Pos);
}

void init()
{
  UName = "hwnd" + WindowHandle(Symbol(), Period());
  NameVar = UName + "Variance";

  FileToNull(UName + "i.dat");

  IndicatorDigits(8);
  SetIndexStyle(0, DRAW_LINE, DRAW_LINE);
  SetIndexBuffer(0, Buffer);

  AmountSymbols = StrToStringS(SymbolsStr, ",", Symbols);
  FilterSymbols();

  if (SymbolKoef)
    SymbolPos = GetSymbolPos(Symbol());
  else
    SymbolPos = AmountSymbols;

  SetIndName();

  SaveConfig(UName + ".ini");

  GlobalVariableSet(UName + "SymbolPos", SymbolPos);
  GlobalVariableSet(NameVar, BooleanToInteger(Variance));
  GlobalVariableSet(UName + "LastTime", Time[0]);
  GlobalVariableSet(UName, 1);

  ArrayResize(Symbols, AmountSymbols);
  ArrayResize(BaseMatrix, AmountSymbols);
  ArrayResize(MOMatrix, AmountSymbols);
  ArrayResize(CvarMatrix, AmountSymbols);
  ArrayResize(Means, AmountSymbols);
  ArrayResize(Divs, AmountSymbols);
  ArrayResize(Vectors, AmountSymbols);
  ArrayResize(V, AmountSymbols);

  CurrTime = GetStartTime(Depth + 1);

  MatrixRows = 0;

  CurrPos = Depth - 1;

  return;
}

void InvertMatrix( double& Matrix[][] )
{
  static int rn[];
  static double str[], strm[];
  int j,k;
  int jved;
  double aved, Tmp;

  ArrayResize(rn, AmountSymbols);
  ArrayResize(str, AmountSymbols);
  ArrayResize(strm, AmountSymbols);

  for (j = 0; j < AmountSymbols; j++)
    rn[j] = j;

  for (int i = 0; i < AmountSymbols; i++)
  {
    aved = -1;

    for (j = 0; j < AmountSymbols; j++)
      if (rn[j] != -1)
      {
        Tmp = MathAbs(Matrix[j][j]);

        if (Tmp > aved)
        {
           aved = Tmp;
           jved = j;
        }
      }

    rn[jved] = -1;

    for (j = 0; j < jved; j++)
    {
      str[j] = Matrix[j][jved];
      strm[j] = str[j] / aved;
    }

    for (j = jved + 1; j < AmountSymbols; j++)
    {
      str[j] = Matrix[jved][j];
      strm[j] = str[j] / aved;
    }

    for (j = 0; j < AmountSymbols; j++)
      for (k = j; k < AmountSymbols; k++)
        Matrix[j][k] -= strm[j] * str[k];

    for (j = 0; j < jved; j++)
      Matrix[j][jved] = strm[j];

    for (j = jved + 1; j < AmountSymbols; j++)
      Matrix[jved][j] = strm[j];

    Matrix[jved][jved] = -1 / aved;
  }

  return;
}

int GetSign( double Num1, double Num2 )
{
  if (Num1 >= 0)
  {
    if (Num2 >= 0)
      return(1);
  }
  else if (Num2 < 0)
    return(1);

  return(-1);
}

void GetOptimalVector( double& Vector[], int Iterations, bool Correlation )
{
  int i, j, k;
  double Max, Tmp;

  if (Correlation)
  {
    for (i = 0; i < AmountSymbols; i++)
      Divs[i] = MathSqrt(Divs[i]);

    for (i = 0; i < AmountSymbols; i++)
    {
      CvarMatrix[i][i] = 1;

      if (Divs[i] != 0)
        for (j = i + 1; j < AmountSymbols; j++)
          if (Divs[j] != 0)
            CvarMatrix[i][j] /= Divs[i] * Divs[j];
          else
            CvarMatrix[i][j] = 0;
      else
        for (j = i + 1; j < AmountSymbols; j++)
          CvarMatrix[i][j] = 0;
    }
  }

  InvertMatrix(CvarMatrix);

  while (Iterations > 0)
  {
    Max = 0;

    for (i = 0; i < AmountSymbols; i++)
      for (j = i; j < AmountSymbols; j++)
      {
        Tmp = MathAbs(CvarMatrix[i][j]);

        if (Tmp > Max)
          Max = Tmp;
      }

    for (i = 0; i < AmountSymbols; i++)
      for (j = i; j < AmountSymbols; j++)
        CvarMatrix[i][j] /= Max;

    for (i = 0; i < AmountSymbols; i++)
      for (j = AmountSymbols - 1; j >= i; j--)
      {
        Tmp = 0;
        k = 0;

        while (k < i)
        {
          Tmp += CvarMatrix[k][i] * CvarMatrix[k][j];

          k++;
        }

        while (k < j)
        {
          Tmp += CvarMatrix[i][k] * CvarMatrix[k][j];

          k++;
        }

        while (k < AmountSymbols)
        {
          Tmp += CvarMatrix[i][k] * CvarMatrix[j][k];

          k++;
        }

        CvarMatrix[j][i] = Tmp;
      }

    for (i = 0; i < AmountSymbols; i++)
      for (j = i + 1; j < AmountSymbols; j++)
        CvarMatrix[i][j] = CvarMatrix[j][i];

    Iterations--;
  }

  Max = 0;

  for (i = 0; i < AmountSymbols; i++)
  {
    Tmp = 0;

    for (j = 0; j < AmountSymbols; j++)
      Tmp += CvarMatrix[i][j] * CvarMatrix[i][j];

    if (Tmp > Max)
    {
       Max = Tmp;
       k = i;
    }
  }

  j = 0;

  for (i = 0; i < AmountSymbols; i++)
    j += GetSign(Vector[i], CvarMatrix[k][i]);

  if (j >= 0)
    Max = MathSqrt(Max);
  else
    Max = -MathSqrt(Max);

  for (i = 0; i < AmountSymbols; i++)
    Vector[i] = CvarMatrix[k][i] / Max;

  return;
}

double GetRecycle( int Pos, int Len, double& Vector[], bool Correlation )
{
  int i;
  double Recycle = 0;

  if (Correlation)
  {
    for (i = 0; i < AmountSymbols;, i++)
      if (Divs[i] != 0)
        Recycle += MOMatrix[i][Pos] * Vector[i] / Divs[i]; // Divs == StdDev
  }
  else
    for (i = 0; i < AmountSymbols;, i++)
      Recycle += MOMatrix[i][Pos] * Vector[i];

  return(Recycle);
}

double GetDivergence( int Pos, int Len, double& Vector[], bool Correlation )
{
  int i, j;
  double Sum, Div = 0;

  if (Correlation)
    for (i = Pos; i > Pos - Len; i--)
    {
      Sum = 0;

      for (j = 0; j < AmountSymbols;, j++)
        if (Divs[j] != 0)
          Sum += MOMatrix[j][i] * Vector[j] / Divs[j]; // Divs == StdDev

      Div += Sum * Sum;
    }
  else
    for (i = Pos; i > Pos - Len; i--)
    {
      Sum = 0;

      for (j = 0; j < AmountSymbols;, j++)
        Sum += MOMatrix[j][i] * Vector[j];

      Div += Sum * Sum;
    }

  Div /= Len;

  return(Div);
}

void deinit()
{
  GlobalVariableDel(UName);
  GlobalVariableDel(UName + "LastTime");
  GlobalVariableDel(UName + "SymbolPos");
  GlobalVariableDel(NameVar);

  ObjectDelete("BeginInterval");
  ObjectDelete("EndInterval");

  FileToNull(UName + ".ini");
  FileToNull(UName + ".dat");
  FileToNull(UName + "i.dat");

  return;
}

void GetData( string FileName )
{
  int time;
  double Div, Recycle;
  int Offset = AmountSymbols * DOUBLE_VALUE;
  int handle = FileOpen(FileName, FILE_BIN|FILE_READ);

  if (FileSize(handle) == 0)
  {
    FileClose(handle);

    return;
  }

  while (FileTell(handle) < FileSize(handle))
  {
    time = FileReadInteger(handle);
    Recycle = FileReadDouble(handle);
    Div = FileReadDouble(handle);

    FileSeek(handle, Offset, SEEK_CUR);

    Buffer[iBarShift(Symbol(), Period(), time)] = iIF(Variance, Div, Recycle);
  }

  FileClose(handle);

  return;
}

void GetSaveData( string FileNameIn, string FileNameOut )
{
  int i, j, time, Pos;
  double Div, Recycle;

  int handleIn = FileOpen(FileNameIn, FILE_BIN|FILE_READ);
  int handleOut = FileOpen(FileNameOut, FILE_BIN|FILE_WRITE);

  while (FileTell(handleIn) < FileSize(handleIn))
  {
    time = FileReadInteger(handleIn);
    FileWriteInteger(handleOut, time);

    Recycle = FileReadDouble(handleIn);
    FileWriteDouble(handleOut, Recycle);

    Div = FileReadDouble(handleIn);
    FileWriteDouble(handleOut, Div);

    FileReadArray(handleIn, V, 0, AmountSymbols);
    FileWriteArray(handleOut, V, 0, AmountSymbols);

    Pos = iBarShift(Symbol(), Period(), time);

    Buffer[Pos] = iIF(SymbolPos < AmountSymbols, MathAbs(V[SymbolPos]), iIF(Variance, Div, Recycle));
  }

  FileClose(handleIn);

  Pos = GetTimePos(time) + 1;

  if (Pos < MatrixRows)
  {
    j = 0;

    for (i = 0; i < AmountSymbols; i++)
      j += GetSign(Vectors[i][Pos], V[i]);
  }

  while (Pos < MatrixRows)
  {
    if (j < 0)
    {
      Recycles[Pos] = -Recycles[Pos];

      for (i = 0; i < AmountSymbols; i++)
        Vectors[i][Pos] = -Vectors[i][Pos];
    }

    time = Times[Pos];
    Div = Divers[Pos];
    Recycle = Recycles[Pos];

    Buffer[iBarShift(Symbol(), Period(), time)] = iIF(SymbolPos < AmountSymbols, MathAbs(Vectors[SymbolPos][Pos]),
                                                  iIF(Variance, Div, Recycle));

    FileWriteInteger(handleOut, time);
    FileWriteDouble(handleOut, Recycle);
    FileWriteDouble(handleOut, Div);

    for (i = 0; i < AmountSymbols; i++)
      FileWriteDouble(handleOut, Vectors[i][Pos]);

    if (Pos == MatrixRows -1)
      for (i = 0; i < AmountSymbols; i++)
        V[i] = Vectors[i][Pos];

    Pos++;
  }

  FileClose(handleOut);

  return;
}

void HideObject( string Name, bool Hide )
{
  if (Hide)
   ObjectSet(Name, OBJPROP_TIMEFRAMES, EMPTY);
  else
   ObjectSet(Name, OBJPROP_TIMEFRAMES, NULL);

 return;
}

void CreateObject( string Name, string Value, int FontSize, int Xcoord, int Ycoord, int Angle, bool Hide, bool Back, int Color )
{
  ObjectCreate(Name, OBJ_LABEL, WindowFind(IndName), 0, 0);
  HideObject(Name, Hide);
  ObjectSet(Name, OBJPROP_ANGLE, Angle);
  ObjectSet(Name, OBJPROP_XDISTANCE, Xcoord);
  ObjectSet(Name, OBJPROP_YDISTANCE, Ycoord);
  ObjectSet(Name, OBJPROP_BACK, Back);
  ObjectSetText(Name, Value, FontSize, FontName, Color);

  return;
}

void CreateObjectBar( string Name, int Xcoord, int Ycoord )
{
  int i, Avg = Height / 2;
  string Tmp;

  CreateObject(Name, Name, 10, Xcoord - 15, Ycoord + 10 + StringLen(Name) * 5, 90, FALSE, FALSE, ColorText);
  CreateObject(Name + "Plus", "+0.0000", 8, Xcoord - 2, Ycoord, 90, TRUE, FALSE, ColorDigits);
  CreateObject(Name + "Minus", "-0.000", 8, Xcoord + 11, Ycoord + 27, -90, TRUE, FALSE, ColorDigits);

  Ycoord -= 5 + Avg;

  for (i = 0; i <= Height; i++)
  {
    Tmp = Name + i;

    if (i > Avg)
      CreateObject(Tmp, "_", 12, Xcoord, Ycoord + i, 0, TRUE, TRUE, ColorMinusBar);
    else if (i < Avg)
      CreateObject(Tmp, "_", 12, Xcoord, Ycoord + i, 0, TRUE, TRUE, ColorPlusBar);
    else
      CreateObject(Tmp, "_", 12, Xcoord, Ycoord + i, 0, TRUE, TRUE, ColorAxis);
  }

  return;
}

void CreateVertLine( string Name, int Color )
{
  ObjectCreate(Name, OBJ_VLINE, 0, 0, 0);
  ObjectSet(Name, OBJPROP_BACK, TRUE);
  ObjectSet(Name, OBJPROP_STYLE, STYLE_DASH);
  ObjectSet(Name, OBJPROP_COLOR, ColorLine);

  return;
}

void CreateBars( int Xcoord, int Ycoord, int XStep )
{
  int i, Avg = Height / 2;
  string Tmp = "";

  ObjectCreate("Arrow", OBJ_ARROW, WindowFind(IndName), 0, 0);
  ObjectSet("Arrow", OBJPROP_COLOR, ColorArrow);

  CreateVertLine("BeginInterval", ColorLine);
  CreateVertLine("EndInterval", ColorLine);

  CreateObject("Depth", "Depth", 12, Xcoord, Ycoord - 20 - Avg, 0, FALSE, FALSE, ColorText);
  CreateObject("Variance", "Variance", 12, Xcoord, Ycoord + 20 - Avg, 0, FALSE, FALSE, ColorText);
  CreateObject("Recycle", "Recycle", 12, Xcoord, Ycoord + 40 - Avg, 0, FALSE, FALSE, ColorText);

  if (SymbolPos < AmountSymbols)
    CreateObject("SymbolKoef", "SymbolKoef", 12, Xcoord, Ycoord + 60 - Avg, 0, FALSE, FALSE, ColorText);

  for (i = 0; i <= (AmountSymbols - 1) * XStep / 9; i++)
    Tmp = Tmp + "_";

  CreateObject("Top", Tmp, 12, Xcoord, Ycoord - 5 - Avg, 0, FALSE, TRUE, ColorAxis);
  CreateObject("Bottom", Tmp, 12, Xcoord, Ycoord - 5 + Avg, 0, FALSE, TRUE, ColorAxis);

  for (i = 0; i < AmountSymbols; i++)
  {
    CreateObjectBar(Symbols[i], Xcoord, Ycoord);

    Xcoord += XStep;
  }

  return;
}

void SetBar( string Name, double Value )
{
  int i, Pos;
  string Tmp;
  int Avg = Height / 2;

  HideObject(Name + "Plus", TRUE);
  HideObject(Name + "Minus", TRUE);

  if (Value >= 0)
    Tmp = "Plus";
  else
    Tmp = "Minus";

  ModifyTextObject(Name + Tmp, DoubleToStr(Value, 4));
  HideObject(Name + Tmp, FALSE);

  for (i = 0; i <= Height; i++)
   HideObject(Name + i, TRUE);

  Pos = Value * Avg;

  if (Value >= 0)
    for (i = Avg; i >= Avg - Pos; i--)
      HideObject(Name + i, FALSE);
  else
    for (i = Avg; i <= Avg - Pos; i++)
      HideObject(Name + i, FALSE);

  return;
}

int GetTimePos( int time )
{
  int Pos = MatrixRows - 1;

  while (Pos > 0)
  {
    if (Times[Pos] <= time)
      break;

    Pos--;
  }

  return(Pos);
}

void ModifyTextObject( string Name, string Text )
{
  int Color = ObjectGet(Name, OBJPROP_COLOR);
  int FontSize = ObjectGet(Name, OBJPROP_FONTSIZE);

  ObjectSetText(Name, Text, FontSize, FontName, Color);

  return;
}

void SetArrow()
{
  ObjectSet("Arrow", OBJPROP_TIME1, Times[MatrixRows - 1]);
  ObjectSet("Arrow", OBJPROP_PRICE1, iIF(SymbolPos < AmountSymbols, MathAbs(Vectors[SymbolPos][MatrixRows - 1]),
                                     iIF(Variance, Divers[MatrixRows - 1], Recycles[MatrixRows - 1])));

  return;
}

void SetVertLine( string Name, int Pos)
{
  ObjectSet(Name, OBJPROP_TIME1, Pos);

  return;
}

void DrawVector( double& Vector[] )
{
  string Tmp;

  SetArrow();

  SetVertLine("BeginInterval", Times[MatrixRows - Depth]);
  SetVertLine("EndInterval", Times[MatrixRows - 1]);

  Tmp = "Depth = " + Depth + " bars (" + TimeToStr(Times[MatrixRows - Depth]) + " - " + TimeToStr(Times[MatrixRows - 1]) + ")";
  ModifyTextObject("Depth", Tmp);

  Tmp = "Variance = " + DoubleToStr(Divers[MatrixRows - 1], 8);
  ModifyTextObject("Variance", Tmp);

  Tmp = "Recycle = " + DoubleToStr(Recycles[MatrixRows - 1], 8);
  ModifyTextObject("Recycle", Tmp);

  if (SymbolPos < AmountSymbols)
  {
    Tmp = Symbol() + " Koef = " + DoubleToStr(MathAbs(Vector[SymbolPos]), 8);
    ModifyTextObject("SymbolKoef", Tmp);
  }

  for (int i = 0; i < AmountSymbols; i++)
    SetBar(Symbols[i], Vector[i]);

  WindowRedraw();

  return;
}

void SaveData( string FileName, int Pos )
{
  int handle = FileOpen(FileName, FILE_BIN|FILE_READ|FILE_WRITE);

  FileSeek(handle, 0, SEEK_END);

  while (Pos < MatrixRows)
  {
    FileWriteInteger(handle, Times[Pos]);
    FileWriteDouble(handle, Recycles[Pos]);
    FileWriteDouble(handle, Divers[Pos]);

    for (int i = 0; i < AmountSymbols; i++)
      FileWriteDouble(handle, Vectors[i][Pos]);

    Pos++;
  }

  FileClose(handle);

  return;
}

double iIF( bool Cond, double Num1, double Num2 )
{
  if (Cond)
    return(Num1);

  return(Num2);
}

void start()
{
  static bool FirstRun = TRUE;
  static int PrevTime = 0;
  string Name = UName + "Done";
  bool Var, FlagChange = FALSE;
  int PrevPos;

  if (FirstRun)
  {
    CreateBars(50, 50 + Height / 2, BarStep);

    FirstRun = FALSE;
  }

  Var = (GlobalVariableGet(NameVar) == 1);

  if ((Var != Variance) && (SymbolPos == AmountSymbols))
  {
    Variance = Var;

    SetIndName();

    GetData(UName + "i.dat");
    SetArrow();
  }

  if (PrevTime != Time[0])
  {
    PrevTime = Time[0];

    GlobalVariableSet(UName + "LastTime", Time[0]);

    GetBaseMatrix();

    PrevPos = CurrPos;

    while (CurrPos < MatrixRows)
    {
      GetCvarMatrix(CurrPos, Depth);
      GetOptimalVector(V, Iterations, Correlation);

      Recycles[CurrPos] = GetRecycle(CurrPos, Depth, V, Correlation);
      Divers[CurrPos] = GetDivergence(CurrPos, Depth, V, Correlation);

      Buffer[iBarShift(Symbol(), Period(), Times[CurrPos])] = iIF(SymbolPos < AmountSymbols, MathAbs(V[SymbolPos]),
                                                              iIF(Variance, Divers[CurrPos], Recycles[CurrPos]));

      for (int i = 0; i < AmountSymbols; i++)
        Vectors[i][CurrPos] = V[i];

      CurrPos++;
    }

    if (PrevPos < MatrixRows)
    {
      DrawVector(V);

      SaveData(UName + "i.dat", PrevPos);

      FlagChange = TRUE;
    }
  }

  if (GlobalVariableCheck(Name))
  {
    GetSaveData(UName + ".dat", UName + "i.dat");
    DrawVector(V);

    GlobalVariableDel(Name);
    FlagChange = TRUE;
  }

  if (FlagChange)
    GlobalVariableSet(UName, GlobalVariableGet(UName) + 1);

  return;
}

Comments