//+------------------------------------------------------------------+
//| 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 2014888888
//--- Inputs
extern double Lots = 0.01 ;
//input double MaximumRisk =0.02;
input double DecreaseFactor=3;
input int MovingPeriod =8;
input int MovingShift =0;
double ls;
//+------------------------------------------------------------------+
//| Calculate open positions |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)//循环检测当前订单队列
{
int buys=0,sells=0;
//---
for(int i=0;i<OrdersTotal();i++)//OrdersTotal()-持仓统计
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)//OrderSymbol() 是指 获得当前选定后的货币对 Symbol() 是指获得当前运行的货币对
{
if(OrderType()==OP_BUY) buys++;
if(OrderType()==OP_SELL) sells++;
}
}
//--- return orders volume
if(buys>0) return(buys);
else return(-sells);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
//开仓=================================================
void CheckForOpen()
{
int res;
//+++++++++++翻倍加仓++++++++++++++++++
int i;
double lot=0.01;
double k=2;
for(i=OrdersHistoryTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS, MODE_HISTORY))
{
if(OrderSymbol()== Symbol() && OrderMagicNumber()==MAGICMA &&(OrderType()==OP_BUY || OrderType()==OP_SELL )) // OrderType()==OP_BUY || OrderType()==OP_SELL
{
if(OrderProfit()<0)
{
lot= NormalizeDouble(OrderLots()*k, 2);
if(lot>0.2)
{
lot=0.01;
}
}
break;
}
}
}
//+++++++++++翻倍加仓++++++++++++++++++
//===绿前
double a,zijin;
double max=0;
for(int k=1;k<20;k++)
{
a=iHigh(Symbol(),0,k);//iClose(Symbol(),0,0)>max
max=MathMax(max,a);
}
double b;
double min=5.55;
for(int c=1;c<20;c++)
{
b=iLow(Symbol(),0,c);//iClose(Symbol(),0,0)>max
min=MathMin(min,b);
}
//============= 根据可用资金的大小, 计算交易数量.
// AccountEquity() AccountFreeMargin()
//============以上是资金管理
//--- sell conditions
if( iClose(Symbol(),0,0)<min )// && lq5<hq5 百分比0.00968=差0.01264=点1270----0.00001=1.311 ======== maq7>maq60 && mah60>mah7 &&
{
res=OrderSend(Symbol(),OP_SELL,lot,Bid,3,0,0,"",MAGICMA,0,Red);//Bid+600*Point //================== Lots
return;
}
//--- buy conditions
if( iClose(Symbol(),0,0)>max )// && lq5>hq5 =========================iOpen(Symbol(),0,0)============ maq60>maq7&&mah7>mah60 &&
{
res=OrderSend(Symbol(),OP_BUY,lot,Ask,3,0,0,"",MAGICMA,0,Blue);//Ask_600*Point
return;
}
//---
}
//+------------------------------------------------------------------+
//平仓策略 |
//+------------------------------------------------------------------+
void CheckForClose()
{
//---
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( (OrderClosePrice()-OrderOpenPrice())>=5 || (OrderClosePrice()-OrderOpenPrice())<=-5 )//========================== OrderProfit()>=40 ||OrderProfit()<=-20
{
if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,White))
Print("OrderClose error ",GetLastError());
}
break;
}
if(OrderType()==OP_SELL)
{
if( (OrderOpenPrice()-OrderClosePrice())>=5 || (OrderOpenPrice()-OrderClosePrice())<=-5)//============================ OrderProfit()>=40 ||OrderProfit()<=-20
{
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();
//---
}
//+------------------------------------------------------------------+
//脚本 OnStart
//指标 OnCalculate
//EA交易 OnTick
// 返回手数计算结果值
// 价差 (绝对点数)手数;
|