MACD Sample ---真正的用面向對象的思路來寫EA [MT4]
作者:
MT4 來源:
cxh99.com 發布時間:2012年05月30日 點擊數:
【
收藏到本網的會員中心】
- 我初步看了下系統自帶的MACD Sample EA,這個實例其實用原來MT4的方式實現起來很簡單。
但是我看了系統自帶的代碼:簡直和C++代碼沒什么區別了:
首先應用頭文件或者說是庫文件。這些文件其實都是類庫文件,每個類里面自帶了許多處理方法
接著定義一個類
然后吧一些變量和方法都封裝到類中。
最后void OnTick()程序實體部分簡單的不可想象:
就是定義一個類的實例,然后調用一個類的方法就完了。
以后有時間我好好分析分析。
//+------------------------------------------------------------------+
//| MACD Sample.mq5 |
//| Copyright 2001-2009, MetaQuotes Software Corp. |
//| http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2001-2009, MetaQuotes Software Corp."
#property link "http://www.mql5.com"
#property version "5.04"
#property description "It is important to make sure that the expert works with a normal"
#property description "chart and the user did not make any mistakes setting input"
#property description "variables (Lots, TakeProfit, TrailingStop) in our case,"
#property description "we check TakeProfit on a chart of more than 2*trend_period bars"
//---
#include <Trade\Trade.mqh> 引用頭文件
#include <Trade\SymbolInfo.mqh>
#include <Trade\PositionInfo.mqh>
#include <Trade\AccountInfo.mqh>
#include <Indicators\Indicators.mqh>
//---
input double InpLots =0.1; // Lots
input int InpTakeProfit =50; // Take Profit (in pips)
input int InpTrailingStop =30; // Trailing Stop Level (in pips)
input double InpMACDOpenLevel =0.3; // MACD open level
input double InpMACDCloseLevel=0.2; // MACD close level
input int InpMATrendPeriod =26; // MA trend period
//---
int ExtTimeOut=10; // time out in seconds between trade operations
//+------------------------------------------------------------------+
//| MACD Sample expert class |
//+------------------------------------------------------------------+
class CSampleExpert
{
protected:
double m_adjusted_point; // point value adjusted for 3 or 5 points
CTrade m_trade; // trading object
CSymbolInfo m_symbol; // symbol info object
CPositionInfo m_position; // trade position object
CAccountInfo m_account; // account info wrapper
//--- indicators
CIndicators *m_indicators; // indicator collection to fast recalculations
CiMACD *m_MACD; // MACD indicator object
CiMA *m_EMA; // moving average indicator object
//--- indicator data for processing
double m_macd_current;
double m_macd_previous;
double m_signal_current;
double m_signal_previous;
double m_ema_current;
double m_ema_previous;
public:
CSampleExpert();
~CSampleExpert() { Deinit(); }
bool Init();
void Deinit();
bool Processing();
protected:
bool InitCheckParameters(int digits_adjust);
bool InitIndicators();
bool LongClosed();
bool ShortClosed();
bool LongModified();
bool ShortModified();
bool LongOpened();
bool ShortOpened();
};
//---
CSampleExpert ExtExpert;
//+------------------------------------------------------------------+
//| Constructor |
//+------------------------------------------------------------------+
CSampleExpert::CSampleExpert()
{
//---
m_adjusted_point=0;
m_indicators=NULL;
m_MACD=NULL;
m_EMA =NULL;
//---
m_macd_current =0;
m_macd_previous =0;
m_signal_current =0;
m_signal_previous=0;
m_ema_current =0;
m_ema_previous =0;
//---
}
//+------------------------------------------------------------------+
//| Initialization and checking for input parameters |
//+------------------------------------------------------------------+
bool CSampleExpert::Init()
{
//--- initialize common information
m_symbol.Name(Symbol()); // symbol
m_trade.SetExpertMagicNumber(12345); // magic
//--- tuning for 3 or 5 digits
int digits_adjust=1;
if(m_symbol.Digits()==3 || m_symbol.Digits()==5) digits_adjust=10;
m_adjusted_point=m_symbol.Point()*digits_adjust;
//--- set default deviation for trading in adjusted points
m_trade.SetDeviationInPoints(3*digits_adjust);
//---
if(!InitCheckParameters(digits_adjust)) return(false);
if(!InitIndicators()) return(false);
//--- ok
return(true);
}
//+------------------------------------------------------------------+
//| Checking for input parameters |
//+------------------------------------------------------------------+
bool CSampleExpert::InitCheckParameters(int digits_adjust)
{
//--- initial data checks
if(InpTakeProfit*digits_adjust<m_symbol.StopsLevel())
{
printf("Take Profit must be greater than %d",m_symbol.StopsLevel());
return(false);
}
if(InpTrailingStop*digits_adjust<m_symbol.StopsLevel())
{
printf("Trailing Stop must be greater than %d",m_symbol.StopsLevel());
return(false);
}
//--- check for right lots amount
if(InpLots<m_symbol.LotsMin() || InpLots>m_symbol.LotsMax())
{
printf("Lots amount must be in the range from %f to %f",m_symbol.LotsMin(),m_symbol.LotsMax());
return(false);
}
if(MathAbs(MathMod(InpLots,m_symbol.LotsStep()))>1.0E-15)
{
printf("Lots amount is not corresponding with lot step %f",m_symbol.LotsStep());
return(false);
}
//--- warning
if(InpTakeProfit<=InpTrailingStop)
printf("Warning: Trailing Stop must be greater than Take Profit");
//--- ok
return(true);
}
//+------------------------------------------------------------------+
//| Initialization of the indicators |
//+------------------------------------------------------------------+
bool CSampleExpert::InitIndicators()
{
//--- create indicators collection
if(m_indicators==NULL)
if((m_indicators=new CIndicators)==NULL)
{
printf("Error creating indicators collection");
return(false);
}
//--- create MACD indicator and add it to collection
if(m_MACD==NULL)
if((m_MACD=new CiMACD)==NULL)
{
printf("Error creating MACD indicator");
return(false);
}
if(!m_indicators.Add(m_MACD))
{
printf("Error adding MACD indicator to collection");
return(false);
}
//--- initialize MACD indicator
if(!m_MACD.Create(NULL,0,12,26,9,PRICE_CLOSE))
{
printf("Error MACD indicator init");
return(false);
}
m_MACD.BuffSize(2);
//--- create EMA indicator and add it to collection
if(m_EMA==NULL)
if((m_EMA=new CiMA)==NULL)
{
printf("Error creating EMA indicator");
return(false);
}
if(!m_indicators.Add(m_EMA))
{
printf("Error adding EMA indicator to collection");
return(false);
}
//--- initialize EMA indicator
if(!m_EMA.Create(NULL,0,InpMATrendPeriod,0,MODE_EMA,PRICE_CLOSE))
{
printf("Error EMA indicator init");
return(false);
}
m_EMA.BuffSize(2);
//--- ok
return(true);
}
//+------------------------------------------------------------------+
//| Function for deleting of dynamic objects |
//+------------------------------------------------------------------+
void CSampleExpert::Deinit()
{
//--- delete indicators collection
if(m_indicators!=NULL)
{
delete m_indicators;
m_indicators=NULL;
m_MACD=NULL;
m_EMA =NULL;
}
//---
}
//+------------------------------------------------------------------+
//| Check for long position closing |
//+------------------------------------------------------------------+
bool CSampleExpert::LongClosed()
{
bool res=false;
//--- should it be closed?
if(m_macd_current>0)
if(m_macd_current<m_signal_current && m_macd_previous>m_signal_previous)
if(m_macd_current>InpMACDCloseLevel*m_adjusted_point)
{
//--- close position
if(m_trade.PositionClose(Symbol()))
printf("Long position by %s to be closed",Symbol());
else
printf("Error closing position by %s : '%s'",Symbol(),m_trade.ResultComment());
//--- processed and cannot be modified
res=true;
}
//---
return(res);
}
//+------------------------------------------------------------------+
//| Check for short position closing |
//+------------------------------------------------------------------+
bool CSampleExpert::ShortClosed()
{
bool res=false;
//--- should it be closed?
if(m_macd_current<0)
if(m_macd_current>m_signal_current && m_macd_previous<m_signal_previous)
if(MathAbs(m_macd_current)>InpMACDCloseLevel*m_adjusted_point)
{
//--- close position
if(m_trade.PositionClose(Symbol()))
printf("Short position by %s to be closed",Symbol());
else
printf("Error closing position by %s : '%s'",Symbol(),m_trade.ResultComment());
//--- processed and cannot be modified
res=true;
}
//---
return(res);
}
//+------------------------------------------------------------------+
//| Check for long position modifying |
//+------------------------------------------------------------------+
bool CSampleExpert::LongModified()
{
bool res=false;
//--- check for trailing stop
if(InpTrailingStop>0)
{
if(m_symbol.Bid()-m_position.PriceOpen()>m_adjusted_point*InpTrailingStop)
{
if(m_position.StopLoss()<m_symbol.Bid()-m_adjusted_point*InpTrailingStop || m_position.StopLoss()==0.0)
{
double sl=m_symbol.Bid()-m_adjusted_point*InpTrailingStop;
double tp=m_position.TakeProfit();
//--- modify position
if(m_trade.PositionModify(Symbol(),sl,tp))
printf("Long position by %s to be modified",Symbol());
else
{
printf("Error modifying position by %s : '%s'",Symbol(),m_trade.ResultComment());
printf("Modify parameters : SL=%f,TP=%f",sl,tp);
}
//--- modified and must exit from expert
res=true;
}
}
}
//---
return(res);
}
//+------------------------------------------------------------------+
//| Check for short position modifying |
//+------------------------------------------------------------------+
bool CSampleExpert::ShortModified()
{
bool res=false;
//--- check for trailing stop
if(InpTrailingStop>0)
{
if((m_position.PriceOpen()-m_symbol.Ask())>(m_adjusted_point*InpTrailingStop))
{
if((m_position.StopLoss()>(m_symbol.Ask()+m_adjusted_point*InpTrailingStop)) || m_position.StopLoss()==0.0)
{
double sl=m_symbol.Ask()+m_adjusted_point*InpTrailingStop;
double tp=m_position.TakeProfit();
//--- modify position
if(m_trade.PositionModify(Symbol(),sl,tp))
printf("Short position by %s to be modified",Symbol());
else
{
printf("Error modifying position by %s : '%s'",Symbol(),m_trade.ResultComment());
printf("Modify parameters : SL=%f,TP=%f",sl,tp);
}
//--- modified and must exit from expert
res=true;
}
}
}
//---
return(res);
}
//+------------------------------------------------------------------+
//| Check for long position opening |
//+------------------------------------------------------------------+
bool CSampleExpert::LongOpened()
{
bool res=false;
//--- check for long position (BUY) possibility
if(m_macd_current<0)
if(m_macd_current>m_signal_current && m_macd_previous<m_signal_previous)
if(MathAbs(m_macd_current)>(InpMACDOpenLevel*m_adjusted_point) && m_ema_current>m_ema_previous)
{
//--- check for free money
if(m_account.FreeMarginCheck(Symbol(),0,InpLots)<0.0)
printf("We have no money. Free Margin = %f",m_account.FreeMargin());
else
{
double price=m_symbol.Ask();
double tp =m_symbol.Ask()+InpTakeProfit*m_adjusted_point;
//--- open position
if(m_trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,InpLots,price,0.0,tp))
printf("Position by %s to be opened",Symbol());
else
{
printf("Error opening BUY position by %s : '%s'",Symbol(),m_trade.ResultComment());
printf("Open parameters : price=%f,TP=%f",price,tp);
}
}
//--- in any case we must exit from expert
res=true;
}
//---
return(res);
}
//+------------------------------------------------------------------+
//| Check for short position opening |
//+------------------------------------------------------------------+
bool CSampleExpert::ShortOpened()
{
bool res=false;
//--- check for short position (SELL) possibility
if(m_macd_current>0)
if(m_macd_current<m_signal_current && m_macd_previous>m_signal_previous)
if(m_macd_current>(InpMACDOpenLevel*m_adjusted_point) && m_ema_current<m_ema_previous)
{
//--- check for free money
if(m_account.FreeMarginCheck(Symbol(),0,InpLots)<0.0)
printf("We have no money. Free Margin = %f",m_account.FreeMargin());
else
{
double price=m_symbol.Bid();
double tp =m_symbol.Bid()-InpTakeProfit*m_adjusted_point;
//--- open position
if(m_trade.PositionOpen(Symbol(),ORDER_TYPE_SELL,InpLots,price,0.0,tp))
printf("Position by %s to be opened",Symbol());
else
{
printf("Error opening SELL position by %s : '%s'",Symbol(),m_trade.ResultComment());
printf("Open parameters : price=%f,TP=%f",price,tp);
}
}
//--- in any case we must exit from expert
res=true;
}
//---
return(res);
}
//+------------------------------------------------------------------+
//| main function returns true if any position processed |
//+------------------------------------------------------------------+
bool CSampleExpert::Processing()
{
//--- refresh rates
if(!m_symbol.RefreshRates()) return(false);
//--- refresh indicators
m_indicators.Refresh();
//--- to simplify the coding and speed up access
//--- data are put into internal variables
m_macd_current =m_MACD.Main(0);
m_macd_previous =m_MACD.Main(1);
m_signal_current =m_MACD.Signal(0);
m_signal_previous=m_MACD.Signal(1);
m_ema_current =m_EMA.Main(0);
m_ema_previous =m_EMA.Main(1);
//--- it is important to enter the market correctly,
//--- but it is more important to exit it correctly...
//--- first check if position exists - try to select it
if(m_position.Select(Symbol()))
{
if(m_position.PositionType()==OP_BUY)
{
//--- try to close or modify long position
if(LongClosed()) return(true);
if(LongModified()) return(true);
}
else
{
//--- try to close or modify short position
if(ShortClosed()) return(true);
if(ShortModified()) return(true);
}
}
//--- no opened position identified
else
{
//--- check for long position (BUY) possibility
if(LongOpened()) return(true);
//--- check for short position (SELL) possibility
if(ShortOpened()) return(true);
}
//--- exit without position processing
return(false);
}
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- create all necessary objects
if(!ExtExpert.Init())
{
ExtExpert.Deinit();
return(-1);
}
//--- ok
return(0);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
ExtExpert.Deinit();
}
//+------------------------------------------------------------------+
//| Expert new tick handling function |
//+------------------------------------------------------------------+
void OnTick()
{
static datetime limit_time=0; // last trade processing time + timeout
//--- don't process if timeout
if(TimeCurrent()>=limit_time)
{
//--- check for data
if(Bars(Symbol(),Period())>2*InpMATrendPeriod)
{
//--- change limit time by timeout in seconds if processed
if(ExtExpert.Processing()) limit_time=TimeCurrent()+ExtTimeOut;
}
}
//---
}
//+------------------------------------------------------------------+
中文字幕av无码不卡免费_蜜臀AV无码精品人妻色欲_亚洲成AV人片在线观看无码不卡_无码专区天天躁天天躁在线
两个人的视频www国产精品|
欧美精品一级|
在线观看视频欧美|
国产精品视频久久久|
欧美老女人xx|
免费短视频成人日韩|
久久精品国产77777蜜臀|
亚洲女人天堂av|
亚洲一区二区三区免费视频|
一本色道久久综合亚洲91
|
国产一区二区三区网站
|
国产日韩欧美一区二区|
国产精品青草久久|
国产精品乱码久久久久久|
国产精品白丝黑袜喷水久久久|
欧美日韩免费一区|
欧美日韩免费|
国产精品久久久久一区二区|
欧美性色综合|
国产精品hd|
国产精品视频一二|
国产精品视频1区|
国产日韩欧美一二三区|
国产真实精品久久二三区
|
亚洲国产精品一区在线观看不卡|
亚洲电影免费在线|
亚洲国产精品悠悠久久琪琪|
亚洲人成网站999久久久综合|
91久久黄色|
日韩视频第一页|
国产精品99久久久久久人|
亚洲一区二区三区四区在线观看|
亚洲一区二区三区中文字幕在线|
亚洲免费在线视频一区 二区|
亚洲欧美日韩国产中文|
欧美在线免费视屏|
看欧美日韩国产|
欧美精品成人一区二区在线观看|
欧美日韩视频在线一区二区观看视频|
欧美午夜免费电影|
国产欧美日韩一区二区三区在线|
国产亚洲一区二区三区在线播放|
一区二区视频免费完整版观看|
亚洲国产你懂的|
夜夜嗨网站十八久久|
亚洲自拍三区|
久久精品成人欧美大片古装|
久久只精品国产|
欧美激情一区二区三级高清视频|
欧美视频在线观看免费网址|
国产麻豆日韩|
影音先锋在线一区|
99re这里只有精品6|
午夜宅男欧美|
蜜桃久久av一区|
欧美私人网站|
国产欧美一区二区三区视频|
亚洲电影第1页|
亚洲一区二区三区在线看
|
国产精品一区二区在线|
依依成人综合视频|
中日韩午夜理伦电影免费|
久久久999精品免费|
欧美日韩精选|
精品999在线播放|
亚洲视频网站在线观看|
久久久国产精品亚洲一区
|
国产精品福利网站|
黄色一区二区三区四区|
夜夜狂射影院欧美极品|
久久av免费一区|
欧美日韩久久精品|
影音先锋国产精品|
亚洲综合清纯丝袜自拍|
免费久久久一本精品久久区|
国产精品日韩精品欧美在线|
亚洲激情不卡|
欧美中文字幕视频|
欧美日韩一级黄|
在线观看精品一区|
午夜精品久久久久久久蜜桃app|
欧美成黄导航|
国产日本欧美一区二区三区在线|
亚洲伦理自拍|
久久资源av|
国产麻豆综合|
艳妇臀荡乳欲伦亚洲一区|
久久久夜精品|
国产美女高潮久久白浆|
日韩一本二本av|
久久亚洲精选|
国产免费观看久久|
亚洲精品国产精品乱码不99按摩
|
亚洲激情小视频|
欧美在线影院|
国产精品男女猛烈高潮激情|
亚洲日本电影在线|
久久综合一区二区|
国产亚洲欧美aaaa|
亚洲欧美日韩一区在线|
欧美日韩成人一区|
亚洲国产视频直播|
久久免费国产精品1|
国产精品久久久一区二区|
日韩午夜av在线|
免费成人毛片|
黄色免费成人|
久久久99爱|
国产一区久久久|
欧美一级专区免费大片|
国产精品视频一二三|
亚洲女人小视频在线观看|
欧美日韩一区二区三区在线
|
欧美夜福利tv在线|
欧美系列一区|
中文精品视频|
欧美日韩在线三区|
99re国产精品|
欧美日韩日日骚|
99国产精品久久久久老师|
欧美国产精品va在线观看|
亚洲国产99精品国自产|
乱码第一页成人|
在线精品国精品国产尤物884a|
久久精品女人的天堂av|
国产一区二区三区在线播放免费观看
|
国产精品免费福利|
亚洲一区二区成人|
国产精品久久久久秋霞鲁丝|
亚洲欧美日韩精品久久亚洲区|
国产精品白丝av嫩草影院|
亚洲午夜日本在线观看|
国产精品成人观看视频免费|
亚洲夜晚福利在线观看|
国产精品亚洲片夜色在线|
午夜精品免费视频|
国产亚洲精品aa午夜观看|
久久精品女人天堂|
在线观看国产日韩|
欧美国产免费|
一区二区不卡在线视频 午夜欧美不卡'|
欧美日韩黄色一区二区|
亚洲一级影院|
国产亚洲精品激情久久|
久久久久久九九九九|
亚洲电影在线免费观看|
欧美精品亚洲一区二区在线播放|
一本综合久久|
国产精品人人爽人人做我的可爱|
欧美伊人影院|
在线日韩电影|
欧美日韩精品|
欧美一级在线视频|
尤物网精品视频|
欧美人与禽性xxxxx杂性|
亚洲性色视频|
红桃视频成人|
欧美黄污视频|
亚洲免费在线视频一区 二区|
国产一区二区按摩在线观看|
欧美成人69|
亚洲一区日韩在线|
精品不卡一区|
欧美日韩日日夜夜|
欧美影院在线|
亚洲人成网站精品片在线观看|
国产精品v亚洲精品v日韩精品
|
国产一区二区0|
欧美韩日视频|
亚洲欧美一区二区原创|
激情综合久久|
欧美日韩亚洲另类|
欧美在线播放一区|
最新成人av在线|
国产欧美日韩综合一区在线观看
|
国产亚洲福利一区|
欧美成年人在线观看|
亚洲主播在线观看|
●精品国产综合乱码久久久久|
欧美三级第一页|
久久久久国产精品午夜一区|
99re热精品|
国外成人免费视频|
欧美日韩国产一级|
久久久成人网|
亚洲一区二区三区四区视频|
伊人久久大香线蕉综合热线|
欧美视频免费在线|
久久在线免费|
午夜精品一区二区三区在线视|
最新热久久免费视频|
国产女精品视频网站免费|
欧美理论在线|
久久久噜噜噜|
亚洲欧美另类国产|
亚洲高清二区|
国产一区二区欧美日韩|
国产精品成人播放|
欧美精品高清视频|
久久躁狠狠躁夜夜爽|