ForexTester指标-MACD

ForexTester自带的MACD指标,怎么看都不习惯,可能是国产股软看的多了,习惯转变不过来。

参照ForexTester论坛上的MACD(passcal版本)源码,改了一个C++版本的,工程见附件。


 https://files.cnblogs.com/files/xixiangyuanye/FTI_MACD_Classic.rar

 

主要代码如下:

//---------------------------------------------------------------------------
// Ishimoku indicator
//---------------------------------------------------------------------------

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

int FastEMAPeriod;
int SlowEMAPeriod;
int EMAPeriod;
int ApplyToPrice;
TIndexBuffer histG, histR;
TIndexBuffer FastEMA, SlowEMA, _MACD, EMA;

char log_buf[256];
char *make_log(const char *fmt, ...)
{
     int len;
     va_list ap;

     memset(log_buf, 0, sizeof(log_buf));
  
     va_start(ap, fmt);
     len = vsnprintf_s(log_buf, sizeof(log_buf), fmt, ap);
     va_end(ap);

     return log_buf;
}

//---------------------------------------------------------------------------
// Initialize indicator
//---------------------------------------------------------------------------
EXPORT void __stdcall Init()
{
  // define properties
  IndicatorShortName("MACD classic");

  SetOutputWindow(ow_SeparateWindow);

  AddLevel(0, psDot, 1, cl_GridColor);

  // register options
  AddSeparator("Coded by http://www.cnblogs.com/xixiangyuanye/");
  AddSeparator("MACD");

  RegOption("Fast EMA period", ot_Integer, &FastEMAPeriod);
  SetOptionRange("Fast EMA period", 1, MaxInt);
  FastEMAPeriod = 12;

  RegOption("Slow EMA period", ot_Integer, &SlowEMAPeriod);
  SetOptionRange("Slow EMA period", 1, MaxInt);
  SlowEMAPeriod = 26;

  RegOption("Apply to price", ot_EnumType, &ApplyToPrice);
  AddOptionValue("Apply to price", "Close");
  AddOptionValue("Apply to price", "Open");
  AddOptionValue("Apply to price", "High");
  AddOptionValue("Apply to price", "Low");
  AddOptionValue("Apply to price", "(High + Low)/2");
  AddOptionValue("Apply to price", "(High + Low + Close)/3");
  AddOptionValue("Apply to price", "(High + Low + Close + Close)/4");
  ApplyToPrice = 0;

  AddSeparator("EMA");
  RegOption("EMA period", ot_Integer, &EMAPeriod);
  SetOptionRange("EMA period", 1, MaxInt);
  EMAPeriod = 9;

  // create buffers
  FastEMA = CreateIndexBuffer();
  SlowEMA = CreateIndexBuffer();
  _MACD = CreateIndexBuffer();
  histG = CreateIndexBuffer();
  histR = CreateIndexBuffer();
  EMA = CreateIndexBuffer();

  IndicatorBuffers(4);
  SetIndexBuffer(0, _MACD);
  SetIndexStyle(0, ds_Line, psSolid, 2, clYellow);
  SetIndexLabel(0, "MACD");

  SetIndexBuffer(1, EMA);
  SetIndexStyle(1, ds_Line, psSolid, 1, clWhite);
  SetIndexLabel(1, "Signal Line");

  SetIndexBuffer(2, histR);
  SetIndexLabel(2, "Power Up");
  SetIndexStyle(2, ds_Histogram, psSolid, 1, clRed);

  SetIndexBuffer(3, histG);
  SetIndexStyle(3, ds_Histogram, psSolid, 1, clGreen);
  SetIndexLabel(3, "Power Down");
}

EXPORT void __stdcall OnParamsChange()
{
}


 double GetPrice(int index)
 {
    double result;
    
    switch(ApplyToPrice) {

      case 0:
          result = Close(index);
          break;
      case 1:   
          result = Open(index);
          break;
      case 2:   
          result = High(index);
          break;
      case 3:   
          result = Low(index);
          break;
      case 4:   
          result = (High(index) + Low(index))/2;
          break;
      case 5:   
          result = (High(index) + Low(index) + Close(index))/3;
          break;
      case 6:   
          result = (High(index) + Low(index) + Close(index)*2)/4;
          break;
      default:
          result = 0;
          break;
    }

    return result;
 }

//---------------------------------------------------------------------------
// Calculate requested bar
//---------------------------------------------------------------------------
EXPORT void __stdcall Calculate(int index)
{
  double k;

  // calculate fast EMA
  k = 2.0/((double)FastEMAPeriod + 1.0);

  if (index == (Bars() - 1)) {
    FastEMA[index] = GetPrice(index);
  } else {
    FastEMA[index] = FastEMA[index + 1] + k*(GetPrice(index) - FastEMA[index + 1]);
  }

  // calculate slow EMA
  k = 2.0/((double)SlowEMAPeriod + 1.0);
  if (index == (Bars() - 1)) {
    SlowEMA[index] = GetPrice(index);
  } else {
    SlowEMA[index] = SlowEMA[index + 1] + k*(GetPrice(index) - SlowEMA[index + 1]);
  }

  // calculate MACD
  _MACD[index] = FastEMA[index] - SlowEMA[index];

  // calculate EMA
  k = 2.0/((double)EMAPeriod + 1.0);
  if (index == (Bars() - 1)) {
      EMA[index] = _MACD[index];
  }
  else {
      EMA[index] = EMA[index + 1] + k*(_MACD[index] - EMA[index + 1]);
  }

  // caculate green & red hist
  if ((_MACD[index] > EMA[index])) {
      histR[index] = (_MACD[index] - EMA[index])*2;
      histG[index] = 0;
  } else {
      histG[index] = (0-(EMA[index] - _MACD[index]))*2;
      histR[index] = 0;
  }
}

 

posted on 2018-02-13 17:29  习相远也  阅读(537)  评论(0)    收藏  举报

导航