# 依据价格相关性的统计数据过滤信号

7 三月 2014, 07:21
0
1 438

### 如何开始

```//+------------------------------------------------------------------+
//|                                                     explorer.mq5 |
//|                        Copyright 2011, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property version   "1.00"
//---Variables---
double profit_percent,open_cur,close_cur;
double open[],close[];
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
return(0);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
/* Calculate percent of closures with increase from the total number */
Print("Percent of closures with increase ",profit_percent,"%");   // Enter data to the Journal
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
//---find out the time---
MqlDateTime time;                        // Create a structure to store time
TimeToStruct(TimeCurrent(),time);         // Structuring the data
day_cur=time.day_of_week;              // Receive the value of the current day
hour_cur=time.hour;                    // Receive the current hour
min_cur=time.min;                      // Receive the current minute
//---Find out the prices---
CopyOpen(NULL,0,0,4,open);ArraySetAsSeries(open,true);
CopyClose(NULL,0,0,4,close);ArraySetAsSeries(close,true);

if(close[1]<open[1]/*&&close[2]<open[2]&&close[3]<open[3]*/ && count==0) // If it closed with a loss
{
open_cur=open[0];                   // Remember open price of the current bar
count=1;
}
if(open_cur!=open[0] && count==1)      // The current bar has closed
{
close_cur=close[1];                 // Remember the close price of the formed bar
count=0;
if(close_cur>=open_cur)profit_trades+=1;  // If the close price is higher than open,
else loss_trades+=1;                      // +1 to closures with profit, otherwise +1 to closures with loss
}
}
//+------------------------------------------------------------------+
```

（第一根柱显示整个时间内的数据，第二根柱显示只一天收盘价下跌的数据，第三根柱显示连续两天收盘价下跌的数据，第四根柱显示连续三天收盘价下跌的数据）

### 前进的另一步

1. 一个自动交易系统，至少显示去年的正结果。
2. 某些确定在价格行为中存在相关性的有趣例子。

TDW（交易日）策略。它允许我们查看会发生什么事情，如果在一个星期的某些交易日中我们只买入，并且在其他交易日仅建立短仓的话。之后，我们可以假定某一天内的价格很大可能比其他交易日内的价格上涨得多。这种现象的原因是什么？地理政治情形、宏观经济统计？或者如 A. Elder 在其著作中所写的，星期一和星期二是停止操作日，星期四和星期五是专业人士的时间？让我们试着来理解。

1. 如果快速移动平均线从下往上穿越慢速移动平均线，并且 MACD 直方图低于零线，则买入
2. 如果快速移动平均线从上往下穿越慢速移动平均线，并且 MACD 高于零线，则卖出

```//+------------------------------------------------------------------+
//|                                                       moving.mqh |
//|                        Copyright 2011, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Класс my_expert                                                  |
//+------------------------------------------------------------------+
class my_expert
{                                                  // Creating a class
// Closed class members
private:
int               ma_red_per,ma_yel_per;          // Periods of MAs
int               ma_red_han,ma_yel_han,macd_han; // Handles
double            sl,ts;                          // Stop orders
double            lots;                           // Lot
double            MA_RED[],MA_YEL[],MACD[];       // Arrays for the indicator values
MqlTradeResult    result;                          // Structure of a server response
// Open class members
public:
void              ma_expert();                                   // Constructor
void get_lot(double lot){lots=lot;}                               // Receiving a lot
void get_periods(int red,int yel){ma_red_per=red;ma_yel_per=yel;} // Receiving the periods of MAs
void get_stops(double SL,double TS){sl=SL;ts=TS;}                  // Receiving the values of stops
void              init();                                         // Receiving the indicator values
bool              check_for_sell();                               // Checking for sell
void              open_sell();                                    // Open sell
void              position_modify();                              // Position modification
};
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
/* Function definition */
//---Constructor---
void my_expert::ma_expert(void)
{
//--- Reset the values of variables
ZeroMemory(ma_red_han);
ZeroMemory(ma_yel_han);
ZeroMemory(macd_han);
}
//---The function for receiving the indicator values---
void  my_expert::init(void)
{
ma_red_han=iMA(_Symbol,_Period,ma_red_per,0,MODE_EMA,PRICE_CLOSE); // Handle of the slow MA
ma_yel_han=iMA(_Symbol,_Period,ma_yel_per,0,MODE_EMA,PRICE_CLOSE); // Handle of the fast MA
macd_han=iMACD(_Symbol,_Period,12,26,9,PRICE_CLOSE);               // Handle of MACDaka
//---Copy data into arrays and set indexing like in a time-series---
CopyBuffer(ma_red_han,0,0,4,MA_RED);
CopyBuffer(ma_yel_han,0,0,4,MA_YEL);
CopyBuffer(macd_han,0,0,2,MACD);
ArraySetAsSeries(MA_RED,true);
ArraySetAsSeries(MA_YEL,true);
ArraySetAsSeries(MACD,true);
}
//---Function to check conditions to open buy---
{
init();  //Receive values of indicator buffers
/* If the fast MA has crossed the slow MA from bottom up between 2nd and 3rd bars,
and there was no crossing back. MACD-hist is below zero */
if(MA_RED[3]>MA_YEL[3] && MA_RED[1]<MA_YEL[1] && MA_RED[0]<MA_YEL[0] && MACD[1]<0)
{
return(true);
}
return(false);
}
//----Function to check conditions to open sell---
bool my_expert::check_for_sell(void)
{
init();  //Receive values of indicator buffers
/* If the fast MA has crossed the slow MA from up downwards between 2nd and 3rd bars,
and there was no crossing back. MACD-hist is above zero */
if(MA_RED[3]<MA_YEL[3] && MA_RED[1]>MA_YEL[1] && MA_RED[0]>MA_YEL[0] && MACD[1]>0)
{
return(true);
}
return(false);
}
{
request.symbol=_Symbol;
request.volume=lots;
request.sl=request.price-sl*_Point;
request.tp=0;
request.deviation=10;
request.type_filling=ORDER_FILLING_FOK;
OrderSend(request,result);
return;
}
//---Open sell---
/* Form a standard trade request to sell */
void my_expert::open_sell(void)
{
request.symbol=_Symbol;
request.volume=lots;
request.price=SymbolInfoDouble(Symbol(),SYMBOL_BID);
request.sl=request.price+sl*_Point;
request.tp=0;
request.deviation=10;
request.type=ORDER_TYPE_SELL;
request.type_filling=ORDER_FILLING_FOK;
OrderSend(request,result);
return;
}
//---Position modification---
void my_expert::position_modify(void)
{
if(PositionGetSymbol(0)==_Symbol)
{     //If a position is for our symbol
request.symbol=_Symbol;
request.deviation=10;
{
/* if distance from price to stop loss is more than trailing stop
and the new stop loss is not less than the previous one */
if(SymbolInfoDouble(Symbol(),SYMBOL_BID)-PositionGetDouble(POSITION_SL)>_Point*ts)
{
if(PositionGetDouble(POSITION_SL)<SymbolInfoDouble(Symbol(),SYMBOL_BID)-_Point*ts)
{
request.sl=SymbolInfoDouble(Symbol(),SYMBOL_BID)-_Point*ts;
request.tp=PositionGetDouble(POSITION_TP);
OrderSend(request,result);
}
}
}
//---If it is a sell position---
else if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
{
/*  if distance from price to stop loss is more than the trailing stop value
and the new stop loss is not above the previous one. Or the stop loss from the moment of opening is equal to zero */
{
(PositionGetDouble(POSITION_SL)==0))
{
request.tp=PositionGetDouble(POSITION_TP);
OrderSend(request,result);
}
}
}
}
}
//+------------------------------------------------------------------```

《利用 MQL5 面向对象编程法编写 EA 交易》一文的作者致敬。没有它我将什么事也做不了！我向不是非常精通这个讨厌的、但是功能异常强大的面向对象编程的每一个人推荐阅读该文。

```//+------------------------------------------------------------------+
//|                                                       Moving.mq5 |
//|                        Copyright 2011, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property version   "1.00"
//---Include a file with the class---
#include <moving.mqh>
//---External Variables---
input int MA_RED_PERIOD=7; // The period of a slow MA
input int MA_YEL_PERIOD=2; // The period of a fast MA
input int STOP_LOSS=800;   // Stop loss
input int TRAL_STOP=800;   // Trailing stop
input double LOTS=0.1;     // Lot
//---Create an object---
my_expert expert;
//---Initialize the MqlDataTime structure---
MqlDateTime time;
int day_of_week;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
//---Initialize the EA
expert.get_periods(MA_RED_PERIOD,MA_YEL_PERIOD);   // Set the MA periods
expert.get_lot(LOTS);                              // Set the lot
expert.get_stops(STOP_LOSS,TRAL_STOP);             // Set stop orders
return(0);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
TimeToStruct(TimeCurrent(),time);
day_of_week=time.day_of_week;
if(PositionsTotal()<1)
{
else if(day_of_week==1 && expert.check_for_sell()==true){expert.open_sell();}
}
else expert.position_modify();
}
//+------------------------------------------------------------------+
```

```if(PositionsTotal()<1){
else if(day_of_week==1&&expert.check_for_sell()==true){expert.open_sell();}}
else expert.position_modify();```

### 总结

explorer.mq5 (2.84 KB)
moving.mq5 (2.28 KB)
moving.mqh (6.98 KB)
Box-Cox 变换