I have a problem setting my stop loss, please help me

 

I´m coding an EA that just sells and I code the patron i want the expert follows but i dont know how to set the stop loss where i want (VIEW IMAGE) Here is my code, please help me.

//+------------------------------------------------------------------+
//|                                                    Robot_50%.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

input double SL = 5;
input double TP = 5;
input int magic = 1;

input double HoraInicial=8;//0-23
input double HoraFinal=17 ;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

   bool trade = true;

   for (int i=0;i<OrdersTotal();i++) {


      int orden_seleccionada = OrderSelect (1,SELECT_BY_POS,MODE_TRADES);
      if (OrderSymbol()==Symbol() && OrderMagicNumber()==magic) {
         trade= false;
            break;
}
}
      if (trade==true ) {

         if (venta()==true && FiltroCompra()==true /*&& OperacionesAbiertas()==0*/) {

            int venta = OrderSend(NULL,OP_SELL,1,Bid,10,SL,TP,"Keltner inverso",magic,0, clrRed);
  }
  }
  
  }
  
//+------------------------------------------------------------------+

     bool venta(){ //Pattern to follow to sell
     
     
     double cuatro = 0;
     double tres = 0;
     double dos = 0;
     double uno = 0;
     double x = 0;
    
     for(int i=2;i<250;i++){
     
      double ind = iCustom(NULL,0,"ZigZag.ex4",20,5,1,0,i); //Here i start to make the stucture i want
      
      if(ind!=0){
      
         if(cuatro==0 && tres==0 && dos==0 && uno==0 && x==0){
            cuatro = ind;
         }
         if(cuatro!=0 && tres==0 && dos==0 && uno==0 && x==0 && ind!=cuatro){
            tres = ind;
         }
         if(cuatro!=0 && tres!=0 && dos==0 && uno==0 && x==0 && ind!=cuatro && ind!=tres){
            dos = ind;
         }
         if(cuatro!=0 && tres!=0 && dos!=0 && uno==0 && x==0 && ind!=cuatro && ind!=tres && ind!=dos){
            uno = ind;
            
         }
         if(cuatro!=0 && tres!=0 && dos!=0 && uno!=0 && x==0 && ind!=cuatro && ind!=tres && ind!=dos && ind!=uno){
            x = ind;
            break;
         }
      }
     } 
     
     if( uno > dos && tres < uno && tres > cuatro && tres > dos && x < uno && cuatro < dos && High[1]>tres+50*Point && Close[2]<tres){
      return true;
     }else{
     return false;
     
     }
     
     }
  
 
   double stoploss=Bid+100*Point; 
   double takeprofit=NormalizeDouble(Ask-200*Point,Digits);
 
      
     
     
     
     
     
   bool FiltroCompra(){
  
      if(Hour()>=HoraInicial && Hour()<=HoraFinal){
      
         return true;
      }
      else
      return false;}
      
      
      
      
    int OperacionesAbiertas(){
  
      int liOrders = 0;
      for (int i = 0; i < OrdersTotal(); i++){
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false){
          continue;
        }
        if (OrderSymbol() != Symbol() || OrderMagicNumber() != magic){
          continue;
        }
        liOrders++;
      }
      return(liOrders);
}
Files:
 

I'll break this down to you. 

1. You want the stoploss to be above 1 then why have you already made an external input that is static ? 

every order that is sent through, the stoploss will keep changing, you need to customize your stoploss using a function.

2. You would want to capture the value of 1 and have it stored as a static variable in a seperate function, once the criteria (signal) is met, that saved value can be inputted as a stop loss with the calculation of the current market price that you opened the order.

3. For example, if you would want to loop every other high, 

this actually takes more precise methods including capturing the high value every hour. 

The code for what you are asking for is quite rare to see on this forum and interesting to analyze. I hope the above helped expand on what you need to do. 

 
Adj007 #:

I'll break this down to you. 

1. You want the stoploss to be above 1 then why have you already made an external input that is static ? 

every order that is sent through, the stoploss will keep changing, you need to customize your stoploss using a function.

2. You would want to capture the value of 1 and have it stored as a static variable in a seperate function, once the criteria (signal) is met, that saved value can be inputted as a stop loss with the calculation of the current market price that you opened the order.

3. For example, if you would want to loop every other high, 

this actually takes more precise methods including capturing the high value every hour. 

The code for what you are asking for is quite rare to see on this forum and interesting to analyze. I hope the above helped expand on what you need to do. 

Hiii, but the problem is that i dont know how to capture the value of nº 1. Do you know how to code it?

 
Ruben Ortega #:

Hiii, but the problem is that i dont know how to capture the value of nº 1. Do you know how to code it?

With some thinking ability it is most probable I can code something like this, I suggest looking into topics:

Finding the highest value in a chart MQL4 (this is for your stop loss). 
How to create a function MQL4(storing values continuously and putting it onto OnTick). 
Getting the count of bars (I have a publication on this, you can check it out)

I suggest start small if you want to achieve something like this, and then build on it. 

Finally, read code. It’s easy to cut and paste code. Reading it, understanding it is why coders fail and leave a half decent project.

 
Adj007 #:
With some thinking ability it is most probable I can code something like this, I suggest looking into topics:

Finding the highest value in a chart MQL4 (this is for your stop loss). 
How to create a function MQL4(storing values continuously and putting it onto OnTick). 
Getting the count of bars (I have a publication on this, you can check it out)

I suggest start small if you want to achieve something like this, and then build on it. 

Finally, read code. It’s easy to cut and paste code. Reading it, understanding it is why coders fail and leave a half decent project.

yeah nice, but how can i set the highest as the stop loss?

 
Ruben Ortega #:

yeah nice, but how can i set the highest as the stop loss?

You need to go through the topics I've said enough to get you started here is some code that can lead up to looking for your stoploss.

Look for the highest value using --> iHighest command

return the bar number that has the highest value ---->  double valuehigh(){
                                                                        }

return the value of which bar is the highest ----> Close[ bar number]

Sort the value on the OnTick using AlertTime so your EA is not overworking every tick ---> using Time[0]

Calculate the price by using the Bid price when the signal occurs.

Or if you want, you can do this in an array which is better imo. But that code should give you an idea of what to do and look for. 
Read through the material I sent over and you can definitely achieve something like this. 


 
Adj007 #:

You need to go through the topics I've said enough to get you started here is some code that can lead up to looking for your stoploss.

Hey thanks, i tried it but i have some problems. I want the high maximum not the high price (Nº 1 in the image) I code this but its is not what i want.

double valuehigh=(Close[iHighest(NULL,0,MODE_HIGH,100,0)]);
Files:
 
Ruben Ortega #:

Hey thanks, i tried it but i have some problems. I want the high maximum not the high price (Nº 1 in the image) I code this but its is not what i want.


iHighest returns the candle number with the highest value through x number of candles. 

Double valuehigh was a custom function not a custom double with a continuous output. 

You need to look into the topics I’ve sent over to you and look into functional programming with C++. 
Reason: