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

CloseTradesMaxOpenDuration() Closes trades active longer than a duration.

| 发表于 2024-10-10 20:58:15 | 显示全部楼层 |复制链接
function name
  1. //+------------------------------------------------------------------+
  2. <font _mstmutation="1" _msttexthash="566925125" _msthash="403">//| Function to close open trades based on duration                   |
  3. //| input: maxOpenHours - maximum duration for open trades in hours  |
  4. //+------------------------------------------------------------------+
  5. void CloseTradesIfOpenLongerThan(int maxOpenHours) {
  6.     double maxOpenTime = maxOpenHours * 3600; // Convert hours to seconds
  7.     int totalOrders = OrdersTotal(); // Get the total number of open trades
  8.     for (int i = totalOrders - 1; i >= 0; i--) {
  9.         if (OrderSelect(i, SELECT_BY_POS)) { // Select the trade by position
  10.             // Check if the trade is open
  11.             if (OrderType() == OP_BUY || OrderType() == OP_SELL) {
  12.                 // Calculate the duration the trade has been open
  13.                 datetime openTime = OrderOpenTime(); // Get the open time of the trade
  14.                 double elapsedTime = TimeCurrent() - openTime; // Calculate elapsed time since the trade opened
  15.                 // If the elapsed time exceeds the maximum allowed time, close the trade
  16.                 if (elapsedTime > maxOpenTime) {
  17.                     if (OrderType() == OP_BUY) {
  18.                         OrderClose(OrderTicket(), OrderLots(), Bid, 3, clrRed); // Close buy trade
  19.                     } else if (OrderType() == OP_SELL) {
  20.                         OrderClose(OrderTicket(), OrderLots(), Ask, 3, clrRed); // Close sell trade</font>
  21.                     }
  22.                 }
  23.             }
  24.         }
  25.     }
  26. }
  27. //+------------------------------------------------------------------+
复制代码
举报

评论 使用道具

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

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