Quantitative Analysis, Risk Management, Modelling, Algo Trading, and Big Data Analysis

Number of Shares for Limit Orders


Limit orders provide investors with an opportunity to buy or sell a specific number of shares at a given/demanded price. In many instances this solution is favored to market orders because of a slightly higher degree of control over risk or potential losses. Given a share price (limit order), $P$, we aim at the estimation of number of shares, $N$, we intend to buy/sell.

The estimation of $N$ is usually not as simple as $N=C/P$ where $C$ denotes a capital for investment. If our trade is a subject to commission fees, we need to include it in estimations. The following simple C++ code provides an attractive estimation for $N$. It also provides an investor with a stop-loss price alert if the stock prices drops by 10%, and displays a list of potential gains/profits in case when the stock prices rises by a specific percentage.

The code is written for long positions only but it can be easily altered to include short-selling.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*   Number of Shares for Limit Order
 *   (c) Quant at Risk, 2012
 *
 *   nshares.cpp
 */
 
#include <iostream>
#include <cmath>
#include <iomanip>
 
using namespace std;
 
// supporting function
void PrintInfo(double goe, double cb, double sc, double sp, double n) {
    double s=(goe*cb+cb+sc)/n; 
    double rs=s-sp;
    cout << setprecision(0) << fixed << setw(4) << right << goe*100.00;
    cout << setprecision(2) << fixed << left << "%:  $" << s << "  +" << 
            rs << "  ($" << setprecision(2) << cb*goe << ")" << endl;
}
 
int main()
{
    // if you are trading with a specific trading company
    // define value of fees charged, e.g. $19.90 per transaction
    // for both Buy and Sell order
    double bc(19.99), sc(19.99);
 
    // assume percentage of risk for your stop-loss
    double re(0.10), r=re;
 
    double e,sp,n,cb,sp_thr,rg,sl,goe,s;
 
    cout << "\ncommision fees:" << endl;
    cout << " buy  $" << bc << ", sell $" << sc << endl;
 
    cout << "\nvalue of capital to invest:" << endl;
    double ci; cin >> ci;
 
    // your equity
    e=ci-bc;
 
    cout << "limit-order share price:" << endl;
    cin >> sp;
 
    cout << "\nassumed risk: " << r*100 << "%" << endl;
 
    // find number of shares to buy
    n=floor(e*re/(sp*r));
    cout << "\n*** BUY: " << (int)n << " shares" << endl;
 
    cb=n*sp+bc;
    cout << "\nestimated cost of purchase:  $" << cb << endl; 
 
    // stop-loss
    sl=(-r*cb+cb+sc)/n;
    cout << "your stop-loss is at:        $" << sl << endl;
 
    // earning threshold
    sp_thr=(cb+sc)/n;
    rg=(sp_thr-sp)/sp;
    cout << "earning threshold:           $" << 
            sp_thr << " (+$" << fixed << setprecision(2) <<
            sp_thr-sp << ")";
    cout << "\n  required growth:           " << 
            100.0*rg << "%" << endl << endl;
 
    // required growth on equity
    double rs;
    cout << "growth/your profit: estimations" << endl;
 
    for(double i=0.01; i<0.06; i=i+0.01) {
        PrintInfo(i,cb,sc,sp,n);
    }
    for(double i=0.1; i<0.501; i=i+0.05) {
        PrintInfo(i,cb,sc,sp,n);
    }
    PrintInfo(1.0,cb,sc,sp,n);
 
    cout << endl;
    return(0);    
}

Complilation and execution

gcc nshares.cpp -o nshares
./nshares

Our example pertains to investing \$21,000.00 in IBM stock. Sending a limit order of \$193.90 per share, we wish to buy $N$ shares. Assuming commission fees of \$19.99 paid to our broker, the estimation suggests 108 shares of IBM to buy.

commision fees:
 buy  $19.99, sell $19.99
 
value of capital to invest:
21000
limit-order share price:
193.90
 
assumed risk: 10%
 
*** BUY: 108 shares
 
estimated cost of purchase:  $20961.2
your stop-loss is at:        $174.862
earning threshold:           $194.27 (+$0.37)
  required growth:           0.19%
 
growth/your profit: estimations
   1%:  $196.21  +2.31  ($209.61)
   2%:  $198.15  +4.25  ($419.22)
   3%:  $200.09  +6.19  ($628.84)
   4%:  $202.03  +8.13  ($838.45)
   5%:  $203.97  +10.07  ($1048.06)
  10%:  $213.68  +19.78  ($2096.12)
  15%:  $223.38  +29.48  ($3144.18)
  20%:  $233.09  +39.19  ($4192.24)
  25%:  $242.79  +48.89  ($5240.30)
  30%:  $252.50  +58.60  ($6288.36)
  35%:  $262.20  +68.30  ($7336.42)
  40%:  $271.90  +78.00  ($8384.48)
  45%:  $281.61  +87.71  ($9432.54)
  50%:  $291.31  +97.41  ($10480.59)
 100%:  $388.36  +194.46  ($20961.19)

By plotting earning threshold and stop-loss lines, by horizontal blue and red lines, respectively, we get a good feeling on potential share price movement in determined zones.

Contact Form Powered By : XYZScripts.com