Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
This module provides FIFO machinery for inventory accounting.
Synopsis
- data Inventory (pprec :: Nat) (sprec :: Nat) (vprec :: Nat) = MkInventory {
- inventoryTotal :: !(Quantity sprec)
- inventoryCurrent :: !(Seq (InventoryEvent pprec sprec))
- inventoryHistory :: !(Seq (InventoryHistoryItem pprec sprec vprec))
- data InventoryEvent (pprec :: Nat) (sprec :: Nat) = InventoryEvent {
- inventoryEventDate :: !Day
- inventoryEventPrice :: !(Quantity pprec)
- inventoryEventQuantity :: !(Quantity sprec)
- data InventoryHistoryItem (pprec :: Nat) (sprec :: Nat) (vprec :: Nat) = MkInventoryHistoryItem {
- inventoryHistoryItemDate :: !Day
- inventoryHistoryItemPnl :: !(Quantity vprec)
- inventoryHistoryItemFst :: !(InventoryEvent pprec sprec)
- inventoryHistoryItemSnd :: !(InventoryEvent pprec sprec)
- updateInventory :: KnownNat pprec => KnownNat sprec => KnownNat vprec => InventoryEvent pprec sprec -> Inventory pprec sprec vprec -> (Seq (InventoryHistoryItem pprec sprec vprec), Inventory pprec sprec vprec)
- updateInventoryVP :: KnownNat pprec => KnownNat sprec => KnownNat vprec => Day -> Quantity pprec -> Quantity sprec -> Inventory pprec sprec vprec -> (Seq (InventoryHistoryItem pprec sprec vprec), Inventory pprec sprec vprec)
- updateInventoryVV :: KnownNat pprec => KnownNat sprec => KnownNat vprec => Day -> Quantity vprec -> Quantity sprec -> Inventory pprec sprec vprec -> (Seq (InventoryHistoryItem pprec sprec vprec), Inventory pprec sprec vprec)
- updateInventoryAux :: KnownNat pprec => KnownNat sprec => KnownNat vprec => Seq (InventoryHistoryItem pprec sprec vprec) -> InventoryEvent pprec sprec -> Inventory pprec sprec vprec -> (Seq (InventoryHistoryItem pprec sprec vprec), Inventory pprec sprec vprec)
- splitEvent :: KnownNat pprec => KnownNat sprec => Quantity sprec -> InventoryEvent pprec sprec -> (InventoryEvent pprec sprec, InventoryEvent pprec sprec)
- addInventoryEvent :: KnownNat pprec => KnownNat sprec => KnownNat vprec => InventoryEvent pprec sprec -> Inventory pprec sprec vprec -> Inventory pprec sprec vprec
- munchAll :: KnownNat pprec => KnownNat sprec => KnownNat vprec => InventoryEvent pprec sprec -> InventoryEvent pprec sprec -> Seq (InventoryEvent pprec sprec) -> Inventory pprec sprec vprec -> (InventoryHistoryItem pprec sprec vprec, Inventory pprec sprec vprec)
- data Munch (sprec :: Nat)
- whatMunch :: KnownNat pprec => KnownNat sprec => InventoryEvent pprec sprec -> InventoryEvent pprec sprec -> Munch sprec
Data Definitions
data Inventory (pprec :: Nat) (sprec :: Nat) (vprec :: Nat) Source #
Data definition that keeps track of inventory for an economic resource.
This data definition is polymorphic over the precision for, respectively:
pprec
precision of the price values,sprec
precision of the inventory event quantities, andvprec
precision of the monetary values such as PnL.
Values of this type should not be directly manipulated. Instead, def
is to
be used for initializing an empty inventory and updateInventory
method (and
convenience wrappers) should be used to update the inventory with new
inventory events.
MkInventory | |
|
Instances
data InventoryEvent (pprec :: Nat) (sprec :: Nat) Source #
Data definition for inventory events.
This data definition is polymorphic over the precision for, respectively:
pprec
precision of the price values, andsprec
precision of the inventory event quantities.
InventoryEvent | |
|
Instances
data InventoryHistoryItem (pprec :: Nat) (sprec :: Nat) (vprec :: Nat) Source #
Data definition for PnL-taking inventory history items.
Essentially, values of this type represent a pnl-taking for a long/short inventory event and a matching short/long inventory event of the same quantity. Date refers to the date of the later event. If prices are different, PnL is non-zero.
This data definition is polymorphic over the precision for, respectively:
pprec
precision of the price values,sprec
precision of the inventory event quantities, andvprec
precision of the monetary values such as PnL.
Values of this type should not be directly manipulated. updateInventory
method (and convenience wrappers) are in charge of creating values of this
data type.
MkInventoryHistoryItem | |
|
Instances
Operations
updateInventory :: KnownNat pprec => KnownNat sprec => KnownNat vprec => InventoryEvent pprec sprec -> Inventory pprec sprec vprec -> (Seq (InventoryHistoryItem pprec sprec vprec), Inventory pprec sprec vprec) Source #
Processes a new inventory event onto the inventory.
Any event with 0
quantity is disregarded.
updateInventoryVP :: KnownNat pprec => KnownNat sprec => KnownNat vprec => Day -> Quantity pprec -> Quantity sprec -> Inventory pprec sprec vprec -> (Seq (InventoryHistoryItem pprec sprec vprec), Inventory pprec sprec vprec) Source #
Convenience wrapper for updateInventory
which works with date, price and
quantity.
updateInventoryVV :: KnownNat pprec => KnownNat sprec => KnownNat vprec => Day -> Quantity vprec -> Quantity sprec -> Inventory pprec sprec vprec -> (Seq (InventoryHistoryItem pprec sprec vprec), Inventory pprec sprec vprec) Source #
Convenience wrapper for updateInventory
which works with date, price and
quantity.
Internal Definitions
updateInventoryAux :: KnownNat pprec => KnownNat sprec => KnownNat vprec => Seq (InventoryHistoryItem pprec sprec vprec) -> InventoryEvent pprec sprec -> Inventory pprec sprec vprec -> (Seq (InventoryHistoryItem pprec sprec vprec), Inventory pprec sprec vprec) Source #
Work-horse function for updating inventory.
This is where the whole trick happens in this module.
splitEvent :: KnownNat pprec => KnownNat sprec => Quantity sprec -> InventoryEvent pprec sprec -> (InventoryEvent pprec sprec, InventoryEvent pprec sprec) Source #
Splits the event into two events as per the given quantity.
If the event has a quantity of 100
and the desired quantity is 10
, this
function spits out two event with same information except that the first has
a quantity of 10
and the second has a quantity of 90
.
addInventoryEvent :: KnownNat pprec => KnownNat sprec => KnownNat vprec => InventoryEvent pprec sprec -> Inventory pprec sprec vprec -> Inventory pprec sprec vprec Source #
Pushes a new inventory event to the inventory.
This function is to be called by the internal machinery that handles most of the critical tasks. Direct calls to this function will bypass the entire machinery.
munchAll :: KnownNat pprec => KnownNat sprec => KnownNat vprec => InventoryEvent pprec sprec -> InventoryEvent pprec sprec -> Seq (InventoryEvent pprec sprec) -> Inventory pprec sprec vprec -> (InventoryHistoryItem pprec sprec vprec, Inventory pprec sprec vprec) Source #
Captures two events of same absolute quantities with different directions into a profit-taking inventory history item and updates the inventory.
data Munch (sprec :: Nat) Source #
Data definition representing how two inventory events should be processed.
whatMunch :: KnownNat pprec => KnownNat sprec => InventoryEvent pprec sprec -> InventoryEvent pprec sprec -> Munch sprec Source #
Figures out how two inventory events should be processed.