|

楼主 |
发表于 2024-4-18 16:36:25
|
显示全部楼层
- // 设置指定类型订单的止盈
- int SetUniformTakeProfit(int magicNumber, double takeProfitPrice, int orderType) {
- int total = OrdersTotal();
- int modifiedCount = 0;
- // 如果没有订单,直接返回
- if (total == 0) return -1;
- // 遍历所有订单
- for (int i = total - 1; i >= 0; i--) {
- if (!OrderSelect(i, SELECT_BY_POS)) continue;
- // 检查订单的魔术编号是否匹配,并且是当前交易品种的订单
- if (OrderMagicNumber() == magicNumber && OrderSymbol() == Symbol() && OrderType() == orderType) {
- // 修改订单的止盈价
- if (OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), takeProfitPrice, 0, clrYellow)) {
- Print("Order #", OrderTicket(), " TP modified to ", takeProfitPrice);
- modifiedCount++;
- } else {
- Print("Failed to modify TP for Order #", OrderTicket());
- }
- }
- }
- // 返回修改的订单数量
- return modifiedCount > 0 ? modifiedCount : -1;
- }
- // 调用止盈设置函数,根据不同订单类型计算新的止盈价
- void AdjustTakeProfits(int magicNumber, double sellAveragePrice, double sellRequiredPriceChange, double buyAveragePrice, double buyRequiredPriceChange, double pointSize) {
- double newTakeProfitPrice;
- for (int i = 0; i < OrdersTotal(); i++) {
- if (OrderSelect(i, SELECT_BY_POS) && OrderMagicNumber() == magicNumber) {
- if (OrderType() == OP_SELL) {
- newTakeProfitPrice = NormalizeDouble(sellAveragePrice - sellRequiredPriceChange * pointSize, 2);
- SetUniformTakeProfit(magicNumber, newTakeProfitPrice, OP_SELL);
- } else if (OrderType() == OP_BUY) {
- newTakeProfitPrice = NormalizeDouble(buyAveragePrice + buyRequiredPriceChange * pointSize, 2);
- SetUniformTakeProfit(magicNumber, newTakeProfitPrice, OP_BUY);
- }
- }
- }
- }
复制代码 |
|