How identify previous low or high from zigzag - mql4

this is icustom calling function and get low ang high value.
double highzigzag = iCustom(NULL,0,"ZigZag",InpDepth,InpDeviation,InpBackstep,1,0);
double lowzigzag = iCustom(NULL,0,"ZigZag",InpDepth,InpDeviation,InpBackstep,2,0);
And how can code previous low and high value to compare as
if previous low > current low ,continue trend
if previous low < current low ,trend change
zigzag

the last placeholder at the function "iCustom(......,X)" is the number of the Bar u want to check in your code: double highzigzag = iCustom(NULL,0,"ZigZag",InpDepth,InpDeviation,InpBackstep,1,HERE); double lowzigzag = iCustom(NULL,0,"ZigZag",InpDepth,InpDeviation,InpBackstep,2,HERE);
u are checking with 0 = Current OpenBar
u need to change it to 1=First Finished Bar or 2=Second Finished Bar as example
double highzigzag = iCustom(NULL,0,"ZigZag",InpDepth,InpDeviation,InpBackstep,1,1);
double lowzigzag = iCustom(NULL,0,"ZigZag",InpDepth,InpDeviation,InpBackstep,2,1);
in this example i'm checking the first finished bar but instead of typing the bar number u should use the function within a for-loop and replace the bar number through "i"
if it helps dont forget to like

Just to complet what has been said by #noSkill06s here is the loop
int longRange = 100;
double highzigzag = 0,
lowzigzag = 0;
for(int i = 1;i<longRange;i++){
highzigzag =
iCustom(Symbol(),PERIOD_CURRENT,"ZigZag",InpDepth,InpDeviation,InpBackstep,1,i);
if(highzigzag!=0) break;
}
for(int i = 1;i<longRange;i++){
lowzigzag =
iCustom(Symbol(),PERIOD_CURRENT,"ZigZag",InpDepth,InpDeviation,InpBackstep,2,i);
if(lowzigzag!=0) break;
}
Comment("hight :"+highzigzag +"\nLow : "+lowzigzag );

Related

How to count how many bars back when closing price crossed and close above certain moving average to known swing low and swing high?

I need to get dynamic bar count using moving average to find swing low and swing high. Please check screenshot for better understanding Thanks in advance
https://imgur.com/OQGy239
double mafast = iMA(NULL,0,15,0,MODE_SMA,PRICE_CLOSE,1);
double maslow = iMA(NULL,0,30,0,MODE_SMA,PRICE_CLOSE,1);
int i=1;
for (i = 1; i<=Bars; i++)
{
if (mafast<maslow || mafast>maslow);
}
int low = iLowest(NULL,0,MODE_LOW,i-1,0);
int high =iHighest(NULL,0,MODE_HIGH,i-1,0);
Print(Low[low]);
Print(High[high]);
}
actual 'Bars' function is getting value for entire chart but i need that should be quantify based on Moving Average price close above or below with strong move

Convert circular, continuous UISlider value to a counter

In my app, I have created a circular, continuous slidable CircularSlider (this one). It has a minimumValue of 1.0 and a maximumValue of 10. Now, I'd like my users to be able to just keep sliding in circles to increase an NSInteger used somewhere in the app.
I tried doing that by writing the code below.
- (void)sliderChanged:(UICircularSlider *)slider {
int val = (int)slider.value;
slider.value = val;
int delta = val - self.oldValue;
if (self.oldValue == self.circularSlider.maximumValue && val == 0) {
delta = 1;
}
self.number += delta;
self.oldValue = val;
}
This works, but really sketch. Sometimes the value will drop by 10, caused by the slider giving me value of 10 and right afterwards a value of 0. It also doesn't work if the users starts scrubbing backwards and numbers start decreasing. I was wondering if there's a better way to achieve the same thing. Any ideas?
I think a better approach is to first figure out what direction the user moved. This can be done by checking which direction has the closest distance between the new and the old position. Care must be taken to check for passing the border. Then it is a simple matter of adjusting the delta depending on direction.
My solution code assumes the the slider starts at zero, which I would recommend you to use instead of 1, since calculations are easier. If you really want to start at 1 it can be adjusted. I also have defined the maximum value as a constant SLIDER_MAX_VALUE, which you could change to a variable instead. Finally, I changed self.number to a CGFloat, so do the cast when using the number instead. This is important, otherwise you get rounding errors when sliding. If you really want an integer, use two variables, and assign the integer variable from the float.
#define SLIDER_MAX_VAL 10
- (void)sliderChanged:(UICircularSlider *)slider {
CGFloat delta = slider.value - self.oldValue;
if ((delta > 0 && delta < SLIDER_MAX_VAL / 2) ||
(delta < 0 && delta < -SLIDER_MAX_VAL / 2)) {
// Moving forward
if (delta < 0)
delta += SLIDER_MAX_VAL;
} else {
// Moving backward
if (delta > 0)
delta -= SLIDER_MAX_VAL;
}
self.number += delta; // Change to CGFloat
self.oldValue = slider.value;
}

(iOS) Accelerometer Graph (convert g-force to +/- 128) granularity

I am using this Accelerometer graph from Apple and trying to convert their G-force code to calculate +/- 128.
The following image shows that the x, y, z values in the labels do not match the output on the graph: (Note that addX:y:z values are what is shown in the labels above the graph)
ViewController
The x, y, z values are received from a bluetooth peripheral, then converted using:
// Updates LABELS
- (void)didReceiveRawAcceleromaterDataWithX:(NSInteger)x Y:(NSInteger)y Z:(NSInteger)z
{
dispatch_async(dispatch_get_main_queue(), ^{
_labelAccel.text = [NSString stringWithFormat:#"x:%li y:%li z:%li", (long)x, (long)y, (long)z];
});
}
// Updates GRAPHS
- (void)didReceiveAcceleromaterDataWithX:(NSInteger)x Y:(NSInteger)y Z:(NSInteger)z
{
dispatch_async(dispatch_get_main_queue(), ^{
float xx = ((float)x) / 8192;
float yy = ((float)y) / 8192;
float zz = ((float)z) / 8192;
[_xGraph addX:xx y:0 z:0];
[_yGraph addX:0 y:yy z:0];
[_zGraph addX:0 y:0 z:zz];
});
}
GraphView
- (BOOL)addX:(UIAccelerationValue)x y:(UIAccelerationValue)y z:(UIAccelerationValue)z
{
// If this segment is not full, then we add a new acceleration value to the history.
if (index > 0)
{
// First decrement, both to get to a zero-based index and to flag one fewer position left
--index;
xhistory[index] = x;
yhistory[index] = y;
zhistory[index] = z;
// And inform Core Animation to redraw the layer.
[layer setNeedsDisplay];
}
// And return if we are now full or not (really just avoids needing to call isFull after adding a value).
return index == 0;
}
- (void)drawLayer:(CALayer*)l inContext:(CGContextRef)context
{
// Fill in the background
CGContextSetFillColorWithColor(context, kUIColorLightGray(1.f).CGColor);
CGContextFillRect(context, layer.bounds);
// Draw the grid lines
DrawGridlines(context, 0.0, 32.0);
// Draw the graph
CGPoint lines[64];
int i;
float _granularity = 16.f; // 16
NSInteger _granualCount = 32; // 32
// X
for (i = 0; i < _granualCount; ++i)
{
lines[i*2].x = i;
lines[i*2+1].x = i + 1;
lines[i*2].y = xhistory[i] * _granularity;
lines[i*2+1].y = xhistory[i+1] * _granularity;
}
CGContextSetStrokeColorWithColor(context, _xColor.CGColor);
CGContextStrokeLineSegments(context, lines, 64);
// Y
for (i = 0; i < _granualCount; ++i)
{
lines[i*2].y = yhistory[i] * _granularity;
lines[i*2+1].y = yhistory[i+1] * _granularity;
}
CGContextSetStrokeColorWithColor(context, _yColor.CGColor);
CGContextStrokeLineSegments(context, lines, 64);
// Z
for (i = 0; i < _granualCount; ++i)
{
lines[i*2].y = zhistory[i] * _granularity;
lines[i*2+1].y = zhistory[i+1] * _granularity;
}
CGContextSetStrokeColorWithColor(context, _zColor.CGColor);
CGContextStrokeLineSegments(context, lines, 64);
}
How can I calculate the above code to show the correct accelerometer values on the graph with precision?
I post this as an aswer not a comment, because I have not enough reputation, but what I'll write might be enough to send you in the right direction, that it even may count as an answer...
Your question still doesn't include what is really important. I assume the calculation of the xx/yy/zz is no problem. Although I have no idea what the 8192 is supposed to mean.
I guess the preblem is in the part where you map your values to pixel coordinates...
the lines[] contains your values in a range of 1/8192th of the values in the label. so your x value of -2 should be at a pixel position of -0.0000something, so slightly(far less than 1 Pixel) above the view... Because you see the line a lot further down there must be some translation in place (not shown in your code)
The second part that is important but not shown is DrawGridlines. Probably in there is a different approach to map the values to pixel-coordinates...
Use the debugger to check what pixel-coordinates you get when draw your +127-line and what you get if you insert the value of +127 in your history-array
And some Ideas for improvements when reading your code:
1.)Put the graph in it's own class that draws one graph(and has only one history. Somehow you seem to have that partially already (otherwise I cannot figure out your _xGraph/_yGraph/_zGraph) But on the other hand you draw all 3 values in one drawLayer??? Currently you seem to have 3*3 history buffers of which 3*2 are filled with zeros...
2.) use one place where you do the calculation of Y that you use both for drawing the grid and drawing the lines...
3.) use CGContextMoveToPoint(); + CGContextAddLineToPoint(); instead of copying into lines[] with these ugly 2*i+1 indecies...

objective-c: proper way to generate a lot of instances of a certain class without creating a memory leak?

I'm making a simple SpriteKit game. It has one randomly generated world made up of lines. These lines are represented by the NHRLineNode class. I generate these lines all at once in the beginning of the level with two for loops, one for each side of the screen. This works fine. In addition to the main gameplay scene of the game, there is a "game over" screen that displays between plays and shows your score etc and a main menu scene. The problem comes when I play the game, die, see the game over screen, and play again. Looking at the memory usage in Xcode, it seems like the memory usage goes up when I first start the game from the menu scene, stays steady throughout the gameplay, and then jumps 6-10 MB when I die. This memory is never regained and the app uses more and more memory every time I play. I think this is because my for loops that generate the platforms are just creating a new instance of the NHRLineNode class, positioning it correctly, and then doing it again. Is this what is causing my memory issues? Or is it more likely something on the game over scene?
Relevant snippets:
The for loops that generate the platforms:
int previousXVal1 = -10;
int previousYVal1 = 425;
int newXPosition = 0;
//How many do you want?
int numToGen = 100;
for(int i = 1; i<=numToGen; i++) {
NHRLineNode *lineGen = [NHRLineNode initAtPosition:CGPointMake(previousXVal1 + arc4random_uniform(85), previousYVal1 - 75)];
[worldNode addChild:lineGen];
if(lineGen.position.x > 390) {
newXPosition = lineGen.position.x - 100; //That should bring it onscreen!
lineGen.position = CGPointMake(newXPosition, lineGen.position.y); //Make the new position
} else if (lineGen.position.x < 100) {
newXPosition = lineGen.position.x + 100; //That will bring it onscreen!!
lineGen.position = CGPointMake(newXPosition, lineGen.position.y);
}
previousXVal1 = lineGen.position.x;
previousYVal1 = lineGen.position.y;
}
//This creates the lines on the right side (performing the inverse calculation on the x pos
//int numToGen = 10;
int previousXVal2 = 350;
int previousYVal2 = 525;
for(int i = 1; i<=numToGen; i++) {
NHRLineNode *lineGen = [NHRLineNode initAtPosition:CGPointMake(previousXVal2 - arc4random_uniform(85), previousYVal2 - 75)];
[worldNode addChild:lineGen];
if(lineGen.position.x > 390) { //It is partially off-screeb=n
newXPosition = lineGen.position.x - 100; //That should bring it onscreen!
lineGen.position = CGPointMake(newXPosition, lineGen.position.y); //Make the new position
} else if (lineGen.position.x < 100) { //It is partially off-screen
newXPosition = lineGen.position.x + 100; //That will bring it onscreen!!
lineGen.position = CGPointMake(newXPosition, lineGen.position.y);
}
previousXVal2 = lineGen.position.x;
previousYVal2 = lineGen.position.y;
}
The initAtPosition method of NHRLineNode:
+(id)initAtPosition:(CGPoint)point {
//This is all the properties of one of the lines in the level
SKSpriteNode *theLine = [SKSpriteNode spriteNodeWithImageNamed:#"newLine"];
//The physics body is slightly smaller than the image itself -- why idk
theLine.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(75,5)];
//It is not affected by gravity
theLine.physicsBody.dynamic = NO;
//Its position is the point given us when the function was called
theLine.position = point;
//Return it for further positioning by the generator
return theLine;
}
Entire implementation of GameOverScene: http://pastebin.com/wUpguueb
Thanks for your help.
Even though you have ARC on your side, sometimes you need to give it "incentive" to free objects, especially when creating many in a loop. Really, this simply has to do with providing scope, so ARC understands that it may release allocated instances.
Try wrapping the bodies of your for loops (i.e. not around the entire for loops), with #autoreleasepool { …} - i.e., as if that #autoreleasepool block is the only statement for the for loop.
Let is know if it helps! I commonly have to do this when iteratively importing data to Core Data.

Using Core Motion in landscape mode

I am currently working on augmented reality and for that purpose I'd like to use the gyroscope and Core Motion. I've studied the Apple pARk sample code, I understand most of the maths I've spend time on reading documentation because at first glance it was not clear! Everything is fine until I try to make it work in landscape mode.
I won't explain all the theory here it would be too long. But for those who experienced it, my problem is, we take the rotation matrix of the attitude to apply this rotation to our coordinates. Ok, it is fine until here, but it seems Core Motion doesn't adapt it to Landscape Mode. I saw similar questions on this subject but it looks like no one has a solution.
So I tried to make my own, here is what I think:
Everytime we rotate the device to landscape, a rotation of +-90° is made (depending on Landscape Left or right). I decided to create a 4X4 rotation matrix to apply this rotation. And then multiply it to the cameraTransform matrix (adaption of the attitude's 3X3 CMRotationMatrix to 4X4), we obtain then the matrix cameraTransformRotated:
- (void)createMatLandscape{
switch(cameraOrientation){
case UIDeviceOrientationLandscapeLeft:
landscapeRightTransform[0] = cos(degreesToRadians(90));
landscapeRightTransform[1] = -sin(degreesToRadians(90));
landscapeRightTransform[2] = 0;
landscapeRightTransform[3] = 0;
landscapeRightTransform[4] = sin(degreesToRadians(90));
landscapeRightTransform[5] = cos(degreesToRadians(90));
landscapeRightTransform[6] = 0;
landscapeRightTransform[7] = 0;
landscapeRightTransform[8] = 0;
landscapeRightTransform[9] = 0;
landscapeRightTransform[10] = 1;
landscapeRightTransform[11] = 0;
landscapeRightTransform[12] = 0;
landscapeRightTransform[13] = 0;
landscapeRightTransform[14] = 0;
landscapeRightTransform[15] = 1;
multiplyMatrixAndMatrix(cameraTransformRotated, cameraTransform, landscapeRightTransform);
break;
case UIDeviceOrientationLandscapeRight:
landscapeLeftTransform[0] = cos(degreesToRadians(-90));
landscapeLeftTransform[1] = -sin(degreesToRadians(-90));
landscapeLeftTransform[2] = 0;
landscapeLeftTransform[3] = 0;
landscapeLeftTransform[4] = sin(degreesToRadians(-90));
landscapeLeftTransform[5] = cos(degreesToRadians(-90));
landscapeLeftTransform[6] = 0;
landscapeLeftTransform[7] = 0;
landscapeLeftTransform[8] = 0;
landscapeLeftTransform[9] = 0;
landscapeLeftTransform[10] = 1;
landscapeLeftTransform[11] = 0;
landscapeLeftTransform[12] = 0;
landscapeLeftTransform[13] = 0;
landscapeLeftTransform[14] = 0;
landscapeLeftTransform[15] = 1;
multiplyMatrixAndMatrix(cameraTransformRotated, cameraTransform, landscapeLeftTransform);
break;
default:
cameraTransformRotated[0] = cameraTransform[0];
cameraTransformRotated[1] = cameraTransform[1];
cameraTransformRotated[2] = cameraTransform[2];
cameraTransformRotated[3] = cameraTransform[3];
cameraTransformRotated[4] = cameraTransform[4];
cameraTransformRotated[5] = cameraTransform[5];
cameraTransformRotated[6] = cameraTransform[6];
cameraTransformRotated[7] = cameraTransform[7];
cameraTransformRotated[8] = cameraTransform[8];
cameraTransformRotated[9] = cameraTransform[9];
cameraTransformRotated[10] = cameraTransform[10];
cameraTransformRotated[11] = cameraTransform[11];
cameraTransformRotated[12] = cameraTransform[12];
cameraTransformRotated[13] = cameraTransform[13];
cameraTransformRotated[14] = cameraTransform[14];
cameraTransformRotated[15] = cameraTransform[15];
break;
}
}
Then just before we update all the points I do this:
multiplyMatrixAndMatrix(projectionCameraTransform, projectionTransform, cameraTransformRotated);
After that the rest of the code remains unchanged, I just want the annotation to be displayed properly in landscape orientation. For now this is the only idea I have, the rendering in landscape is not good, I move the device to the right or the left hand-side, the annotations go down or up (like it was when I didn't add this code).
Has anyone come up with a solution? I'll keep on searching, especially on the CMRotationMatrix, it doesn't seem it is a typical rotation matrix, I can't find any documentation saying precisely what are the different elements of this matrix.
I just managed to adapt this (Apple's pARk sample) to landscape (right) yesterday and would like to share the changes made. It appears to work correctly, but please call out any mistakes. This only supports landscape right but can probably be adapted easily for left.
In ARView.m,
In -(void)initialize, switch the bounds height and width
createProjectionMatrix(projectionTransform, 60.0f*DEGREES_TO_RADIANS, self.bounds.size.height*1.0f / self.bounds.size.width, 0.25f, 1000.0f);
In -(void)startCameraPreview
[captureLayer setOrientation:AVCaptureVideoOrientationLandscapeRight];
In -(void)drawRect:
//switch x and y
float y = (v[0] / v[3] + 1.0f) * 0.5f;
float x = (v[1] / v[3] + 1.0f) * 0.5f;
poi.view.center = CGPointMake(self.bounds.size.width-x*self.bounds.size.width, self.bounds.size.height-y*self.bounds.size.height); //invert x

Resources