Function Collection

Moderator: admin

Line Angle

Postby Apprentice » Thu Mar 14, 2013 3:47 am

Code: Select all

function GetAngleTrendLineWave(x1, y1, x2, y2, x3, y3)
   local length  = math.abs(x3 - x1);               
   local correct = math.abs(y2-y3)/math.abs(y2-y1);
   local kF = 1 / math.pow(1.18, length/13);         
   local angle = 90 * (1-correct) * kF;           
   if y1>y3 then angle = angle * (-1); end
   return angle;
end
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 17398
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: Function Collection

Postby Apprentice » Sat Mar 16, 2013 7:04 am

netting entry stop and netting entry limit joined into OCO
Code: Select all
function Init()
    strategy:name("Netting ELS test");
    strategy:description("");
   
    strategy.parameters:addString("Account", "Account", "", "");
    strategy.parameters:setFlag("Account", core.FLAG_ACCOUNT);
end

local Offer;
local Account;
local PointSize;

function Prepare()
    Offer = core.host:findTable("offers"):find("Instrument", instance.bid:instrument()).OfferID;
    PointSize = core.host:findTable("offers"):find("Instrument", instance.bid:instrument()).PointSize;
    Account = instance.parameters.Account;
end

function Update()
    if instance.ask:size() <= 1 then
        return;
    end
    local rate = instance.ask[NOW] + 50 * PointSize;
    local command1 = createEntryStopOrderCommand(Offer, Account, "B", nil, "y", rate, nil, "GTC");
    local rate = instance.ask[NOW] - 50 * PointSize;
    local command2 = createEntryLimitOrderCommand(Offer, Account, "B", nil, "y", rate, nil, "GTC");
   
    local command = core.valuemap();
    command.Command = "CreateOCO";
    command:append(command1);
    command:append(command2);
    local success, msg = terminal:execute(101, command);
    if not success then
        terminal:alertMessage("", 0, "Failed to create orders: " .. msg .. "'", core.now());
    end
end

function AsyncOperationFinished(cookie, success, message)

end

-- Create an Entry Stop Order
-- http://www.fxcodebase.com/documents/IndicoreSDK/web-content.html?key=TradingCommandsCreateOrder_SE.html
function createEntryStopOrderCommand(offerId, accountId, buySell, amount, netQuantity, rate, trailUpdatePips, gtc, customId, contingencyID)
    local valuemap = core.valuemap();
   
    valuemap.Command = "CreateOrder";
    valuemap.OrderType = "SE";
    valuemap.OfferID = offerId;
    valuemap.AcctID = accountId;
    valuemap.BuySell = buySell;
    valuemap.Quantity = amount;
    valuemap.NetQtyFlag = netQuantity;
    valuemap.Rate = rate;
    valuemap.TrailUpdatePips = trailUpdatePips;
    valuemap.GTC = gtc;
    valuemap.CustomID = customId;
    valuemap.ContingencyID = contingencyID;
   
    return valuemap;
end

-- Create an Entry Limit Order
-- http://www.fxcodebase.com/documents/IndicoreSDK/web-content.html?key=TradingCommandsCreateOrder_LE.html
function createEntryLimitOrderCommand(offerId, accountId, buySell, amount, netQuantity, rate, trailUpdatePips, gtc, customId, contingencyID)
    local valuemap = core.valuemap();
   
    valuemap.Command = "CreateOrder";
    valuemap.OrderType = "LE";
    valuemap.OfferID = offerId;
    valuemap.AcctID = accountId;
    valuemap.BuySell = buySell;
    valuemap.Quantity = amount;
    valuemap.NetQtyFlag = netQuantity;
    valuemap.Rate = rate;
    valuemap.TrailUpdatePips = trailUpdatePips;
    valuemap.GTC = gtc;
    valuemap.CustomID = customId;
    valuemap.ContingencyID = contingencyID;
   
    return valuemap;
end
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 17398
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Log file within TSII

Postby Apprentice » Thu Apr 18, 2013 4:39 am

Code: Select all
--[[
Logging.
]]
local __log_path = string.gsub(d-e-b-u-g.getinfo(1,"S").source,"^(.+\\)[^\\]+$", "%1");
function log(str)
logname='log'..os.date('%Y%m%d')
local file = assert(io.open(__log_path .. logname, "a"));
local record = os.date() .. " " .. str;
--print(record);
file:write(record .. "\n");
end
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 17398
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Close all at total Equity

Postby Apprentice » Thu Apr 18, 2013 4:45 am

Closes ALL Open Positions, PLUS any pending Entry and Exit Orders
Code: Select all
-- Strategy profile initialization routine
-- Defines Strategy profile properties and Strategy parameters
-- TODO: Add minimal and maximal value of numeric parameters and default color of the streams
function Init()
    strategy:name("Close all at total Equity");
    strategy:description("Closes ALL Open Positions, PLUS any pending Entry and Exit Orders, when the Account EQUITY reaches a certain target dollar");

    strategy.parameters:addGroup("Price Parameters");
    strategy.parameters:addString("TF", "TF", "Time frame ('t1', 'm1', 'm5', etc.)", "t1");
    strategy.parameters:setFlag("TF", core.FLAG_PERIODS);
    strategy.parameters:addGroup("Parameters");
    strategy.parameters:addDouble("EL", "Equity Level", "Total Equity Level", 200000);

    strategy.parameters:addGroup("Trading Parameters");
    strategy.parameters:addBoolean("AllowTrade", "Allow strategy to trade", "", false);
    strategy.parameters:addString("Account", "Account to trade on", "", "");
    strategy.parameters:setFlag("Account", core.FLAG_ACCOUNT);
    strategy.parameters:addInteger("Amount", "Trade Amount in Lots", "", 1, 1, 100);
    strategy.parameters:addBoolean("SetLimit", "Set Limit Orders", "", false);
    strategy.parameters:addInteger("Limit", "Limit Order in pips", "", 30, 1, 10000);
    strategy.parameters:addBoolean("SetStop", "Set Stop Orders", "", false);
    strategy.parameters:addInteger("Stop", "Stop Order in pips", "", 30, 1, 10000);
    strategy.parameters:addBoolean("TrailingStop", "Trailing stop order", "", false);

    strategy.parameters:addGroup("Notification");
    strategy.parameters:addBoolean("ShowAlert", "Show Alert", "", true);
    strategy.parameters:addBoolean("PlaySound", "Play Sound", "", true);
    strategy.parameters:addBoolean("RecurSound", "Recurrent Sound", "", false);
    strategy.parameters:addFile("SoundFile", "Sound File", "", "");
    strategy.parameters:setFlag("SoundFile", core.FLAG_SOUND);
    strategy.parameters:addBoolean("SendEmail", "Send Email", "", true);
    strategy.parameters:addString("Email", "Email", "", "");
    strategy.parameters:setFlag("Email", core.FLAG_EMAIL);
end

-- strategy instance initialization routine
-- Processes strategy parameters and creates output streams
-- TODO: Calculate all constants, create instances all necessary indicators and load all required libraries
-- Parameters block
local EL;
local gSource = nil; -- the source stream
local AllowTicks = true;
local PlaySound;
local RecurrentSound;
local SoundFile;
local Email;
local SendEmail;
local AllowTrade;
local Account;
local Amount;
local BaseSize;
local SetLimit;
local Limit;
local SetStop;
local Stop;
local TrailingStop;
local Offer;
local CanClose;
--TODO: Add variable(s) for your indicator(s) if needed


local nexecuting = 0;

-- Routine
function Prepare(nameOnly)
    EL = instance.parameters.EL;

    local name = profile:id() .. "(" .. instance.bid:instrument() .. ", " .. tostring(EL) .. ")";
    instance:name(name);

    if nameOnly then
        return ;
    end

    if (not(AllowTicks)) then
        assert(instance.parameters.TF ~= "t1", "The strategy cannot be applied on ticks.");
    end

    ShowAlert = instance.parameters.ShowAlert;

    PlaySound = instance.parameters.PlaySound;
    if PlaySound then
        RecurrentSound = instance.parameters.RecurSound;
        SoundFile = instance.parameters.SoundFile;
    else
        SoundFile = nil;
    end
    assert(not(PlaySound) or (PlaySound and SoundFile ~= ""), "Sound file must be specified");

    SendEmail = instance.parameters.SendEmail;
    if SendEmail then
        Email = instance.parameters.Email;
    else
        Email = nil;
    end
    assert(not(SendEmail) or (SendEmail and Email ~= ""), "E-mail address must be specified");


    AllowTrade = instance.parameters.AllowTrade;
    if AllowTrade then
        Account = instance.parameters.Account;
        Amount = instance.parameters.Amount;
        BaseSize = core.host:execute("getTradingProperty", "baseUnitSize", instance.bid:instrument(), Account);
        Offer = core.host:findTable("offers"):find("Instrument", instance.bid:instrument()).OfferID;
        CanClose = core.host:execute("getTradingProperty", "canCreateMarketClose", instance.bid:instrument(), Account);
        SetLimit = instance.parameters.SetLimit;
        Limit = instance.parameters.Limit * instance.bid:pipSize();
        SetStop = instance.parameters.SetStop;
        Stop = instance.parameters.Stop * instance.bid:pipSize();
        TrailingStop = instance.parameters.TrailingStop;
    end

    gSource = ExtSubscribe(1, nil, instance.parameters.TF, instance.parameters.Type == "Bid", "bar");
    --TODO: Find indicator's profile, intialize parameters, and create indicator's instance (if needed)
end

-- strategy calculation routine
-- TODO: Add your code for decision making
-- TODO: Update the instance of your indicator(s) if needed
function ExtUpdate(id, source, period)
    if AllowTrade then
        local Equity = core.host:findTable("accounts"):find("AccountID", Account).Equity;
        if Equity >= EL then
            -- Level Reached Close all Now
            if nexecuting > 0 then
               return ;
            end
            closeAllTrades();
            removeAllOrders();
        end
    end
end

function closeAllTrades()
    local offerListLong     = {};
    local offerListShort    = {};
    local iLong = 0;
    local iShort = 0;
    local enum, row;
    enum = core.host:findTable("trades"):enumerator();
    row = enum:next();
    while (row ~= nil) do
        if row.AccountID == Account then
            if CanClose then
                -- close trades immediatelly if we can close
                local valuemap = core.valuemap();
                valuemap.Command = "CreateOrder";
                valuemap.OrderType = "CM";
                valuemap.OfferID = offer;
                valuemap.AcctID = Account;
                valuemap.Quantity = row.Lot;
                valuemap.TradeID = row.TradeID;
                if row.BS == "B" then
                    valuemap.BuySell = "S";
                else
                    valuemap.BuySell = "B";
                end
                local success, msg = terminal:execute(100, valuemap);
                if not (success) then
                    terminal:alertMessage(instance.bid:instrument(), instance.bid[NOW], "create order failed:" .. msg, instance.bid:date(NOW));
                end
            else
                if row.BS == "B" then
                    iLong = iLong +1;
                    offerListLong[iLong] = row.OfferID;
                else
                    iShort = iShort +1;
                    offerListShort[iShort] = row.OfferID;
                end
            end
        end
        row = enum:next();
    end
    local i ;
    if iLong > 0 then
        for i = 1, iLong, 1 do
            local valuemap = core.valuemap();
            valuemap.Command = "CreateOrder";
            valuemap.OrderType = "CM";
            valuemap.OfferID = offerListLong[i];
            valuemap.AcctID = Account;
            valuemap.NetQtyFlag = "Y";
            valuemap.BuySell = "S";
     
            local success, msg = terminal:execute(100, valuemap);
            if not (success) then
                terminal:alertMessage(instance.bid:instrument(), instance.bid[NOW], "net order create order failed:", instance.bid:date(NOW));
            end
        end
    end
    if iShort > 0 then
        for i = 1, iShort, 1 do
            local valuemap = core.valuemap();
            valuemap.Command = "CreateOrder";
            valuemap.OrderType = "CM";
            valuemap.OfferID = offerListShort[i];
            valuemap.AcctID = Account;
            valuemap.NetQtyFlag = "Y";
            valuemap.BuySell = "B";
     
            local success, msg = terminal:execute(100, valuemap);
            if not (success) then
                terminal:alertMessage(instance.bid:instrument(), instance.bid[NOW], "net order create order failed:" .. msg, instance.bid:date(NOW));
            end
        end
    end

end
function removeAllOrders()
    local enum, row;
    enum = core.host:findTable("orders"):enumerator();
    row = enum:next();
    while (row ~= nil) do
        if row.FixStatus == "W" then
            local valuemap = core.valuemap();
            valuemap.Command = "DeleteOrder";
            valuemap.OrderID = row.OrderID;
            success, msg = terminal:execute(200, valuemap);
            if not(success) then
                terminal:alertMessage(instance.bid:instrument(), instance.bid[NOW], "Failed delete order " .. msg, instance.bid:date(NOW));
            else
                nexecuting = nexecuting + 1;
            end
        end
        row = enum:next();
    end
end

function ExtAsyncOperationFinished(cookie, success, message, message1, message2)
    if  cookie == 200 then --Trade Close Or Open Event
        nexecuting = nexecuting - 1;
        if not(success) then
            terminal:alertMessage(instance.bid:instrument(), instance.bid[NOW], "Failed delete order:" .. msg, instance.bid:date(NOW));
        end
    end
end
dofile(core.app_path() .. "\\strategies\\standard\\include\\helper.lua");
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 17398
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

2D Array Sort

Postby Apprentice » Sun Sep 22, 2013 1:13 pm

BubbleSort.lua
(2.4 KiB) Downloaded 128 times

Code: Select all
   InputData[1]={};
    InputData[1][1]=instance.parameters.Input1;
    InputData[1][2]=instance.parameters.Input2;
    InputData[1][3]=instance.parameters.Input3;
    InputData[1][4]=instance.parameters.Input4;
    InputData[1][5]=instance.parameters.Input5;   
    Sorted= BubbleSort(InputData,1,1, 5 );

function BubbleSort(Data, index,raw, columns)
 
 local new=Data;
 local j,k, temp;
 local Sort=true;
 
   

     while Sort do
    Sort=false;
   
            for j = 2, columns , 1 do
                  
                    if new[index][j] <  new[index][j-1] then
                     Sort=true;                  
                           
                           for k = 1, raw, 1 do
                           temp= new[k][j-1];
                           new[k][j-1]= new[k][j];
                           new[k][j]= temp;
                           end
                      
                    end
               
          
          
          end
   
    end
 
  return new;
end


BubbleSort Key.lua
(2.44 KiB) Downloaded 125 times


Code: Select all
   InputData[1]={};
    InputData[1][1]=instance.parameters.Input1;
    InputData[1][2]=instance.parameters.Input2;
    InputData[1][3]=instance.parameters.Input3;
    InputData[1][4]=instance.parameters.Input4;
    InputData[1][5]=instance.parameters.Input5;

   Key= BubbleSortKey(InputData,1,1, 5 );


function BubbleSortKey(Data, index,raw, columns)
 
 local Key={};
 local Temp;
 local Sort=true;
 
   
   for i=1, columns, 1 do
   Key[i]=i;
   end
   
   

     while Sort do
    Sort=false;
   
            for i = 2, columns , 1 do
                  
                    if Data[index][Key[i]] <  Data[index][Key[i-1]] then
                     Sort=true;                  
                     
                            Temp= Key[i];                     
                     Key[i]=Key[i-1];
                     Key[i-1]=Temp;                      
                    end
          
          end
   
    end
 
  return Key;
end
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 17398
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Finding highest entry price in "trades"-table

Postby Apprentice » Tue Nov 12, 2013 2:51 am

finding highest entry price in "trades"-table

Code: Select all
function findhighestprice()
    local storage1, enum2, row2={},{};
    enum2 = core.host:findTable("trades"):enumerator();
    row2 = enum2:next();

    while row2 ~= nil do
        if row2.Instrument == instance.bid:instrument() and
           row2.Open>storage1 then
            storage1=row2.Open;
        end

        row2 = enum:next();
    end
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 17398
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Last closed trade amount

Postby Apprentice » Wed Dec 25, 2013 2:30 pm

Code: Select all
function getLatestClosedTrade()
    local enum, row, latest_row;
    enum = core.host:findTable("closed trades"):enumerator();
    row = enum:next();
    while row ~= nil do
        if row.AccountID == Account and
           row.OfferID == Offer and
           (row.BS == BuySell or BuySell == nil) then
            if latest_row == nil or latest_row.CloseTime < row.CloseTime then
                latest_row = row;
            end
        end
        row = enum:next();
    end

    return latest_row;
end

function AmountX()
    local row = getLatestClosedTrade();
    if row ~= nil then
        return ((row.AmountK/Limit)*row.AmountK+row.AmountK);
    end
    return nil;
end

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

Slope To Angle

Postby Apprentice » Wed Jan 01, 2014 5:28 am

x1, x , x = context:positionOfBar (i-Period);
y1=(source[i-Period]);
x2, x , x = context:positionOfBar (i);
y2= (source[i]);

function AngleCalculation(x1,y1, x2,y2)

return (math.atan2((y2-y1), (x2-x1)) * 180 / math.pi);
end
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 17398
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Get Days In Month

Postby Apprentice » Mon Nov 24, 2014 5:47 am

Version 1
Code: Select all
 function get_days_in_month(month, year)
  local days_in_month = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }   
  local d = days_in_month[month]
   
  -- check for leap year
  if (month == 2) then
    if (math.mod(year,4) == 0) then
     if (math.mod(year,100) == 0)then               
      if (math.mod(year,400) == 0) then                   
          d = 29
      end
     else               
      d = 29
     end
    end
  end

  return d 
end

Version 2
Code: Select all
local days_in_month = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }

function is_leap_year(year)
    return year % 4 == 0 and (year % 100 ~= 0 or year % 400 == 0)
  end

  function get_days_in_month(month, year)
    if month == 2 and is_leap_year(year) then
      return 29
    else
      return days_in_month[month]
    end
  end
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 17398
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia

Re: Function Collection

Postby moomoofx » Wed Jan 28, 2015 7:00 pm

Parsing and Comparing Timeframes
Useful for when you want to find out which timeframe is "bigger" than the other etc...

Code: Select all
function CompareTimeFrame(tf1, tf2)
    local value1 = ConvertTFToValue(tf1);
    local value2 = ConvertTFToValue(tf2);

    if value1 > value2 then
        return 1;
    end

    if value1 < value2 then
        return -1;
    end

    return 0;
end

function ConvertTFToValue(tf)
    local unit = string.sub(tf,1,1);
    local count = tonumber(string.sub(tf,-1 * (string.len(tf) - 1)));

    if unit == "Y" then
        return 365 * count;
    elseif unit == "M" then
        return 30 * count;
    elseif unit == "D" then
        return count;
    elseif unit == "W" then
        return 7 * count;
    elseif unit == "H" then
        return count / 24;
    elseif unit == "m" then
        return count / 1440;
    elseif unit == "t1" then
        return 0;
    else
        core.host:trace("Unknown Timeframe: " .. tf);
    end

    return 0;
end
Last edited by moomoofx on Wed Jan 28, 2015 7:06 pm, edited 1 time in total.
User avatar
moomoofx
FXCodeBase: Confirmed User
 
Posts: 193
Joined: Wed Oct 23, 2013 10:26 pm
Location: Okinawa, Japan. http://moomooforex.com

PreviousNext

Return to Indicator Development

Who is online

Users browsing this forum: No registered users and 3 guests