编程。MT4平台。
//+------------------------------------------------------------------+
//| MACD Sample.mq4 |
//| Copyright ?2005, MetaQuotes Software Corp. | //| http://www..com/doc/964228840.html,/ |
//+------------------------------------------------------------------+
extern double TakeProfit = 50; //盈利50点就出场 extern double Lots = 0.1; //每单手数0.1 extern double TrailingStop = 30;//移动止损30 extern double MACDOpenLevel=3; //开仓指标之一 extern double MACDCloseLevel=2; //平仓指标之一 extern double MATrendPeriod=26; //取26指数平滑平均线
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start() {
double MacdCurrent, MacdPrevious, SignalCurrent; //即当前MACD(红绿柱子),之前一根的MACD,和一般来说参数是9的对EMA移动平均线
double SignalPrevious, MaCurrent, MaPrevious; //前一根K线
的signal,现在的MA,前一根的MA
int cnt, ticket, total; //cnt是,ticket是判断开平仓是否成功的,其实就是flag.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\"); //如果K线小于100根,那么判断不能做.
return(0); }
if(TakeProfit<10) //如果设定的利润小于10点,那么判断程序有问题(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,12,26,9,PRICE_CLOSE,MODE_MAIN,0); //初始化MACD/signal与MA.
MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1); //iMacd最后一个参数是往回移动值,即取上一根K线的数据
SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_
SIGNAL,0);
SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
MaCurrent=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,0);
MaPrevious=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,1);
total=OrdersTotal(); //total定义为orders数量.(持仓单) if(total<1) //如果total小于1,那么进入保证金充足判定. {
// no opened orders identified
if(AccountFreeMargin()<(1000*Lots)) //如果保证金足够(1手1000),那么进入下一个判定:开仓判定.
{
Print(\"We return(0); }
// check for long position (BUY) possibility //开仓判定: if(MacdCurrent<0 && MacdCurrent>SignalCurrent && MacdPrevious
MathAbs(MacdCurrent)>(MACDOpenLevel*Point) 仓.
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,\"macd sample\//尝试多单开仓,盈利目标50点(TakeProfit)
&&
MaCurrent>MaPrevious) //并且MA呈上升趋势,那么就尝试多单开
have
no
money.
Free
Margin
=
\
AccountFreeMargin());//如果不足,打印保证金不足
if(ticket>0) //无固定止损.ticket是1时成功. { //
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print(\"BUY order opened : \表示多单已开仓.
} //
else Print(\"Error opening BUY order : \//ticket=0时开仓失败,打印错误结果.
return(0); }
// check for short position (SELL) possibility
if(MacdCurrent>0 && MacdCurrentSignalPrevious && //如果MACD大于0,并且MACD下破signal,并且Macd的绝对值大于3*1点
MacdCurrent>(MACDOpenLevel*Point) && MaCurrent {
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,\"macd sample\//尝试空单开仓,盈利目标50点,无固定止损
if(ticket>0) {
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print(\"SELL order opened : \表示空单已开仓.
}
else Print(\"Error opening SELL order : \否则开仓失败,打印错误结果.
return(0); }
return(0); }
// it is important to enter the market correctly,
// but it is more important to exit it correctly... for(cnt=0;cnt {
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); //选择第cnt个单子.
if(OrderType()<=OP_SELL && // check for opened position //检验开仓类别
OrderSymbol()==Symbol()) // check for symbol //Symbol - 交易系统称号
{ //
if(OrderType()==OP_BUY) // long position is opened //进入多单检验.
{
// should it be closed?
if(MacdCurrent>0 && MacdCurrentSignalPrevious && //如果Macd高位死叉,出场.
MacdCurrent>(MACDCloseLevel*Point)) {
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
return(0); // exit }
// check for trailing stop
if(TrailingStop>0) //如果有移动止损,判定: { //
if(Bid-OrderOp
enPrice()>Point*TrailingStop) //如果当前的卖价减去开仓价格大于移动止损
{ //那么判定:(现在已经上升了一段了)
if(OrderStopLoss() {
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);//那么更改止损值到现在的卖价-移动止损
return(0); } } } }
else // go to short position {
// should it be closed?
if(MacdCurrent<0 && MacdCurrent>SignalCurrent && MacdPrevious(MACDCloseLevel*Point)) //同理,判断空单是否需要平仓.
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
return(0); // exit }
// check for trailing stop if(TrailingStop>0) {
if((OrderOpenPrice()-Ask)>(Point*TrailingStop)) {
if((OrderStopLoss()>(Ask+Point*TrailingStop)) (OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*Trai
||
lingStop,OrderTakeProfit(),0,Red);
return(0); } } } } } }
return(0); }
// the end.
因篇幅问题不能全部显示,请点此查看更多更全内容