How do you write a function that returns the low and the top of the moving average for a specified number of candles in mql4? - mql4

I hope someone guides me,
I want to calculate the lowest moving average level during a certain number of candles, as well as the highest moving average level during a certain number of candles.
That is, I want the moving average top and the moving average bottom, not the price.
How do I write a function that returns these values ​​to me?
Thank you for your interest and help

You should try to attempt to write the code yourself, you will learn a lot more and get more help along the way when you hit any problems.
int startCandle=1;
int endCandle=100;
double highest=NULL, lowest=NULL;
for(int i=startCandle; i<=endCandle; i++)
{
double ma=iMA(NULL, 0, 10, 0, MODE_SMA, PRICE_CLOSE, i);
if(highest==NULL){highest=ma; lowest=highest;}
if(ma>highest) highest=ma;
if(ma<lowest) lowest=ma;
}

Related

How To Find Remaining Distance To Move UIView Between 2 Points

SCENARIO
I am working on an application that keeps track of blood glucose levels into a graph. On the graph there are "markings" (ex: -200mg) going in vertical order along the y axis on the right side of the screen and "hours" (ex: -12:00 PM) will be along the x axis on the bottom of the graph. I have to plot out little 'dots' to display what the blood glucose level was throughout the way.
ISSUE
I am trying to calculate how to position the 'dots' in the correct time and mg level and I'm having difficulty calculating the positions. I can access the "markings" and retrieve it's marking.center.x to indicate which 'Time Slot' (x axis) and the marking.center.y to indicate which 'MG Level' the 'dot' needs to go into. Problem is it isn't always exactly 12:00 PM or 200mg where it will need to be placed. In fact that would be very rare.
WHAT I NEED
Based on the following variables:
dot.mgLevel
The dot will already know where it needs to go based on the information retrieved from the medical device. It will know the time and mgLevel to assign itself.
marking.mgLevel
The markings will each have evenly distributed values that such as -100mg, -200mg, -300mg ect...
timemarking.timeslot
Each time marking on the bottom will each have evenly distributed times allocated every 30 min. Such as -12:00PM, -12:30PM, -1:00PM ect...
If the dot has a mg Level of 330mg and the closest marking on the mg Level is 300mg, then I need to be able to calculate how much further up the dot needs to move from 300 going towards the 400mg marker.
SO...
If the distance between the markings are 100pt and the dot's mgLevel is 330mg, then I know that I need to move the dot from the 300mg marking toward the 400mg marking by exactly 30pt. That's because it's simple math because the distance between the markings is 100. But in real life it isn't 100, so I need to be able to calculate this.
MY ULTIMATE QUESTION
Say distance between markings is 241 and each marking represents multiples of a hundred. Say my dot has a mgLevel of 412. How do I calculate how far I need to move the dot so that it will be in the correct place?
I THINK?
I think I need to make 241 equal 100%. But I need help.
Distance between markings is 241pt
Markings are multiples of 100mg
1mg will occupy 2.41pt. So 412mg will occupy (2.41 * 412) pt. To know how much to move for the next dot, take the difference in mg and multiply by 2.41.
In general, if distance between 2 markings in points is d, markings are multiples of m, and desired accuracy is k decimal places, 1mg will occupy g:
let divisor = pow(10.0, Double(k))
let g = round((d/m)*divisor) / divisor

How random is arc4random (mac os x)? (or what am I doing wrong?)

I'm playing with an optimized game of life implementation in swift/mac_os_x. First step: randomize a big grid of cells (50% alive).
code:
for(var i=0;i<768;i++){
for(var j=0;j<768;j++){
let r = Int(arc4random_uniform(100))
let alive = (aliveOdds > r)
self.setState(alive,cell: Cell(tup:(i,j)),cells: aliveCells)
}
}
I expect a relatively uniform randomness. What I get has definite patterns:
Zooming in a bit on the lower left:
(I've changed the color to black on every 32 row and column, to see if the patterns lined up with any power of 2).
Any clue what is causing the patterns? I've tried:
replacing arc4random with rand().
adding arc4stir() before each arc4random_uniform call
shifting the display (to ensure the pattern is in the data, not a display glitch)
Ideas on next steps?
You cannot hit period or that many regular non-uniform clusters of arc4random on any displayable set (16*(2**31) - 1).
These are definitely signs of the corrupted/unininitialized memory. For example, you are initializing 768x768 field, but you are showing us 1024xsomething field.
Try replacing Int(arc4random_uniform(100)) with just 100 to see.

How to move image with low values?

The problem is simple: I want to move (and later, be able to rotate) an image. For example, every time i press the right arrow on my keyboard, i want the image to move 0.12 pixels to the right, and every time i press the left arrow key, i want the image to move 0.12 pixels to the left.
Now, I have multiple solutions for this:
1) simply add the incremental value, i.e.:
image.x += 0.12;
this is of course assuming that we're going to the right.
2) i multiplicate the value of a single increment by the times i already went into this particular direction + 1, like this:
var result:Number = 0.12 * (numberOfTimesWentRight+1);
image.x = result;
Both of these approaches work but produce similiar, yet subtly different, results. If we add some kind of button component that simply resets the x and y coordinates of the image, you will see that with the first approach the numbers don't add up correctly.
it goes from .12, .24, .359999, .475 etc.
But with the second approach it works well. (It's pretty obvious as to why though, it seems like += operations with Numbers are not really precise).
Why not use the second approach then? Well, i want to rotate the image as well. This will work for the first attempt, but after that the image will jump around. Why? In the second approach we never took the original position of the image in account. So if the origin-point shifts a bit down or up because you rotated your image, and THEN you try to move the image again: it will move to the same position as if you hadn't rotated before.
Alright, to make this short:
How can i reliably move, scale and rotate images for 1/10 of a pixel?
Short answer: I don't know! You're fighting with floating point math!
Luckily, I have a workaround, if you don't mind.
You store the location (x and y) of the image in a separate variable... at a larger scale. Such as 100x. So 123.45 becomes 12345, and you then divide by 100 to set the attribute that flash uses to display.
Yes, there are limits to number sizes too, but if you're willing to accept some error rate, and the fact that you'll be limited to, I dunno, a million pixels in each direction, you can fit it in a regular int. The only rounding error you will encounter will be a single rounding error when you divide by 100 (or the factor you used). So instead of the compound rounding error which you described (0.12 * 4 = 0.475), you should see things like 0.47999999. Which doesn't matter because it's, well, so small.
To expand on #Pimgd answer a bit, you're probably hitting a floating point error (multiple +='s will exaggerate the error more than one *='s) - Numbers in Flash are 53-bit precision.
There's also another thing to keep in mind, which is probably playing a bigger role with such small movement values; Flash positions all objects using twips, which is roughly about 1/20th of a pixel, or 0.05, so all values are rounded to this. When you say image.x += 0.12, it's actually the equivalent of image.x += 0.10, hence which the different becomes apparent; you're losing 0.02 of a pixel with every move.
You should be able to get around it by moving to another scale, as #Pimgd says, or just storing your position separately - i.e. work from a property _x rather than image.x so you're not losing that precision everytime:
this._x += 0.12;
image.x = this._x;

Adding two integers

In my game, the speed of the ball increases by a certain number every 15 seconds when the game has started. Right now I have an int which is the speed of the ball and the the method:
ballSpeed += 1;
This works but when I decrease the amount that the ball speed is increased by, which is going to be less than 1, for some reason it doesn't work.
For example, if I do:
ballSpeed += .9
, for some reason it doesn't work.
I thought that I just couldn't see the difference because it was too small but when i have .99 it still doesn't work and I would be able to tell the difference with that number.
Any answer is greatly appreciated.
Thanks
You're using integers? .9 is not an integer value so you'd have to use a different datatype (eg:float) to do so.
Since ballSpeed is an integer, it can only take on whole values. So using the code
ballSpeed += .9;
will cause it to round down, and the end result being ballSpeed won't change at all. So 1 is the smallest change you can make.
You can't use an int to hold a decimal number. Try a float instead of an int.

Opencv cvHoughCircles-Parameters doubts

I have some doubts about cvHoughCircles parameters. I have an image that has some circles and I want to count them, the count gives me a wrong number of circles.
So I don't know how to choose some function's parameters like:
dp,min_dist,param1,param2,min_radius, max_radius.
I don't know what numbers I use in this parameters. How Can I calculate this?
Choosing the parameters depends on the images you are using. An explanation of the parameters themselves can be found in the reference here
http://opencv.willowgarage.com/documentation/cpp/imgproc_feature_detection.html#cv-houghcircles
Using the function with the following parameters
HoughCircles(gray, circles, CV_HOUGH_GRADIENT,2, gray->rows/4, 200, 100, 10, 50);
Will make it search for circles with a dp of 2, a minimum distance between the circles of 1/4 of the image and accumulator values of max 200,100 that determine what to accept as a circle. The 10 and 50 are minimum and maximum radius for the circles to accept.
If you have trouble finding these parameters try to make a test program that attaches these values to sliders so you can see the result on a test image.
Especially param2 is something that is difficult to determine by measuring. Because you know how many circles are in your image you can do a parameter crawl in the following way:
for(int i=0;i<200;i++) {
cv::HoughCircles(gray, circles, CV_HOUGH_GRADIENT,2, gray->rows/4, 200, i, 10, 50);
std::cout<<"HoughCircles with param2="<<i<<" gives "<<circles.size()<<" circles"<<endl;
}
I don't know how param1 and 2 are exactly related but you could do the same with a double for loop to find the optimum. The other values need to be measured from the picture. In stead of making a screenshot you can also save this image with the function:
cvSaveImage("image.jpg",gray);
To make sure you are really measuring the exact picture.

Resources