Page 4 of 4

Re: Function Collection

PostPosted: Wed Jan 28, 2015 7:02 pm
by moomoofx
A date and time stamped Trace/Log function.
Very useful when debugging.

For use in strategies.
Code: Select all
function ExtTrace(message)
    local theDate = core.dateToTable(core.host:execute("convertTime", 1, 4, math.max(instance.bid:date(instance.bid:size() - 1), instance.ask:date(instance.ask:size() - 1))));
    core.host:trace(string.format("%d/%02d/%02d %02d:%02d:%02d ", theDate.year, theDate.month, theDate.day, theDate.hour, theDate.min, theDate.sec) .. " " .. message);
end


For use in indicators.
Code: Select all
function ExtTrace(message, date)
    local theDate = core.dateToTable(core.host:execute("convertTime", 1, 4, date));
    core.host:trace(string.format("%d/%02d/%02d %02d:%02d:%02d ", theDate.year, theDate.month, theDate.day, theDate.hour, theDate.min, theDate.sec) .. " " .. message);
end

Re: Function Collection

PostPosted: Wed Jan 28, 2015 7:07 pm
by moomoofx
Extend the String with a split function.

Code: Select all
-- ---------------------------------------------------------
-- Adds a split function to strings.
-- @param sep the separator
-- ---------------------------------------------------------
function string:split(sep)
    local sep, fields = sep or ";", {}
    local pattern = string.format("([^%s]+)", sep)
    self:gsub(pattern, function(c) fields[#fields + 1] = c end)
    return fields
end

Re: Function Collection

PostPosted: Thu Aug 25, 2016 8:38 am
by Cactus
These are very good. I just wanted to say I find the codes very useful, if you have any more functions you would want to share that will be nice I'm sure the community will benefit thanks in advance.

Re: Function Collection

PostPosted: Mon Oct 30, 2017 7:05 am
by mariareese
@Apprentice.. code suggestions are helpful for me at some points .. thanku ..

LagrangeInterpolation

PostPosted: Mon Oct 15, 2018 3:11 pm
by Apprentice
local y=LagrangeInterpolation(x, x1, x2, x3, y1, y2, y3);

Code: Select all
function LagrangeInterpolation(x, x1, x2, x3, y1, y2, y3)
local A=(y1*(x-x2)*(x-x3))/((x1-x2)*(x1-x3));
local B=(y2*(x-x1)*(x-x3))/((x2-x1)*(x2-x3));
local C=(y3*(x-x1)*(x-x2))/((x3-x1)*(x3-x2));
return A+B+C;
end

For lines defined by three points, will try to approximate line values.

Generating uniform random numbers

PostPosted: Fri Dec 16, 2022 2:55 pm
by Apprentice
TSLA.us H1 (12-16-2022 2053).png

Generating uniform random numbers.lua
(5.88 KiB) Downloaded 141 times