extern double TakeProfit = 50;//止损价格
extern double Lots = 0.1;//开仓0.1手
extern double TrailingStop = 30;
extern double MACDOpenLevel=3;//交易所点差
extern double MACDCloseLevel=2;
extern double MATrendPeriod=5;//ma 参数
extern double PircMin=100;//最小保证金比例
extern double UserID=681668;//用户制定ID号码
//+------------------------------------------------------------------+
//|
|
//+------------------------------------------------------------------+
int start()
{
double MacdCurrent,
MacdPrevious, SignalCurrent;
double SignalPrevious,
MaCurrent, MaPrevious;
int cnt, ticket, total;
// initial data checks
// it is important to make sure that the expert works with a
normal
// chart and the user did not make any mistakes setting
external
// variables (Lots, StopLoss, TakeProfit,
// TrailingStop) in our case, we check TakeProfit
// on a chart of less than 100 bars
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}
// to simplify the coding and speed up access
// data are put into internal variables
MacdCurrent=iMACD(NULL,0,5,34,5,PRICE_CLOSE,MODE_MAIN,0);//引用macd指标线当前值
MacdPrevious=iMACD(NULL,0,5,34,5,PRICE_CLOSE,MODE_MAIN,1);//macd指标前一个值
SignalCurrent=iMACD(NULL,0,5,34,5,PRICE_CLOSE,MODE_SIGNAL,0);//信号线当前值
SignalPrevious=iMACD(NULL,0,5,34,5,PRICE_CLOSE,MODE_SIGNAL,1);//信号线前一个值
MaCurrent=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,0);//ma指标的当前值
MaPrevious=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,1);//ma指标的前一个值。
total=OrdersTotal();//返回市场和挂单的总数
if(total<1)
{
// no opened orders identified
if(AccountFreeMargin()<PircMin)
{
Print("没有资金了 = ", AccountFreeMargin());
return(0);
}
// check for long position (BUY) possibility
if(MacdCurrent<0 &&
MacdCurrent>SignalCurrent
&&
MacdPrevious<SignalPrevious
&&
MathAbs(MacdCurrent)>(MACDOpenLevel*Point)
&&
MaCurrent>MaPrevious)//金叉
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"macd智能交易",UserID,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY
order opened : ",OrderOpenPrice());
}
else Print("错误的买操作 : ",GetLastError());
return(0);
}
// check for short position (SELL) possibility
if(MacdCurrent>0 &&
MacdCurrent<SignalCurrent
&&
MacdPrevious>SignalPrevious
&&
MacdCurrent>(MACDOpenLevel*Point)
&&
MaCurrent<MaPrevious)//死叉
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"macd
sample",16384,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL
order opened : ",OrderOpenPrice());
}