[Solved] Need help crafting an inverse function to match an existing one
Hey folks... I'm working on a utility that helps me decide when the time is right to place a trade.
I base it on a ratio. If my investment is above a certain ratio relative to my total portfolio size, I'll sell some, and when it falls below, I'll buy some.
(over-simplified, but accurate enough for this purpose)
Pasting my scratchpad notes below.
I'm already burning my brain out trying to remember what little C++ I knew years ago... trying to recall 30yr old algebra is shorting out my neurons completely.
At this point, I'm either close to finishing it, or I've stuck myself down a dead end in the conversion.
In my scratchpad, all values are known for testing, but in practice, the first equation would find my ratio based on current price.
The equation I need, would allow me to select a target ratio, and have it show me the price the ticker would have to reach in order to hit that ratio.
So while the first one I'm looking for 'ratio', in the 2nd one (unfinished), I'm looking for 'price'.
Anyways, here's my scratchpad...
All variables are unsigned floats, and the scope of Ratio is between (but not including) 0.0 and 1.0 (as those cases are handled before reaching the calculation to avoid any divide by zero issues. Ratios also get trimmed to 4 decimal places, with 5/4 rounding, so the results don't have to be completely precise. A difference of ±0.0001 is something I can easily live with.
Thanks for any help offered to solve this.
-- Smoov
edit: deleted last attempt in my example as it didn't add up and was very wrong
edit2: put it back in as I ran the math again and it did add up after all. I made a typo when I first ran the math it seems. still stuck tho.
Hey folks... I'm working on a utility that helps me decide when the time is right to place a trade.
I base it on a ratio. If my investment is above a certain ratio relative to my total portfolio size, I'll sell some, and when it falls below, I'll buy some.
(over-simplified, but accurate enough for this purpose)
Pasting my scratchpad notes below.
I'm already burning my brain out trying to remember what little C++ I knew years ago... trying to recall 30yr old algebra is shorting out my neurons completely.
At this point, I'm either close to finishing it, or I've stuck myself down a dead end in the conversion.
In my scratchpad, all values are known for testing, but in practice, the first equation would find my ratio based on current price.
The equation I need, would allow me to select a target ratio, and have it show me the price the ticker would have to reach in order to hit that ratio.
So while the first one I'm looking for 'ratio', in the 2nd one (unfinished), I'm looking for 'price'.
Anyways, here's my scratchpad...
Code:
Currency: 7397.4771 | Commodity Portfolio Invested
Commodity: 9.2498 | Value Total Ratio
Price High: 659.999 6104.8587502 13502.3358502 0.4521335284449745
Price Last: 614.9 5687.70202 13085.17912 0.4346674942574267
Price Low: 572.0999 5291.80965502 12689.28675502 0.4170297162625402
To find ratio: Value = Commodity * Price
Total = Value + Currency
Ratio = Value / Total
Commodity * Price
Ratio = -------------------------------- = ( Co * P ) / ( ( Co * P ) + Cu )
( Commodity * Price ) + Currency
Ratio = ( Comm * Price ) / ( ( Comm * Price ) + Curr )
0.4521335284449745 ( 9.2498 * 659.999 ) / ( ( 9.2498 * 659.999 ) + 7397.4771 )
( Comm * Price ) = ( ( Comm * Price ) + Curr ) * Ratio
( 9.2498 * 659.999 ) = ( ( 9.2498 * 659.999 ) + 7397.4771 ) * 0.4521335284449745
Price = ( ( ( Comm * Price ) + Curr ) * Ratio ) / Comm
659.999 = ( ( ( 9.2498 * 659.999 ) + 7397.4771 ) * 0.4521335284449745 ) / 9.2498
P = ( Co * P + Cu ) * R / Co <-- (this is as far as I have figured out)
^ ^ <-- (I'm stuck on getting that 2nd P out from the right of the equal)
Thanks for any help offered to solve this.
-- Smoov
edit: deleted last attempt in my example as it didn't add up and was very wrong
edit2: put it back in as I ran the math again and it did add up after all. I made a typo when I first ran the math it seems. still stuck tho.
Last edited: