普乐智能

查看: 4249|回复: 1

当给多单设置止盈时 空单却无法设置

[复制链接]

338

主题

424

帖子

1670

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1670
发表于 2024-4-18 16:35:58 | 显示全部楼层 |阅读模式
  1. int SetTakeProfitByType(int magicNumber, double takeProfitPrice, int orderType) {
  2. int total = OrdersTotal();
  3. int modifiedCount = 0;

  4. // 如果没有订单,直接返回
  5. if (total == 0) return -1;

  6. // 遍历所有订单
  7. for (int i = total - 1; i >= 0; i--) {
  8.     if (!OrderSelect(i, SELECT_BY_POS)) continue;

  9.     // 检查订单的魔术编号是否匹配,并且是当前交易品种的订单
  10.     if (OrderMagicNumber() == magicNumber && OrderSymbol() == Symbol() && OrderType() == orderType) {
  11.         // 修改订单的止盈价
  12.         if (OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), takeProfitPrice, 0, clrYellow)) {
  13.             Print("Order #", OrderTicket(), " TP modified to ", takeProfitPrice);
  14.             modifiedCount++;
  15.         } else {
  16.             Print("Failed to modify TP for Order #", OrderTicket());
  17.         }
  18.     }
  19. }

  20. // 返回修改的订单数量
  21. return modifiedCount > 0 ? modifiedCount : -1;
  22. }                        double newTakeProfitPrice;

  23. <font color="#ff0000"> if (OrdersTotal() > 0 && OrderSelect(0, SELECT_BY_POS) && OrderMagicNumber() == 编号)</font>
  24. {
  25.     if (OrderType() == OP_SELL)
  26.     {
  27.         newTakeProfitPrice = NormalizeDouble(sellAveragePrice,2)-NormalizeDouble(sellRequiredPriceChange,2)*pointSize;
  28.         SetUniformTakeProfit(编号, newTakeProfitPrice,OP_SELL);
  29.     }
  30.     else if (OrderType() == OP_BUY)
  31.     {
  32.         newTakeProfitPrice = NormalizeDouble(buyAveragePrice,2)+NormalizeDouble(buyRequiredPriceChange,2)*pointSize;
  33.         SetUniformTakeProfit(编号, newTakeProfitPrice,OP_BUY);
  34.     }
  35. }
  36. }   
复制代码
码改进点
  • 对OrderSelect的调用

    • 在第二段代码中,OrderSelect(0, SELECT_BY_POS) 仅选择了第一个订单进行处理。如果只有空单而没有多单是第一个订单,或者反之,则只会处理一个订单。这可能是您遇到设置止盈困难的原因。
  • 遍历所有订单

    • 为了避免上述问题,您应该遍历所有订单,并对符合条件的每个订单进行处理。
  • 代码冗余

    • 您可以将止盈设置的逻辑集中在一个函数中,以避免重复代码,提高代码的可维护性和可读性。


回复

使用道具 举报

338

主题

424

帖子

1670

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1670
 楼主| 发表于 2024-4-18 16:36:25 | 显示全部楼层
  1. // 设置指定类型订单的止盈
  2. int SetUniformTakeProfit(int magicNumber, double takeProfitPrice, int orderType) {
  3.     int total = OrdersTotal();
  4.     int modifiedCount = 0;

  5.     // 如果没有订单,直接返回
  6.     if (total == 0) return -1;

  7.     // 遍历所有订单
  8.     for (int i = total - 1; i >= 0; i--) {
  9.         if (!OrderSelect(i, SELECT_BY_POS)) continue;

  10.         // 检查订单的魔术编号是否匹配,并且是当前交易品种的订单
  11.         if (OrderMagicNumber() == magicNumber && OrderSymbol() == Symbol() && OrderType() == orderType) {
  12.             // 修改订单的止盈价
  13.             if (OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), takeProfitPrice, 0, clrYellow)) {
  14.                 Print("Order #", OrderTicket(), " TP modified to ", takeProfitPrice);
  15.                 modifiedCount++;
  16.             } else {
  17.                 Print("Failed to modify TP for Order #", OrderTicket());
  18.             }
  19.         }
  20.     }

  21.     // 返回修改的订单数量
  22.     return modifiedCount > 0 ? modifiedCount : -1;
  23. }

  24. // 调用止盈设置函数,根据不同订单类型计算新的止盈价
  25. void AdjustTakeProfits(int magicNumber, double sellAveragePrice, double sellRequiredPriceChange, double buyAveragePrice, double buyRequiredPriceChange, double pointSize) {
  26.     double newTakeProfitPrice;

  27.     for (int i = 0; i < OrdersTotal(); i++) {
  28.         if (OrderSelect(i, SELECT_BY_POS) && OrderMagicNumber() == magicNumber) {
  29.             if (OrderType() == OP_SELL) {
  30.                 newTakeProfitPrice = NormalizeDouble(sellAveragePrice - sellRequiredPriceChange * pointSize, 2);
  31.                 SetUniformTakeProfit(magicNumber, newTakeProfitPrice, OP_SELL);
  32.             } else if (OrderType() == OP_BUY) {
  33.                 newTakeProfitPrice = NormalizeDouble(buyAveragePrice + buyRequiredPriceChange * pointSize, 2);
  34.                 SetUniformTakeProfit(magicNumber, newTakeProfitPrice, OP_BUY);
  35.             }
  36.         }
  37.     }
  38. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|11wo.com

Copyright © 2001-2013 Comsenz Inc.Template by Comsenz Inc.All Rights Reserved.

Powered by Discuz!X3.4( 备案号:桂ICP备18000909号-1 )QQ

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