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

CloseTradesMaxOpenDuration() Closes trades active longer than a duration.

[复制链接]

4万

主题

54

回帖

4万

积分

管理员

积分
45954
发表于 2026-8-27 21:21:00 | 显示全部楼层 |阅读模式
function name

//+------------------------------------------------------------------+
<font _mstmutation="1" _msttexthash="566925125" _msthash="403">//| Function to close open trades based on duration                   |
//| input: maxOpenHours - maximum duration for open trades in hours  |
//+------------------------------------------------------------------+
void CloseTradesIfOpenLongerThan(int maxOpenHours) {
    double maxOpenTime = maxOpenHours * 3600; // Convert hours to seconds
    int totalOrders = OrdersTotal(); // Get the total number of open trades

    for (int i = totalOrders - 1; i >= 0; i--) {
        if (OrderSelect(i, SELECT_BY_POS)) { // Select the trade by position
            // Check if the trade is open
            if (OrderType() == OP_BUY || OrderType() == OP_SELL) {
                // Calculate the duration the trade has been open
                datetime openTime = OrderOpenTime(); // Get the open time of the trade
                double elapsedTime = TimeCurrent() - openTime; // Calculate elapsed time since the trade opened

                // If the elapsed time exceeds the maximum allowed time, close the trade
                if (elapsedTime > maxOpenTime) {
                    if (OrderType() == OP_BUY) {
                        OrderClose(OrderTicket(), OrderLots(), Bid, 3, clrRed); // Close buy trade
                    } else if (OrderType() == OP_SELL) {
                        OrderClose(OrderTicket(), OrderLots(), Ask, 3, clrRed); // Close sell trade</font>
                    }
                }
            }
        }
    }
}

//+------------------------------------------------------------------+复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-10 02:19 , Processed in 0.104194 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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