Getting Error 10016 invalid Stop, But Sl and Tp is too far

 

with the code below:

#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#include<Trade\Trade.mqh>
CTrade trade;
input int SmallMovingAverage=1;
input int BigMovingAverage=12;
input int ma_Period=900;
string signal="";
string res="";
double ATR=0;
double min_ATR=25;


void OnTick()
  {
//---
   double SmallMovingAverageArray[];
   double BigMovingAverageArray[];
   double ATR_tmp=iATR(NULL,NULL,ma_Period);
   if (ATR_tmp<min_ATR)
   {
    ATR=min_ATR*_Point;
   }
   else
   {
    ATR=ATR_tmp*_Point;
   }
   //double ATR=ATR_/MathPow(10,_Digits);
   int SmallMovingAverageDefinition=iMA(_Symbol,_Period,SmallMovingAverage,0,MODE_EMA,PRICE_CLOSE);
   int BigMovingAverageDefinition=iMA(_Symbol,_Period,BigMovingAverage,0,MODE_EMA,PRICE_CLOSE);

   CopyBuffer(SmallMovingAverageDefinition,0,0,4,SmallMovingAverageArray);
   CopyBuffer(BigMovingAverageDefinition,0,0,4,BigMovingAverageArray);

   if(BigMovingAverageArray[1]>SmallMovingAverageArray[1]+(ATR)
      && BigMovingAverageArray[2]<SmallMovingAverageArray[2]
     )
     {
      signal="READY_TO_BUY";
     }
//
   if(BigMovingAverageArray[1]<SmallMovingAverageArray[1]-(ATR)
      && BigMovingAverageArray[2]>SmallMovingAverageArray[2])
     {
      signal="READY_TO_SELL";
     }


   if(signal=="READY_TO_BUY" && (NormalizeDouble(SmallMovingAverageArray[1],_Digits)==NormalizeDouble(BigMovingAverageArray[1],_Digits)))
     {
      signal="BUY";
     }
   if(signal=="READY_TO_SELL" && (SmallMovingAverageArray[1]==BigMovingAverageArray[1]))
     {
      signal="SELL";
     }


   if(signal=="SELL" && PositionsTotal()<1)
     {
      double tickSize=   SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
     double Bid=NormalizeDouble(MathRound(SymbolInfoDouble(_Symbol,SYMBOL_BID)*tickSize)/tickSize,_Digits);
      double sl=NormalizeDouble(MathRound((Bid+2*ATR)*tickSize)/tickSize,_Digits);
     double tp=NormalizeDouble(MathRound((Bid-4*ATR)*tickSize)/tickSize,_Digits);
    
   //   double Bid=MathRound((SymbolInfoDouble(_Symbol,SYMBOL_BID)/tickSize)*_Point);
   //   double sl=MathRound(((Bid+2*ATR)/tickSize)*tickSize) ;
   //   double tp=MathRound(((Bid-4*ATR)/tickSize)*tickSize);
   
   
     
      
      trade.Sell(0.01,NULL,Bid,sl,tp,NULL);
      res=(trade.ResultRetcodeDescription());
      Alert("The signal is now:", signal);
      Alert(res);
     }

   if(signal=="BUY" && PositionsTotal()<1)
     {
      double tickSize=SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_SIZE);
      
    //  double symbol_Ask=(MathFloor(SymbolInfoDouble(_Symbol,SYMBOL_ASK)/tickSize))*_Point;
   //  double Ask=(MathRound(symbol_Ask/tickSize)*tickSize);
   //  double sl=(MathRound((Ask-2*ATR)/tickSize)*tickSize);
   //  double tp=(MathRound((Ask+4*ATR)/tickSize)*tickSize);
    
     
     double Ask=NormalizeDouble(MathRound(SymbolInfoDouble(_Symbol,SYMBOL_ASK)/tickSize)*tickSize,_Digits);
      double sl=NormalizeDouble(MathRound((Ask-2*ATR)/tickSize)*tickSize,_Digits);
     double tp=NormalizeDouble(MathRound((Ask+4*ATR)/tickSize)*tickSize,_Digits);
      
      trade.Buy(0.01,NULL,sl,tp,NULL);
      res=(trade.ResultRetcode()  );
      Alert("The signal is now:", signal);
      Alert(res);
     }
  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+

lso Set a Pending Order using Ask, tp, sl with related Values.

Here is my Watch Expersions

Documentation on MQL5: Constants, Enumerations and Structures / Codes of Errors and Warnings / Trade Server Return Codes
Documentation on MQL5: Constants, Enumerations and Structures / Codes of Errors and Warnings / Trade Server Return Codes
  • www.mql5.com
Trade Server Return Codes - Codes of Errors and Warnings - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

Fix your code.

You didn't provide enough technical information to help.

 
I Send my whole Code
 
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  2.   int SmallMovingAverageDefinition=iMA(_Symbol,_Period,SmallMovingAverage,0,MODE_EMA,PRICE_CLOSE);
       int BigMovingAverageDefinition=iMA(_Symbol,_Period,BigMovingAverage,0,MODE_EMA,PRICE_CLOSE);
    
       CopyBuffer(SmallMovingAverageDefinition,0,0,4,SmallMovingAverageArray);
       CopyBuffer(BigMovingAverageDefinition,0,0,4,BigMovingAverageArray);

    Perhaps you should read the manual, especially the examples.
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020.03.08)
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020.07.05)
              How to call indicators in MQL5 - MQL5 Articles (2010)

 
Reza Hasani #:
I Send my whole Code
That's not the point. We have no idea about the data and so the results of this code.
Reason: