Function Collection
Posted: Wed Nov 14, 2012 5:42 am
Here i will, u can post the functions that you can use as part of your own implementation.
Your last stop on a way to find a fast growing community of forex traders and software developers. Best forex charts indicators are available here for download. Custom development of indicators and translation to .LUA is also available on request.
http://fxcodebase.com/code/
function getBidAskList()
local BID = {};
local ASK = {};
local count = 0;
local row, enum;
enum = core.host:findTable("offers"):enumerator();
row = enum:next();
while row ~= nil do
count = count + 1;
BID[count] = row.Bid;
ASK[count] = row.Ask;
row = enum:next();
end
return BID, ASK;
end
function tradesCount(BuySell)
local enum, row;
local count = 0;
enum = core.host:findTable("trades"):enumerator();
row = enum:next();
-- NG: to get the true count we must NOT stop when count is not a zero or
-- the function will return 1 or 0 only and will work as "haveTrades"
-- while count == 0 and row ~= nil do
while row ~= nil do
if row.AccountID == Account and
row.OfferID == Offer and
(row.BS == BuySell or BuySell == nil) then
count = count + 1;
end
row = enum:next();
end
return count;
end
function haveTrades(BuySell)
local enum, row;
local found = false;
enum = core.host:findTable("trades"):enumerator();
row = enum:next();
while (not found) and (row ~= nil) do
if row.AccountID == Account and
row.OfferID == Offer and
(row.BS == BuySell or BuySell == nil) then
found = true;
end
row = enum:next();
end
return found;
end
function checkReady(table)
return core.host:execute("isTableFilled", table);
end
function Coloring (value, mid)
local color;
if value <= mid then
color = core.rgb(255 * (value / mid), 255, 0)
else
color = core.rgb(255, 255 - 255 * ((value - mid) / mid), 0)
end
return color;
end
function Coloring (value, revers)
if value <= 50 then
r = 255
g = 255*value/50
b = 0
else
r = 255*(100-value)/50
g = 255
b = 0
end
if revers then
r, g = g, r
end
return core.rgb(r, g, 0);
end
function Coloring (value, mid)
local color = core.rgb(255-(255/100)*value, (255/100)*value, 0)
return color;
end
function convertHSVtoRGB(hue, saturation, value)
-- http://en.wikipedia.org/wiki/HSL_and_HSV
-- Hue is an angle (0..360), Saturation and Value are both in the range 0..1
local hi = (math.floor(hue / 60)) % 6;
local f = hue / 60 - math.floor(hue / 60);
value = value * 255;
local v = math.floor(value);
local p = math.floor(value * (1 - saturation));
local q = math.floor(value * (1 - f * saturation));
local t = math.floor(value * (1 - (1 - f) * saturation));
if hi == 0 then
return core.rgb(v, t, p);
elseif hi == 1 then
return core.rgb(q, v, p);
elseif hi == 2 then
return core.rgb(p, v, t);
elseif hi == 3 then
return core.rgb(p, q, v);
elseif hi == 4 then
return core.rgb(t, p, v);
else
return core.rgb(v, p, q);
end
end
function getRainbowColour(ratio)
-- Using HSV colour system, where Hue is changed in proportion to ratio
ratio = math.max(0, math.min(1, ratio));
-- NOTE: hue is an angular value and can be 0..360, but it wraps around back to 'red' again
-- which looks a bit weird so I limit to 300 degrees
local hue = 300 * (1 - ratio);
-- Convert back to RGB
return convertHSVtoRGB(hue, 1, 1);
end
function getShadedColour(colour, ratio)
-- Using HSV colour system, where Hue is set from RGB, and Value is set to the ratio
ratio = math.max(0, math.min(1, ratio));
-- Split into RGB components
-- NOTE: internally the color is stored as BGR for some reason
local r = colour % 256;
local g = math.floor(colour / 256) % 256;
local b = math.floor(colour / 65536) % 256;
-- Compute the Hue from the RGB (http://en.wikipedia.org/wiki/Hue)
local hue = 180 * math.atan2(math.sqrt(3) * (g - b), 2 * r - g - b) / math.pi;
-- Convert back to RGB
return convertHSVtoRGB(hue, 1, ratio);
end
function getFadedColour(colour, ratio)
-- Using HSV colour system, where Hue is set from RGB, and Saturation is set to the ratio
ratio = math.max(0, math.min(1, ratio));
-- Split into RGB components
-- NOTE: internally the color is stored as BGR for some reason
local r = colour % 256;
local g = math.floor(colour / 256) % 256;
local b = math.floor(colour / 65536) % 256;
-- Compute the hue from the RGB (http://en.wikipedia.org/wiki/Hue)
local hue = 180 * math.atan2(math.sqrt(3) * (g - b), 2 * r - g - b) / math.pi;
-- Convert back to RGB
return convertHSVtoRGB(hue, ratio, 1);
end
function getInstrumentList()
local list={};
local count = 0;
local row, enum;
enum = core.host:findTable("offers"):enumerator();
row = enum:next();
while row ~= nil do
count = count + 1;
list[count] = row.Instrument;
row = enum:next();
end
return list, count;
end
function FindInstrument(Instrument)
local row, enum;
local Flag= false;
enum = core.host:findTable("offers"):enumerator();
row = enum:next();
while row ~= nil do
if Instrument == row.Instrument then
Flag= true;
break;
end
row = enum:next();
end
return Flag;
end
function getPipCost()
local PipCost = {};
local count = 0;
local row, enum;
enum = core.host:findTable("offers"):enumerator();
row = enum:next();
while row ~= nil do
count = count + 1;
PipCost[count] = row.PipCost;
row = enum:next();
end
return PipCost;
end
function getPointSize()
local SIZE = {};
local count = 0;
local row, enum;
enum = core.host:findTable("offers"):enumerator();
row = enum:next();
while row ~= nil do
count = count + 1;
SIZE[count] = row.PointSize;
row = enum:next();
end
return SIZE;
end
Copy ( before, after);
function Copy (from, to)
local i;
for i = 1, #from, 1 do
to[i]= from[i];
end
end