Function Collection

Moderator: admin

Function Collection

Postby Apprentice » 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.
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Bid Ask List

Postby Apprentice » Wed Nov 14, 2012 5:49 am

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
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Trade Count

Postby Apprentice » Wed Nov 14, 2012 6:22 am

tradesCount (true)
Returns the number of Long Trades
tradesCount (false)
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


haveTrades (true)
Returns true if we have Long Trades
haveTrades (false)
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
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: Function Collection

Postby Apprentice » Wed Nov 14, 2012 6:28 am

(checkReady("trades"))
Check if "trades" table is ready for use.
(checkReady("orders"))
Check if "orders" table is ready for use.

Code: Select all
function checkReady(table)
    return core.host:execute("isTableFilled", table);
end
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: Function Collection

Postby Apprentice » Wed Nov 14, 2012 6:31 am

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).

Red - Green - Yellow

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


Red - Green - Yellow
Code: Select all
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


Red - Green
Code: Select all
function Coloring (value, mid)

local color =  core.rgb(255-(255/100)*value, (255/100)*value, 0)
 
return  color;
end
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: Function Collection

Postby Apprentice » Wed Nov 14, 2012 6:32 am

--#############################################################################
-- 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);
    end
end

--#############################################################################
-- 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
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

InstrumentList

Postby Apprentice » Sun Nov 18, 2012 11:22 am

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
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Pip Cost

Postby Apprentice » Sun Nov 18, 2012 11:23 am

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
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

PointSize

Postby Apprentice » Sun Nov 18, 2012 11:24 am

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
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Array Copy

Postby Apprentice » Fri Dec 21, 2012 4:30 am

Code: Select all
Copy ( before, after);
 
function Copy (from, to)

local i;

   for i = 1, #from, 1 do
   
   to[i]= from[i];

   end

end
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Next

Return to Indicator Development

Who is online

Users browsing this forum: No registered users and 12 guests