How to get price of Chart HLine objects and calculate Fibonacci levels - mql4

Three part question:
How to find 2 user created horizontal lines on a chart by name and return the price of each.
Then determine which HLine was crossed by the price most recently to determine trend direction.
Calculate Fibonacci levels based on prices and direction

double value = ObjectGetDouble(0,nameOfHLine,OBJPROP_PRICE1);
this is your value if you have name of the object, if you dont have it - need to loop over all objects:
string name;
for(int i=ObjectsTotal()-1;i>=0;i--){
name = ObjectName(i);
if(ObjectType(name)!=OBJ_HLINE) continue;
}

Working example of Fibonacci object that can be edited by the user and printing of fibonacci levels.
#include <ChartObjects/ChartObjectsFibo.mqh>
CChartObjectFibo *Fibo;
int OnInit()
{
Fibo = new CChartObjectFibo();
#Create object and set some defaults
if(!Fibo.Create(0,"Fibonacci",0,Time[5],Open[5],Time[0],Open[0]))
{
return(INIT_FAILED);
}
# Allow user to drag object
Fibo.Selectable(true);
return(INIT_SUCCEEDED);
}
void OnDeinit(const int reason)
{
delete Fibo;
}
void OnTick()
{
string level_description;
double level_value;
string printString="Fibonacci Levels - ";
# Get the two anchor prices
double p1 = Fibo.GetDouble(OBJPROP_PRICE,0);
double p2 = Fibo.GetDouble(OBJPROP_PRICE,1);
# Calculate range
double range=MathAbs(p1-p2);
for(int i=0;i<Fibo.LevelsCount();i++)
{
level_description=Fibo.LevelDescription(i);
# Calculate price of level
level_value=(p2>p1)?p2-range*Fibo.LevelValue(i):p2+range*Fibo.LevelValue(i);
printString=StringFormat("%s %s:%.5f",printString,level_description,level_value);
}
Print(printString);
}

Difficult to understand exactly what you are after, not sure if you are trying to find the graphical objects or just calculate levels based on the prices. Assuming you have the price of the two horizontal lines, the following structure and function can be used to calculate Fibonacci levels. (price 1 is earlier in time than price 2).
Calculation based on formula found here
struct FibLevel {
double retrace38;
double retrace50;
double retrace61;
double extension61;
double extension100;
double extension138;
double extension161;
};
void FibLevel(double price1, double price2,FibLevel &fiblevel)
{
double range = MathAbs(price1-price2);
fiblevel.retrace38 =(price1<price2)?price2-range*0.382:price1+range*0.382;
fiblevel.retrace50 =(price1<price2)?price2-range*0.500:price1+range*0.500;
fiblevel.retrace61 =(price1<price2)?price2-range*0.618:price1+range*0.618;
fiblevel.extension61 =(price1<price2)?price2+range*0.618:price1-range*0.618;
fiblevel.extension100=(price1<price2)?price2+range :price1-range;
fiblevel.extension138=(price1<price2)?price2+range*1.382:price1-range*1.382;
fiblevel.extension161=(price1<price2)?price2+range*1.618:price1-range*1.618;
}

Related

Convert a continuous range of doubles to a discrete int range

i want to map a continuous range of double values [10.0,20.0] to a byte in the range of [100,200];
The following must apply (even when converting back and forth multiple times):
convertToByte(convertToDouble(y)) == y
Thanks.
With help of the comments to the question we're using the following code:
int convertToByte(
double initial,
double minValue,
double maxValue,
int minByte,
int maxByte,
) {
double value = initial - minValue;
double valueRange = maxValue - minValue;
int byteRange = maxByte - minByte;
double valueSteps = valueRange / byteRange;
double byte = (value / valueSteps);
return (minByte + byte.round()).clamp(minByte, maxByte);
}
This does not provide a solution for the specified test, but a deterministic answer for a specific value.
When converting a byte back to a value and vice versa multiple times the output always stays the same.
This is what we needed for our application.

Truncate to 2 decimal places without rounding

Can't find or work out a solution to this that works with Dart.
I've already tried:
1. toStringAsFixed() - this rounds
2. double.parse(number.toStringAsFixed()) - this rounds
3. num - num % 0.01 - this rounds
I've found some solutions on SO but they either use functions that aren't available on Flutter/Dart or didn't work for me.
Any help would be greatly appreciated.
To build on #Irn beautiful Answer.
The function below lets you specify how many fractional digits / how many decimal places you want
double truncateToDecimalPlaces(num value, int fractionalDigits) => (value * pow(10,
fractionalDigits)).truncate() / pow(10, fractionalDigits);
Example
truncateToDecimalPlace(4321.92385678, 3) // 4321.923
Or my favorite way is to use Dart extensions
extension TruncateDoubles on double {
double truncateToDecimalPlaces(int fractionalDigits) => (this * pow(10,
fractionalDigits)).truncate() / pow(10, fractionalDigits);
}
This lets you call the function on a double object like thus
Usage
4321.92385678.truncateToDecimalPlaces(3); // 4321.923
Try
double truncateToHundreths(num value) => (value * 100).truncate() / 100;
There will be cases where you lose precision, but that requires you to use almost all 53 bits of double precision in the number (when multiplying by 100 loses precision to begin with).
Going trough strings is a significant overhead that I would avoid if possible, unless you actually expect to convert the number to a string anyway afterwards.
(value * 100).truncateToDouble() / 100
Example:
var lat = 29.4562
lat * 100 = 2945.62
2945.62 truncateToDouble() = 2945
2945 / 100 = 29.45
I have used the below method
String toFixed(double value, [int decimalPlace = 1]) {
try {
String originalNumber = value.toString();
List<String> formattedNumber = originalNumber.split('.');
return "${formattedNumber[0]}.${formattedNumber[1].substring(0, decimalPlace)}";
} catch (_) {}
return value.toString();
}

How to generate a random number in dart excluding a particular no.?

So guys how do we generate a random number between a range but that shouldnt contain a particular no. in that range in dart?
If you want to print random numbers from 0 to 999 except say, the number 100.
Then the following code fragment will be sufficient.
import 'dart:math';
void main() {
var n = 100;
do {
r = rng.nextInt(1000);
} while (r == n);
print(r);
}
Depends on requirements for time, and distribution of result, say you wish to preserve even distribution and want to avoid calling a new random number, and are using the range 0-2000 and filling in 100
import 'dart:math';
void main() {
var n = 100;
do {
r = rng.nextInt(2000);
}
if (r >= n){
r++
}
print(r);
}
Let me show u an example;
// created my method to get random numbers and use wherever I want
// used math library to create numbers randomly and equaled to numbers.
// now we have to check if it will contains 0 so we will call method again to create new random numbers list which one no including 0.
import 'dart:math';
int void randomNumbers()
{
int numbers=Random().NextInt(10);
if(numbers!=0)
{
return numbers;
}
else
{
int newNumbers= randomNumbers();
return newNumbers,
}
}
so u can call that method created below in anytime to anywhere.
Here is an example: We set a FlatButton and when pressed, the var "leftdicenumber" receives a random number betxeen 1 and 6:
FlatButton(
onPressed: () {
leftdicenumber = Random().nextInt(6) + 1;
},);

Fitness Function - What to do, if value of one Chromosome features is much higher than other ones

I am trying to write a Fitness Function for my Genetic Algorithm. I have got three features (F1,F2,F3 - All are numeric variables), which are taken into consideration for rating the chromosome. If all of them are equally important I can write such equation:
If their importance would be different, I can multiply features by different constants.
It will all work, if values of all three features would have the same interval.
But what if F1 and F2 could have a value from (0,10) and F3 for example(0,49)?
How could I approximately scale it? Or should I approach it differently?
Scale all your values to [0..1]
Assign a weight / importance to every value
Multiply every scaled value with its weight
Divide the sum of all values from step 3 by the sum of all weights
I don't know which language you are using, so here is some C# code:
Feature Class
public class Feature
{
public double Importance { get; set; }
public double Value { get; set; }
public double LowValue { get; set; }
public double HighValue { get; set; }
public double ScaledValue
{
get
{
// Make sure value is within bounds
double intermediate = Math.Max(Math.Min(LowValue, HighValue),
Math.Min(Math.Max(LowValue, HighValue), Value));
// Scale from 0 to 1 within defined bounds
double scaledValue = (intermediate - LowValue)/(HighValue - LowValue);
return scaledValue;
}
}
}
Example Calculation
// low importance (1), values from [20..-10] (less is better)
var f1 = new Feature
{
Importance = 1,
Value = 0,
LowValue = 20,
HighValue = -10
};
// low importance (1), values from [0..10] (more is better)
var f2 = new Feature
{
Importance = 1,
Value = 1,
LowValue = 0,
HighValue = 10
};
// high importance (5), values from [0..49]
var f3 = new Feature
{
Importance = 5,
Value = 25,
LowValue = 0,
HighValue = 49
};
var features = new[] {f1, f2, f3};
var score = features.Sum(f => f.ScaledValue*f.Importance)/features.Sum(f => f.Importance);
Debug.WriteLine(score);

Ordered sequential text matching

I want to match the strings and get a score in the following manner,
string 1: 4556677, string 2: 2556677, score: 0
string 1: 123345873009, string 2: 123345873112, score: 9
string 1: 22334567, string 2: 22334500, score: 6
So the score represents common first n digits, from left to right.
I have a list of 100K string 1 and 30M string 2, I would like to filter down all the pairs (string 1 and 2) with a score greater than 'x'.
Is there an algorithm available to do this task instead of brutal force sequential matching? I have tables stored in apache hive/hbase and would like to implement the approach either in spark or java mapreduce. Any help is much appreciated.
I conclude that your "score" represents the leftmost character position at which the strings differed.
Never mind "mapreduce," plain-Jane Java can do this very easily.
**
public int score( String string1, String string2 ) {
char sbuf1[] = string1.toCharArray();
char sbuf2[] = string2.toCharArray();
int complen = sbuf1.length;
if( sbuf2.length < complen ) {
complen = sbuf2.length;
}
for(
int i = 0; i < complen; i++ ) {
if( sbuf1[ i ] !=
sbuf2[ i ] ) {
return
i;
}
}
return -1; //
indicates no mismatch detected before one string exhausted
}
**

Resources