Newbie need help to create Stop Loss Function..

 
Hi,

I have been trading for over 5 years, I understand how to trade, and now I want to learn how to code mql4 my own.

I want to create a "Stop Loss" function.

Example, I have 3 types of Stop Losses:
a. Based on last high low of 10 bars (already have code)
b. based on the last candle hi/low 
c. fixed pips

Now I'd like to make a "MyStopLoss" function. This is where I can't make it correct...  I found this simple SL calculation:  double CalculateStopLoss (bool isLong, double EntryPrice, int pips)  and this SL pips for me has 3 choices.

How to make a function for these 3 choices? tried below.. does not work..  Please help?


enum ENUM_SL_TYPE { SL_TYPE1 /*Last High/Low*/, SL_TYPE2 /*Last Candle High/Low*/, SL_TYPE3 /*Based on Pips */};

input ENUM_SL_TYPE       StopLossType         = SL_TYPE2;     // StopLoss Type
input double             StopLossPips         = 10;           // StopLoss in Pips


// --- my SL Function ---


double MyStopLoss (bool isBuy, double EntryPrice, StopLossType);
   
   {
       
      if (isBuy == true);
      double EntryPrice == Ask;
      double StopLossType =0;  
       
      {
         if (StopLossType == SL_TYPE1) StopLossType = PreviousLow (NULL,OpenPrice);  
         if (StopLossType == SL_TYPE2) StopLossType = iLow(NULL, 0, 1); 
         else StopLossType == SL_TYPE3;
         return (StopLossType);    
      }
   
 
      double CalculateStopLoss;
   
      {
          CalculateStopLoss = Ask - StopLossType* MyPipValue();
      }
     
      else
     
      {
          CalculateStopLoss= Ask + StopLossType * MyPipValue ();
      }
     
      return CalculateStopLoss;

   }


I am still very new to all this, I may make mistakes on small things like semicolons, etc.. or probably the whole functions is wrong.

Please help. 

Basic Principles - Trading Operations - MetaTrader 5 Help
Basic Principles - Trading Operations - MetaTrader 5 Help
  • www.metatrader5.com
is an instruction given to a broker to buy or sell a financial instrument. There are two main types of orders: Market and Pending. In addition, there are special Take Profit and Stop Loss levels. is the commercial exchange (buying or selling) of a financial security. Buying is executed at the demand price (Ask), and Sell is performed at the...
 
  1. Don't post code that will not even compile!
          if (isBuy == true);                 «««««««« if(condition) statement; The if is done.
          double EntryPrice == Ask;           «««««««« If you remove the semicolon above, the if stops here.
          double StopLossType =0;  
          {                                   «««««««« why have you isolated this code group?
             if (StopLossType == SL_TYPE1) StopLossType = PreviousLow (NULL,OpenPrice);  
             if (StopLossType == SL_TYPE2) StopLossType = iLow(NULL, 0, 1); 
             else StopLossType == SL_TYPE3;
             return (StopLossType);    
          }
          double CalculateStopLoss;
          {                                   «««««««« why have you isolated this code group?
              CalculateStopLoss = Ask - StopLossType* MyPipValue();
          }                                   «««««««« If you had used strict, the above line doesn't compile.
         
          else                                ««««««« else without a matching if.
          {
              CalculateStopLoss= Ask + StopLossType * MyPipValue ();
          }
         
  2. You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.

  3. Don't use NULL.
    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not. OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
    5. Cloud Protector Bug? - MQL4 programming forum 2020.07.25
    6. Always use strict. Fixing the warnings will save you hours of debugging, but you must understand the differences.

 
Thank you I need to learn more... I want to make my 3 Stop Loss types a function. So later I only need to call "MyStopLoss", if you know what I meant. 
How to do that?
 
Mohamed Elsayed: I need to learn more... I want to make … a function. … How to do that?

Yes you do. Learn to code. Then write one!

You haven't stated a problem, you stated a want.
     How To Ask Questions The Smart Way. 2004
          Prune pointless queries.

You have only four choices:
  1. Search for it. Do you expect us to do your research for you?

  2. Beg at:

  3. MT4: Learn to code it.
    MT5: Begin learning to code it.
    If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into your code.

  4. or pay (Freelance) someone to code it. Top of every page is the link Code Base.
              Hiring to write script - General - MQL5 programming forum 2019.08.21

We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using CODE button) and state the nature of your problem.
          No free help 2017.04.21

 

Understood Mr.Reoder.

I didn't mean you to do ALL for me, I was wondering where did I do wrong in creating my first function and how to do that. This is my first time to learn programming, I have read mql Book and will re-read again, and bought courses on Udemy. What you said is right, I need to able to read my code out loud and make sense of it, which still not. So I will keep learning till I can read my own code out loud and make sense for me. Thanks for your advice. :-)