Download MetaTrader 5

converting ask to a double

To add comments, please log in or register
boh113
26
boh113  

Hi guys,

 

so i'm attempting to create code for a Take Profit on a buy trade. code will be a user function to be called upon when order placement happens.

The rules are: Take Profit to be set at the closest institutional level 00 , 20 , 50 , 80 as long as it's more than the Stop Loss ie greater than 1R.

Not doing so great, have broken it down in to a few steps

1. I need to identify levels that are above the ask price.

2. compare the pip (including fractional value) to each of the levels.

3. identify which level fits the criteria.

4. use the variable for that level to set variable (double TP; or something) to use in the ordersend()

So i'm currently at a point that I can create the levels except for the 00 level.

The code below is written so i can check as i go buy using the alert function.

i'm very new at coding and trying to learn bits as i go.

 sorry if my code sucks or there is easier ways, this is all i could come up with. 

any help would be appreciated 


void OnTick()
   {
   a=Ask;
   b=StringSubstr(a,0,4);
   c=StringConcatenate(b,200);
   d=StringConcatenate(b,500);
   e=StringConcatenate(b,800);
   f=c;
   g=d;
   h=e;
   if (f>a)
      {
      i=(f-a);
      return;
      }
   if (a>f && g>a)
      {
      i=(g-a);
      return;
      }
   if (a>g && h>a)
      {
      i=(h-a);
      return;
      }
   Alert (i);
   return;
  }
eevviill6
27
eevviill6  
string ask=DoubleToString(Ask,Digits);
  int dig2_Ask=int(StringSubstr(ask,StringLen(ask)-2,2));

if(dig2_Ask>=70) Alert("TP to 00 level!");
else
  if(dig2_Ask>=50) Alert("TP to 70 level!");
else
  if(dig2_Ask>=20) Alert("TP to 50 level!");
else
Alert("TP to 20 level!");
whroeder1
15567
whroeder1  
b=StringSubstr(a,0,4);
c=StringConcatenate(b,200);
d=StringConcatenate(b,500);
e=StringConcatenate(b,800);
string ask=DoubleToString(Ask,Digits);
  int dig2_Ask=int(StringSubstr(ask,StringLen(ask)-3,2));

if(dig2_Ask>=70) Alert("TP to 00 level!");
else
  if(dig2_Ask>=50) Alert("TP to 70 level!");
else
  if(dig2_Ask>=20) Alert("TP to 50 level!");
else
Alert("TP to 20 level!");

You're trying to generate pip levels 00, 20, 50, 80, and 100.

  1. code breaks on JPY pairs and on 4 digit brokers.
  2. Code becomes self documenting when you use meaningful variable names
  3. No need for strings. Adjust for 4/5 digit brokers and JPY pairs:
    double            round_down(    double v, double to){
                                                    return to * MathFloor(v / to); }
    double            round_up(      double v, double to){
                                                    return to * MathCeil( v / to); }
    double            round_nearest( double v, double to){
                                                    return to * MathRound(v / to); }
    ////////////////////////////////////////////////////////////////////////////////
    double pip = _Digits % 2 == 0 ? _Point : 10.0 * _Point;
    double level00 = round_down(Ask, 100*pip);
    double level20 = level00 + 20*pip;
    double level50 = level00 + 50*pip;
    double level80 = level00 + 80*pip;

Guillermo
108
Guillermo  
Hello,

I am currently using the same approach in my EA.

double pip = _Digits % 2 == 0 ? _Point : 10.0 * _Point;

However, in a theoretical future, brokers might expand to 6 digits (4 if JPY), right? So this could be a potential problem. Just curious.

Regards.
honest_knave
Moderator
2332
honest_knave  
Guillermo:
Hello,

I am currently using the same approach in my EA.

double pip = _Digits % 2 == 0 ? _Point : 10.0 * _Point;

However, in a theoretical future, brokers might expand to 6 digits (4 if JPY), right? So this could be a potential problem. Just curious.

Regards.

That solution is elegant which is why it is used so frequently, but there are other options.

You could use simple if-else statements, or a case select, testing Digits.

It would be more lines of code but not really any performance hit as normally it only runs once.

Guillermo
108
Guillermo  
honest_knave:

That solution is elegant which is why it is used so frequently, but there are other options.

You could use simple if-else statements, or a case select, testing Digits.

It would be more lines of code but not really any performance hit as normally it only runs once.

Yes, but wouldn't them all suffer the same problem?

If I'm not mistaken, the current "trend" for currencies is the following:
Non JPY pairs: 5 digits (4 digits in some brokers)
JPY pairs: 3 digits (2 digits in some brokers)

Whether you check for "Digits%2" or "Digits == 2 || Digits == 4", what I mean is that maybe in a near future (I'm not sure about it, I started learning about Forex in January) the trend might increase a Digit for both (4 if JPY, 6 for non-JPY), making most of the existing code using similar workarounds outdated.

Am I missing something? Or is it just that the moment for next Digit increase is not even near?

honest_knave
Moderator
2332
honest_knave  
Guillermo:
Yes, but wouldn't them all suffer of the same problem?

If I'm not mistaken, the current "trend" is the following:
Non JPY pairs: 5 digits (4 digits in some brokers)
JPY pairs: 3 digits (2 digits in some brokers)

Whether you check for Digits%2 or Digits == 2 || Digits == 4, what I mean is that maybe in a near future (I'm not sure about it, I started learning about Forex in January) the trend might increase a Digit for both (4 if JPY, 6 for non-JPY), making most of the existing code using similar workarounds outdated.

Am I missing something? Or is it just that the moment for next Digit increase is not even near?

I don't see it happening any time soon, but if it was you could use something very simple like this:

double pip = Point;
if(Digits==3 || Digits==5)      { pip*=10;  }
else if(Digits==4 || Digits==6) { pip*=100; }

Just keep adding more else statements as they increase the number of digits! (again, unlikely)

At the risk of opening a large can of worms, a pip doesn't really exist anyway. It is just a relic of the past which many people work in. If you keep your code working in points you shouldn't go far wrong.

___________________ 

Edit: actually, that code would fail as JPY crosses would now have 4 digits, which would seem like an old 4 digit non-JPY cross. You'd need more code in there to deal with that. Are there even 4 digits brokers out there anymore??

Guillermo
108
Guillermo  
honest_knave:

Edit: actually, that code would fail on JPY crosses (which would now have 4 digits). You'd need more code in there to deal with that. Are there even 4 digits brokers out there anymore??


That's exactly what I meant. I haven't come across any, but who knows... Better safe than sorry!

Kind regards.
whroeder1
15567
whroeder1  
Except if Digits==4 a pip is a point.
honest_knave: I don't see it happening any time soon, but if it was you could use something very simple like this:
double pip = Point;
if(Digits==3 || Digits==5)      { pip*=10;  }
else if(Digits==4 || Digits==6) { pip*=100; }
honest_knave
Moderator
2332
honest_knave  
whroeder1:
Except if Digits==4 a pip is a point.
That is what we discussed above
Guillermo
108
Guillermo  
whroeder1:
Except if Digits==4 a pip is a point.
Yes, but in that case, the "Digits == 4" we would be referring to the new, hypothetical, extended new JPY quotes by some brokers that would offer 4 digits for that pairs (in which, the pip, would still be worth 0.01).
12
To add comments, please log in or register