-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ValuationCalcultor utility, update PositionItem to use ValuationC…
…alculator.
- Loading branch information
1 parent
8853d73
commit 208c480
Showing
3 changed files
with
308 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
const Decimal = require('@barchart/common-js/lang/Decimal'), | ||
is = require('@barchart/common-js/lang/is'); | ||
|
||
const InstrumentType = require('./../data/InstrumentType'); | ||
|
||
module.exports = (() => { | ||
'use strict'; | ||
|
||
class ValuationCalculator { | ||
constructor() { | ||
|
||
} | ||
|
||
static calculate(instrument, price, quantity) { | ||
let priceToUse = null; | ||
|
||
if (is.number(price)) { | ||
priceToUse = new Decimal(price); | ||
} else if (price instanceof Decimal) { | ||
priceToUse = price; | ||
} | ||
|
||
if (priceToUse === null) { | ||
return null; | ||
} | ||
|
||
const calculator = calculators.get(instrument.type); | ||
|
||
return calculator(instrument, priceToUse, quantity); | ||
} | ||
|
||
toString() { | ||
return `[ValuationCalculator]`; | ||
} | ||
} | ||
|
||
function calculateForCash(instrument, price, quantity) { | ||
return new Decimal(quantity); | ||
} | ||
|
||
function calculateForEquity(instrument, price, quantity) { | ||
return price.multiply(quantity); | ||
} | ||
|
||
function calculateForEquityOption(instrument, price, quantity) { | ||
const priceMultiplier = instrument.option.multiplier; | ||
|
||
return price.multiply(priceMultiplier).multiply(quantity); | ||
} | ||
|
||
function calculateForFund(instrument, price, quantity) { | ||
return price.multiply(quantity); | ||
} | ||
|
||
function calculateForFuture(instrument, price, quantity) { | ||
const minimumTick = instrument.future.tick; | ||
const minimumTickValue = instrument.future.value; | ||
|
||
return price.divide(minimumTick).multiply(minimumTickValue).multiply(quantity); | ||
} | ||
|
||
function calculateForFutureOption(instrument, price, quantity) { | ||
const minimumTick = instrument.option.tick; | ||
const minimumTickValue = instrument.option.value; | ||
|
||
return price.divide(minimumTick).multiply(minimumTickValue).multiply(quantity); | ||
} | ||
|
||
function calculateForOther(instrument, price, quantity) { | ||
return price.multiply(quantity); | ||
} | ||
|
||
const calculators = new Map(); | ||
|
||
calculators.set(InstrumentType.CASH, calculateForCash); | ||
calculators.set(InstrumentType.EQUITY, calculateForEquity); | ||
calculators.set(InstrumentType.EQUITY_OPTION, calculateForEquityOption); | ||
calculators.set(InstrumentType.FUND, calculateForFund); | ||
calculators.set(InstrumentType.FUTURE, calculateForFuture); | ||
calculators.set(InstrumentType.FUTURE_OPTION, calculateForFutureOption); | ||
calculators.set(InstrumentType.OTHER, calculateForOther); | ||
|
||
return ValuationCalculator; | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.