1investagramsAngularApp.controller('riskManagementCalculatorUserControlController', 2 function ($scope, $rootScope, userApiService) { 3 4 $scope.IsLoading = false; 5 6 $scope.IsFirstLoad = true; 7 8 var typingTimer = 1000; //timer identifier 9 var doneTypingInterval = 1000; //time in ms 10 11 $scope.CurrentValueAtRisk = 1; 12 13 $scope.RiskManagementCalculator = { 14 PortfolioSize: 0, 15 RiskTolerance: 1, 16 ValueAtRiskMin: 0.5, 17 ValueAtRiskMax: 1, 18 ValueAtRisk: 1, 19 PortfolioAllocation: 20, 20 PortfolioAllocationMax: 20, 21 PortfolioAllocationSize: 0, 22 EntryPrice: 0, 23 CutlossPrice: 0, 24 TargetPrice: 0, 25 SharesPerUnit: 0, 26 CashAtRisk: 0, 27 isMaxAllocation: false 28 }; 29 30 $scope.SetRiskManagementCalculatorResult = function () { 31 32 $rootScope.$broadcast("SetRiskManagementCalculatorResult", $scope.RiskManagementCalculator); 33 $scope.SelectCalculator(0); 34 }; 35 36 $scope.SetRiskTolerance = function (riskToleranceType) { 37 38 var riskMgntCal = $scope.RiskManagementCalculator; 39 40 if (riskToleranceType === 1) { 41 riskMgntCal.ValueAtRisk = 1; //(0.5 - 1.00 %) 42 riskMgntCal.RiskTolerance = 1; 43 riskMgntCal.ValueAtRiskMin = 0.5; 44 riskMgntCal.ValueAtRiskMax = 1; 45 riskMgntCal.PortfolioAllocation = 0; 46 riskMgntCal.PortfolioAllocationMax = 20; 47 } 48 else if (riskToleranceType === 2) { 49 riskMgntCal.ValueAtRisk = 2.5; //(1.01 - 4.99%) 50 riskMgntCal.RiskTolerance = 2; 51 riskMgntCal.ValueAtRiskMin = 1.01; 52 riskMgntCal.ValueAtRiskMax = 2.5; 53 riskMgntCal.PortfolioAllocation = 0; 54 riskMgntCal.PortfolioAllocationMax = 50; 55 } 56 else if (riskToleranceType === 3) { 57 riskMgntCal.ValueAtRisk = 5; //(5% and up ) 58 riskMgntCal.RiskTolerance = 3; 59 riskMgntCal.ValueAtRiskMin = 5; 60 riskMgntCal.ValueAtRiskMax = 50; 61 riskMgntCal.PortfolioAllocation = 0; 62 riskMgntCal.PortfolioAllocationMax = 100; 63 } 64 else if (riskToleranceType === 4) { 65 // riskMgntCal.ValueAtRisk = 0; 66 riskMgntCal.RiskTolerance = 4; 67 riskMgntCal.ValueAtRiskMin = 0; 68 riskMgntCal.ValueAtRiskMax = 100; 69 riskMgntCal.PortfolioAllocation = 0; 70 riskMgntCal.PortfolioAllocationMax = 100; 71 72 if ($scope.IsFirstLoad) { 73 var portAllocMaxCache = LocalStorageCache.PortfolioAllocationMax.Get(); 74 riskMgntCal.PortfolioAllocationMax = (portAllocMaxCache !== undefined) ? portAllocMaxCache : 100; 75 } 76 } 77 78 var isSaveToCache = (riskToleranceType !== 4); 79 80 if (!$scope.IsFirstLoad) { 81 $scope.CalculateRiskTolerance(isSaveToCache); 82 } 83 }; 84 85 $scope.CalculateRiskTolerance = function (isSaveToCache) { 86 87 var riskData = $scope.RiskManagementCalculator; 88 89 riskData.CashAtRisk = (riskData.ValueAtRisk * riskData.PortfolioSize) / 100; 90 91 if (riskData.EntryPrice === null || riskData.CutlossPrice === null || riskData.EntryPrice === 0 || riskData.CutlossPrice === 0 || riskData.EntryPrice < riskData.CutlossPrice) { 92 riskData.SharesPerUnit = 0; 93 riskData.PortfolioAllocationSize = 0; 94 return; 95 } 96 97 // Quantity of Shares = Cash at Risk / [ (E - CL) * 1.012 ] = 1,000 / [ (1.00 - 0.95)*1.012 ] 98 99 var amountExpectedToLoss = (riskData.EntryPrice / ((riskData.EntryPrice - riskData.CutlossPrice) * 1.012)); 100 101 102 // Portfolio Allocation Size = % risk per trade / [(E - CL) * 1.012 ] 103 104 var portAllocationSize = 0; 105 // Port Alloc = [ E / (E-CL) * 1.012 ] x VAR 106 portAllocationSize = (amountExpectedToLoss > 0) ? riskData.ValueAtRisk * amountExpectedToLoss : 0; 107 108 if (portAllocationSize >= riskData.PortfolioAllocationMax) { 109 portAllocationSize = riskData.PortfolioAllocationMax; 110 riskData.isMaxAllocation = true; 111 } 112 113 riskData.PortfolioAllocationSize = (portAllocationSize > 1) ? portAllocationSize : 1; 114 115 // Should calculate last, dependent on calculating PortfolioAllocation if max or not 116 var sharesPerUnit = $scope.CalculateSharesPerUnit(riskData); 117 riskData.SharesPerUnit = isNaN(sharesPerUnit) ? 0 : sharesPerUnit; 118 119 if (gaAuthenticatedUserId > 0 && isSaveToCache && riskData.ValueAtRisk > 0) { 120 $scope.SaveRiskToleranceSelectedByUser(); 121 } 122 }; 123 124 $scope.GetBoardLotByPrice = function (price) { 125 126 var minBoardLot = 0; 127 if (price >= 0.0001 && price <= 0.0099) { 128 minBoardLot = 1000000; 129 } 130 else if (price >= 0.010 && price <= 0.049) { 131 minBoardLot = 100000; 132 } 133 else if (price >= 0.050 && price <= 0.249 || 134 price >= 0.250 && price <= 0.495) { 135 minBoardLot = 10000; 136 } 137 else if (price >= 0.50 && price <= 4.99) { 138 minBoardLot = 1000; 139 } 140 else if (price >= 5.0 && price <= 9.99 || 141 price >= 10 && price <= 19.98 || 142 price >= 20 && price <= 49.95) { 143 minBoardLot = 100; 144 } 145 else if (price >= 50 && price <= 99.95 || 146 price >= 100 && price <= 199.90 || 147 price >= 200 && price <= 499.80 || 148 price >= 500 && price <= 999.50) { 149 minBoardLot = 10; 150 } 151 else if (price >= 1000) { 152 minBoardLot = 5; 153 } 154 155 return minBoardLot; 156 157 }; 158 159 $scope.CalculateSharesPerUnit = function (riskData) { 160 161 var boardLot = $scope.GetBoardLotByPrice(riskData.EntryPrice); 162 163 var cashAtRiskByPortAllocation = ((riskData.PortfolioSize * riskData.PortfolioAllocationSize) / 100); 164 165 var sharesPerPrice = (cashAtRiskByPortAllocation / riskData.EntryPrice); 166 var sharesPerUnit = Math.floor(sharesPerPrice / boardLot) * boardLot; 167 168 return (sharesPerUnit > 1) ? sharesPerUnit : boardLot; //Minimum shares 169 }; 170 171 $scope.SaveRiskToleranceSelectedByUser = function () { 172 173 clearTimeout(typingTimer); 174 175 if ($scope.CurrentValueAtRisk !== null && $scope.RiskManagementCalculator.ValueAtRisk !== null) { 176 typingTimer = setTimeout(function () { 177 userApiService.saveRiskToleranceSelectedByUser($scope.RiskManagementCalculator.ValueAtRisk, $scope.RiskManagementCalculator.PortfolioAllocationSize).then(function (result) { 178 if (result.data) { 179 LocalStorageCache.PortfolioAllocationMax.Set($scope.RiskManagementCalculator.PortfolioAllocationMax); 180 LocalStorageCache.RiskTolerance.Set($scope.RiskManagementCalculator.RiskTolerance); 181 $scope.CurrentValueAtRisk = $scope.RiskManagementCalculator.ValueAtRisk; 182 } 183 }, 184 function (error) { 185 showErrorResponse(error); 186 }); 187 }, doneTypingInterval); 188 } 189 }; 190 191 $scope.GetValueAtRiskFromDBCache = function () { 192 193 userApiService.getValueAtRiskByUserId().then(function (result) { 194 if (result) { 195 $scope.setValueAtRiskAndPostProcess(result.data); 196 } 197 }, function (error) { 198 showErrorResponse(error); 199 }); 200 }; 201 202 $scope.setValueAtRiskAndPostProcess = function (valueAtRisk) { 203 $scope.RiskManagementCalculator.ValueAtRisk = valueAtRisk; 204 $scope.GetRiskToleranceFromCache(); 205 }; 206 207 $scope.GetRiskToleranceFromCache = function () { 208 209 var riskToleranceCache = LocalStorageCache.RiskTolerance.Get(); 210 if (riskToleranceCache === undefined) { 211 riskToleranceCache = 1; 212 } 213 $scope.SetRiskTolerance(riskToleranceCache); 214 215 216 $scope.IsFirstLoad = false; 217 }; 218 219 220 $scope.InitValue = function () { 221 if ($scope.UserNav.IsUserLoggedOn) { 222 $scope.GetValueAtRiskFromDBCache(); 223 } 224 else { 225 $scope.setValueAtRiskAndPostProcess(1); 226 } 227 }; 228 229 $scope.InitValue(); 230 } 231);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.