# 交易货币篮子时可用的形态第二部分

24 四月 2017, 10:15
0
6 490

### 使用移动平均的篮子货币指数

```//+------------------------------------------------------------------+
//|                                                 testDistance.mq5 |
//|                                   2016 MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   2
input color   clr= clrGreen;
input color   clrMA = clrMagenta;
input int maperiod  = 10; //MA 周期数
double ind[],ma[];
//+------------------------------------------------------------------+
//| 自定义指标初始化函数                                                 |
//+------------------------------------------------------------------+
//int h,h1;
int OnInit()
{
//--- 指标缓冲区映射
ArraySetAsSeries(ind,true);
SetIndexBuffer(0,ind);

IndicatorSetString(INDICATOR_SHORTNAME,"testdistance");
IndicatorSetInteger(INDICATOR_DIGITS,2);
PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_LINE);
PlotIndexSetInteger(0,PLOT_LINE_STYLE,STYLE_SOLID);
PlotIndexSetInteger(0,PLOT_LINE_WIDTH,2);
PlotIndexSetInteger(0,PLOT_LINE_COLOR,clr);
PlotIndexSetString(0,PLOT_LABEL,"_tstdistance_");
ArraySetAsSeries(ma,true);
SetIndexBuffer(1,ma);
PlotIndexSetInteger(1, PLOT_DRAW_TYPE, DRAW_LINE           );
PlotIndexSetInteger(1, PLOT_LINE_STYLE, STYLE_SOLID            );
PlotIndexSetInteger(1, PLOT_LINE_WIDTH, 1            );
PlotIndexSetInteger(1, PLOT_LINE_COLOR, clrMA            );
PlotIndexSetString (1, PLOT_LABEL, "_tstdistance_MA" );
//---
return(INIT_SUCCEEDED);
}

bool bDirect[]={false,false,false,false,true,true,true};
int iCount=7;

double GetValue(int shift)
{
double res=1.0,t;
double dBuf[1];
for(int i=0; i<iCount; i++)
{
t=CopyClose(pair[i],PERIOD_CURRENT,shift,1,dBuf);
if(!bDirect[i]) dBuf[0]=1/dBuf[0];
res*=dBuf[0];
}//end for (int i = 0; i < iCount; i++)
return (NormalizeDouble(MathPow (res, 1/(double)iCount), _Digits) );
}
//+------------------------------------------------------------------+
//| 自定义指标迭代函数                                                  |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
{
if(prev_calculated==0 || rates_total>prev_calculated+1)
{
int rt=rates_total;
for(int i=1; i<rt; i++)
{
ind[i]= GetValue(i);
}
rt -= maperiod;
for (int i = 1; i< rt; i++)
{
ma[i] = GetMA(ind, i, maperiod, _Digits);
}
}
else
{
ind[0]= GetValue(0);
ma[0] = GetMA(ind, 0, maperiod, _Digits);
}
//--- 返回 prev_calculated 的值用于下一次调用
return(rates_total);
}
void OnDeinit(const int reason)
{
string text;
switch(reason)
{
case REASON_PROGRAM:
text="指标通过调用 ExpertRemove() 函数终止运行";break;
case REASON_INITFAILED:
text="这个数值表示 OnInit() 处理函数 "+__FILE__+" 返回了非零数值";break;
case REASON_CLOSE:
text="终端已经被关闭"; break;
case REASON_ACCOUNT:
text="账户已经改变";break;
case REASON_CHARTCHANGE:
text="交易品种或者时段已经改变";break;
case REASON_CHARTCLOSE:
text="图表被关闭";break;
case REASON_PARAMETERS:
text="输入参数已经改变";break;
case REASON_RECOMPILE:
text="程序 "+__FILE__+" 被重新编译";break;
case REASON_REMOVE:
text="程序 "+__FILE__+" 被从图表上删除";break;
case REASON_TEMPLATE:
text="图表上应用了新的模板";break;
default:text="其它原因";
}
PrintFormat("%s",text);
}
//+------------------------------------------------------------------+
double GetMA(const double& arr[], int index , int period, int digit) {
double m = 0;
for (int j = 0; j < period; j++)  m += arr[index + j];
m /= period;
return (NormalizeDouble(m,digit));
}
```

```string pair[]={"EURUSD", "EURJPY", "EURCHF", "EURGBP", "EURNZD", "EURCAD", "EURAUD"};
bool bDirect[]={true,true,true,true,true,true,true};
```

• 指数图与移动平均的交叉显示了可能的趋势反转，如果在货币对的第二个货币中也有类似(或者反转)的交叉，那就会利润更高。例如，对于 EURUSD, 如果美元指数向上与移动平均交叉，欧元指数应该向下与移动平均交叉。这种情况的信号就是一个货币走强而同时另一个货币走弱。
• 如果两个货币的交叉点是在同一个方向，就不要进入市场。在那种情况下，很可能是平盘。
• 交叉点应该清晰可见。我们在前面的文章中已经探讨过。

### 使用之字转向(ZigZag)快速分析

```//+------------------------------------------------------------------+
//|                                                 iUniZigZagSW.mq5 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 6
#property indicator_plots   3
//--- 最高点绘图
#property indicator_label1  "High"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrGreen
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- 最低点绘图
#property indicator_label2  "Low"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrGreen
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- 之字转向绘图
#property indicator_label3  "ZigZag"
#property indicator_type3   DRAW_SECTION
#property indicator_color3  clrRed
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1
//--- 方向绘图
#property indicator_label4  "Direction"
#property indicator_type4   DRAW_LINE
#property indicator_style4  STYLE_SOLID
#property indicator_width4  1
//--- LastHighBar(最近的高点柱形) 绘图
#property indicator_label5  "LastHighBar"
#property indicator_type5   DRAW_LINE
#property indicator_style5  STYLE_SOLID
#property indicator_width5  1
//--- LastLowBar(最近的低点柱形) 绘图
#property indicator_label6  "LastLowBar"
#property indicator_type6   DRAW_LINE
#property indicator_style6  STYLE_SOLID
#property indicator_width6  1
#include <ZigZag\CSorceData.mqh>
#include <ZigZag\CZZDirection.mqh>
#include <ZigZag\CZZDraw.mqh>
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
enum EDirection
{
Dir_NBars=0,
Dir_CCI=1
};
//--- 输入参数
input EDirection  DirSelect=Dir_NBars;
input int                  CCIPeriod   =  14;
input ENUM_APPLIED_PRICE   CCIPrice    =  PRICE_TYPICAL;
input int                  ZZPeriod=14;
input string               name="index-usd-zz.txt";

CZZDirection*dir;
CZZDraw*zz;
//--- 指标缓冲区
double         HighBuffer[];
double         LowBuffer[];
double         ZigZagBuffer[];
double         DirectionBuffer[];
double         LastHighBarBuffer[];
double         LastLowBarBuffer[];
//+------------------------------------------------------------------+
//| 自定义指标初始化函数                                                 |
//+------------------------------------------------------------------+
int h;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
{
switch(DirSelect)
{
case Dir_NBars:
dir=new CNBars(ZZPeriod);
break;
case Dir_CCI:
dir=new CCCIDir(CCIPeriod,CCIPrice);
break;
}
if(!dir.CheckHandle())
{
return(INIT_FAILED);
}
zz=new CSimpleDraw();
//--- 指标缓冲区映射
SetIndexBuffer(0,HighBuffer,INDICATOR_DATA);
SetIndexBuffer(1,LowBuffer,INDICATOR_DATA);
SetIndexBuffer(2,ZigZagBuffer,INDICATOR_DATA);
SetIndexBuffer(3,DirectionBuffer,INDICATOR_CALCULATIONS);
SetIndexBuffer(4,LastHighBarBuffer,INDICATOR_CALCULATIONS);
SetIndexBuffer(5,LastLowBarBuffer,INDICATOR_CALCULATIONS);
h=FileOpen(name,FILE_CSV|FILE_WRITE|FILE_ANSI,',');
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
if(CheckPointer(dir)==POINTER_DYNAMIC)
{
delete(dir);
}
if(CheckPointer(zz)==POINTER_DYNAMIC)
{
delete(zz);
}
}
//+------------------------------------------------------------------+
//| 自定义指标迭代函数                                                  |
//+------------------------------------------------------------------+
int ind=0;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const int begin,
const double &price[]
)
{
int start;
if(prev_calculated==0)
{
start=0;
}
else
{
start=prev_calculated-1;
}
for(int i=start;i<rates_total;i++)
{
HighBuffer[i]=price[i];
LowBuffer[i]=price[i];
}
int rv;
rv=dir.Calculate(rates_total,
prev_calculated,
HighBuffer,
LowBuffer,
DirectionBuffer);
if(rv==0)return(0);
zz.Calculate(rates_total,
prev_calculated,
HighBuffer,
LowBuffer,
DirectionBuffer,
LastHighBarBuffer,
LastLowBarBuffer,
ZigZagBuffer);
if(ind<= 10) ind++;
if(ind == 10)
{
double mx=0,mn=1000000;
double lg;
for(int i=0;i<rates_total;i++)
{
if(ZigZagBuffer[i]==0 || ZigZagBuffer[i]==EMPTY_VALUE) continue;
if(ZigZagBuffer[i] > mx) mx = ZigZagBuffer[i];
if(ZigZagBuffer[i] < mn) mn = ZigZagBuffer[i];
}
lg=mx-mn;
PrintFormat("Min index: %.05f Max index: %.05f Length: %.05f",mn,mx,lg);
lg/=100;
double levels[100];
int    count[100];
ArrayInitialize(count,0);
for(int i=1; i<101; i++) levels[i-1]=NormalizeDouble(lg*i,_Digits);
mn=0;
for(int i=0;i<rates_total;i++)
{
if(ZigZagBuffer[i]==0 || ZigZagBuffer[i]==EMPTY_VALUE) continue;
if(mn==0) mn=ZigZagBuffer[i];
else
{
lg=MathAbs(mn-ZigZagBuffer[i]);
for(int j=0; j<100; j++)
{
if(lg<levels[j])
{
count[j]++;
break;
}
}
mn=ZigZagBuffer[i];
}
}
for(int i=0; i<100; i++)
{
PrintFormat("%d level: %.05f count: %d",i,levels[i],count[i]);
FileWrite(h,i,levels[i],count[i]);
}
FileClose(h);
}
return(rates_total);
}
//+------------------------------------------------------------------+

```

0
0.01193 2975
1
0.02387
850
2
0.0358
197
3
0.04773
54
4
0.05967
17

• 当跟随趋势进入市场时，如果把之字转向应用到篮子货币指数的组合指标时，片段的大小超过某个值，我们就要小心。这里片段的大小是指之字转向在价格(Y)轴上的投影。

### 联合使用不同的组合指标

• 在这种情况下什么是最好类型的振荡指标?
• 使用这些方法有什么明显的限制吗?
• 识别出的形态和确认在使用不同类型的振荡指标时正确性如何?

### 文章中使用的程序:

# 名称

描述
1 testIndexMA.mq5 指标 带有移动平均的测试组合美元指数指标。
2 testIndexMA2.mq5 指标 带有移动平均的测试组合欧元指数指标。
3 testIndexZig-Zag1.mq5 指标 可以衡量和记录片段长度的测试之字转向指标。
4 testWPR.mq5 指标 用于欧元的测试组合 WPR 指标。
5 testWPRjpy.mq5 指标 用于美元的测试组合 WPR 指标。
6 testDistance.mq5 指标 画出两个指标差距的测试组合指标。这里的是 testWPR.mq5 和 testWPRjpy.mq5 (EUR 和 USD).

testIndexMA.mq5 (4.82 KB)
testIndexMA2.mq5 (4.96 KB)
ZZdata.zip (37.28 KB)
wpr.zip (45.32 KB)