找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
首页论坛EAHub 外汇论坛
查看: 13|回复: 0

量化交易入门--写第一个量化程序

[复制链接]

4万

主题

54

回帖

4万

积分

管理员

积分
45954
发表于 2026-8-23 05:15:00 | 显示全部楼层 |阅读模式
我们写一个自动交易程序,逻辑如下· 下一个多单· 检查当前盈利情况· 盈利大于1美金止盈平仓,或者亏损大于1美金止损平仓· 退出程序//+------------------------------------------------------------------+//|                                                  QuantSample.mq4 |//|                        Copyright 2020, MetaQuotes Software Corp. |//|                                             https://www.mql5.com |//+------------------------------------------------------------------+#property strict//+------------------------------------------------------------------+//| Expert initialization function                                   |//+------------------------------------------------------------------+int OnInit()  {//---   //---   return(INIT_SUCCEEDED);  }//+------------------------------------------------------------------+//| Expert deinitialization function                                 |//+------------------------------------------------------------------+void OnDeinit(const int reason)  {//---     }//+------------------------------------------------------------------+//| Expert tick function                                             |//+------------------------------------------------------------------+void OnTick()  {//---    int ticket = 0;    if(OrdersTotal() == 0) {//检查当前是否有订单,没有则下单      //send order      ticket = OrderSend(Symbol(), OP_BUY, 0.01, MarketInfo(Symbol(), MODE_ASK), 100, 0.0, 0.0, "comment", 1024, 0,0xFFFFFFFF);    }

    double profit = 0;    for(int i = 0; i < OrdersTotal(); i++){ //计算当前盈亏      if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) break;      profit = OrderProfit();    }

    if(profit > 1.0 || profit < -1.0) { //当前盈亏满足条件则平仓退出      //close order      for (int index = OrdersTotal() - 1 ; index >= 0 ; index = index - 1) {          if(!OrderSelect(index, SELECT_BY_POS, MODE_TRADES)){ //选择订单            PrintFormat(">>> select failed closing trade <%s> at index %d", Symbol(), index);            continue;          }

          bool close_result = true ;          switch(OrderType()) {              case OP_BUY :                    close_result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(), MODE_BID),100,0xFFFFFFFF) ; //关闭订单                  break;              case OP_SELL :                    close_result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),100,0xFFFFFFFF) ;                    break;          }      }      ExpertRemove();//退出EA    }  }//+------------------------------------------------------------------+
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|EAHub外汇论坛

GMT+8, 2026-10-12 14:21 , Processed in 0.105621 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表