how to solve the problem : failed instant sell 0.001 EURUSD(UST+3) at 1.0906 [Invalid volume]

 

i am a newbie of mt5 trader. i was practicing my coding with the moving average strategy. but there is a problem happened.

i can run my code but the journal always shows me "failed instant sell 0.001 EURUSD(UST+3) at 1.0906 [Invalid volume]" when i make the order.

is anyone can help me to point out what the reason is and how to solve it? thank you.

//+------------------------------------------------------------------+
//|                                           ALPHATRENDTEST0725.mq5 |
//|                        Copyright 2023, MetaQuotes Ltd.ChuMinChen |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd.ChuMinChen"
#property link      "https://www.mql5.com"
#property version   "1.08"
#property strict

// 定义全局变量
input double coeff = 1;
input int AP = 14;
input bool showsignalsk = true;
input bool novolumedata = false;
input double LotSize = 0.1;
input string InputTradeSymbol = "USDJPY";
input ENUM_ORDER_TYPE_FILLING fillingType = ORDER_FILLING_IOC;
string TradeSymbol;

double upT[];
double downT[];
double AlphaTrend[];
double ATR[];
double Low[];
double High[];

int OnInit()
{
   TradeSymbol = InputTradeSymbol;

   ArraySetAsSeries(upT, true);
   ArraySetAsSeries(downT, true);
   ArraySetAsSeries(AlphaTrend, true);
   ArraySetAsSeries(ATR, true);
   ArraySetAsSeries(Low, true);
   ArraySetAsSeries(High, true);

   int totalBars = Bars(TradeSymbol, _Period);
   Print("OnInit - TradeSymbol: ", TradeSymbol, " totalBars: ", totalBars);
   ArrayResize(upT, totalBars + AP);
   ArrayResize(downT, totalBars + AP);
   ArrayResize(ATR, totalBars + AP);
   ArrayResize(AlphaTrend, totalBars + AP);

   return(INIT_SUCCEEDED);
}

bool CrossedAbove(double line1, double line2, double prev_line1, double prev_line2) {
   return (line1 > line2 && prev_line1 <= prev_line2);
}

bool CrossedBelow(double line1, double line2, double prev_line1, double prev_line2) {
   return (line1 < line2 && prev_line1 >= prev_line2);
}


void SendOrder(ENUM_ORDER_TYPE orderType, const double price)
{
   MqlTradeRequest request;
   MqlTradeResult result;
   
   ZeroMemory(request);
   
   request.action = TRADE_ACTION_DEAL;
   request.symbol = TradeSymbol;
   request.volume = LotSize;
   request.type = orderType;
   request.price = price;
   request.deviation = 10;
   request.magic = 24072023;
   request.comment = (orderType == ORDER_TYPE_BUY) ? "Buy Order" : "Sell Order";
   
   // Set the appropriate filling mode
   request.type_filling = fillingType; 

   if(!OrderSend(request, result) || result.retcode != TRADE_RETCODE_DONE)
   {
      Print("OrderSend failed with error #", GetLastError());
      Print("Retcode: ", result.retcode, " Comment: ", result.comment);
   }
   else
   {
      Print("OrderSend succeeded. Retcode: ", result.retcode, " Comment: ", result.comment);
   }
}

void OnTick()
{
   double ask = SymbolInfoDouble(TradeSymbol, SYMBOL_ASK);
   double bid = SymbolInfoDouble(TradeSymbol, SYMBOL_BID);

   int totalBars = Bars(TradeSymbol, _Period);
   Print("OnTick - TradeSymbol: ", TradeSymbol, " totalBars: ", totalBars);

   if(totalBars < AP + 10) {
      Print("Not enough data yet. Waiting for more bars.");
      return;
   }

   int copiedLow = CopyLow(TradeSymbol, _Period, 0, totalBars, Low);
   int copiedHigh = CopyHigh(TradeSymbol, _Period, 0, totalBars, High);
   if(copiedLow == -1 || copiedHigh == -1) {
      Print("Error in copying data. Error code: ", GetLastError());
      return;
   }

   int loopEnd = MathMin(MathMin(totalBars - AP, copiedLow), copiedHigh);
   
   for(int i = 0; i < loopEnd && i < ArraySize(ATR) && i < ArraySize(Low) && i < ArraySize(High) && i < ArraySize(AlphaTrend); i++) {
      ATR[i] = iATR(TradeSymbol, _Period, AP);
      upT[i] = Low[i] - ATR[i] * coeff;
      downT[i] = High[i] + ATR[i] * coeff;

      double currentIndicatorValue = novolumedata ? iRSI(TradeSymbol, _Period, 14, PRICE_CLOSE) : iMFI(TradeSymbol, _Period, 14, VOLUME_REAL);
      AlphaTrend[i] = currentIndicatorValue >= 50 ? MathMax(upT[i], (i > 0 ? AlphaTrend[i-1] : upT[i])) : MathMin(downT[i], (i > 0 ? AlphaTrend[i-1] : downT[i]));
   }

   if(showsignalsk) {
      if(CrossedAbove(AlphaTrend[0], AlphaTrend[1], AlphaTrend[1], AlphaTrend[2])) {
         Print("BUY SIGNAL!");
         SendOrder(ORDER_TYPE_BUY, ask);
      }
      if(CrossedBelow(AlphaTrend[0], AlphaTrend[1], AlphaTrend[1], AlphaTrend[2])) {
         Print("SELL SIGNAL!");
         SendOrder(ORDER_TYPE_SELL, bid);
      }
   }
}
Files:
result.png  115 kb
 
Check the minimum (and maximum) permissible volume.
 
Carl Schreiber #:
Check the minimum (and maximum) permissible volume.
Sorry! can you tell more detail? or if you don't mind? can you tell me how to apply it into my code? 
 
Johnny陳 #: Sorry! can you tell more detail?

What part of checking the minimum volume, maximum, and lotstep was unclear?

 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 

Please search before you post ...

Articles

The checks a trading robot must pass before publication in the Market

MetaQuotes, 2016.08.01 09:30

Before any product is published in the Market, it must undergo compulsory preliminary checks in order to ensure a uniform quality standard. This article considers the most frequent errors made by developers in their technical indicators and trading robots. An also shows how to self-test a product before sending it to the Market.