## Function Collection

### Function Collection

Here i will, u can post the functions that you can use as part of your own implementation.

Apprentice
FXCodeBase: Confirmed User

Posts: 19000
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Returns, Two Arrays,
First, with Bid, Second with Ask price,
for all currency pairs.

Code: Select all
`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`

Apprentice
FXCodeBase: Confirmed User

Posts: 19000
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Returns the number of Long Trades
Returns the number of Short Trades

Code: Select all
`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`

Returns true if we have Long Trades
Returns true if we have Short Trades

Code: Select all
`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`

Apprentice
FXCodeBase: Confirmed User

Posts: 19000
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

### Re: Function Collection

Check if "orders" table is ready for use.

Code: Select all
`function checkReady(table)    return core.host:execute("isTableFilled", table);end`

Apprentice
FXCodeBase: Confirmed User

Posts: 19000
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

### Re: Function Collection

Values sent to function must be in the range from 0 to 100,
Return colors in RGB formar.
Range from red to green, with yellow in the middle (Traffic light).

Code: Select all
`function Coloring (value, mid)local color;if value <= mid thencolor = core.rgb(255 * (value / mid), 255, 0) else color = core.rgb(255, 255 - 255 * ((value - mid) / mid), 0)endreturn  color;end`

Apprentice
FXCodeBase: Confirmed User

Posts: 19000
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

### Re: Function Collection

--#############################################################################
-- Convert an HSV colour to RGB
--#############################################################################
Code: Select all
`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);    endend`

--#############################################################################
-- Generate 'rainbow' RGB colour from ratio
--#############################################################################
Code: Select all
`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`

--#############################################################################
-- Generate 'darker' RGB colour from base colour and ratio
--#############################################################################
Code: Select all
`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`

--#############################################################################
-- Generate 'lighter' RGB colour from base colour and ratio
--#############################################################################
Code: Select all
`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`

Apprentice
FXCodeBase: Confirmed User

Posts: 19000
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

### InstrumentList

Returns, two variables.
First, Array With List of all currency pairs, to which we are subscribed.
Second, Variable, which keeps the total number of currency pairs.

Code: Select all
`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`

Code: Select all
`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`

Apprentice
FXCodeBase: Confirmed User

Posts: 19000
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

### Pip Cost

Code: Select all
`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`

Apprentice
FXCodeBase: Confirmed User

Posts: 19000
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

### PointSize

Code: Select all
`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`

Apprentice
FXCodeBase: Confirmed User

Posts: 19000
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

### Array Copy

Code: Select all
`Copy ( before, after); function Copy (from, to)local i;   for i = 1, #from, 1 do      to[i]= from[i];   endend`

Apprentice
FXCodeBase: Confirmed User

Posts: 19000
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Next