#include <windows.h>
#include <string>

#include "StrategyInterfaceUnit.h"
#include "TechnicalFunctions.h"


PChar Currency = NULL;
int Timeframe;

int IndMACD;
int IndMA;





EXPORT void __stdcall InitStrategy()
{
  StrategyShortName("ObjectsTest");
  StrategyDescription("Test of moving objects");

  RegOption("Currency", ot_Currency, &Currency);

  RegOption("Timeframe", ot_TimeFrame, &Timeframe);
  Timeframe = PERIOD_M15;
}

EXPORT void __stdcall DoneStrategy()
{
  free(Currency);
}

EXPORT void __stdcall  ResetStrategy()
{
    IndMACD = CreateIndicator("XAUUSD", 1, "MACD", "8;20;12;Close");
    std::string str = const_cast<char*>(std::to_string(IndMACD).c_str());
    Print(const_cast<char*>( std::to_string(IndMACD).c_str()));

    char buff[1000];

    sprintf(buff, "%d;0;0;%s;Close", 5, StrMAType(ma_EMA));

    IndMA = CreateIndicator("XAUUSD", 1, "MovingAverage", buff);


}

EXPORT void __stdcall GetSingleTick()
{
  char* names[] = {"Line0", "Line1", "Line2", "Line3", "Fibo1",
    "Fibo2", "Fibo3", "Fibo4", "Text1"};

  if (strcmp(Currency, Symbol()) != 0) return;

  SetCurrencyAndTimeframe(Currency, Timeframe);

  if (Bars() < 80) return;

  if (!ObjectExists(names[0]))
  {
    ObjectCreate(names[0], obj_VLine, 0, Time(0), 0);
    ObjectSet(names[0], OBJPROP_STYLE, psDot);
    ObjectSet(names[0], OBJPROP_COLOR, clYellow);
    ObjectSet(names[0], OBJPROP_WIDTH, 1);
  }

 // create horizontal line
  if (!ObjectExists(names[1]))
    ObjectCreate(names[1], obj_HLine, 0, 0, Close(0));

  // create trend line
  if (!ObjectExists(names[2]))
    ObjectCreate(names[2], obj_TrendLine, 0, Time(10), Close(10),
      Time(0), Close(0));

  // create ray
  if (!ObjectExists(names[3]))
    ObjectCreate(names[3], obj_Ray, 0, Time(10), Open(10),
      Time(0), Open(0));

  // create fibo retracement
  if (!ObjectExists(names[4]))
  {
    ObjectCreate(names[4], obj_FiboRetracement, 0, Time(40), Close(40),
      Time(10), Close(10));
    ObjectSet(names[4], OBJPROP_FIBOCLOSEDENDS, 1);
    ObjectSet(names[4], OBJPROP_FIBOSHOWPRICE, 0);
    ObjectSet(names[4], OBJPROP_FIBOENDWIDTH, 40);

    // define 3 levels
    ObjectSet(names[4], OBJPROP_FIBOLEVELS, 3);
    for (int i=0; i <= 2; i++)
    {
      ObjectSet(names[4], OBJPROP_FIBOLEVELN, i);
      ObjectSet(names[4], OBJPROP_LEVELVALUE, i*50);
      ObjectSet(names[4], OBJPROP_LEVELCOLOR, clBlue);
      ObjectSet(names[4], OBJPROP_LEVELSTYLE, psDot);
      ObjectSet(names[4], OBJPROP_LEVELWIDTH, 1);
    }
  }

  // create fibo fan
  if (!ObjectExists(names[5]))
  {
    ObjectCreate(names[5], obj_FiboFan, 0, Time(80), Close(80),
      Time(50), Close(50));

    // define 3 levels
    ObjectSet(names[5], OBJPROP_FIBOLEVELS, 3);
    for (int i=0; i <= 2; i++)
    {
      ObjectSet(names[5], OBJPROP_FIBOLEVELN, i);
      ObjectSet(names[5], OBJPROP_LEVELVALUE, i*30);
      ObjectSet(names[5], OBJPROP_LEVELCOLOR, clGreen);
      ObjectSet(names[5], OBJPROP_LEVELSTYLE, psSolid);
      ObjectSet(names[5], OBJPROP_LEVELWIDTH, 1);
    }
  }

  // create text
  if (!ObjectExists(names[8]))
  {
    ObjectCreate(names[8], obj_Text, 0, Time(10), Close(0));
    ObjectSetText(names[8], "Test text", 12, "Arial", clYellow);
    ObjectSet(names[8], OBJPROP_VALIGNMENT, tlBottom);
    ObjectSet(names[8], OBJPROP_HALIGNMENT, taLeftJustify);
  }


  // set vertical line props
  ObjectSet(names[0], OBJPROP_TIME1, Time(0));

  // set horizontal line props
  ObjectSet(names[1], OBJPROP_PRICE1, Close(0));

  // set trend line props
  ObjectSet(names[2], OBJPROP_TIME1, Time(10));
  ObjectSet(names[2], OBJPROP_PRICE1, Close(10));
  ObjectSet(names[2], OBJPROP_TIME2, Time(0));
  ObjectSet(names[2], OBJPROP_PRICE2, Close(0));

  // set ray props
  ObjectSet(names[3], OBJPROP_TIME1, Time(10));
  ObjectSet(names[3], OBJPROP_PRICE1, Open(10));
  ObjectSet(names[3], OBJPROP_TIME2, Time(0));
  ObjectSet(names[3], OBJPROP_PRICE2, Open(0));

  // set fibo retracement props
  ObjectSet(names[4], OBJPROP_TIME1, Time(40));
  ObjectSet(names[4], OBJPROP_PRICE1, Close(40));
  ObjectSet(names[4], OBJPROP_TIME2, Time(10));
  ObjectSet(names[4], OBJPROP_PRICE2, Close(10));

  // set fibo fan props
  ObjectSet(names[5], OBJPROP_TIME1, Time(80));
  ObjectSet(names[5], OBJPROP_PRICE1, Close(80));
  ObjectSet(names[5], OBJPROP_TIME2, Time(50));
  ObjectSet(names[5], OBJPROP_PRICE2, Close(50));

  // set text props
  ObjectSet(names[8], OBJPROP_TIME1, Time(10));
  ObjectSet(names[8], OBJPROP_PRICE1, Close(0));
}
