-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
171 lines (151 loc) · 6.01 KB
/
index.js
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
'use strict';
// waiting for https://github.com/tylerfloyd/TastyWorks/pull/5 to be merged.
// In the meantime use https://github.com/iloire/TastyWorks
const TastyWorks = require('../TastyWorks');
//const TastyWorks = require('tasty-works-api');
const stock = require('./stock');
const util = require('./util');
const chart = require('./chart');
const path = require('path');
const blackscholes = require('black-scholes');
const credentials = {
username: process.env.TW_USER,
password: process.env.TW_PASSWORD
};
// input parameters
const DEFAULT_CHANGES_IN_SPY = [-5, -3, -2, -1, 1, 2, 3, 5];
const DEFAULT_CHANGES_IN_UNDERLYING = [-15, -10, -5, 5, 10, 15, 20];
const RISK_FREE_INTEREST_RATE = 0.03; // risk-free interest rate. Used in black scholes formula
const GO_FORWARD_DAYS_IN_TIME = [0, 10, 20];
const cacheMarketMetrics = {};
const getMarketMetrics = async (ticker) => {
if (cacheMarketMetrics[ticker]) {
return cacheMarketMetrics[ticker];
}
const metrics = await TastyWorks.marketMetrics(process.env.TW_ACCOUNT_ID, [ticker]);
cacheMarketMetrics[ticker] = metrics;
return metrics;
};
const getDataForUnderlying = (positionsForUnderlaying, betaWeightedChangePercentage, currentPriceUnderlying, volatility, riskFreeInterestRate, goForwardDaysInTime) => {
const newUnderlyingSimulatedPrice = currentPriceUnderlying * (1 + betaWeightedChangePercentage / 100);
const data = {
goForwardDaysInTime,
newUnderlyingSimulatedPrice,
betaWeightedChangePercentage,
positions: {},
pl: 0
};
for (const p of positionsForUnderlaying) {
const expirationYears = util.getExpirationInYears(new Date(), p.symbol);
const simulatedExpirationYears = expirationYears - goForwardDaysInTime / 365;
const isEquityOption = p['instrument-type'] == 'Equity Option';
const isEquity = p['instrument-type'] == 'Equity';
let simulatedPrice;
if (isEquity) {
simulatedPrice = newUnderlyingSimulatedPrice;
}
else {
const optionType = util.getOptionType(p.symbol);
const strikePrice = isEquityOption ? util.getStrikePriceOptions(p.symbol) : util.getStrikePriceFutures(p.symbol);
simulatedPrice = blackscholes.blackScholes(
newUnderlyingSimulatedPrice,
strikePrice,
simulatedExpirationYears <= 0 ? 0 : simulatedExpirationYears, // account for option expired.
volatility,
riskFreeInterestRate,
optionType
);
}
const direction = p['quantity-direction'];
const quantity = p['quantity'];
const short = direction === 'Short';
const currentPrice = Number(p['mark-price']);
const currentValue = currentPrice * p.quantity * p.multiplier * (short ? -1 : 1);
const simulatedValue = simulatedPrice * p.quantity * p.multiplier * (short ? -1 : 1);
const pl = (simulatedValue - currentValue);
data.positions[p.symbol] = {
direction,
quantity,
currentPrice,
simulatedPrice,
currentValue,
simulatedValue,
expirationYears,
simulatedExpirationYears,
pl
};
data.pl += pl;
};
return data;
};
const getPositions = async () => {
TastyWorks.setUser(credentials);
return TastyWorks.authorization()
.then(token => {
TastyWorks.setAuthorizationToken(token);
return true;
})
.then(() => TastyWorks.accounts())
.then(accounts => TastyWorks.setUser({ accounts }))
.then(() => TastyWorks.positions(process.env.TW_ACCOUNT_ID))
.then((positionsRes) => positionsRes.items)
};
const chartRisk = async (options = {}) => {
return getRisk(options)
.then((data) => {
const chartData = Object.keys(data)
.map(Number).sort((a, b) => a - b)
.map(k => ({ key: k, value: data[k].total }));
chart.plot(chartData, path.join(options.path, 'simulation'), 'Risk simulator');
return chartData;
})
}
const getRisk = async (options = {}) => {
const forwardExpirationDaysInTime = options.forwardExpirationDaysInTime || GO_FORWARD_DAYS_IN_TIME;
const riskFreeInterestRate = options.riskFreeInterest || RISK_FREE_INTEREST_RATE;
return getPositions()
.then(positions => util.groupBy(positions, 'underlying-symbol'))
.then(async groups => {
const risk = {};
for (const underlying of Object.keys(groups).sort()) {
const currentPriceUnderlying = (await stock(underlying)).price.regularMarketPrice;
const metrics = (await getMarketMetrics(underlying)).items[0];
const volatility = metrics['implied-volatility-index'];
const beta = Number(metrics.beta) || 1;
risk[underlying] = {
meta: {
forwardExpirationDaysInTime,
beta,
currentPriceUnderlying,
volatility,
riskFreeInterestRate,
},
byForwardExpirationDaysInTime: [],
};
for (const expirationForwardDays of forwardExpirationDaysInTime) {
const forExpiration = {
expirationForwardDays, simulations: {
byChangeInSPYIndex: [],
byChangeInUnderlying: []
}
};
for (const changePercentage of (options.percentageChangesinSPY || DEFAULT_CHANGES_IN_SPY)) {
const betaWeightedChange = changePercentage * beta;
forExpiration.simulations.byChangeInSPYIndex.push({
changePercentage,
simulation: getDataForUnderlying(groups[underlying], betaWeightedChange, currentPriceUnderlying, volatility, riskFreeInterestRate, expirationForwardDays)
});
}
for (const changePercentage of (options.percentageChangesinUnderlying || DEFAULT_CHANGES_IN_UNDERLYING)) {
forExpiration.simulations.byChangeInUnderlying.push({
changePercentage,
simulation: getDataForUnderlying(groups[underlying], changePercentage, currentPriceUnderlying, volatility, riskFreeInterestRate, expirationForwardDays)
});
}
risk[underlying].byForwardExpirationDaysInTime.push(forExpiration);
}
}
return risk;
})
};
module.exports = { getRisk, chartRisk };