MQL4交易机器人: 实现多空单对冲策略,自动止盈,循环挂单
MQL4交易机器人: 实现多空单对冲策略,自动止盈,循环挂单
以下是经过修改的MQL4代码,实现了以下功能:
- 多空单对冲: 当多单挂单触发时会自动删除空单挂单,反之亦然。* 自动止盈: 当多空总利润达到预设目标后,自动平仓所有订单。* 循环挂单: 在止盈后,重新开始挂单多单和空单,实现自动交易循环。cpp//+------------------------------------------------------------------+//| 7777.mq4 |//| Copyright 2023, MetaQuotes Ltd. |//| https://www.mql5.com |//+------------------------------------------------------------------+#property copyright 'Copyright 2023, MetaQuotes Ltd.'#property link 'https://www.mql5.com'#property version '1.00'#property strict
//+------------------------------------------------------------------+//| Global variables |//+------------------------------------------------------------------+int MAGIC_NUMBER = 92133;double INITIAL_LOTS = 0.5;double REPLENISH_MULTIPLIER = 0.6;int REPLENISH_ATTEMPTS = 5;double PROFIT_TARGET = 40;double MIN_FLUCTUATION = MarketInfo(Symbol(), MODE_POINT) * 100;
//+------------------------------------------------------------------+//| Expert initialization function |//+------------------------------------------------------------------+int OnInit(){ return (INIT_SUCCEEDED);}
//+------------------------------------------------------------------+//| Expert deinitialization function |//+------------------------------------------------------------------+void OnDeinit(const int reason){ // Close all open orders CloseOrders();}
//+------------------------------------------------------------------+//| Expert tick function |//+------------------------------------------------------------------+void OnTick(){ double totalProfit = 0;
// Check if there are any open orders with the specified magic number for (int i = OrdersTotal() - 1; i >= 0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MAGIC_NUMBER) { totalProfit += OrderProfit(); } }
// Check if all positions have been closed if (totalProfit >= PROFIT_TARGET) { CloseOrders(); return; }
// Check if there are any open positions bool hasLongPosition = false; bool hasShortPosition = false;
for (int i = OrdersTotal() - 1; i >= 0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MAGIC_NUMBER) { if (OrderType() == OP_BUY) { hasLongPosition = true; } else if (OrderType() == OP_SELL) { hasShortPosition = true; } } }
// Place pending orders if there are no open positions if (!hasLongPosition && !hasShortPosition) { // Determine the prices for the pending orders double askPrice = NormalizeDouble(MarketInfo(Symbol(), MODE_ASK), Digits); double bidPrice = NormalizeDouble(MarketInfo(Symbol(), MODE_BID), Digits); double buyPrice = askPrice + MIN_FLUCTUATION; double sellPrice = bidPrice - MIN_FLUCTUATION;
// Open buy order if (!hasLongPosition) { if (CountOrders(OP_BUYSTOP) == 0) { // Delete sell order if exists if (CountOrders(OP_SELLSTOP) > 0) { DeleteOrders(OP_SELLSTOP); }
if (OpenBuyOrder(INITIAL_LOTS, buyPrice)) { Print('Opened buy order'); } else { Print('Failed to open buy order'); } } }
// Open sell order if (!hasShortPosition) { if (CountOrders(OP_SELLSTOP) == 0) { // Delete buy order if exists if (CountOrders(OP_BUYSTOP) > 0) { DeleteOrders(OP_BUYSTOP); }
if (OpenSellOrder(INITIAL_LOTS, sellPrice)) { Print('Opened sell order'); } else { Print('Failed to open sell order'); } } } }}
//+------------------------------------------------------------------+//| Open buy order function |//+------------------------------------------------------------------+bool OpenBuyOrder(double lots, double price){ int ticket = OrderSend(Symbol(), OP_BUYSTOP, lots, price, 0, 0, 0, 'Buy Order', MAGIC_NUMBER, 0, Green); if (ticket > 0) { if (OrderSelect(ticket, SELECT_BY_TICKET)) { return true; } else { Print('Failed to select order with ticket ', ticket); } } else { Print('Failed to send buy order'); } return false;}
//+------------------------------------------------------------------+//| Open sell order function |//+------------------------------------------------------------------+bool OpenSellOrder(double lots, double price){ int ticket = OrderSend(Symbol(), OP_SELLSTOP, lots, price, 0, 0, 0, 'Sell Order', MAGIC_NUMBER, 0, Red); if (ticket > 0) { if (OrderSelect(ticket, SELECT_BY_TICKET)) { return true; } else { Print('Failed to select order with ticket ', ticket); } } else { Print('Failed to send sell order'); } return false;}
//+------------------------------------------------------------------+//| Close all orders function |//+------------------------------------------------------------------+void CloseOrders(){ for (int i = OrdersTotal() - 1; i >= 0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MAGIC_NUMBER) { if (!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0)) { Print('Failed to close order with ticket ', OrderTicket()); } } }}
//+------------------------------------------------------------------+//| Delete orders of a specific type |//+------------------------------------------------------------------+void DeleteOrders(int orderType){ for (int i = OrdersTotal() - 1; i >= 0; i--) { if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MAGIC_NUMBER && OrderType() == orderType) { if (!OrderDelete(OrderTicket())) { Print('Failed to delete order with ticket ', OrderTicket()); } }
原文地址: https://www.cveoy.top/t/topic/bYOA 著作权归作者所有。请勿转载和采集!