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.

AssetUniverse
classdef AssetUniverse < handle
% 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, Transient = true)        
        Name = '';        
        Assets = {};
        Directory = '';
        assetFiles = {};
    end
    
    events (ListenAccess = 'public', NotifyAccess = 'public')
        % UpdateAssetPrices is the Universe event, requires the assets to
        % update their historical prices. An asset is subscribed to
        % UpdateAssetPrices when it is added/loaded from a file into universe, subject
        % to supportability by the asset class.        
        UpdateAssetPrices        
    end
    
    methods
        function AUObj = AssetUniverse (Name, Directory)            
            AUObj.Name = Name;
            AUObj.Directory = Directory;
        end
            
        function ret = AddAsset(AUObj, newAsset)
            % Save asset to the Universe directory
            % Need to add logic to check for valid asset type.
            
            ret = false;
            % Check if asset already exists in the universe
            if (AUObj.isAssetIn(newAsset))
                return;
            end
                
            assetFile = AUObj.saveAssetToUniverse(newAsset);
            
            if (isempty(assetFile))
                return;
            end
                                    
            if ((size(AUObj.assetFiles,1) == 0) && (size(AUObj.Assets,1) == 0))
                AUObj.assetFiles = { assetFile };
                AUObj.Assets = { newAsset };                         
                AUObj.subscribeAssetToUniverseEvents(AUObj.Assets{size(AUObj.Assets,1)});
            else               
                AUObj.assetFiles = [  AUObj.assetFiles; { assetFile } ];    
                AUObj.Assets = [  AUObj.Assets; { newAsset } ];    
                AUObj.subscribeAssetToUniverseEvents(AUObj.Assets{size(AUObj.Assets,1)});
            end
            
            ret = size(AUObj.Assets,1);
        end
                
        function ret = DeleteAsset(AUObj, id)                    
            % Check if the specified id is valid 
            
            ret = false;
            if ((size(AUObj.assetFiles,1) == 0) && (size(AUObj.Assets,1) == 0))            
                return;                
            elseif ( (id < 1) || (id > (size(AUObj.Assets,1))) || (id > (size(AUObj.assetFiles,1))) )                    
                return;            
            end
            
            % Delete asset file from filesystem
            delete(AUObj.assetFiles{id});
            AUObj.assetFiles(id) = [];
            AUObj.Assets(id) = [];
            
            ret = true;        
        end
        
        function ret = GetAssetFiles(AUObj)
            ret = AUObj.assetFiles;            
        end
        
        % LoadAssetFiles loads data from files listed in assetFiles
        % property to Assets property and returns the number of assets
        % loaded.
        function ret = LoadAssetFiles(AUObj)
            ret = 0;
            % LoadAssetFiles is used to load asset data for a previously
            % saved universe (to keep idividual asset variables in a
            % separate files). It is allowed to be executed only if no
            % assets present in Assets property.
            if (size(AUObj.Assets,1) > 0)
                return;
            end
            
            if (size(AUObj.assetFiles,1) > 0)
                for i=1:size(AUObj.assetFiles,1),                    
                    tempStruct = load(AUObj.assetFiles{i});                                        
                    AUObj.Assets{i,1} = tempStruct.(char(fieldnames(tempStruct)));
                    AUObj.subscribeAssetToUniverseEvents(AUObj.Assets{i,1});
                    ret = ret + 1;
                end
            else
                return;
            end                                   
        end
        
        function saveObj = saveobj(AUObj)
            if (size(AUObj.Assets,1) > 0)                
                        
                for i=1:size(AUObj.Assets,1),                
                    assetFile  = AUObj.saveAssetToUniverse(AUObj.Assets{i});
                end
            end

            saveObj = struct('Name', AUObj.Name, 'Directory', AUObj.Directory, 'assetFiles', { AUObj.assetFiles});
        end
           
        function res = isAssetIn(AUObj, asset)
            res = false;
            
            if isempty(AUObj.Assets)
                return;
            end
            
            for t=1:size(AUObj.Assets,1),                
                if (asset == AUObj.Assets{t})
                        res = t;
                        break;
                end                
            end
        end

    end

    methods (Access = private)
        function ret = saveAssetToUniverse(AUObj, newAsset)            
            ret = '';
            assetDir = [ AUObj.Directory, '/', newAsset.AssetClass, '/', newAsset.AssetCategory ]; 
            if ~(isdir(assetDir))
                stat = mkdir(assetDir);
                if (stat == 0)
                    return;
                end
            end
            ret = newAsset.Save(assetDir);
        end                
        
        function ret = subscribeAssetToUniverseEvents(AUObj, asset)
            ret = false;
            if (ismember('UpdateAssetPrices', asset.SupportedEvents))
                asset.ListenForEvent(AUObj, 'UpdateAssetPrices');
                ret = true;
            end                        
        end
        

    end
    
    methods (Static)
      function newObj = loadobj(obj)
         if isstruct(obj)            
            newObj = FM.AssetUniverse(obj.Name, obj.Directory);            
            
            % Assign property values from struct
            newObj.Name = obj.Name;
            newObj.Directory = obj.Directory;
            newObj.assetFiles = obj.assetFiles;
                        
            if ~isempty(newObj.assetFiles)
                newObj.LoadAssetFiles();                
            end                        
         end
      end
    end

end

Contact us