Code covered by the BSD License  

Highlights from
FinMetrics

  • fm(varargin) This source file is subject to version 3 of the GPL license,
  • AssetThis source file is subject to version 3 of the GPL license,
  • AssetUniverseThis source file is subject to version 3 of the GPL license,
  • AxlThis source file is subject to version 3 of the GPL license,
  • CashPositionThis source file is subject to version 3 of the GPL license,
  • CashTransactionThis source file is subject to version 3 of the GPL license,
  • Config This source file is subject to version 3 of the GPL license,
  • ConsoleMenuThis source file is subject to version 3 of the GPL license,
  • ConsoleMenuItemThis source file is subject to version 3 of the GPL license,
  • CurrencyAssetThis source file is subject to version 3 of the GPL license,
  • ExchangeTradedAssetThis source file is subject to version 3 of the GPL license,
  • FinMetricsThis source file is subject to version 3 of the GPL license,
  • PortfolioThis source file is subject to version 3 of the GPL license,
  • PositionThis source file is subject to version 3 of the GPL license,
  • StatisticsThis source file is subject to version 3 of the GPL license,
  • StockPositionThis source file is subject to version 3 of the GPL license,
  • StockTransactionThis source file is subject to version 3 of the GPL license,
  • TextUIThis source file is subject to version 3 of the GPL license,
  • TextUILocaleThis source file is subject to version 3 of the GPL license,
  • TextUIQuestionThis source file is subject to version 3 of the GPL license,
  • TextUIQuestionnaireThis source file is subject to version 3 of the GPL license,
  • TransactionThis source file is subject to version 3 of the GPL license,
  • View all files

FinMetrics

by

 

Open source/open architecture quantitative portfolio management environment.

StockTransaction
classdef StockTransaction < FM.Transaction 
% This source file is subject to version 3 of the GPL license, 
% that is bundled with this package in the file LICENSE, and is 
% available online at http://www.gnu.org/licenses/gpl.txt
%
% This source file can be linked to GPL-incompatible facilities, 
% produced or made available by MathWorks, Inc.
    
    properties  (SetAccess = private, GetAccess = public)
        % Stock order type, possible options are Buy, Sell, Sell Short, Buy
        % to Cover
        Action = '';
        % Number of stocks in order
        Quantity = 0;        
        % Purchase or sale price
        Price;
        PreviousPrice;  
    end
    
    methods
        function Tobj = StockTransaction(params)            
            Tobj.Action = params.Action;
            Tobj.Asset = params.Asset;
            Tobj.Quantity = params.Quantity;            
        end

        function SetPrice(Tobj, price)
            Tobj.PreviousPrice = Tobj.Price;
            Tobj.Price = price;
        end
        
        function rslt = Summary(Tobj)
            rslt = sprintf('%s-%s: %s %.f %s-%s', Tobj.Asset.AssetClass, Tobj.Asset.AssetCategory, Tobj.Action, Tobj.Quantity, Tobj.Asset.Exchange, Tobj.Asset.Symbol);
            if (isempty(Tobj.CommittedOn) == false)
                rslt = sprintf('%s, committed on: %s', rslt, datestr(Tobj.CommittedOn));
            elseif (isempty(Tobj.PreviouslyCommittedOn) == false)
                rslt = sprintf('%s, uncommitted. Previouse commitment was on: %s', rslt, datestr(Tobj.PreviouslyCommittedOn));
            end
        end
        
        % The function will produces an offseting transaction to the
        % provided one. This function is at the core of the
        % ReverseTransaction functionality.
        
        function rslt = Reverse(Tobj)
            
            switch upper(Tobj.Action)
                case 'BUY'
                    Action = 'Sell';
                case 'SELL'
                    Action = 'Buy';
                case 'SELL SHORT'
                    Action = 'Buy to Cover';
                case 'BUY TO COVER'
                    Action = 'Sell Short';
                otherwise
                    rslt = '';
                    return;
            end
            
            rslt = FM.StockTransaction(struct('Asset', Tobj.Asset, 'Action', Action, 'Quantity', Tobj.Quantity));
            
            % Later replace with a better logic to just copy all the properties.
            rslt.CommittedOn = Tobj.CommittedOn;
            rslt.PreviouslyCommittedOn = Tobj.PreviouslyCommittedOn;
            rslt.Price = Tobj.Price;
            rslt.PreviousPrice = Tobj.PreviousPrice;            
        end
        
        function pos = GenPosition(Tobj)
        
            switch upper(Tobj.Action)
                case 'BUY'
                    dir = 'Long';                     
                    quant = Tobj.Quantity;
                case 'SELL'
                    dir = 'Long';
                    quant = 0 - Tobj.Quantity;
                case 'BUY TO COVER'
                    dir = 'Short';
                    quant = Tobj.Quantity;
                case 'SELL SHORT'
                    dir = 'Short';
                    quant = 0 - Tobj.Quantity;
                otherwise
                    pos = false;
                    return;                    
            end
            
            pos = FM.StockPosition(Tobj.Asset, dir, quant);            
        end
        
        function ret = UpdateAsset(Tobj, asset)
            ret = false;
            
            if ~(Tobj.Asset == asset)
                return;
            else
                Tobj.Asset = asset;
                ret = true;
            end
        end
    end
    
end

Contact us