论坛全局菜单下方 - TICKMILL 285X70论坛全局菜单下方 - ThinkMarkets285X70论坛全局菜单下方 - 荔枝返现285X70论坛全局菜单下方 -  icmarkets285X70
查看:952回复:9
ChannelSea
注册时间2016-03-03
[MT4-EA]2分钟就可以完成的,基于MACD的策略(给初学者看的一个程序模板)
楼主发表于:2016-03-15 13:11只看该作者倒序浏览
1楼 电梯直达
电梯直达
//+------------------------------------------------------------------+//| |//| 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;}
TK29帖子1楼右侧xm竖版广告90-240
个性签名

韬客社区www.talkfx.co

广告
TK30+TK31帖子一樓廣告
TK30+TK31帖子一樓廣告
luwei
注册时间2016-03-21
发表于:2016-03-23 01:19只看该作者
2楼
这乱的。。。。。不过还是感谢分享
个性签名

韬客社区www.talkfx.co

wxds
注册时间2012-11-26
发表于:2016-03-23 01:32只看该作者
3楼
2 这乱的。。。。。不过还是感谢分享
xinma
注册时间2016-03-19
发表于:2016-03-23 05:21只看该作者
4楼
看起来有点意思
个性签名

韬客社区www.talkfx.co

广告
论坛谏言--外汇交易不应是你投资的全部,交易外汇也不应是你生活的全部
bestea
注册时间2016-03-22
发表于:2016-03-24 12:53只看该作者
5楼
大概是回车键被论坛过滤掉了吧。 谢谢分享框架。
ChannelSea
注册时间2016-03-03
楼主发表于:2016-03-25 12:52只看该作者
6楼
to luwei,wxds : 没有办法,我上传代码的时候,代码是有回车的,不知道为什么回车被删了。 to xinma:谢谢。 to bestea:你说的没错。
KYH
注册时间2015-08-24
发表于:2016-03-26 09:32只看该作者
7楼
这乱的。。。。。不过还是感谢分享
个性签名

韬客社区www.talkfx.co

广告
论坛谏言--外汇交易不应是你投资的全部,交易外汇也不应是你生活的全部
ChannelSea
注册时间2016-03-03
pensh
注册时间2016-07-24
发表于:2016-08-09 13:49只看该作者
9楼
学习一下谢谢分享
个性签名

韬客社区www.talkfx.co

广告
论坛谏言--外汇交易不应是你投资的全部,交易外汇也不应是你生活的全部
haichong
注册时间2016-08-08

本站免责声明:

1、本站所有广告及宣传信息均与韬客无关,如需投资请依法自行决定是否投资、斟酌资金安全及交易亏损风险;

2、韬客是独立的、仅为投资者提供交流的平台,网友发布信息不代表韬客的观点与意思表示,所有因网友发布的信息而造成的任何法律后果、风险与责任,均与韬客无关;

3、金融交易存在极高法律风险,未必适合所有投资者,请不要轻信任何高额投资收益的诱导而贸然投资;投资保证金交易导致的损失可能超过您投入的资金和预期。请您考虑自身的投资经验及风险承担能力,进行合法、理性投资;

4、所有投资者的交易帐户应仅限本人使用,不应交由第三方操作,对于任何接受第三方喊单、操盘、理财等操作的投资和交易,由此导致的任何风险、亏损及责任由投资者个人自行承担;

5、韬客不隶属于任何券商平台,亦不受任何第三方控制,韬客不邀约客户投资任何保证金交易,不接触亦不涉及投资者的任何资金及账户信息,不代理任何交易操盘行为,不向客户推荐任何券商平台,亦不存在其他任何推荐行为。投资者应自行选择券商平台,券商平台的任何行为均与韬客无关。投资者注册及使用韬客即表示其接受和认可上述声明,并自行承担法律风险。

版权所有:韬客外汇论坛 www.talkfx.com 联络我们:[email protected]