How to convert the OBJ_TREND time1 and price1 into the available coordinates OBJ_BUTTON - mql4

How to convert the OBJ_TREND time1 and price1 into the available coordinates OBJ_BUTTON
OBJPROP_ XDISTANCE,OBJPROP_ YDISTANCE

use ChartTimePriceToXY() function to convert time and price to x and y

Related

How can I split a float (1,2) into 1 and 2 integers?

I'm learning F# and have an assignment where I have to treat a float as a coordinate. For example float 2.3 would be treated as a coordinate (2.3) where x is 2 and y is 3.
How can I split the float to calculate with it?
I am trying to make a function to calculate the length of a vector:
let lenOfVec (1.2, 2.3) and using pythagoras' method to get the length of hypotenuse.
But I am already stuck at splitting up the float.
Hope some can help!
Having at your disposal libraries as rich as F#/.NET offer the task of splitting a float into two can be done with one short line of code:
let splitFloat n = n.ToString().Split('.') |> Array.map float
library function ToString() converts the argument n (supposedly float) to a string
library functionSplit('.') applied to this string converts it into an array of two strings representing the first number before decimal dot and the second number after the dot
finally this array of 2 strings is converted by applying library function float to the each array element with the help of just another library function Array.map, producing the array of two sought floats
Being applied to a random float number the outlined chain of conversions looks like
123.456 --> "123.456" --> [|123;456|] --> [|123.0;456.0|]
Stealing from a few other answers on here, something like this seems to work for a few examples:
open System
///Takes in a float and returns a tuple of the the two parts.
let split (n: float) =
let x = Math.Truncate(n)
let bits = Decimal.GetBits(decimal n)
let count = BitConverter.GetBytes(bits.[3]).[2]
let dec = n - x
let y = dec * Math.Pow(10., float count)
x, y
Examples:
2.3 -> (2.0, 3.0)
200.123 -> (200.0, 123.0)
5.23 -> (5.0, 23.0)
Getting the X is easy, as you can just truncate the decimal part.
Getting the Y took input from this answer and this one.

Adding float numbers to Decimal numbers

I have this code:
var weightSum: Float = 3.14159
let weightPerPortionGrams: Decimal = 0.999999
weightSum = weightSum + (weightPerPortionGrams)
The numbers are examples.
I get an error:
Binary operator '+ =' can not be applied to type 'Float' and 'Decimal'.
Does anyone know how to fix it?
To convert a Decimal to a float, you can either do this:
weightSum += Float(truncating: weightPerPortionGrams as NSNumber)
or this:
weightSum += (weightPerPortionGrams as NSNumber).floatValue
Swift needs both variables to be of the same type to make use of the "+" operator, so you would need to either convert your Decimal to type Float or the other way around before summing them:
weightPerPortionGramsFloat = (weightPerPortionGrams as NSNumber).floatValue
or
weightSumDecimal = (weightSum as NSNumber).decimalValue
Greetings!
func + (left: Float, right: Decimal) -> Float {
return left + Float(right.description)!
}
var weightSum: Float = 3.14159
let weightPerPortionGrams: Decimal = 0.999999
weightSum = weightSum + weightPerPortionGrams
print(weightSum)
// prints 4.141589
Hope it helps!
This is because the variables weightSum and weightPerPortionGrams have different types. Luckily you can convert these variables in Swift.
So, to make this work, you should convert the type of weightPerPortionGrams to the one of weightSum, so both variables become the same type:
weightSum = weightSum + NSDecimalNumber(decimal: weightPerPortionGrams).floatValue
Note that Decimal might not handle the floating points quite well.
You can also do an extra cast, to make sure that we are using the methods of NSDecimalNumber and not of NSNumber:
let double = NSDecimalNumber(decimal: weightPerPortionGrams).doubleValue
weightSum = weightSum + Float(double)

Remove . from decimal ruby

I have a decimal column price. I got two prices: 10.00 and 11.50
I need to transform
10.00 into 1000
11.50into 1150
How I can do that in controller?
Just multiply the number by 100 and then do to_i on the result:
f = 10.0
n = (f * 100).to_i
If it's a string:
"10.00".gsub(".", "")
If it's a decimal number
(10.00 * 100).to_i == 1000
(10.00).to_floor
Convert to string, replace dot, convert to int
my_number = 10.56
my_number_without_decimal_point = my_number.to_s().gsub(".", "").to_i()
puts "#{my_number_without_decimal_point}"
prints 1056

How to convert Delphi TDateTime to String with microsecond precision

I need to convert a TDateTime to a String with microsecond precision.
In case of millisecond precision it is possible to use formatting:
DateTimeToString(Result, 'd.m.yyyy hh:nn:ss.zzz', dateTime);
but I need three more digits (microseconds).
It is possible to take the fractional part and divide it by 1/86400/1000000 but I'm looking for more efficient way to convert it.
The accuracy of a date-time varies depending how far away from "zero" you are.
A Delphi TDateTime is actually an 8-byte floating point Double, with zero being 12/30/1899 12:00:00 am.
We can figure out the precision of a TDateTime by incrementing the floating point datetime by the smallest quantum possible:
function AddQuantumToDateTime(const dt: TDateTime): TDateTime;
var
overlay: Int64 absolute Result;
begin
Result := dt;
overlay := overlay+1;
end;
With this, we can figure out the smallest increment that a TDateTime can even handle. It varies with the date being used, as the further you are from zero, the larger the quantum amount is:
12/31/1899: ±0 ns
1/1/1900: ±0 ns
1/1/1970: ±314 ns
1/1/2000: ±629 ns
1/1/2016: ±629 ns
1/1/2038: ±629 ns
1/1/3000: ±5,029 ns
1/1/4000: ±10,058 ns
1/1/5000: ±20,117 ns
1/1/6000: ±20,117 ns
1/1/7000: ±20,117 ns
1/1/8000: ±40,233 ns
1/1/9000: ±40,233 ns
1/1/9999: ±40,233 ns
So for the time being, a DateTime can give you a resolution of about half a microsecond.
While the Windows FILETIME structure does support a resolution of 100ns, the SYSTEMTIME structure only supports down to the millisecond:
typedef struct _SYSTEMTIME {
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME;
Microsoft SQL Server's new datetime2(7) returns datetime strings with up to seven digit (100 ns) of fractional second accuracy:
SELECT CAST('20160802' AS datetime2(6)) AS TheNow
TheNow
==========================
2016-08-02 00:00:00.000000
Your question then is how to convert a TDateTime to a string that contains microsecond (billionths of a second) precision. You already have your answer:
function DateTimeToStrUs(dt: TDatetime): string;
var
us: string;
begin
//Spit out most of the result: '20160802 11:34:36.'
Result := FormatDateTime('yyyymmdd hh":"nn":"ss"."', dt);
//extract the number of microseconds
dt := Frac(dt); //fractional part of day
dt := dt * 24*60*60; //number of seconds in that day
us := IntToStr(Round(Frac(dt)*1000000));
//Add the us integer to the end:
// '20160801 11:34:36.' + '00' + '123456'
Result := Result + StringOfChar('0', 6-Length(us)) + us;
end;
Where:
DateTimeToStrUs(Now)
Returns:
20160802 11:34:36.482364

How to convert GPS coordinates to decimal in Lua?

I need to convert GPS coordinates from WGS84 to decimal using Lua.
I am sure it's been done before, so I am looking for a hint to a code snippet.
corrected question: Code to convert DMS (Degress Minutes Seconds) to DEG ((decimal) Degrees) in Lua?
examples:
Vienna: dms: 48°12'30" N 16°22'28" E
or
Zurich: dms: 47°21'7" N 8°30'37" E
The difficulty I find is to get the numbers out of these strings.
Especially how to handle the signs for degree (°) minutes (') and seconds (").
So that I would have for example a table coord{} per coordinate to deal with.
coord {1} [48]
coord {2} [12]
coord {3} [30]
coord {4} [N]
coord {5} [16]
coord {6} [22]
coord {7} [28]
coord {8} [E]
Suggestions are appreciated, thanks.
Parse the string latlon = '48°12'30" N 16°22'28" E' into DMS+heading components:
This is your string (note the escaped single-quote):
latlon = '48°12\'30" N 16°22\'28" E'
Break it down into two steps: the lat/lon, then components of each. You need captures "()", ignore spaces around the heading (N and E) with "%s*":
lat, ns, lon, ew = string.match(latlon, '(.*)%s*(%a)%s*(.*)%s*(%a)')
The lat is now 48°12'30", ns is 'N', lon is 16°22'28", ew is 'E'. For components of lat, step by step:
-- string.match(lat, '48°12'30"') -- oops the ' needs escaping or us
-- string.match(lat, '48°12\'30"')
-- ready for the captures:
-- string.match(lat, '(48)°(12)\'(30)"') -- ready for generic numbers
d1, m1, s1 = string.match(lat, '(%d+)°(%d+)\'(%d+)"')
d2, m2, s2 = string.match(lon, '(%d+)°(%d+)\'(%d+)"')
Now that you know (d1, m1, s1, ns) and (d2, m2, s2, ew), you have:
sign = 1
if ns=='S' then sign = -1 end
decDeg1 = sign*(d1 + m1/60 + s1/3600)
sign = 1
if ew=='W' then sign = -1 end
decDeg2 = sign*(d2 + m2/60 + s2/3600)
For your values of lat, you get decDeg1 = 48.208333 which is the correct value according to online calculators (like http://www.satsig.net/degrees-minutes-seconds-calculator.htm).

Resources