libcalculatorfinance v0.0.1 LibCalculatorFinance.General
General functions that can be used for both trading and investing.
Summary
Functions
calculate_average_price
Calculate the average price, based on previous transactions. It requires a SharesPrice struct list
calculate_leveraged_contracts
Calculates the number of contracts to buy, according to an algorithm that determines an ideal amount of leverage
calculate_percentage_of
Calculate what percentage a_value is from a_from_value
convert_from_orig
Returns a price, with an exchange rate applied to it
convert_to_orig
Returns a price, in the original currency, with the exchange rate no longer applied to it
version
Returns the version string of the LibCalculatorFinance library
Functions
calculate_average_price
Calculate the average price, based on previous transactions. It requires a SharesPrice struct list.
Note
S1 = 415, P1 = 23.65, S2 = 138, P2 = 16.50
When you need to buy new stocks and you need to log this for accounting purposes, you need to know what the average price was that you paid. You only know the total number of shares you have, called S3. The price is the average of all prices you paid on buy/ sell of all previous transactions.
S1 P1 + S2 P2 = S3 * P3
=> P3 = (S1 P1 + S2 P2) / (S1 + S2)
=> P3 = (415 23.65 + 138 16.50) / 553
=> P3 = 21.8657
Examples
iex> Float.round(LibCalculatorFinance.General.calculate_average_price([%SharesPrice{sp_shares: 415, sp_price: 23.65}, %SharesPrice{sp_shares: 138, sp_price: 16.50}]), 6)
21.865732
calculate_leveraged_contracts
Calculates the number of contracts to buy, according to an algorithm that determines an ideal amount of leverage.
Examples
iex> LibCalculatorFinance.General.calculate_leveraged_contracts(4.0)
5.0
calculate_percentage_of
Calculate what percentage a_value is from a_from_value.
Examples
iex> LibCalculatorFinance.General.calculate_percentage_of(2.0, 50.0)
4.0
convert_from_orig
Returns a price, with an exchange rate applied to it.
Examples
iex> LibCalculatorFinance.General.convert_from_orig(12.0, 0.5)
6.0
convert_to_orig
Returns a price, in the original currency, with the exchange rate no longer applied to it.
Examples
iex> LibCalculatorFinance.General.convert_to_orig(6.0, 0.5)
12.0