Buy orders are being placed but sells orders are getting errors?

 

Hi, I am making an EA with RSI that buy and sells at certain deviations from the mean, so far the code is working fine but the system is only executing the buy orders and not the sell orders?
 
The error is " 2021.10.26 17:24:16.153    2021.10.21 12:20:00   failed instant buy 1 BTCUSD at 66354.407 sl: 66454.407 tp: 65854.407 [Invalid stops]
 " and " 2021.10.26 17:24:16.153    2021.10.21 12:20:00   CTrade::OrderSend: instant buy 1.00 BTCUSD at 66354.407 sl: 66454.407 tp: 65854.407 [invalid stops]
 "
 
Side note: Also I am looking for a formal or informal partner/code auditee to help me answer questions about simple errors in my code, I can send small payments, just to get an more experience MQL5 programmer support, if interested let me know.


Here is the code:
#property copyright "Copyright 2021, NENFB LLC"
#property link      "https://www.NENFBLLC.com"
#property version   "2.00"

#include <Trade/Trade.mqh>

//-------------------------------------------------------------------------

// Initialized Variables

input double Lots = 1;           
input int TpPoints =  500000;
input int SlPoints = 100000;

double upper_band = 62.50;
double mid_upper_band = 56.25;
double mid_lower_band = 43.75;
double lower_band = 37.50;
int mid_rsi = 50; 

input int ma_period = 42;

int handle;
int totalBars;

CTrade trade;

int OnInit()
  {
  
   totalBars = iBars(_Symbol,PERIOD_CURRENT);  
   handle = iRSI(_Symbol,PERIOD_CURRENT,ma_period,PRICE_OPEN);
   
   Print("OnInit...");
   return(INIT_SUCCEEDED);
  }

//-------------------------------------------------------------------------

void OnDeinit(const int reason)
  {

   Print("OnDeinit...");
   
  }

//-------------------------------------------------------------------------

void OnTick()
  {
   
   int bars = iBars(_Symbol,PERIOD_CURRENT);
   
   if(totalBars != bars){
      totalBars = bars;
   
      double rsi[];
      // ArrayResize(rsi,1000,1000);
      
      CopyBuffer(handle,0,1,2,rsi);
      
      if(rsi[1] < lower_band && rsi[0] > lower_band){
         Print("Buy Signal");
         
         double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
         ask = NormalizeDouble(ask,_Digits);
         
         double sl = ask - SlPoints *_Point;
         sl = NormalizeDouble(sl,_Digits);
         
         double tp = ask + TpPoints *_Point;
         tp = NormalizeDouble(tp,_Digits);
         
         trade.Buy(Lots,_Symbol,ask,sl,tp);
         
      }else if(rsi[1] > upper_band && rsi[0] < upper_band){
         Print("Sell Signal");
         
         double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
         bid = NormalizeDouble(bid,_Digits);
         
         double sl = bid + SlPoints *_Point;
         sl = NormalizeDouble(sl,_Digits);
         
         double tp = bid - TpPoints *_Point;
         tp = NormalizeDouble(tp,_Digits);
         
         trade.Buy(Lots,_Symbol,bid,sl,tp);
      }    
   } 
  }
  
 

You're trying to buy instead of selling — trade.Buy is in both branches.


Stops are calculated to the correct side, just the operation itself is wrong.

 
  1. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).


  2. For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              New candle - MQL4 programming forum #3 (2014)

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum (2011)

  3.          Print("Sell Signal");
             ⋮         
             trade.Buy(Lots,_Symbol,bid,sl,tp);
    As Haruto Rat already posted.
 
The code worked, thank you.