乐于分享
好东西不私藏

按订单注释定时自动平仓的EA源码分享

按订单注释定时自动平仓的EA源码分享
HI,大家好
今天周六,老规矩上EA,
今天是一款辅助的EA,可以提供定时来平仓的,适合手单用户:
使用的方式也很简单:

1、在创建订单时,订单注释以“定时平仓”开头"

2、接着加一个半角逗号“,”"

3、接着加yyyy-mm-dd hh:ii格式的平仓时间(如“2026-11-09 23:51”)"

完整注释如:“定时平仓,2026-11-09 23:51”,以此固定格式为注释下订单" 

这样,你的单子可以根据你的注释的时间来进行平仓了,这就满足了一些朋友不想点位平仓,想根据时间点来平仓的方式!

源码:

#property link "https://www.mql5.com"#property version "狼人EA微:waihuipai"#property description "定时平仓"#property description "使用方法:"#property description "  1、在创建订单时,订单注释以“定时平仓”开头"#property description "  2、接着加一个半角逗号“,”"#property description "  3、接着加yyyy-mm-dd hh:ii格式的平仓时间(如“2016-11-09 23:51”)"#property description "  完整注释如:“定时平仓,2016-11-09 23:51”,以此固定格式为注释下订单"#property description "加载EA,允许实时交易,保持电脑开启"#property description "提醒:本功能对当前账户所有订单有效"#property strictinput   color TitleColor    = clrYellow;input   color ListColor     = clrKhaki;struct TimeCloseOrders  // 订单平仓订单{    bool    isUse;    string  symbol;    double  open_price;    int     type;    double  sl;    double  tp;    double  close_time;    string  comm;};TimeCloseOrders tco[100];voidadd_tco(TimeCloseOrders &obj){    int tco_len = ArraySize(tco);    for(int i=0; i<tco_len; i++)    {        if(tco[i].isUse==false)        {            tco[i].isUse        = true;            tco[i].symbol       = obj.symbol;            tco[i].open_price   = obj.open_price;            tco[i].type         = obj.type;            tco[i].sl           = obj.sl;            tco[i].tp           = obj.tp;            tco[i].close_time   = obj.close_time;            tco[i].comm         = obj.comm;            break;        }    }}voidtext_out(int idx, string str, string comm, int x, int y, int size, color clr){    string obj = StringFormat("list_%02d", idx);    ObjectCreate    (0, obj, OBJ_LABEL, 000);    ObjectSetInteger(0, obj, OBJPROP_XDISTANCE, x);    ObjectSetInteger(0, obj, OBJPROP_YDISTANCE, y);    ObjectSetString (0, obj, OBJPROP_TEXT, str);    ObjectSetString (0, obj, OBJPROP_FONT, "宋体");    ObjectSetString (0, obj, OBJPROP_TOOLTIP, comm);    ObjectSetInteger(0, obj, OBJPROP_FONTSIZE, size);    ObjectSetInteger(0, obj, OBJPROP_COLOR, clr);}intOnInit(){    string  comm = "";    string  str  = "定时平仓";    string  obj  = "title";    ObjectCreate    (0, obj, OBJ_LABEL, 000);    ObjectSetInteger(0, obj, OBJPROP_XDISTANCE, 15);    ObjectSetInteger(0, obj, OBJPROP_YDISTANCE, 20);    ObjectSetString (0, obj, OBJPROP_TEXT, str);    ObjectSetString (0, obj, OBJPROP_FONT, "黑体");    ObjectSetString (0, obj, OBJPROP_TOOLTIP, comm);    ObjectSetInteger(0, obj, OBJPROP_FONTSIZE, 12);    ObjectSetInteger(0, obj, OBJPROP_COLOR, TitleColor);    return (INIT_SUCCEEDED);}voidOnDeinit(constint reason){    ObjectsDeleteAll();}voidOnTick(){    // 初始化数组    int tco_len = ArraySize(tco);    for(int i=0; i<tco_len; i++)    {        tco[i].isUse        = false;        tco[i].symbol       = "";        tco[i].open_price   = 0;        tco[i].type         = 0;        tco[i].sl           = 0;        tco[i].tp           = 0;        tco[i].close_time   = 0;        tco[i].comm         = "";    }    // 查找定时平仓订单    int total   = OrdersTotal();    for (int i = 0; i < total; i++)    {        if (OrderSelect(i, SELECT_BY_POS) == false)            continue;        string comm = OrderComment();        int pos = StringFind(comm, "定时平仓");        if(pos==0)        {            string      symbol  = OrderSymbol();            int         type    = OrderType();            int         dot     = StringFind(comm, ",");            string      do_time = StringSubstr(comm, dot + 1);            StringReplace(do_time, "-"".");   // 处理日期写法中的横线            datetime    time    = StringToTime(do_time);            TimeCloseOrders obj;            obj.symbol      = symbol;            obj.open_price  = OrderOpenPrice();            obj.type        = type;            obj.sl          = OrderStopLoss();            obj.tp          = OrderTakeProfit();            obj.close_time  = time;            obj.comm        = comm;            add_tco(obj);            datetime this_time  = TimeCurrent();            if(this_time>=time)            {                double  ask      = SymbolInfoDouble(symbol, SYMBOL_ASK);                double  bid      = SymbolInfoDouble(symbol, SYMBOL_BID);                if(type==OP_BUY)                    OrderClose(OrderTicket(), OrderLots(), bid, 3, clrRed);                else if(type==OP_SELL)                    OrderClose(OrderTicket(), OrderLots(), ask, 3, clrRed);            }        }    }    // 显示预平仓列表    for(int i=0; i<tco_len; i++)    {        if(tco[i].isUse==true)        {            string str  = StringFormat("%8s  %3.5f  %s  %3.5f  %3.5f  %s",                                    tco[i].symbol,                                    tco[i].open_price,                                    (tco[i].type==OP_SELL ? "SELL" : "BUYY"),                                    tco[i].sl,                                    tco[i].tp,                                    TimeToStr(tco[i].close_time, TIME_DATE|TIME_MINUTES));            text_out(i+100, str, tco[i].comm, 2050 + i*209, ListColor);        }        else            break;    }}