您现在的位置:程序化交易>> 外汇现货>> MT5>> MT5知识>>正文内容

MT5编程实例课程(阿郎)---第三讲(均线交叉EA) [MT4]

  • #property copyright "Simon Yau"
    #property link "http://www.fx998.cn"
    #property version "1.00"
    int maFastHandle, maSlowHandle; //定义两个指标操作句柄,用来操作两条均线
    double maFast[], maSlow[]; //定义两个数组,用于保存均线的数值
    //+------------------------------------------------------------------+
    //| Expert initialization function |
    //+------------------------------------------------------------------+
    int OnInit()
    {
    //---
    //在初始化函数OnInit中,分别指定两个操作句柄所对应的均线指标
    maFastHandle = iMA(Symbol(), PERIOD_CURRENT, 5, 0, MODE_SMA, PRICE_CLOSE); //对应5期均线
    maSlowHandle = iMA(Symbol(), PERIOD_CURRENT, 10, 0, MODE_SMA, PRICE_CLOSE); //对应10期均线

    ArraySetAsSeries(maFast, true); //将数组设置为倒序保存,即图表上方向由右向左
    ArraySetAsSeries(maSlow, true); //同上
    //---
    return(0);
    }
    //+------------------------------------------------------------------+
    //| Expert deinitialization function |
    //+------------------------------------------------------------------+
    //+------------------------------------------------------------------+
    //| Expert tick function |
    //+------------------------------------------------------------------+
    void OnTick()
    {
    //---
    MqlTradeRequest request;
    MqlTradeResult result;

    CopyBuffer(maFastHandle, 0, 0, 3, maFast); //将句柄对应的指标数值复制入数组
    CopyBuffer(maSlowHandle, 0, 0, 3, maSlow); //同上

    if (maFast[1]>maSlow[1] && maFast[2]<=maSlow[2]) //数组获得指标的数值后,就可以进行均线交叉的判定了
    {
    request.action = TRADE_ACTION_DEAL;
    request.symbol = Symbol();
    request.volume = 0.1;
    request.price = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
    request.type = ORDER_TYPE_BUY;

    OrderSend(request, result); //下单
    }
    else if (maFast[1]<maSlow[1] && maFast[2]>=maSlow[2])
    {
    request.action = TRADE_ACTION_DEAL;
    request.symbol = Symbol();
    request.volume = 0.1;
    request.price = SymbolInfoDouble(Symbol(), SYMBOL_BID);
    request.type = ORDER_TYPE_SELL;

    OrderSend(request, result);
    }
    }
    作者:阿郎(shawnyau)栽自火线论坛http://www.fx998.cn。
    Lesson3代码.rar (2 K) 下载次数:155

【字体: 】【打印文章】【查看评论

相关文章

    没有相关内容