forked from faangbait/lets-play-bitburner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathif.stock.short.js
83 lines (69 loc) · 2.47 KB
/
if.stock.short.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
/**
*
* @param {import(".").NS} ns
* @param {*} stockObject
* @returns
*/
export const stockExtenderShort = (ns, stockObject) => {
stockObject._goshort = function (shares) {
return ns.stock.sell(stockObject.ticker, shares) * shares
}
stockObject.max_short = function () {
let shares = (ns.getServerMoneyAvailable("home") - 100000) / stockObject.price.bear
shares = Math.floor(Math.min(shares, stockObject.maxShares - stockObject.position.bear))
if (shares * stockObject.price.bear > 2000000) {
return stockObject._goshort(shares)
}
}
stockObject.shortCost = function (shares) {
return (shares * stockObject.price.bear) + 100000
}
stockObject.unshort = function (shares=stockObject.position.bear) {
return ns.stock.sellShort(stockObject.ticker, shares);
}
stockObject.absoluteForecast = function () {
try {
return Math.abs(stockObject.forecast - .5)
} catch {}
}
return stockObject;
};
/**
*
* @param {import(".").NS} ns
* @param {*} stockObject
* @returns
*/
export const stockExtenderLimit = (ns, stockObject) => {
stockObject.orders = { get () {
return ns.stock.getOrders()[stockObject.ticker]
}
}
stockObject.cancelAllOrders = function () {
return stockObject.orders.forEach(o => ns.stock.cancelOrder(stockObject.ticker, o.shares, o.price, o.type, o.position))
}
stockObject.order = function (shares, price, type, position) {
ns.stock.placeOrder(stockObject.ticker, shares, price, type, position)
}
stockObject.stopLoss = function () {
// an order placed at 90% of current buy price
// that sells the stock upon the price dipping
stockObject.order(
stockObject.position.bear || stockObject.position.bull,
(stockObject.position.bull) ? stockObject.price.bull * .9 : stockObject.price.bear * 1.1,
"Stop Sell",
(stockObject.position.bull) ? "L" : "S"
)
}
stockObject.limitExit = function () {
// an order placed at 110% of purchase price
// that exits the position upon the price rising
stockObject.order(
stockObject.position.bear || stockObject.position.bull,
(stockObject.position.bull) ? stockObject.price.bull * 1.1 : stockObject.price.bear * .9,
"Limit Sell",
(stockObject.position.bull) ? "L" : "S"
)
}
return stockObject;
};