1、MT4 自动交易系统实例四实际运行测试合格程序(可以在进行优化)extern double TakeProfit = 950; /盈利 50 点就出场extern double Lots =6; /每单手数 0.1extern double TrailingStop = 370;/移动止损 30extern double MACDOpenLevel=3; /开仓指标之一extern double MACDCloseLevel=2; /平仓指标之一extern double MATrendPeriod=26; /取 26 指数平滑平均线int start()double MacdCurrent,
2、 MacdPrevious, SignalCurrent; /即当前 MACD(红绿柱子),之前一根的MACD,和一般来说参数是 9 的对 EMA 移动平均线double SignalPrevious, MaCurrent, MaPrevious; /前一根 K 线的 signal,现在的 MA,前一根的 MAint cnt, ticket, total; /cnt 是,ticket 是判断开平仓是否成功的,其实就是 flag.total是持仓单数量./ initial data checks/ it is important to make sure that the expert work
3、s 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 barsif(BarsSignalCurrent /尝试多单开仓,盈利目标 50 点(TakeProfit)if(ticket0) /无固定止损.ticket 是 1时成功. /if(OrderSelec
4、t(ticket,SELECT_BY_TICKET,MODE_TRADES) Print(“BUY order opened : “,OrderOpenPrice(); /表示多单已开仓. /else Print(“Error opening BUY order : “,GetLastError(); /ticket=0 时开仓失败,打印错误结果.return(0); / check for short position (SELL) possibilityif(MacdCurrent0 /表示空单已开仓 .else Print(“Error opening SELL order : “,Ge
5、tLastError(); /否则开仓失败,打印错误结果.return(0); return(0);/ it is important to enter the market correctly, / but it is more important to exit it correctly. for(cnt=0;cnt0 / close positionreturn(0); / exit/ check for trailing stopif(TrailingStop0) /如果有移动止损,判定: /if(Bid-OrderOpenPrice()Point*TrailingStop) /如果当
6、前的卖价减去开仓价格大于移动止损 /那么判定:(现在已经上升了一段了)if(OrderStopLoss()SignalCurrent / close positionreturn(0); / exit/ check for trailing stopif(TrailingStop0) if(OrderOpenPrice()-Ask)(Point*TrailingStop)if(OrderStopLoss()(Ask+Point*TrailingStop) | (OrderStopLoss()=0)OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);return(0);return(0);/ the end.