-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
181 lines (143 loc) · 4.68 KB
/
main.go
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
172
173
174
175
176
177
178
179
180
181
package main
import (
"crypto-selling-algorithm/feargreedindex"
"crypto-selling-algorithm/pricehistory"
"fmt"
"time"
)
const timeLayout = "2006-01-02"
type SellingCriteria struct {
SellingPercentage int
ProfitFactor float64
FearAndGreedSellingThreshold int
ChangePercentage float64
ChangePercentageCount int
}
type CoinInfo struct {
CoinSymbol string
AmountBought float64
HighestBuyPrice float64
}
type RelativePrice struct {
Date time.Time
EthPrice float64
CoinPrice float64
RelativeToEthPrice float64
ChangePercentage float64
}
func main() {
sc := &SellingCriteria{
SellingPercentage: 25,
ProfitFactor: 2.0, // due to taxes
FearAndGreedSellingThreshold: 45, // chosen by science
ChangePercentage: 5.0, // chosen by science
ChangePercentageCount: 3, // chosen by science
}
coinInfos := []*CoinInfo{
{ // 100 USD invested @ 25 USD per coin -> 4 coins bought
CoinSymbol: "SOL",
HighestBuyPrice: 4,
AmountBought: 25.0,
},
{ // 100 USD invested @ 0.40 USD per coin -> 250 coins bought
CoinSymbol: "ADA",
HighestBuyPrice: 0.4,
AmountBought: 250.0,
},
}
fgiData := readFearAndGreed()
ethPrices := readHistory("ETH")
for _, ci := range coinInfos {
coinPrices := readHistory(ci.CoinSymbol)
relativePrices := calcRelativePrices(ethPrices, coinPrices)
calcChangePercentages(relativePrices)
calcSellingOpportunities(fgiData, relativePrices, sc, ci)
}
}
func readFearAndGreed() []*feargreedindex.FearAndGreedDatapoint {
fgi, err := feargreedindex.New().FromFile("data/fgi.csv")
if err != nil {
panic(err)
}
return fgi
}
func readHistory(symbol string) []*pricehistory.PriceHistoryEntry {
phr := pricehistory.NewReader()
coinPrices, err := phr.FromFile(fmt.Sprintf("data/%s-usd-max.csv", symbol))
if err != nil {
panic(err)
}
return coinPrices
}
func calcRelativePrices(ethPrices, coinPrices []*pricehistory.PriceHistoryEntry) []*RelativePrice {
if len(ethPrices) != len(coinPrices) {
panic("pricehistory slices must have the same length")
}
prices := make([]*RelativePrice, 0, len(ethPrices))
for idx, ethEntry := range ethPrices {
ethPrice := ethEntry.Price
coinPrice := coinPrices[idx].Price
prices = append(prices, &RelativePrice{
Date: ethEntry.Date,
EthPrice: ethPrice,
CoinPrice: coinPrice,
RelativeToEthPrice: coinPrice / ethPrice,
})
}
return prices
}
func calcChangePercentages(relativePrices []*RelativePrice) {
for idx, p := range relativePrices {
if idx == 0 {
p.ChangePercentage = 0.0
continue
}
// ((newvalue / oldvalue) - 1) * 100
// examples:
// 200 / 100 = 2
// 2 - 1 = 1
// 1 * 100 = 100 %
// 100 / 200 = 0,5
// 0,5 - 1 = -0,5
// -0,5 * 100 = -50 %
prevVal := relativePrices[idx-1]
p.ChangePercentage = ((p.RelativeToEthPrice / prevVal.RelativeToEthPrice) - 1) * 100.0
}
}
func calcSellingOpportunities(fgiData []*feargreedindex.FearAndGreedDatapoint, relativePrices []*RelativePrice, sellingCriteria *SellingCriteria, coinInfo *CoinInfo) {
if len(fgiData) != len(relativePrices) {
panic("fearAndGreed slice must have the same length as relaivePrices slice")
}
fmt.Printf("%s - SELLING OPPORTUNITIES\n", coinInfo.CoinSymbol)
coinAmount := coinInfo.AmountBought
changesAboveThreshold := 0
totalGains := 0.0
for i := 0; i < len(fgiData); i++ {
fgiDatapoint := fgiData[i]
relativePrice := relativePrices[i]
// price of coin must be at least HighestBuyingPrice * ProfitFactor defined in SellingCriteria
if relativePrice.CoinPrice < coinInfo.HighestBuyPrice*sellingCriteria.ProfitFactor {
continue
}
// fear and greed index must be at least FearAndGreedSellingThreshold defined in SellingCriteria
if fgiDatapoint.Value < sellingCriteria.FearAndGreedSellingThreshold {
continue
}
// price change percentage must be at least ChangePercentage defined in SellingCriteria
if relativePrice.ChangePercentage >= sellingCriteria.ChangePercentage {
changesAboveThreshold++
} else {
changesAboveThreshold = 0
}
// positive changes must be at least the same as ChangePercentageCount defined in SellingCriteria
if changesAboveThreshold >= sellingCriteria.ChangePercentageCount {
sellAmount := coinAmount * float64(sellingCriteria.SellingPercentage) / 100.0
coinAmount = coinAmount - sellAmount
totalGains = totalGains + float64(sellAmount)*relativePrice.CoinPrice
fmt.Printf("\t%s - sell: %.1f coins - coins left: %.1f\n", relativePrice.Date.Format(timeLayout), sellAmount, coinAmount)
}
}
fmt.Printf("TOTAL GAINS: %f\n", totalGains)
fmt.Println("")
fmt.Println("")
}