实例介绍
【实例截图】
【核心代码】
// ------------------------------------------------------------------
//| Moving Average.mq4 |
//| Copyright 2005-2014, MetaQuotes Software Corp. |
//| http://www.mql4.com |
// ------------------------------------------------------------------
#property copyright "2005-2014, MetaQuotes Software Corp."
#property link "http://www.mql4.com"
#property description "Moving Average sample expert advisor"
#define MAGICMA 20131111
//--- Inputs
input double Lots =0.01;
input double MaximumRisk =0.02;
input double DecreaseFactor=0;
input int MovingPeriod =200;
//input int MovingPeriod1 =60;
//input int MovingPeriod2 =10;
//input int MovingShift =200;
input double TrailingStop =150;
//input double Dian =30;
//input double RsiG =70;
// ------------------------------------------------------------------
//| Calculate open positions |
// ------------------------------------------------------------------
int CalculateCurrentOrders(string symbol)
{
int buys=0,sells=0;
//---
for(int i=0;i<OrdersTotal();i )
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
{
if(OrderType()==OP_BUY) buys ;
if(OrderType()==OP_SELL) sells ;
}
}
//--- return orders volume
if(buys>0) return(buys);
else return(-sells);
}
// ------------------------------------------------------------------
//| Calculate optimal lot size |
// ------------------------------------------------------------------
double LotsOptimized()
{
double lot=Lots;
int orders=HistoryTotal(); // history orders total
int losses=0; // number of losses orders without a break
//--- select lot size
// lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
lot=0.01;
//--- calcuulate number of losses orders without a break
if(DecreaseFactor>0)
{
for(int i=orders-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false)
{
Print("Error in history!");
break;
}
if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL)
continue;
//---
if(OrderProfit()>0) break;
if(OrderProfit()<0) losses ;
}
if(losses>1)
// lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
lot=0.01;
}
//--- return lot size
if(lot<0.1) lot=0.01;
return(lot);
}
// ------------------------------------------------------------------
//| Check 【语法】double iMA( string symbol, int timeframe, int period, int ma_shift, int ma_method, int applied_price, int shift)
/*1、symbol指定货币对,NULL为默认当前货币对
2、timeframe时间周期,0为当前时间周期
3、period平均线周期,通常选7、14、28
4、ma_shift偏移量,默认选0
5、ma_methodMA方法,通常选MODE_EMA
6、applied_price应用价格。默认选收盘价PRICE_CLOSE
7、shift指定柱值,0为当前柱,1为前一个柱,以此类推
【代码】
iMA(NULL,0,7,0,MODE_EMA,PRICE_CLOSE,0)
iMA(NULL,0,14,0,MODE_EMA,PRICE_CLOSE,0)
iMA(NULL,0,28,0,MODE_EMA,PRICE_CLOSE,0)
for open order conditions |
// ------------------------------------------------------------------ */
void CheckForOpen()
{
double ma;
int res;
//--- go trading only for first tiks of new bar
if(Volume[0]>1) return;
//--- get Moving Average
//ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_EMA,PRICE_CLOSE,0);
//ma=iMA(NULL,0,MovingPeriod,0,MODE_EMA,PRICE_CLOSE,i);
ma=iMA(NULL,0,MovingPeriod,0,MODE_EMA,PRICE_CLOSE,0);
// ma=iMA(NULL,0,MovingPeriod1,0,MODE_EMA,PRICE_CLOSE,0);
// ma2=iRSI(NULL,0,60,PRICE_CLOSE,0);
//--- sell conditions
if(Open[1]>ma && Close[1]<ma )
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,0,Bid TrailingStop*Point,0,"",MAGICMA,0,Red);
return;
}
/* else if(Open[0]>ma && Close[0]>ma && Open[1]>ma1 && Close[1]<ma1)
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,0,Bid TrailingStop*Point,0,"",MAGICMA,0,Red);
return;
}*/
//--- buy conditions
if(Open[1]<ma && Close[0]>ma)
// if(ma2>66.5)
{
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Ask,0,Ask-TrailingStop*Point,0,"",MAGICMA,0,Blue);
return;
}
/* else if(Open[0]<ma && Close[0]<ma && Open[1]<ma1 && Close[1]>ma1)
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Bid,0,Bid TrailingStop*Point,0,"",MAGICMA,0,Red);
return;
}*/
//---
}
// ------------------------------------------------------------------
//| Check for close order conditions |
// ------------------------------------------------------------------
void CheckForClose()
{
double ma;
//--- go trading only for first tiks of new bar
if(Volume[0]>1) return;
//--- get Moving Average
ma=iMA(NULL,0,MovingPeriod,0,MODE_EMA,PRICE_CLOSE,0);
// ma1=iMA(NULL,0,MovingPeriod1,0,MODE_EMA,PRICE_CLOSE,0);
//---
for(int i=0;i<OrdersTotal();i )
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
//--- check order type
if(OrderType()==OP_BUY)
{
if(Open[1]>ma && Close[1]<ma)
{
if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,White))
Print("OrderClose error ",GetLastError());
}
break;
}
if(OrderType()==OP_SELL)
{
if(Open[1]<ma && Close[1]>ma )
{
if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,White))
Print("OrderClose error ",GetLastError());
}
break;
}
}
//---
}
// ------------------------------------------------------------------
//| OnTick function |
// ------------------------------------------------------------------
void OnTick()
{
//--- check for history and trading
if(Bars<100 || IsTradeAllowed()==false)
return;
//--- calculate open orders by current symbol
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
else CheckForClose();
//---
}
// ------------------------------------------------------------------
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论