Need help crafting an inverse function to match an existing one

Smoovious

New member
Joined
Dec 22, 2013
Messages
2
[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...
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)
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. :(
 
Last edited:
Trying to decipher your post

are you saying you want to turn this formula

Ratio\(\displaystyle =\frac{Commodity \cdot Price}{(Commodity \cdot Price)+Currency)}\)

\(\displaystyle R=\frac{Co \cdot P}{(Co \cdot P)+Cu}\)

into one that solves for Price?
Yes, you understood it perfectly, thank you. :)

(I tend to speak code instead of math, sorry)

I just tested it out, and I got all 3 prices I expected to get.

Thank you SOOO much... I don't think using a 1-R below R would have ever occurred to me, I've forgotten so much of what I never used.

Was quick too.

(and, rather humbling as well :D )

Marking topic as solved.

-- Smoov

edit: guess a mod will have to mark it solved when they see it.
 
Last edited:
Top