Coding Help Required

 

hello friends is that I am doing a code if it is compiled but when I go to do the backtesting it says error order Send error 130 what can I do


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

//|                                           Inversor Cristiano.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql4.com"
#property version   "1.00"
#property strict
#property script_show_inputs

extern int MagicNumber=10003;
extern int startHour =9;
extern int TakeProfit = 50;
extern int StopLoss = 10;
extern double Lots = 0.01;
extern int TrailingStop=5;
extern int MA_Period = 369;
extern int Slippage=3;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   //Print("Nuestro robot se ha cargado a la grafica...");
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   //Print("Nuestro robot se ha eliminado de la grafica..");
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+


void OnTick()

{
    double mipoint=Point;
    if(Digits==3 || Digits==5) mipoint=Point*10;

    bool comprar = True;
    int ticket = 0;
    
    double ma = iMA(NULL,0,MA_Period,1,MODE_SMA,PRICE_CLOSE,1);
    
    if(Hour()== startHour)
    {
    
    if(comprar == true)
    {
     
     comprar = false;
     
     bool resultado;
     resultado = OrderSelect(ticket, SELECT_BY_TICKET);
     if(resultado == true){
      
      if(OrderCloseTime() == 0){
      
        bool resultado2;
        resultado2 = OrderClose(ticket, Lots, OrderClosePrice(),10);
        
        if(resultado2 == false)
        {
        Alert("Error cerrando orden #: ", ticket);
        }
      
      }
     
     }
     
      if(Open[0]< Open[startHour])
    {
    if((Ask>iMA(NULL,0,MA_Period,1,MODE_SMA,PRICE_CLOSE,1))) // Here is your open buy rule
    {
    ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,3,3,"EA Inversor Cristiano");
       if(ticket < 0)
       {
         TrailingStop=5;
         StopLoss=0;
         TakeProfit=0;
         if(TrailingStop>0) TrailingStop=Ask+TrailingStop*mipoint;
         if(TakeProfit>0)TakeProfit=Ask+TakeProfit*mipoint;
         if(StopLoss>0)StopLoss=Ask-StopLoss*mipoint;
       }
      
    }
    }
       
    if(Open[0]< Open[startHour])
    {
   if((Bid<iMA(NULL,0,MA_Period,1,MODE_SMA,PRICE_CLOSE,1))) // Here is your open Sell rule 
   {
    ticket = OrderSend (Symbol(),OP_SELL,Lots,Bid,Slippage,3,3,"EA Inversor Cristiano");
      if(ticket < 0)
      {
         TrailingStop=5;
         StopLoss=0;
         TakeProfit=0;
         if(TrailingStop>0) TrailingStop=Bid+TrailingStop*mipoint;
         if(TakeProfit>0) TakeProfit=Bid-TakeProfit*mipoint;
         if(StopLoss>0) StopLoss=Bid+StopLoss*mipoint;
      }
      
    }
    
    } 
    }
   }
   
   
   else
   {
   comprar = true;
   }
  }

MQL4: automated forex trading, strategy tester and custom indicators with MetaTrader
MQL4: automated forex trading, strategy tester and custom indicators with MetaTrader
  • www.mql4.com
MQL4: automated forex trading, strategy tester and custom indicators with MetaTrader
 
The Stoploss and Takeprofit values are incorrect. Please make sure if you want to set 3 points of  Stoploss or Takeprofit, you first convert it into points and then add/subtract it from order open price.
 
//+------------------------------------------------------------------+

//|                                           Inversor Cristiano.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql4.com"
#property version   "1.00"
#property strict
#property script_show_inputs

extern int MagicNumber = 10003;
extern int startHour = 9;
extern int TakeProfit = 50;
extern int StopLoss = 10;
extern double Lots = 0.01;
extern int TrailingStop = 5;
extern int MA_Period = 369;
extern int Slippage = 3;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
//Print("Nuestro robot se ha cargado a la grafica...");
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
//Print("Nuestro robot se ha eliminado de la grafica..");
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+


void OnTick()

  {
   double mipoint = Point;
   if(Digits == 3 || Digits == 5)
      mipoint = Point * 10;
   bool comprar = True;
   int ticket = 0;
   double ma = iMA(NULL, 0, MA_Period, 1, MODE_SMA, PRICE_CLOSE, 1);
   if(Hour() == startHour)
     {
      if(comprar == true)
        {
         comprar = false;
         bool resultado;
         resultado = OrderSelect(ticket, SELECT_BY_TICKET);
         if(resultado == true)
           {
            if(OrderCloseTime() == 0)
              {
               bool resultado2;
               resultado2 = OrderClose(ticket, Lots, OrderClosePrice(), 10);
               if(resultado2 == false)
                 {
                  Alert("Error cerrando orden #: ", ticket);
                 }
              }
           }
         if(Open[0] < Open[startHour])
           {
            if((Ask > iMA(NULL, 0, MA_Period, 1, MODE_SMA, PRICE_CLOSE, 1))) // Here is your open buy rule
              {
               double StopLevel = MarketInfo(_Symbol, MODE_STOPLEVEL);
               double sl = Ask - (3 * mipoint);
               double tp = Ask + (3 * mipoint);
               if(!tp_valid(OP_BUY, tp))
                 {
                  Print("for buy the Min TP should be: ", Bid + StopLevel, " but now is: ", tp, " the stoplevel is: ", StopLevel);
                  tp = Bid + StopLevel + _Point;
                 }
               if(!sl_valid(OP_BUY, sl))
                 {
                  Print("for buy the Min SL should be: ", Bid - StopLevel, " but now is: ", sl, " the stoplevel is: ", StopLevel);
                  sl = Bid - StopLevel - _Point;
                 }
               ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 3, 3, "EA Inversor Cristiano");
               if(ticket < 0)
                 {
                  TrailingStop = 5;
                  StopLoss = 0;
                  TakeProfit = 0;
                  if(TrailingStop > 0)
                     TrailingStop = Ask + TrailingStop * mipoint;
                  if(TakeProfit > 0)
                     TakeProfit = Ask + TakeProfit * mipoint;
                  if(StopLoss > 0)
                     StopLoss = Ask - StopLoss * mipoint;
                 }
              }
           }
         if(Open[0] < Open[startHour])
           {
            if((Bid < iMA(NULL, 0, MA_Period, 1, MODE_SMA, PRICE_CLOSE, 1))) // Here is your open Sell rule
              {
               double StopLevel = MarketInfo(_Symbol, MODE_STOPLEVEL);
               double sl = Bid + (3 * mipoint);
               double tp = Ask - (3 * mipoint);
               if(!tp_valid(OP_SELL, tp))
                 {
                  Print("for buy the Min TP should be: ", Bid + StopLevel, " but now is: ", tp, " the stoplevel is: ", StopLevel);
                  tp = Bid + StopLevel + _Point;
                 }
               if(!sl_valid(OP_SELL, sl))
                 {
                  Print("for buy the Min SL should be: ", Bid - StopLevel, " but now is: ", sl, " the stoplevel is: ", StopLevel);
                  sl = Bid - StopLevel - _Point;
                 }
               ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 3, 3, "EA Inversor Cristiano");
               if(ticket < 0)
                 {
                  TrailingStop = 5;
                  StopLoss = 0;
                  TakeProfit = 0;
                  if(TrailingStop > 0)
                     TrailingStop = Bid + TrailingStop * mipoint;
                  if(TakeProfit > 0)
                     TakeProfit = Bid - TakeProfit * mipoint;
                  if(StopLoss > 0)
                     StopLoss = Bid + StopLoss * mipoint;
                 }
              }
           }
        }
     }
   else
     {
      comprar = true;
     }
  }

//+------------------------------------------------------------------+
//|          check the tp  price: valid==true                                                        |
//+------------------------------------------------------------------+
bool tp_valid(int order_type, double tp)
  {
   double StopLevel = MarketInfo(_Symbol, MODE_STOPLEVEL);
   if(order_type == OP_BUY)
     {
      if(tp - Bid < StopLevel)
         return(false);
     }
   else
      if(order_type == OP_SELL)
        {
         if(Ask - tp < StopLevel)
            return(false);
        }
   return true;
  }
//+------------------------------------------------------------------+
//|          check the  sl price: valid==true                                                        |
//+------------------------------------------------------------------+
bool sl_valid(int order_type, double sl)
  {
   double StopLevel = MarketInfo(_Symbol, MODE_STOPLEVEL);
   if(order_type == OP_BUY)
     {
      if(Bid - sl < StopLevel)
         return(false);
     }
   else
      if(order_type == OP_SELL)
        {
         if(sl - Ask < StopLevel)
            return(false);
        }
   return true;
  }
//+------------------------------------------------------------------+

i added two functions to check the Tp and SL and then open the oders

Reason: