Regular expression to trim a price and store each value in a variable

 

Hello Everyone,


I am new to mql5 & programming in general and need a little help here.

I need to break (trim) each number "if the dot is found should be ignored, and store it in a separate variable. where i can start dealing with it.

For example:

Gold price now is 1884.55


I want to divide it into 6 variables:

V1= 1;

V2=8;

V3=8;

V4=4;

V5=5;

V6=5;

V7=0;

V8=0;

V9=0;

V10=0;

and if the more digits in the price not exist, to be filled with Zero the remaining of the 10 variables  slots , any help here please ?


Regards ,,

Ahmad




 

 
Ahmad ElGendi: any help here please ?
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum 2019.05.06
              Messages Editor

  2. Help you with what? You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your problem.
              No free help 2017.04.21

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum 2018.05.12

    We're not going to code it for you (although it could happen if you are lucky or the problem is interesting).
              No free help 2017.04.21

  3. Convert the price to a string. Isolate the appropriate character. Convert it to an int. Store in the appropriate variable. Repeat for all. What's the problem?
 
Ahmad ElGendi: I am new to mql5 & programming in general and need a little help here. I need to break (trim) each number "if the dot is found should be ignored, and store it in a separate variable. where i can start dealing with it.


First question is ... Why? This is important because you may be creating more difficulty for yourself if you approach the problem (and the solution) in the wrong way.

Here is one possible solution, a numerical approach  ...

  1. Don't use a sequence of variables for that. That is sloppy coding (and also much more difficult)! Use an Array! Follow the link and read the documentation on arrays!
  2. To get rid of the decimal point, and to facilitate processing the number to split the individual digits, multiply the value first by the desired factor and then typecast it into an integer or long data type and assign it to a variable for later usage.
  3. Now create a loop (e.g. "for" loop) that will go over the number of digits you want to process.
  4. For each loop iteration, obtain the current digit with the mod of 10 ( "%" operator - Remainder of division). and assign it to the array element by use of the loop's index.
  5. Advance to the next digit by dividing the number by 10 and continue the loop.

That was the basics of the numerical approach. You could probably also approach the problem using string conversion as William mentioned, with either the DoubleToString() function or the StringFormat() function. You would then loop through each character sub-string, and convert it back into a number. This method is not so efficient but it might be easier for you to understand.

However, all of this is just theoretical if you are not able to understand it or lack the knowledge to carry it out. For that, I suggest you do some reading ...

Forum on trading, automated trading systems and testing trading strategies

Something Interesting to Read

Sergey Golubev, 2017.09.16 05:48

Expert Advisor Programming for MetaTrader 5


This book will teach you the following concepts:

  • Learn the basics of MQL5, including variables and data types, operators, functions, event handlers, and object-oriented programming.
  • Place, modify and close market and pending orders.
  • Calculate, verify and add stop loss and take profit prices to an open position.
  • Add a flexible trailing stop and/or break even stop to your strategy.
  • Manage your trade risk with money management.
  • Use pending orders to scale in and out of positions.
  • Use price, time and indicator data in your expert advisors.
  • Control program execution by trading on new bar open, and add flexible trade timers to your strategies.
  • Walk through the creation of several basic trading strategies from start to finish.
  • Inform the user with dialog boxes, email alerts, mobile notifications and sounds.
  • Draw trend lines, arrows and text labels on the chart.
  • Read and write data to CSV files.
  • Learn the basics of creating indicators, scripts and libraries in MetaEditor.
  • Debug, test and optimize your trading strategy.
  • And much more!

Whether you’re an experienced programmer moving from MQL4, or a novice just starting with MQL5, this book will give you the foundation to quickly program fully-featured and robust trading systems.

 
Fernando Carreiro:


First question is ... Why? This is important because you may be creating more difficulty for yourself if you approach the problem (and the solution) in the wrong way.

Here is one possible solution, a numerical approach  ...

  1. Don't use a sequence of variables for that. That is sloppy coding (and also much more difficult)! Use an Array! Follow the link and read the documentation on arrays!
  2. To get rid of the decimal point, and to facilitate processing the number to split the individual digits, multiply the value first by the desired factor and then typecast it into an integer or long data type and assign it to a variable for later usage.
  3. Now create a loop (e.g. "for" loop) that will go over the number of digits you want to process.
  4. For each loop iteration, obtain the current digit with the mod of 10 ( "%" operator - Remainder of division). and assign it to the array element by use of the loop's index.
  5. Advance to the next digit by dividing the number by 10 and continue the loop.

That was the basics of the numerical approach. You could probably also approach the problem using string conversion as William mentioned, with either the DoubleToString() function or the StringFormat() function. You would then loop through each character sub-string, and convert it back into a number. This method is not so efficient but it might be easier for you to understand.

However, all of this is just theoretical if you are not able to understand it or lack the knowledge to carry it out. For that, I suggest you do some reading ...


Hello Fernando , 

<Non English Deleted>

I will follow the learning path of course , I am already planning for some programming courses, but i need any help to break up the Price, I do it in excel very easily and I have good results , the reverse happens when the summation of numbers equals to (9). based on 3-6-9 

any guidance will be appreciated ,, <Non English Deleted> :)  

WHY , a good quest

 
Ahmad ElGendi: any guidance will be appreciated

Both I and William have already provided you with guidance on both numerical and string approaches. Since you barely know anything about coding, any extra information or details would not be understandable to you.

Reason: