C++ - Cannot get Indicator Value from EMA to use in strategy

How to create strategies and indicators
Message
Author
Mickatron
Posts: 10
Joined: Sat May 30, 2015 8:07 pm

C++ - Cannot get Indicator Value from EMA to use in strategy

#1 Postby Mickatron » Sun May 31, 2015 4:05 am

Hi,

I've spent much of the day learning Forex Tester C++. I'm a web dev but have never used C++ or Delphi, Delphi seemed to have much more support but I'm unable to find a free version.

Anyway I've been creating an EMA cross strategy just to get my head around things, though I cannot seem to get indicator values. They just keep printing 0.000000.

Can anyone see anything wrong with the below code which maybe causing this issue?

Code: Select all

#include <windows.h>
#include "StrategyInterfaceUnit.h"
#include "TechnicalFunctions.h"


/* ----------------------------- *\
   Variable declarations
\* ----------------------------- */

/* Parameter  Vars
---------------------------------*/

int     Timeframe = PERIOD_H4;
char*   Currency = "EURUSD";
//Trade Vars
int      LotSize = 1;
bool    UseLimit = false;
int     ProfitLimit = 60;
bool    UseStopLoss = false;
int     StopLoss = 30;

// Indicator Vars
int      MAFast = 30;
int    MASlow = 10;

/* Strategy Code Vars
--------------------------------- */
int IndMAFast;
int IndMASlow;

bool isFastMAAbove = false;
bool isFirst = false;

EXPORT void __stdcall InitStrategy()
{
   StrategyShortName("TestStrategy");
   StrategyDescription("An EMA Cross Strategy written in C++");


   /* Strategy Parameters
   --------------------------------- */
   RegOption("D", ot_Integer, &MAFast);
   SetOptionRange("MAFast", 0, 30);
   
   RegOption("MASlow", ot_Integer, &MASlow);
   SetOptionRange("MASlow", 0, 10);

   RegOption("Timeframe", ot_TimeFrame, &Timeframe);


   RegOption("LotSize", ot_Integer, &LotSize);

   RegOption("UseLimit", ot_Boolean, &UseLimit);
   RegOption("ProfitLimit", ot_Integer, &ProfitLimit);

   RegOption("UseStopLoss", ot_Boolean, &UseStopLoss);
   RegOption("StopLoss", ot_Integer, &StopLoss);

}


EXPORT void __stdcall DoneStrategy()
{

}


EXPORT void __stdcall  ResetStrategy()
{

   /* Create Indicators
   --------------------------------- */
   //Create fast MA line
   char buff[1000];

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

   IndMAFast = CreateIndicator(Currency, MAFast, "MovingAverage", buff);
   //IndMASlow = CreateIndicator(Currency, MASlow, "MovingAverage", buff);
   
   // isFirst run flag
   isFirst = true;
}




EXPORT void __stdcall GetSingleTick()
{
   // check our currency
   if (strcmp(Symbol(), Currency) != 0) {
      return;
   };

   // set currency and timeframe
   SetCurrencyAndTimeframe(Currency, Timeframe);

   // check number of bars and EMA period
   if (Bars() < MASlow || Bars() < MAFast) { return; };


   // update the indicator values
   double MAFastCurrentValue = GetIndicatorValue(IndMAFast, 0, 2);
   char pbuff[1000];
   sprintf(pbuff, "%f", MAFastCurrentValue);
   Print(pbuff);
   Print(Symbol());

   //double MASlowCurrentValue = GetIndicatorValue(IndMASlow, 0, 2);
   int OrderHandle;


   if (MAFastCurrentValue) {
      isFastMAAbove = true;
   }
   else {
      isFastMAAbove = false;
   }


   if (MAFastCurrentValue ) {
      //Fast line crosses under slow line
      isFastMAAbove = false;
      
      // TODO: if open Sell positions Close them

      // Open Buy position
      if (SendInstantOrder(Currency, op_Buy, LotSize, 0, 0, "", 0, OrderHandle) == true) {
         //success
         Print("Buy Position Opened");
      }
      else {
         //failure
         Print("Buy Purchase Failed");
      }

   }
   

   if (MAFastCurrentValue ) {
      //Fast line crosses over slow line
      isFastMAAbove = true;

      // TODO: if open Buy positions Close them

      
      // Open Sell position
      if (SendInstantOrder(Currency, op_Sell, LotSize, 0, 0, "", 0, OrderHandle) == true) {
         //success
         Print("Sell Position Opened");
      }
      else {
         //failure
         Print("Sell Purchase Failed");
      }

   }

   // isFirst run flag
   isFirst = false;
}

Mickatron
Posts: 10
Joined: Sat May 30, 2015 8:07 pm

Re: C++ - Cannot get Indicator Value from EMA to use in stra

#2 Postby Mickatron » Sun May 31, 2015 5:25 am

So I worked this out.

Changed the Buff id to 0. I found the Buff id by visiting help > Strategy API > List of Indicators Parameters.

So this;
double MAFastCurrentValue = GetIndicatorValue(IndMAFast, 0, 2);

Became
double MAFastCurrentValue = GetIndicatorValue(IndMAFast, 0, 0);


Return to “FT API”

Who is online

Users browsing this forum: No registered users and 3 guests