ForexTester指标-MA Cross

ForexTester指标,均线交叉,2条均线集成在一起,不用分别添加了。

C++工程见附件。

 

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

 

主要代码:

 

 

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

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

int FastMAPeriod;
int SlowMAPeriod;
int ApplyToPrice;
TIndexBuffer FastMA, SlowMA;

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("MA Cross");

  SetOutputWindow(ow_ChartWindow);

  AddLevel(0, psDot, 1, cl_GridColor);

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

  RegOption("Fast MA period", ot_Integer, &FastMAPeriod);
  SetOptionRange("Fast MA period", 1, MaxInt);
  FastMAPeriod = 5;

  RegOption("Slow MA period", ot_Integer, &SlowMAPeriod);
  SetOptionRange("Slow MA period", 1, MaxInt);
  SlowMAPeriod = 10;

  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;

  // create buffers
  FastMA = CreateIndexBuffer();
  SlowMA = CreateIndexBuffer();;

  IndicatorBuffers(2);
  SetIndexBuffer(0, FastMA);
  SetIndexStyle(0, ds_Line, psSolid, 2, clYellow);
  SetIndexLabel(0, "FastMA");

  SetIndexBuffer(1, SlowMA);
  SetIndexStyle(1, ds_Line, psSolid, 2, clWhite);
  SetIndexLabel(1, "SlowMA");
}

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 sum;
    int i = 0, dist = 0;

    // FastMA
    dist = Bars()-index;
    if (dist > FastMAPeriod)
        dist = FastMAPeriod;

    sum = 0.0;
    for (i = index; i < index + dist; ++i) {
        sum = sum + GetPrice(i);
    }
    FastMA[index] = sum/dist;

    // SlowMA
    dist = Bars()-index;
    if (dist > SlowMAPeriod)
        dist = SlowMAPeriod;
    
    sum = 0.0;
    for (i = index; i < index + dist; ++i) {
        sum = sum + GetPrice(i);
    }
    SlowMA[index] = sum/dist;
}

 

posted on 2018-02-13 23:04  习相远也  阅读(708)  评论(0)    收藏  举报

导航