-
Notifications
You must be signed in to change notification settings - Fork 0
/
jump.go
163 lines (132 loc) · 4.98 KB
/
jump.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
package handlers
import (
"fmt"
"strings"
"time"
"github.com/shopspring/decimal"
"gopkg.in/telebot.v3"
"github.com/erwanlbp/trading-bot/pkg/config/configfile"
"github.com/erwanlbp/trading-bot/pkg/model"
"github.com/erwanlbp/trading-bot/pkg/repository"
"github.com/erwanlbp/trading-bot/pkg/telegram"
"github.com/erwanlbp/trading-bot/pkg/util"
)
func (p *Handlers) NextJump(c telebot.Context) error {
cc, _, err := p.Repository.GetCurrentCoin()
if err != nil {
return c.Send("Failed getting current coin: " + err.Error())
}
diffs, err := p.Repository.GetDiff(repository.FromCoin(cc.Coin), repository.OrderBy("diff desc"), repository.Limit(p.Conf.Telegram.Handlers.NbDiffDisplayed))
if err != nil {
return c.Send("Error while getting next jump info, please retry: " + err.Error())
}
if len(diffs) == 0 {
return c.Send("No diff found")
}
var ts string
msg := util.ToASCIITable(diffs, []string{"Pair", "Ratio diff"}, nil, func(diff model.Diff) []string {
ts = fmt.Sprintf("Jump at : %s\nNeeds gain of %s\n", diff.Timestamp.Format(time.DateTime), diff.NeededDiff.Mul(decimal.NewFromInt(100)).StringFixed(1))
return []string{diff.LogSymbol(), diff.Diff.Mul(decimal.NewFromInt(100)).StringFixed(1) + " %"}
})
parts := []string{ts, telegram.FormatForMD(msg)}
return c.Send(strings.Join(parts, "\n"))
}
func (p *Handlers) BestJump(c telebot.Context) error {
diffs, err := p.Repository.GetDiff(repository.OrderBy("diff desc"), repository.Limit(p.Conf.Telegram.Handlers.NbDiffDisplayed))
if err != nil {
return c.Send("Error while getting best jumps: " + err.Error())
}
if len(diffs) == 0 {
return c.Send("No diff found")
}
var ts string
msg := util.ToASCIITable(diffs, []string{"Pair", "Ratio diff"}, nil, func(diff model.Diff) []string {
ts = fmt.Sprintf("Best jump at : %s\nNeeds gain of %s\n", diff.Timestamp.Format(time.DateTime), diff.NeededDiff.Mul(decimal.NewFromInt(100)).StringFixed(1))
return []string{diff.LogSymbol(), diff.Diff.Mul(decimal.NewFromInt(100)).StringFixed(1) + " %"}
})
parts := []string{ts, telegram.FormatForMD(msg)}
return c.Send(strings.Join(parts, "\n"))
}
func (p *Handlers) LastTenJumps(c telebot.Context) error {
jumps, err := p.Repository.GetJumps(repository.OrderBy("timestamp desc"), repository.Limit(10))
if err != nil {
return c.Send("Error while getting last ten jump, please retry")
}
if len(jumps) < 1 {
return c.Send("No jump found in DB")
}
msg := util.ToASCIITable(jumps, []string{"Date", "Pair"}, nil, func(jump model.Jump) []string {
return []string{
jump.Timestamp.Format(time.DateOnly) + "\n" + jump.Timestamp.Format(time.TimeOnly),
util.LogSymbol(jump.FromCoin, jump.ToCoin),
}
})
return c.Send(telegram.FormatForMD(msg))
}
func (p *Handlers) EditJump(c telebot.Context) error {
var messageParts []string = []string{
"Copy and paste the command",
"Edit the config and send it to validate",
"Or ignore this message to do nothing",
}
jump := p.Conf.Jump
messageParts = append(messageParts, fmt.Sprintf(
"`/edit_jump when:%s decrease:%s after:%s min:%s`",
jump.WhenGain, jump.DecreaseBy, jump.After, jump.Min,
))
return c.Send(strings.Join(messageParts, "\n"), telebot.RemoveKeyboard, configurationMenu)
}
func (p *Handlers) ValidateJumpEdit(c telebot.Context) error {
var jumpConf configfile.Jump = util.Copy(p.Conf.Jump)
parts := c.Args()
for _, part := range parts {
splitted := strings.Split(part, ":")
if len(splitted) != 2 {
continue
}
switch arg := splitted[1]; splitted[0] {
case "when":
val, err := decimal.NewFromString(arg)
if err != nil {
return c.Send(fmt.Sprintf("couldn't parse 'when' (%s) argument: %s", arg, err.Error()))
}
jumpConf.WhenGain = val
case "decrease":
val, err := decimal.NewFromString(arg)
if err != nil {
return c.Send(fmt.Sprintf("couldn't parse 'decrease' (%s) argument: %s", arg, err.Error()))
}
jumpConf.DecreaseBy = val
case "after":
val, err := time.ParseDuration(arg)
if err != nil {
return c.Send(fmt.Sprintf("couldn't parse 'after' (%s) argument: %s", arg, err.Error()))
}
jumpConf.After = val
case "min":
val, err := decimal.NewFromString(arg)
if err != nil {
return c.Send(fmt.Sprintf("couldn't parse 'min' (%s) argument: %s", arg, err.Error()))
}
jumpConf.Min = val
default:
continue
}
}
configFile, err := configfile.ParseConfigFile()
if err != nil {
return c.Send(fmt.Sprintf("Failed to parse config.yaml: %s", err.Error()))
}
if jumpConf == configFile.Jump {
return c.Send("Nothing to change with config.yaml file content")
}
if err := configfile.CopyFileToBackup(); err != nil {
return c.Send("Failed to backup the config.yaml: " + err.Error())
}
newConf := util.Copy(*p.Conf)
newConf.Jump = jumpConf
if err := newConf.SaveToFile(); err != nil {
return c.Send("Failed to save conf to file: " + err.Error())
}
return c.Send("Saved jump config.\n⚠️*You'll need to reload the config file for it to be effective*⚠️\nNew conf is:\n"+PrepareConfContentForMessage(newConf), mainMenu)
}