Handling TP, SL, Pips values based on Stop Level and Digits

 

Hello,

I am editing in EA which is based on scalping, and i am normally using 1 Pips as Take Profits. Some broker have Stop Level above 10 Points / 1 Pips which provide an error to EA Invalid Stop and other getting failed. For this reason, i made nested if else statement to cover both type of brokers (brokers with Stop Level and Brokers with no stop Levels).

Code :

int StopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL);

if (StopLevel > 0 && (Digits == 3 || Digits == 5) && TakeProfit != 0 && StopLoss != 0) {
    TP = (StopLevel / 10) + TakeProfit;
    SL = (StopLevel / 10) + StopLoss;
    Range = (StopLevel / 10) + Range_Gap;
    Trailing_TP = (StopLevel / 10) + Trailing_TakeProfit;
    Trailing_Gap = (StopLevel / 10) + Trailing_Gaps;
} 
else if (StopLevel > 0 && Digits != 3 && Digits != 5 && TakeProfit != 0 && StopLoss != 0) {
    TP = StopLevel + TakeProfit;
    SL = StopLevel + StopLoss;
    Range = StopLevel + Range_Gap;
    Trailing_TP = StopLevel + Trailing_TakeProfit;
    Trailing_Gap = StopLevel + Trailing_Gaps;
} 
else if (StopLevel > 0 && (Digits == 3 || Digits == 5) && TakeProfit == 0 && StopLoss == 0) {
    TP = TakeProfit;
    SL = StopLoss;
    Range = (StopLevel / 10) + Range_Gap;
    Trailing_TP = (StopLevel / 10) + Trailing_TakeProfit;
    Trailing_Gap = (StopLevel / 10) + Trailing_Gaps;
} 
else if (StopLevel > 0 && Digits != 3 && Digits != 5 && TakeProfit == 0 && StopLoss == 0) {
    TP = TakeProfit;
    SL = StopLoss;
    Range = StopLevel + Range_Gap;
    Trailing_TP = StopLevel + Trailing_TakeProfit;
    Trailing_Gap = StopLevel + Trailing_Gaps;
} 
else if (StopLevel == 0) {
    TP = TakeProfit;
    SL = StopLoss;
    Range = Range_Gap;
    Trailing_TP = Trailing_TakeProfit;
    Trailing_Gap = Trailing_Gaps; 
}


Is this good practices to do like this? and if any other alternative to define value based on Stop_Level?

 
For investigating about good habits in coding you may refer to CodeBase and other educational materials on this website.
 
Yashar Seyyedin #:
For investigating about good habits in coding you may refer to CodeBase and other educational materials on this website.
I have checked, and I didn't find any solution or code for automatic adjustment based on stop level that can be used for all kinds of brokers, such as brokers with 3-4 digits, brokers with 2-4 digits, brokers with no stop level, and brokers with stop level. That's why I tried my best to create the code myself, and the code above is an example of my attempt.
 
anuj71 #:
I have checked, and I didn't find any solution or code for automatic adjustment based on stop level that can be used for all kinds of brokers, such as brokers with 3-4 digits, brokers with 2-4 digits, brokers with no stop level, and brokers with stop level. That's why I tried my best to create the code myself, and the code above is an example of my attempt.
Maybe a better approach will be doing all calculations as if minimum stop level does not exist. And adding that value to calculated SL/TP in final OrderModify call. That wouldn't be a big deal to shift all stops by a small fraction.
If you really need close stops that violates the minimum stop level often that is not a good approach in general.
 
I think there's a logic error here.
Stop level is an integer being divided by another integer that would most probably end up zero. You should go like this: StopLevel / 10.0
And Best is using point value instead of pips for more accuracy and compatibility among different symbols.
 

I don't understand why people make life difficult for themselves😄

Let me tell you a secret😄

Pip does not exist, there is only point. Pip exists only in the heads of people who are not able to adapt to the fact that the price of a symbol can have a different number of decimal places and are ready to complicate their lives only in order to avoid using their brains when setting up an adviser.

The parameters of all my programs use only point. If your broker's point is 10 times less than the value you are used to, then multiply the settings by 10 yourself. Multiplying by 10 in your head is not at all difficult.

This is my subjective opinion. I'm always amazed at people who use pips. What do you need pips for? Give at least one reason for using pips. Avoid multiplying by 10?

 
Vladislav Boyko #:

I don't understand why people make life difficult for themselves😄

Let me tell you a secret😄

Pip does not exist, there is only point. Pip exists only in the heads of people who are not able to adapt to the fact that the price of a symbol can have a different number of decimal places and are ready to complicate their lives only in order to avoid using their brains when setting up an adviser.

The parameters of all my programs use only point. If your broker's point is 10 times less than the value you are used to, then multiply the settings by 10 yourself. Multiplying by 10 in your head is not at all difficult.

This is my subjective opinion. I'm always amazed at people who use pips. What do you need pips for? Give at least one reason for using pips. Avoid multiplying by 10?

I agree and Understood very clearly. Now, my plan is to change my EA whole code by replacing Pips = 10 * Point to just Pips = Point. This Forum thread is not about the Pips but Stop_Level. Sometime we will not notice the StopLevel manually and for that reason i want to automate Stop Level. 
 
anuj71 #:
I agree and Understood very clearly. Now, my plan is to change my EA whole code by replacing Pips = 10 * Point to just Pips = Point. This Forum thread is not about the Pips but Stop_Level. Sometime we will not notice the StopLevel manually and for that reason i want to automate Stop Level. 

Use your own discretion, I'm not suggesting you change your code or give up pips.

I just expressed my opinion. My opinion may be wrong.

 
Vladislav Boyko #:

Use your own discretion, I'm not suggesting you change your code or give up pips.

I just expressed my opinion. My opinion may be wrong.

No, Your write. it hard to manage Pips calculation. There is several type of pairs and brokers and many different settings. Point is default in all of them as the smallest digit.

For Pips we use double like extern double TP = 10 because later we are converting this to Point based on digit and while converting it can have decimal and if i wanted to directly use Point should i use int  like extern int TP = 100? because decimal have no role while using Point. Later i can use this value with OrderSend() or OrderModify()


My question is simple, i want to direct use Point in OrderSend() or OrderModify(), i have to get the value of Take Profit and StopLoss in double or int

 
anuj71 #:

No, Your write. it hard to manage Pips calculation. There is several type of pairs and brokers and many different settings. Point is default in all of them as the smallest digit.

For Pips we use double like extern double TP = 10 because later we are converting this to Point based on digit and while converting it can have decimal and if i wanted to directly use Point should i use int  like extern int TP = 100? because decimal have no role while using Point. Later i can use this value with OrderSend() or OrderModify()

My question is simple, i want to direct use Point in OrderSend() or OrderModify(), i have to get the value of Take Profit and StopLoss in double or int

If your settings are in points (not pips), then you don't need the fractional part.

input int inpTakeProfit = 100; // Take profit

Then you will do something like this

// To simplify the example, let’s imagine that the order opening price is equal to ask.
double orderOpenPrice = Ask;
//---
double tpBuy = NormalizeDouble(orderOpenPrice + inpTakeProfit * Point(), Digits());

But I don't like to multiply by Point() every time.

During initialization, you can multiply by Point() each parameter in points (one time)

#property strict

input int inpTakeProfit = 100; // Take profit (in points)

double tpSize;

int OnInit()
  {
   tpSize = inpTakeProfit * Point();
   return(INIT_SUCCEEDED);
  }

void OnTick()
  {
   // To simplify the example, let’s imagine that the order opening price is equal to ask.
   double orderOpenPrice = Ask;
   //---
   double tpBuy = NormalizeDouble(orderOpenPrice + tpSize, Digits());
  }

You can, just in case, make sure that you are connected to the trading server during initialization

int OnInit()
  {
   if(!IsConnected())
     {
      Alert("No connection to trade server!");
      return(INIT_FAILED);
     }
   tpSize = inpTakeProfit * Point();
   return(INIT_SUCCEEDED);
  }
Reason: