-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnaiveGrid.ts
55 lines (52 loc) · 1.71 KB
/
naiveGrid.ts
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
import { Exchange, Trade } from 'exchange'
import { PriceHistory } from 'coin'
import { GridStrategy } from './grid'
/**
* A strategy that simply trades when the price of a coin changes by a certain percentage.
* It's naive because it doesn't pay attention to cost basis, and can end up buying all the way down then actually selling lower when the price goes up just a little.
*/
export class NaiveGridStrategy extends GridStrategy {
getTrades({
priceHistory,
coinAmount,
cashAmount,
exchange
}: {
priceHistory: PriceHistory
coinAmount: number
cashAmount: number
exchange: Exchange
}): {
trades: Trade[]
endingCoinAmount: number
endingCashAmount: number
} {
let trades = []
let referencePrice = priceHistory.startingPrice
let buyPrice = this.getBuyPrice(referencePrice)
let sellPrice = this.getSellPrice(referencePrice)
for (let historicalPrice of priceHistory.prices) {
let { trade, newCoinAmount, newCashAmount } = this.getTrade({
historicalPrice,
buyPrice,
sellPrice,
coinAmount,
cashAmount,
exchange
})
if (trade) {
trades.push(trade)
coinAmount = newCoinAmount
cashAmount = newCashAmount
referencePrice = trade.price
buyPrice = this.getBuyPrice(referencePrice)
sellPrice = this.getSellPrice(referencePrice)
}
}
return {
trades,
endingCoinAmount: coinAmount,
endingCashAmount: cashAmount
}
}
}