查看:958回复:9
[MT4-EA]2分钟就可以完成的,基于MACD的策略(给初学者看的一个程序模板)
//+------------------------------------------------------------------+//| |//| Copyright 2013-2015, Channel-Sea Inc. |//| http://www.channel-sea.com |//+------------------------------------------------------------------+// 做多/做空
int
CMD_BUY = 0
;int
CMD_SELL = 1
;// 开仓/关仓
int
CMD_OPEN = 0
;int
CMD_CLOSE = 1
;// 做多开仓/做空开仓/做多关仓/做空关仓
int
CMD_OPENLONG = 1
;int
CMD_OPENSHORT = 2
;int
CMD_CLOSELONG = -1
;int
CMD_CLOSESHORT = -2
;// 不操作
int
CMD_NOTRADE = -99
;// 做多仓位/做空仓位
double
g_dblLongPosCount = 0
;double
g_dblShortPosCount = 0
;string
g__tradeSymbolName = "EURUSD
";// 当且仅当一个投资品的tick信息收到后,一个tick事件才会被触发
void
OnTick(){ TradeStrategy();}// 处理数据过程的主方法
void
TradeStrategy(){ // iLongCommand代表如何处理做多仓位的指令
// iShortCommand代表如何处理做空仓位的指令
// iLongCommand可能是CMD_OPENLONG/CMD_CLOSELONG/CMD_NOTRADE中的一个
// iShortCommand可能是CMD_OPENSHORT/CMD_CLOSESHORT/CMD_NOTRADE中的一个
int
iLongCommand; int
iShortCommand; iLongCommand = DetermineLongCommand(); iShortCommand = DetermineShortCommand(); PerformTradeStrategy( iLongCommand, iShortCommand, 1
, 1
);}// 决定做多仓位控制指令的方法
int
DetermineLongCommand(){ // iLongSignalAdvice代表如何处理做多仓位的信号
// 它不同于iLongCommand
// 指令在信号出现后才被决定
// 它可能是CMD_OPENLONG/CMD_CLOSELONG/CMD_NOTRADE中的一个
int
iLongSignalAdvice; iLongSignalAdvice = ListenToLongAdvisor(); // 做多仓位被打开后,同样方向的仓位将不会被接受
if( (g_dblLongPosCount > 0
) && (iLongSignalAdvice == CMD_OPENLONG
) ) { return CMD_NOTRADE
; } // 如果不存在做多仓位,则不允许做关闭做多仓位的操作
if( (g_dblLongPosCount <= 0
) && (iLongSignalAdvice == CMD_CLOSELONG
) ) { return CMD_NOTRADE
; } return iLongSignalAdvice;}// 决定做空仓位控制指令的方法
int
DetermineShortCommand(){ // iShortSignalAdvice代表如何处理做空仓位的信号
// 它不同于iShortCommand
// 指令在信号出现后才被决定
// iShortSignalAdvice可能是CMD_OPENSHORT/CMD_CLOSESHORT/CMD_NOTRADE中的一个
int
iShortSignalAdvice; iShortSignalAdvice = ListenToShortAdvisor(); // 做空仓位被打开后,同样方向的仓位将不会被接受
if( (g_dblShortPosCount > 0
) && (iShortSignalAdvice == CMD_OPENSHORT
) ) { return CMD_NOTRADE
; } // 如果不存在做空仓位,则不允许做关闭做空仓位的操作
if( (g_dblShortPosCount <= 0
) && (iShortSignalAdvice == CMD_CLOSESHORT
) ) { return CMD_NOTRADE
; } return iShortSignalAdvice;}// 根据指令发送订单的方法
// dblLongPosDiff代表做多开仓订单的手数
// dblShortPosDiff代表做空开仓订单的手数
void
PerformTradeStrategy( int
iLongCommand, int
iShortCommand, double
dblLongPosDiff, double
dblShortPosDiff ){ double
dblLots; if( iLongCommand == CMD_OPENLONG
) { // 做多开仓仓位
g_dblLongPosCount += dblLongPosDiff; dblLots = dblLongPosDiff; OrderSend( g__tradeSymbolName, OP_BUY
, dblLots, Ask
, 0
, 0
, 0
, NULL
, 888888
, 0
, Green
); } else if( iLongCommand == CMD_CLOSELONG
) { // 做多关仓仓位
g_dblLongPosCount -= dblLongPosDiff; dblLots = dblLongPosDiff; OrderSend( g__tradeSymbolName, OP_SELL
, dblLots, Bid
, 0
, 0
, 0
, NULL
, 888888
, 0
, Red
); } if( iShortCommand == CMD_OPENSHORT
) { // 做空开仓仓位
g_dblShortPosCount += dblShortPosDiff; dblLots = dblShortPosDiff; OrderSend( g__tradeSymbolName, OP_SELL
, dblLots, Bid
, 0
, 0
, 0
, NULL
, 888888
, 0
, Red
); } else if( iShortCommand == CMD_CLOSESHORT
) { // 做空关仓仓位
g_dblShortPosCount -= dblShortPosDiff; dblLots = dblShortPosDiff; OrderSend( g__tradeSymbolName, OP_BUY
, dblLots, Ask
, 0
, 0
, 0
, NULL
, 888888
, 0
, Green
); }}// 给出做多信号的方法
int
ListenToLongAdvisor(){ int
iLongSignalAdvice = CMD_NOTRADE
; int
FastEMA_Rule1 = 12; int
SlowEMA_Rule1 = 26; int
SignalSMA_Rule1 = 9; double
Rule1 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule1, SlowEMA_Rule1, SignalSMA_Rule1, PRICE_CLOSE
, MODE_MAIN
, 1); int
FastEMA_Rule2 = 12; int
SlowEMA_Rule2 = 26; int
SignalSMA_Rule2 = 9; double
Rule2 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule2, SlowEMA_Rule2, SignalSMA_Rule2, PRICE_CLOSE
, MODE_SIGNAL
, 1); int
FastEMA_Rule3 = 12; int
SlowEMA_Rule3 = 26; int
SignalSMA_Rule3 = 9; double
Rule3 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule3, SlowEMA_Rule3, SignalSMA_Rule3, PRICE_CLOSE
, MODE_SIGNAL
, 2); int
FastEMA_Rule4 = 12; int
SlowEMA_Rule4 = 26; int
SignalSMA_Rule4 = 9; double
Rule4 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule4, SlowEMA_Rule4, SignalSMA_Rule4, PRICE_CLOSE
, MODE_MAIN
, 2); if(1==1 && Rule1 > Rule2 && Rule3 > Rule4 ){ // 根据交易规则给出做多信号
iLongSignalAdvice = CMD_OPENLONG
; } int
FastEMA_Rule5 = 12; int
SlowEMA_Rule5 = 26; int
SignalSMA_Rule5 = 9; double
Rule5 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule5, SlowEMA_Rule5, SignalSMA_Rule5, PRICE_CLOSE
, MODE_SIGNAL
, 1); int
FastEMA_Rule6 = 12; int
SlowEMA_Rule6 = 26; int
SignalSMA_Rule6 = 9; double
Rule6 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule6, SlowEMA_Rule6, SignalSMA_Rule6, PRICE_CLOSE
, MODE_MAIN
, 1); int
FastEMA_Rule7 = 12; int
SlowEMA_Rule7 = 26; int
SignalSMA_Rule7 = 9; double
Rule7 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule7, SlowEMA_Rule7, SignalSMA_Rule7, PRICE_CLOSE
, MODE_MAIN
, 2); int
FastEMA_Rule8 = 12; int
SlowEMA_Rule8 = 26; int
SignalSMA_Rule8 = 9; double
Rule8 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule8, SlowEMA_Rule8, SignalSMA_Rule8, PRICE_CLOSE
, MODE_SIGNAL
, 2); if(1==1 && Rule5 > Rule6 && Rule7 > Rule8 ){ // 根据交易规则给出做多信号
iLongSignalAdvice = CMD_CLOSELONG
; } return iLongSignalAdvice;}// 给出做空信号的方法
int
ListenToShortAdvisor(){ int
iShortSignalAdvice = CMD_NOTRADE
; int
FastEMA_Rule9 = 12; int
SlowEMA_Rule9 = 26; int
SignalSMA_Rule9 = 9; double
Rule9 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule9, SlowEMA_Rule9, SignalSMA_Rule9, PRICE_CLOSE
, MODE_SIGNAL
, 1); int
FastEMA_Rule10 = 12; int
SlowEMA_Rule10 = 26; int
SignalSMA_Rule10 = 9; double
Rule10 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule10, SlowEMA_Rule10, SignalSMA_Rule10, PRICE_CLOSE
, MODE_MAIN
, 1); int
FastEMA_Rule11 = 12; int
SlowEMA_Rule11 = 26; int
SignalSMA_Rule11 = 9; double
Rule11 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule11, SlowEMA_Rule11, SignalSMA_Rule11, PRICE_CLOSE
, MODE_MAIN
, 2); int
FastEMA_Rule12 = 12; int
SlowEMA_Rule12 = 26; int
SignalSMA_Rule12 = 9; double
Rule12 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule12, SlowEMA_Rule12, SignalSMA_Rule12, PRICE_CLOSE
, MODE_SIGNAL
, 2); if(1==1 && Rule9 > Rule10 && Rule11 > Rule12 ){ // 根据交易规则给出做空信号
iShortSignalAdvice = CMD_OPENSHORT
; } int
FastEMA_Rule13 = 12; int
SlowEMA_Rule13 = 26; int
SignalSMA_Rule13 = 9; double
Rule13 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule13, SlowEMA_Rule13, SignalSMA_Rule13, PRICE_CLOSE
, MODE_MAIN
, 1); int
FastEMA_Rule14 = 12; int
SlowEMA_Rule14 = 26; int
SignalSMA_Rule14 = 9; double
Rule14 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule14, SlowEMA_Rule14, SignalSMA_Rule14, PRICE_CLOSE
, MODE_SIGNAL
, 1); int
FastEMA_Rule15 = 12; int
SlowEMA_Rule15 = 26; int
SignalSMA_Rule15 = 9; double
Rule15 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule15, SlowEMA_Rule15, SignalSMA_Rule15, PRICE_CLOSE
, MODE_SIGNAL
, 2); int
FastEMA_Rule16 = 12; int
SlowEMA_Rule16 = 26; int
SignalSMA_Rule16 = 9; double
Rule16 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule16, SlowEMA_Rule16, SignalSMA_Rule16, PRICE_CLOSE
, MODE_MAIN
, 2); if(1==1 && Rule13 > Rule14 && Rule15 > Rule16 ){ // 根据交易规则给出做空信号
iShortSignalAdvice = CMD_CLOSESHORT
; } return iShortSignalAdvice;}
int
CMD_BUY = 0
;int
CMD_SELL = 1
;// 开仓/关仓
int
CMD_OPEN = 0
;int
CMD_CLOSE = 1
;// 做多开仓/做空开仓/做多关仓/做空关仓
int
CMD_OPENLONG = 1
;int
CMD_OPENSHORT = 2
;int
CMD_CLOSELONG = -1
;int
CMD_CLOSESHORT = -2
;// 不操作
int
CMD_NOTRADE = -99
;// 做多仓位/做空仓位
double
g_dblLongPosCount = 0
;double
g_dblShortPosCount = 0
;string
g__tradeSymbolName = "EURUSD
";// 当且仅当一个投资品的tick信息收到后,一个tick事件才会被触发
void
OnTick(){ TradeStrategy();}// 处理数据过程的主方法
void
TradeStrategy(){ // iLongCommand代表如何处理做多仓位的指令
// iShortCommand代表如何处理做空仓位的指令
// iLongCommand可能是CMD_OPENLONG/CMD_CLOSELONG/CMD_NOTRADE中的一个
// iShortCommand可能是CMD_OPENSHORT/CMD_CLOSESHORT/CMD_NOTRADE中的一个
int
iLongCommand; int
iShortCommand; iLongCommand = DetermineLongCommand(); iShortCommand = DetermineShortCommand(); PerformTradeStrategy( iLongCommand, iShortCommand, 1
, 1
);}// 决定做多仓位控制指令的方法
int
DetermineLongCommand(){ // iLongSignalAdvice代表如何处理做多仓位的信号
// 它不同于iLongCommand
// 指令在信号出现后才被决定
// 它可能是CMD_OPENLONG/CMD_CLOSELONG/CMD_NOTRADE中的一个
int
iLongSignalAdvice; iLongSignalAdvice = ListenToLongAdvisor(); // 做多仓位被打开后,同样方向的仓位将不会被接受
if( (g_dblLongPosCount > 0
) && (iLongSignalAdvice == CMD_OPENLONG
) ) { return CMD_NOTRADE
; } // 如果不存在做多仓位,则不允许做关闭做多仓位的操作
if( (g_dblLongPosCount <= 0
) && (iLongSignalAdvice == CMD_CLOSELONG
) ) { return CMD_NOTRADE
; } return iLongSignalAdvice;}// 决定做空仓位控制指令的方法
int
DetermineShortCommand(){ // iShortSignalAdvice代表如何处理做空仓位的信号
// 它不同于iShortCommand
// 指令在信号出现后才被决定
// iShortSignalAdvice可能是CMD_OPENSHORT/CMD_CLOSESHORT/CMD_NOTRADE中的一个
int
iShortSignalAdvice; iShortSignalAdvice = ListenToShortAdvisor(); // 做空仓位被打开后,同样方向的仓位将不会被接受
if( (g_dblShortPosCount > 0
) && (iShortSignalAdvice == CMD_OPENSHORT
) ) { return CMD_NOTRADE
; } // 如果不存在做空仓位,则不允许做关闭做空仓位的操作
if( (g_dblShortPosCount <= 0
) && (iShortSignalAdvice == CMD_CLOSESHORT
) ) { return CMD_NOTRADE
; } return iShortSignalAdvice;}// 根据指令发送订单的方法
// dblLongPosDiff代表做多开仓订单的手数
// dblShortPosDiff代表做空开仓订单的手数
void
PerformTradeStrategy( int
iLongCommand, int
iShortCommand, double
dblLongPosDiff, double
dblShortPosDiff ){ double
dblLots; if( iLongCommand == CMD_OPENLONG
) { // 做多开仓仓位
g_dblLongPosCount += dblLongPosDiff; dblLots = dblLongPosDiff; OrderSend( g__tradeSymbolName, OP_BUY
, dblLots, Ask
, 0
, 0
, 0
, NULL
, 888888
, 0
, Green
); } else if( iLongCommand == CMD_CLOSELONG
) { // 做多关仓仓位
g_dblLongPosCount -= dblLongPosDiff; dblLots = dblLongPosDiff; OrderSend( g__tradeSymbolName, OP_SELL
, dblLots, Bid
, 0
, 0
, 0
, NULL
, 888888
, 0
, Red
); } if( iShortCommand == CMD_OPENSHORT
) { // 做空开仓仓位
g_dblShortPosCount += dblShortPosDiff; dblLots = dblShortPosDiff; OrderSend( g__tradeSymbolName, OP_SELL
, dblLots, Bid
, 0
, 0
, 0
, NULL
, 888888
, 0
, Red
); } else if( iShortCommand == CMD_CLOSESHORT
) { // 做空关仓仓位
g_dblShortPosCount -= dblShortPosDiff; dblLots = dblShortPosDiff; OrderSend( g__tradeSymbolName, OP_BUY
, dblLots, Ask
, 0
, 0
, 0
, NULL
, 888888
, 0
, Green
); }}// 给出做多信号的方法
int
ListenToLongAdvisor(){ int
iLongSignalAdvice = CMD_NOTRADE
; int
FastEMA_Rule1 = 12; int
SlowEMA_Rule1 = 26; int
SignalSMA_Rule1 = 9; double
Rule1 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule1, SlowEMA_Rule1, SignalSMA_Rule1, PRICE_CLOSE
, MODE_MAIN
, 1); int
FastEMA_Rule2 = 12; int
SlowEMA_Rule2 = 26; int
SignalSMA_Rule2 = 9; double
Rule2 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule2, SlowEMA_Rule2, SignalSMA_Rule2, PRICE_CLOSE
, MODE_SIGNAL
, 1); int
FastEMA_Rule3 = 12; int
SlowEMA_Rule3 = 26; int
SignalSMA_Rule3 = 9; double
Rule3 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule3, SlowEMA_Rule3, SignalSMA_Rule3, PRICE_CLOSE
, MODE_SIGNAL
, 2); int
FastEMA_Rule4 = 12; int
SlowEMA_Rule4 = 26; int
SignalSMA_Rule4 = 9; double
Rule4 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule4, SlowEMA_Rule4, SignalSMA_Rule4, PRICE_CLOSE
, MODE_MAIN
, 2); if(1==1 && Rule1 > Rule2 && Rule3 > Rule4 ){ // 根据交易规则给出做多信号
iLongSignalAdvice = CMD_OPENLONG
; } int
FastEMA_Rule5 = 12; int
SlowEMA_Rule5 = 26; int
SignalSMA_Rule5 = 9; double
Rule5 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule5, SlowEMA_Rule5, SignalSMA_Rule5, PRICE_CLOSE
, MODE_SIGNAL
, 1); int
FastEMA_Rule6 = 12; int
SlowEMA_Rule6 = 26; int
SignalSMA_Rule6 = 9; double
Rule6 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule6, SlowEMA_Rule6, SignalSMA_Rule6, PRICE_CLOSE
, MODE_MAIN
, 1); int
FastEMA_Rule7 = 12; int
SlowEMA_Rule7 = 26; int
SignalSMA_Rule7 = 9; double
Rule7 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule7, SlowEMA_Rule7, SignalSMA_Rule7, PRICE_CLOSE
, MODE_MAIN
, 2); int
FastEMA_Rule8 = 12; int
SlowEMA_Rule8 = 26; int
SignalSMA_Rule8 = 9; double
Rule8 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule8, SlowEMA_Rule8, SignalSMA_Rule8, PRICE_CLOSE
, MODE_SIGNAL
, 2); if(1==1 && Rule5 > Rule6 && Rule7 > Rule8 ){ // 根据交易规则给出做多信号
iLongSignalAdvice = CMD_CLOSELONG
; } return iLongSignalAdvice;}// 给出做空信号的方法
int
ListenToShortAdvisor(){ int
iShortSignalAdvice = CMD_NOTRADE
; int
FastEMA_Rule9 = 12; int
SlowEMA_Rule9 = 26; int
SignalSMA_Rule9 = 9; double
Rule9 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule9, SlowEMA_Rule9, SignalSMA_Rule9, PRICE_CLOSE
, MODE_SIGNAL
, 1); int
FastEMA_Rule10 = 12; int
SlowEMA_Rule10 = 26; int
SignalSMA_Rule10 = 9; double
Rule10 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule10, SlowEMA_Rule10, SignalSMA_Rule10, PRICE_CLOSE
, MODE_MAIN
, 1); int
FastEMA_Rule11 = 12; int
SlowEMA_Rule11 = 26; int
SignalSMA_Rule11 = 9; double
Rule11 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule11, SlowEMA_Rule11, SignalSMA_Rule11, PRICE_CLOSE
, MODE_MAIN
, 2); int
FastEMA_Rule12 = 12; int
SlowEMA_Rule12 = 26; int
SignalSMA_Rule12 = 9; double
Rule12 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule12, SlowEMA_Rule12, SignalSMA_Rule12, PRICE_CLOSE
, MODE_SIGNAL
, 2); if(1==1 && Rule9 > Rule10 && Rule11 > Rule12 ){ // 根据交易规则给出做空信号
iShortSignalAdvice = CMD_OPENSHORT
; } int
FastEMA_Rule13 = 12; int
SlowEMA_Rule13 = 26; int
SignalSMA_Rule13 = 9; double
Rule13 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule13, SlowEMA_Rule13, SignalSMA_Rule13, PRICE_CLOSE
, MODE_MAIN
, 1); int
FastEMA_Rule14 = 12; int
SlowEMA_Rule14 = 26; int
SignalSMA_Rule14 = 9; double
Rule14 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule14, SlowEMA_Rule14, SignalSMA_Rule14, PRICE_CLOSE
, MODE_SIGNAL
, 1); int
FastEMA_Rule15 = 12; int
SlowEMA_Rule15 = 26; int
SignalSMA_Rule15 = 9; double
Rule15 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule15, SlowEMA_Rule15, SignalSMA_Rule15, PRICE_CLOSE
, MODE_SIGNAL
, 2); int
FastEMA_Rule16 = 12; int
SlowEMA_Rule16 = 26; int
SignalSMA_Rule16 = 9; double
Rule16 = iMACD("EURUSD", PERIOD_M15
, FastEMA_Rule16, SlowEMA_Rule16, SignalSMA_Rule16, PRICE_CLOSE
, MODE_MAIN
, 2); if(1==1 && Rule13 > Rule14 && Rule15 > Rule16 ){ // 根据交易规则给出做空信号
iShortSignalAdvice = CMD_CLOSESHORT
; } return iShortSignalAdvice;}
发表于:2016-03-23 01:19只看该作者
2楼
这乱的。。。。。不过还是感谢分享
韬客社区www.talkfx.co
发表于:2016-03-23 01:32只看该作者
3楼
2
这乱的。。。。。不过还是感谢分享
韬客社区www.talkfx.co
发表于:2016-03-23 05:21只看该作者
4楼
看起来有点意思
韬客社区www.talkfx.co
发表于:2016-03-24 12:53只看该作者
5楼
大概是回车键被论坛过滤掉了吧。
谢谢分享框架。
韬客社区www.talkfx.co
6楼
to luwei,wxds : 没有办法,我上传代码的时候,代码是有回车的,不知道为什么回车被删了。
to xinma:谢谢。
to bestea:你说的没错。
8楼
to KYH:谢谢捧场。
韬客社区www.talkfx.co
发表于:2016-08-09 13:49只看该作者
9楼
学习一下谢谢分享
发表于:2016-08-09 15:16只看该作者
10楼
回复下载
韬客社区www.talkfx.co