General Topics
Markets
Technical Topics
Brokerage Firms
Community Lounge
Site Support

# avgx.cpp: A formula for flexible AveragingDown + AveragingUp in C++

Discussion in 'Programming' started by mutluit, Oct 26, 2012.

1. ### mutluit

Code:
```/*
avgx.cpp

A utility function for calculating flexible "averaging down" and "averaging up"
using an analytical method, ie. using a formula instead of a costly iterative method.

Written by: mutluit at tinystockexchange.com

2012-10-27-Sa

Platform: every modern C++ compiler should compile this. Tested with g++ on Debian GNU/Linux

Compile: g++ -o avgx.exe avgx.cpp

The math:
pps = (a*b + c*d) / (a+c)           # a=qty1, b=price1, c=qty2, d=price2, pps=price per share
pl% = ((a+c)*d / (a*b + c*d) - 1.0) * 100 * (fShort ? -1.0 : 1.0)
--> (using solver at [url]www.quickmath.com[/url]):
#c = -(a*b*p - 100*a*d + 100*a*b) / (d*p)
c = -(a*b*p + (100*a*b - 100*a*d) * (fShort ? -1.0 : 1.0)) / (d*p)
d = -(a*b*p + 100*a*b * (fShort ? -1.0 : 1.0)) / (c*p - 100*a * (fShort ? -1.0 : 1.0))

Beware:
there is intentionally no plausibility of the user parameters built-in, ie. garbage in will give garbage out,

Examples:
./avgx.exe
gives
usage: avgx.exe curqty pricebought lastprice fShort wantPLpct

./avgx.exe 1000 100 90 0 -5
gives:

./avgx.exe 1000 100 110 1 -5
gives

But the following parameter set will give, in the context of financial instruments, a nonsensical result,
although the method is correct, but the input is wrong:
./avgx.exe 1000 100 110 0 -5
gives
*/

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;

{ // rc: 0=ok

double c = 0;
const double  plpct1 = ((a+c)*d / (a*b + c*d) - 1.0) * 100 * (AfShort ? -1.0 : 1.0);

c                    = -(a*b*p + (100*a*b - 100*a*d) * (AfShort ? -1.0 : 1.0)) / (d*p);
const double  plpct2 = ((a+c)*d / (a*b + c*d) - 1.0) * 100 * (AfShort ? -1.0 : 1.0);

const double  pps    = (a*b + c*d) / (a+c);

printf("PLpct=%.2lf AddQty=%.2lf PLpctNew=%.2lf pps=%.4lf\n", plpct1, c, plpct2, pps);

return 0;
}

int main(int argc, char* argv[])
{ // rc: 0=ok
if (argc < 6)
{
printf("usage: avgx.exe curqty pricebought lastprice fShort wantPLpct\n");
return 1;
}
int ixArg = 0;
const bool   AfShort      = !!atoi(argv[++ixArg]);

}

```

2. ### Daring

I would like to understand this, is there an English explanation ?

3. ### mutluit

It's a little commandline program written in the programming language C++, with comments at the beginning.
It simply calculates how many shares one needs to buy additionally to average down the loss to a desired percent value.
There are some examples given in the comment section of the code.
One needs a C++ compiler to generate the executable program.
Under the Linux operating system such tools like compiler etc. are usually included,
under Windows you would need to install a C++ compiler first, there are some free available, for example in cygwin, mingw etc. --> just google.

Since the algorithm (the formula) is given, it should be easy to use it in a spreadsheet application like Microsoft Excel or OpenOffice Calc etc.
It's basically this line:
c = -(a*b*p + (100*a*b - 100*a*d) * (fShort ? -1.0 : 1.0)) / (d*p)

4. ### 2rosy

you made something simple into something complex. no one is going to compile a little c++ formula to get answer. They wont compile anything.

MAKE IT EASY

5. ### mutluit

Sorry, I don't have time, the code is intended more for sw-developers as they know what it is and how to use it,
or howto convert it to other programming languages.

Why not post your simple version?

ET IS FREE BECAUSE OF THE FINANCIAL SUPPORT FROM THESE COMPANIES: