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.

StockPosition
classdef StockPosition < FM.Position
% 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        
    end
    
    methods
        function SPobj = StockPosition(asset, direction, quantity)            
            SPobj.Direction = direction;
            SPobj.Quantity = quantity;
            SPobj.Asset = asset;            
        end
        
        function rslt = Summary(SPobj)
            rslt = sprintf('Open on %s %s %s-%s %s-%s %.f', datestr(SPobj.OpenDate), SPobj.Direction, SPobj.Asset.AssetClass, SPobj.Asset.AssetCategory, SPobj.Asset.Exchange, SPobj.Asset.Symbol, SPobj.Quantity);
        end
        
        % Overriding plus function to implement the summation of two
        % positions.
        function ret = plus(a,b)
            % First check if we work with oranges only
            if (a.Asset == b.Asset)
                if (strcmpi(a.Direction, 'Long') && strcmpi(b.Direction, 'Long'))
                    dir = 'Long';
                    quant = a.Quantity + b.Quantity;
                    if (quant < (FM.Config.FloatTol * -1))
                        ret = false;
                        return;
                    end                    
                elseif (strcmpi(a.Direction, 'Short') && strcmpi(b.Direction, 'Short'))
                    dir = 'Short';
                    quant = a.Quantity + b.Quantity;
                    if (quant > FM.Config.FloatTol)
                        ret = false;
                        return;
                    end                
                else
                    ret = false;
                    return;
                end
                
                ret = FM.StockPosition(a.Asset, dir, quant);
                ret.OpenDate = a.OpenDate;
                return;                                        
            else
                ret = false;
                return;
            end
        end
        
        function ret = UpdateAsset(SPobj, asset)
            ret = false;
            
            if ~(SPobj.Asset == asset)
                return;
            else
                SPobj.Asset = asset;
                ret = true;
            end
        end    
        
        function ret = Value(SPobj, params)
            ret = false;
            
            if ~isfield(params, 'Time') 
                return;
            end
            
            if ~isfield(params, 'Price') 
                price = SPobj.Asset.GetPrice(params.Time);
                
                if (price == -1)
                    return;
                else
                    price.OPEN = double(fts2mat(price.OPEN) * SPobj.Quantity);
                    price.HIGH = double(fts2mat(price.HIGH) * SPobj.Quantity);
                    price.LOW = double(fts2mat(price.LOW) * SPobj.Quantity);
                    price.CLOSE = double(fts2mat(price.CLOSE) * SPobj.Quantity);
                    
                    ret = price;
                    return;
                end
            elseif isnumeric(params.Price)                
                    ret = double(params.Price * SPobj.Quantity);                
            end            
        end
    end
    
end

Contact us