EXP_RecycleShadow

Miscellaneous
Uses files from the file systemIt reads information from a fileIt reads information from a fileIt reads information from a fileIt reads information from a file
0 Views
0 Downloads
0 Favorites
EXP_RecycleShadow
#define PAUSE 100

#define MAX_AMOUNTSYMBOLS 15
#define MAX_POINTS 100000

string SymbolsStr;
bool Variance;
int Height, Depth;

string Symbols[MAX_AMOUNTSYMBOLS];
int AmountSymbols;

double Vectors[][MAX_POINTS], Divers[MAX_POINTS], Recycles[MAX_POINTS];
int CurrTime, Times[MAX_POINTS];

int MatrixRows;

string UName, NameVar, SymbolPosName;
double V[];
int SymbolPos;

string FontName = "Arial";

void GetConfig( string FileName )
{
  int handle = FileOpen(FileName, FILE_CSV|FILE_READ);

  SymbolsStr = FileReadString(handle);

  FileReadNumber(handle); // Correlation
  Depth = FileReadNumber(handle);
  FileReadNumber(handle); // Iterations
  Height = FileReadNumber(handle);

  FileClose(handle);

  return;
}

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 GetRightSideTime()
{
  int Pos = WindowFirstVisibleBar() - WindowBarsPerChart();

  if (Pos < 0)
    Pos = 0;

  return(Time[iBarShift(Symbol(), Period(), GlobalVariableGet(UName + "LastTime")) + Pos]);
}

void GetData( string FileName )
{
  int i;
  int handle = FileOpen(FileName, FILE_BIN|FILE_READ);

  ArrayResize(Vectors, AmountSymbols);

  MatrixRows = 0;

  while (FileTell(handle) < FileSize(handle))
  {
    Times[MatrixRows] = FileReadInteger(handle);
    Recycles[MatrixRows] = FileReadDouble(handle);
    Divers[MatrixRows] = FileReadDouble(handle);

    for (i = 0; i < AmountSymbols; i++)
      Vectors[i][MatrixRows] = FileReadDouble(handle);

    MatrixRows++;
  }

  FileClose(handle);

  return;
}

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

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

    Pos--;
  }

  return(Pos);
}

void GetVector( int time, double& V[] )
{
  int Pos = GetTimePos(time);

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

  return;
}

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

 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;
}

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

  return(Num2);
}

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()
{
  int Pos = GetTimePos(CurrTime);

  ObjectSet("Arrow", OBJPROP_TIME1, Times[Pos]);
  ObjectSet("Arrow", OBJPROP_PRICE1, iIF(SymbolPos < AmountSymbols, MathAbs(Vectors[SymbolPos][Pos]),
                                     iIF(Variance, Divers[Pos], Recycles[Pos])));

  return;
}

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

  return;
}

void DrawVector( double& Vector[] )
{
  string Tmp, Tmp2 = "";
  int time, Pos = GetTimePos(CurrTime);

  SetArrow();

  if (Pos < Depth - 1)
  {
    time = Time[iBarShift(Symbol(), Period(), Times[0]) + Depth - 1 - Pos];

    Tmp2 = " (about)";
  }
  else
    time = Times[Pos - Depth + 1];

  SetVertLine("BeginInterval", time);
  SetVertLine("EndInterval", Times[Pos]);

  Tmp = "Depth = " + Depth + " bars (" + TimeToStr(time) + Tmp2 + " - " + TimeToStr(Times[Pos]) + ")";
  ModifyTextObject("Depth", Tmp);

  Tmp = "Variance = " + DoubleToStr(Divers[Pos], 8);
  ModifyTextObject("Variance", Tmp);

  Tmp = "Recycle = " + DoubleToStr(Recycles[Pos], 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 CheckVariance()
{
  bool Var = (GlobalVariableGet(NameVar) == 1);

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

    SetArrow();
  }

  return;
}

bool GetGlobalVar( string Name, int& Value )
{
  if (!GlobalVariableCheck(Name))
    return(FALSE);

  Value = GlobalVariableGet(Name);

  return(TRUE);
}

void Shadowing3( int PrevGlobal )
{
  int Global, PrevTime = 0;

  while (!IsStopped())
  {
    if (!GetGlobalVar(UName, Global))
      break;

    if (Global != PrevGlobal)
      break;

    RefreshRates();

    CurrTime = GetRightSideTime();

    CheckVariance();

    if (CurrTime != PrevTime)
    {
      GetVector(CurrTime, V);
      DrawVector(V);

      PrevTime = CurrTime;
    }

    Sleep(PAUSE);
  }

  return;
}

void Shadowing2()
{
  int PrevGlobal, Global;

  if (!GetGlobalVar(UName, PrevGlobal))
    return;

  while (!IsStopped())
  {
    if (!GetGlobalVar(UName, Global))
      break;

    CheckVariance();

    if (Global != PrevGlobal)
    {
      PrevGlobal = Global;

      GetData(UName + "i.dat");

      Shadowing3(Global);
    }

    Sleep(PAUSE);
  }

  return;
}

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

  while (!IsStopped())
  {
    if (GlobalVariableCheck(UName))
    {
      GetConfig(UName + ".ini");
      Variance = (GlobalVariableGet(NameVar) == 1);
      SymbolPos = GlobalVariableGet(SymbolPosName);

      AmountSymbols = StrToStringS(SymbolsStr, ",", Symbols);
      ArrayResize(V, AmountSymbols);

      Shadowing2();
    }

    Sleep(PAUSE);
  }

  return;
}

void init()
{
  Shadowing();

  return;
}

void start()
{
  return;
}

Comments