设为首页 收藏本站 切换语言

mql5源码修改,MT5修改,MQL5代码修改,开仓平仓修改。

| 发表于 2024-10-10 22:04:45 | 显示全部楼层 |复制链接
任务编号:126200 悬赏任务2H币 悬赏任务 : 按雇主需求进行任务后提交稿件,被采纳后即获得佣金。

距截止: 00天00时00分00秒 雇主已托管赏金:6 H币
雇主发布需求24.10.10 雇主托管佣金24.10.10 投稿者投稿 雇主开始选稿 任务完成

任务大厅共需3个合格投稿 | 每稿将获得2 H币 | 每人交稿次数不限 可多次任务

投诉举报 联系Ta 我来承接 已有0 个投稿 | 已采纳0 稿 | 拒绝0 稿 | 还需要3 稿

任务需求:
开仓和平仓不太对,修改一下,谢谢。

入场与平仓

入场与平仓

入离场错误

入离场错误


下面是代码。
  1. //+------------------------------------------------------------------+
  2. //|                                                      
  3. //|                                   Copyright 2024,TradingVUE Ltd. |
  4. //|                                       https://www.tradingvue.net |
  5. //+------------------------------------------------------------------+
  6. #property copyright "Copyright 2024,TradingVUE Ltd."
  7. #property link      "https://www.tradingvue.net"
  8. #property version   "1.00"

  9. #include <Expert\Money\MoneyFixedRisk.mqh>
  10. #include <Trade\SymbolInfo.mqh>
  11. #include <Trade\Trade.mqh>
  12. #include <Trade\PositionInfo.mqh>

  13. CSymbolInfo m_symbol;
  14. CTrade      m_trade;
  15. CPositionInfo   m_position;
  16. CMoneyFixedRisk m_money;

  17. input int      maLength     = 233;              // 均线长度
  18. input int      Inp_src      = PRICE_MEDIAN;     // 均线源值
  19. input double   fib1         = 1.618;            // 裴波那契水平1
  20. input double   fib2         = 2.618;            // 裴波那契水平2
  21. input double   fib3         = 4.236;            // 裴波那契水平3
  22. input int      kcMultiplier = 2 ;               // 肯特纳倍数
  23. input int      kcLength     = 89;               // 肯特纳长度

  24. input double   Inp_lot        = 0.1;              // 下单手数
  25. input int      inp_magic      = 5555;             // 幻数
  26. input bool     CloseAll       = true;                             // 全平
  27. input bool     CloseAllProfit = false;                            // 平盈利
  28. input bool     CloseAllLoss   = false;                            // 平亏损

  29. int handle_ma, handle_tr, handle_atr, handle_rsi;


  30. //+------------------------------------------------------------------+
  31. //| Expert initialization function                                   |
  32. //+------------------------------------------------------------------+
  33. int OnInit()
  34.   {
  35. //---
  36.    m_symbol.Name(Symbol());//初始化商品品种
  37.    
  38.    int dig = 1;
  39.    if(m_symbol.Digits() == 3 || m_symbol.Digits() == 5)
  40.       dig = 10;
  41.    if(m_symbol.Digits() == 2)
  42.       dig = 100;      
  43.    m_money.Init(GetPointer( m_symbol), PERIOD_CURRENT, m_symbol.Point() * dig );

  44.    handle_ma = iMA(m_symbol.Name(), PERIOD_CURRENT, maLength, 0, MODE_EMA, Inp_src);//获取指标
  45.    handle_tr = iATR(m_symbol.Name(), PERIOD_CURRENT, 1);//获取指标
  46.    handle_atr = iMA(m_symbol.Name(), PERIOD_CURRENT, kcLength, 0, MODE_SMA, handle_tr);//获取指标
  47.    handle_rsi = iRSI(m_symbol.Name(), PERIOD_CURRENT,14, Inp_src);
  48.    ChartIndicatorAdd(ChartID(), 0, handle_ma);//画图。 将指标可视化
  49.    
  50.    
  51.    //m_trade.SetExpertMagicNumber(inp_magic);//幻数
  52.    //m_trade.SetMarginMode();
  53.    
  54.    //滑点
  55.    //m_trade.SetTypeFillingBySymbol(m_symbol.Name());
  56.    //m_trade.SetDeviationInPoints(10);   
  57. //---
  58.    return(INIT_SUCCEEDED);
  59.   }
  60. //+------------------------------------------------------------------+
  61. //| Expert deinitialization function                                 |
  62. //+------------------------------------------------------------------+
  63. void OnDeinit(const int reason)
  64.   {
  65. //---
  66.    
  67.   }
  68. //+------------------------------------------------------------------+
  69. //| Expert tick function                                             |
  70. //+------------------------------------------------------------------+
  71. void OnTick()
  72. {  
  73. //---
  74.    SetTime();
  75.    
  76.    
  77.    if(!isNewBar())
  78.    {
  79.       return;
  80.    }  
  81.    
  82.    Refresh();
  83.      
  84.    // 获取指标中的数据--
  85.    double kcAverageTrueRange[], ma[], rsi[];//数组,存放指标数据
  86.    
  87.    //Copy方式 获取数值
  88.    ArraySetAsSeries(kcAverageTrueRange, true);//倒序排列,形成时间序列排序
  89.    ArraySetAsSeries(ma, true);
  90.    ArraySetAsSeries(rsi, true);
  91.    
  92.    //读取指标数据
  93.    int to_copy=2;
  94.    GetArray(handle_atr, 0, 0, to_copy, kcAverageTrueRange);
  95.    GetArray(handle_ma,  0, 0, to_copy, ma);
  96.    GetArray(handle_rsi, 0, 0, to_copy, rsi);
  97.    
  98.    double kcUpper[2],kcLower[2];
  99.    //double fbUpper1[2],fbLower1[2];   
  100.    //double fbUpper2[2],fbLower2[2];
  101.    double fbUpper3[2],fbLower3[2];
  102.    for(int i=0; i<2 ; i++)
  103.    {
  104.       kcUpper[i] = ma[i] + kcMultiplier * kcAverageTrueRange[i];
  105.       kcLower[i] = ma[i] - kcMultiplier * kcAverageTrueRange[i];
  106.       
  107.       // Calculate the Fibonacci Bands
  108.       //fbUpper1[i] = ma[i] + fib1 * (kcUpper[i] - ma[i]);
  109.       //fbUpper2[i] = ma[i] + fib2 * (kcUpper[i] - ma[i]);
  110.       fbUpper3[i] = ma[i] + fib3 * (kcUpper[i] - ma[i]);
  111.       //fbLower1[i] = ma[i] - fib1 * (ma[i] - kcLower[i]);
  112.       //fbLower2[i] = ma[i] - fib2 * (ma[i] - kcLower[i]);
  113.       fbLower3[i] = ma[i] - fib3 * (ma[i] - kcLower[i]);   
  114.       
  115.    }


  116.    

  117.    //开多单
  118.    if(Close(0)<fbLower3[0] && Close(1)>fbLower3[1] && rsi[0]<40)
  119.    {
  120.       m_trade.Buy(Inp_lot);
  121.       //OpenSell(Inp_lot, sl, tp);
  122.    }  
  123.    //开空单
  124.    if(Close(0)>fbUpper3[0] && Close(1)<fbUpper3[1] && rsi[0]>60)
  125.    {
  126.       m_trade.Sell(Inp_lot);
  127.       //OpenBuy(Inp_lot, sl, tp);
  128.    }

  129.    if(PositionsTotal() == 0 )   return;

  130.    // 基线交叉时全部平仓
  131.    if((Close(0)<ma[0] && Close(1)>ma[1]) || (Close(0)>ma[0] && Close(1)<ma[1]))
  132.    {
  133.    for(int i=PositionsTotal()-1;i>=0;i--) // returns the number of current positions
  134.       if(m_position.SelectByIndex(i))     // selects the position by index for further access to its properties
  135.         {
  136.          if(CloseAll) // m_trade.PositionClose(m_position.Symbol());
  137.             ClosePosition(m_position.Symbol()); // close a position by the specified symbo
  138.          else if(CloseAllProfit)
  139.            {
  140.             if(m_position.Commission()+m_position.Swap()+m_position.Profit()>0.0)
  141.                ClosePosition(m_position.Symbol()); // close a position by the specified symbo
  142.            }
  143.          else if(CloseAllLoss)
  144.            {
  145.             if(m_position.Commission()+m_position.Swap()+m_position.Profit()<0.0)
  146.                ClosePosition(m_position.Symbol());  // close a position by the specified symbo
  147.            }
  148.         }
  149.    }   
  150.    
  151.         
  152. }
  153. //+------------------------------------------------------------------+


  154. //刷新函数
  155. bool Refresh(void)
  156. {
  157.    if(!m_symbol.RefreshRates())
  158.    {
  159.       Print("refresh error");
  160.       return (false);
  161.    }
  162.    if(m_symbol.Ask() == 0 || m_symbol.Bid() == 0)
  163.    {
  164.       Print("refresh error");
  165.       return (false);
  166.    }
  167.    
  168.    return(true);
  169. }


  170. //买入函数
  171. bool OpenBuy(double lot, double sl = 0.0, double tp = 0.0)
  172. {
  173.    if (sl > 0 && tp > 0)
  174.    {
  175.       sl = m_symbol.NormalizePrice(sl);//返回规范价格形式
  176.       tp = m_symbol.NormalizePrice(tp);
  177.    }
  178.    
  179.    if(m_trade.Buy(lot, m_symbol.Name(), m_symbol.Ask(), sl, tp))
  180.    {
  181.       return(true);
  182.    }
  183.    else
  184.    {
  185.       Print("Error:", GetLastError());
  186.       return(false);
  187.    }
  188. }
  189. //卖出函数
  190. bool OpenSell(double lot, double sl = 0.0, double tp = 0.0)
  191. {
  192.    if (sl > 0 && tp > 0)
  193.    {
  194.       sl = m_symbol.NormalizePrice(sl);//返回规范价格形式
  195.       tp = m_symbol.NormalizePrice(tp);
  196.    }
  197.    
  198.    if(m_trade.Sell(lot, m_symbol.Name(), m_symbol.Bid(), sl, tp))
  199.    {
  200.       return(true);
  201.    }
  202.    else
  203.    {
  204.       Print("Error:", GetLastError());
  205.       return(false);
  206.    }
  207. }


  208. //获取开盘价格
  209. double Open(const int idx)
  210. {
  211.    return iOpen(m_symbol.Name(), PERIOD_CURRENT, idx);//品种,周期,K线
  212. }
  213. //获取收盘价格
  214. double Close(const int idx)
  215. {
  216.    return iClose(m_symbol.Name(), PERIOD_CURRENT, idx);
  217. }
  218. //获取最低价格
  219. double Low(const int idx)
  220. {
  221.    return iLow(m_symbol.Name(), PERIOD_CURRENT, idx);
  222. }
  223. //获取最高价格
  224. double High(const int idx)
  225. {
  226.    return iHigh(m_symbol.Name(), PERIOD_CURRENT, idx);
  227. }


  228. //检查是不是新K线
  229. bool isNewBar()
  230. {
  231.    static datetime last_time = 0;
  232.    datetime lastbar_time = datetime(SeriesInfoInteger(Symbol(), Period(), SERIES_LASTBAR_DATE));
  233.    if(last_time == 0)
  234.    {
  235.       last_time = lastbar_time;
  236.       return(false);
  237.    }
  238.    if(last_time != lastbar_time)
  239.    {
  240.       last_time = lastbar_time;
  241.       return(true);
  242.    }
  243.    return(false);
  244. }

  245. //显示交易时间
  246. void SetTime(void)
  247. {
  248.    if(ObjectFind(0, "LastTime") < 0)
  249.    {
  250.       //时间初始化
  251.       ObjectCreate(0, "LastTime", OBJ_LABEL, 0, 0, 0);
  252.       //x坐标
  253.       ObjectSetInteger(0, "LastTime", OBJPROP_XDISTANCE, 100);
  254.       //Y坐标
  255.       ObjectSetInteger(0, "LastTime", OBJPROP_YDISTANCE, 2);
  256.       //文本颜色
  257.       ObjectSetInteger(0, "LastTime", OBJPROP_COLOR, clrDimGray);
  258.       ObjectSetInteger(0, "LastTime", OBJPROP_CORNER, CORNER_RIGHT_UPPER);
  259.       ObjectSetInteger(0, "LastTime", OBJPROP_ANCHOR, ANCHOR_RIGHT_UPPER);
  260.       
  261.    }
  262.    
  263.    MqlRates rates[];
  264.    CopyRates(m_symbol.Name(), 0, 0, 1, rates);
  265.    
  266.    int total = int(rates[0].time + PeriodSeconds() - TimeCurrent());
  267.    int s = total % 60;
  268.    int m = (total - s) / 60;
  269.    
  270.    //标签文本
  271.    ObjectSetString(0, "LastTime", OBJPROP_TEXT, " Next Bar:" + IntegerToString(m) + ":" + IntegerToString(s) );
  272.    //字体大小
  273.    ObjectSetInteger(0, "LastTime", OBJPROP_FONTSIZE, 10);
  274.    //禁止鼠标选择
  275.    ObjectSetInteger(0, "LastTime", OBJPROP_SELECTABLE, false);
  276.    //绘制
  277.    ChartRedraw(0);
  278. }



  279. //获取数组
  280. bool GetArray(const int handle, const int buffer, const int start_pos, const int count, double &arr_buffer[])
  281. {
  282.    bool result = false;
  283.    
  284.    if(!ArrayIsDynamic(arr_buffer))
  285.    {
  286.       Print("is not dynamic array!");//非动态数组错误,返回
  287.       return(false);
  288.    }
  289.    
  290.    ArrayFree(arr_buffer);//清空数组
  291.    ResetLastError();//重置最后错误为空
  292.    
  293.    int copied = CopyBuffer(handle, buffer, start_pos, count, arr_buffer);//返回值是复制的数据数,错误时是-1
  294.    
  295.    if(copied != count)
  296.    {
  297.       Print("Failed to copy data from the indicator, error code: %d", GetLastError());
  298.       return(false);
  299.    }
  300.    else
  301.    {
  302.       return(true);
  303.    }
  304.    
  305.    return(result);
  306. }



  307. //+------------------------------------------------------------------+
  308. //| Close selected position                                          |
  309. //+------------------------------------------------------------------+
  310. void ClosePosition(const string symbol)
  311.   {
  312.    if(InitTrade(symbol))
  313.       m_trade.PositionClose(m_position.Ticket()); // close a position by the specified symbo
  314.   }
  315. //+------------------------------------------------------------------+
  316. //| Init trade object                                                |
  317. //+------------------------------------------------------------------+
  318. bool InitTrade(const string symbol)
  319.   {
  320.    if(!m_symbol.Name(symbol)) // sets symbol name
  321.       return(false);
  322. //---
  323.    if(IsFillingTypeAllowed(symbol,SYMBOL_FILLING_FOK))
  324.       m_trade.SetTypeFilling(ORDER_FILLING_FOK);
  325.    else if(IsFillingTypeAllowed(symbol,SYMBOL_FILLING_IOC))
  326.       m_trade.SetTypeFilling(ORDER_FILLING_IOC);
  327.    else
  328.       m_trade.SetTypeFilling(ORDER_FILLING_RETURN);
  329. //---
  330.    return(true);
  331. //---
  332.   }
  333. //+------------------------------------------------------------------+
  334. //| Checks if the specified filling mode is allowed                  |
  335. //+------------------------------------------------------------------+
  336. bool IsFillingTypeAllowed(string symbol,int fill_type)
  337.   {
  338. //--- Obtain the value of the property that describes allowed filling modes
  339.    int filling=(int)SymbolInfoInteger(symbol,SYMBOL_FILLING_MODE);
  340. //--- Return true, if mode fill_type is allowed
  341.    return((filling & fill_type)==fill_type);
  342.   }
  343. //+------------------------------------------------------------------+
复制代码
举报

评论 使用道具

发新帖
EA交易
您需要登录后才可以评论 登录 | 立即注册

简体中文
繁體中文
English(英语)
日本語(日语)
Deutsch(德语)
Русский язык(俄语)
بالعربية(阿拉伯语)
Türkçe(土耳其语)
Português(葡萄牙语)
ภาษาไทย(泰国语)
한어(朝鲜语/韩语)
Français(法语)