这是我的 EA,追踪止损不起作用。是平台代码的问题吗? [关闭]

问题描述 投票:0回答:0

这是我的 EA,追踪止损不起作用。是平台代码的问题吗?这是我的 MQL5 代码。当我运行代码时,一切都按预期工作,但追踪止损不起作用。我应该做哪些改变?

#property copyright "TEST"

\#property link      "https://www.mql5.com"

\#property version   "1.00"

\#property strict

// Define input parameters

input int     TradeType  = OP_SELL;

input double  LotSize    = 0.01;

input double  StopLoss   = 20;

input double  TakeProfit = 200;

input double  TrailingStop = 5;

// Define global variables

int ticket = 0;

double trailing_stop;

bool is_sell = false;

void OnTick()

{

// Calculate CCI values

double cci1 = iCCI(\_Symbol, PERIOD_M1, 3, PRICE_TYPICAL, 0);

double cci2 = iCCI(\_Symbol, PERIOD_M1, 14, PRICE_TYPICAL, 0);

double cci3 = iCCI(\_Symbol, PERIOD_M1, 21, PRICE_TYPICAL, 0);

double cci4 = iCCI(\_Symbol, PERIOD_M1, 40, PRICE_TYPICAL, 0);

// Check if all CCI values are above/below the threshold

bool all_above_50 = (cci1 \> 50) && (cci2 \> 50) && (cci3 \> 50) && (cci4 \> 50);

bool all_below_minus_50 = (cci1 \< -50) && (cci2 \< -50) && (cci3 \<
-50) && (cci4 \< -50);

// Check if there is an open position

if (ticket == 0)

{

if (all_below_minus_50 && !is_sell)

{

// Place multiple sell orders

for (int i = 0; i \< 10; i++)

{

ticket = OrderSend(\_Symbol, TradeType, LotSize, Bid, 3, Ask + StopLoss \* Point, Ask - TakeProfit \* Point, "Successful = " + "😃");

if (ticket \> 0)

{

// Update global variables

is_sell = true;

trailing_stop = Bid - TrailingStop \* Point; // Initialize trailing_stop

Alert("Sell order executed with ticket number ", ticket);

}

else

{

// Print a message to the Experts tab in the Terminal

Alert("Order Send Error = " + "??", ticket);

}

}

}

else if (all_above_50 && is_sell)

{

// Close the sell position

OrderClose(ticket, LotSize, Bid, 3, Yellow);

// Reset global variables

ticket = 0;

is_sell = false;

Alert("Sell position closed with ticket number ", ticket);

}

}

else

{

// Check the status of the open position

if (OrderSelect(ticket, SELECT_BY_TICKET))

{

if (OrderCloseTime() == 0)

{

// Enforce hard stop

double hard_stop_loss = OrderOpenPrice() + StopLoss \* Point;

// Update the trailing stop

trailing_stop = MathMin(trailing_stop, OrderOpenPrice() - TrailingStop \* Point);

Print("Trailing stop: ", trailing_stop);

if (OrderOpenPrice() - Bid \> TakeProfit \* Point)

{

// Close the sell position

OrderClose(ticket, LotSize, Bid, 3, Yellow);

// Reset global variables

ticket = 0;

is_sell = false;

Alert("Sell position closed with ticket number ", ticket);

}

else if (Bid \<= trailing_stop || Bid \<= hard_stop_loss)

{

// Move the trailing stop

trailing_stop = MathMax(OrderOpenPrice() - TrailingStop \* Point, hard_stop_loss);

Print("Trailing stop: ", trailing_stop);

OrderModify(ticket, OrderOpenPrice(), trailing_stop, OrderTakeProfit(), 0, Yellow);

}

}

else

{

// Reset global variables

ticket = 0;

is_sell = false;

}

}

}
trading mql5 mt4
© www.soinside.com 2019 - 2024. All rights reserved.