-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.go
108 lines (103 loc) · 2.7 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
/*
// TODO:
// 1. tests and DB
// 2. consider having exchange interface, signal provider interface
// 3. auto-backtesting and parameter optimization with report to notifier
// 4. funding rate arbitrage
*/
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
util "github.com/cashmen/crypto-flash/pkg/util"
"time"
character "github.com/cashmen/crypto-flash/pkg/character"
exchange "github.com/cashmen/crypto-flash/pkg/exchange"
"sync"
)
const version = "3.4.2-beta"
const update = "1. Add funding rate arbitrage provider for simulation test.\n" +
"2. Funding pool logic test.\n" +
"3. Funding rate arbitrage profit calculation includes trading fee and hedge profit (or cost)."
const tag = "Crypto Flash"
type bot struct {
Owner string
Key string
Secret string
SubAccount string
Telegram_Id int64
Strategy string
}
type lineConfig struct {
Channel_Secret string
Channel_Access_Token string
}
type config struct {
Mode string
Notify bool
Bots []bot
Line lineConfig
Telegram string
}
func loadConfig(fileName string) config {
var c config
bytes, err := ioutil.ReadFile(fileName)
if err != nil {
util.Error(tag, err.Error())
}
json.Unmarshal(bytes, &c)
return c
}
func main() {
var wg sync.WaitGroup
config := loadConfig("config.json")
fmt.Printf("Crypto Flash v%s initialized. Update: \n%s\n", version, update)
var n *character.Notifier
if config.Notify && config.Mode != "backtest" {
n = character.NewNotifier(config.Line.Channel_Secret,
config.Line.Channel_Secret, config.Telegram)
for _, bot := range config.Bots {
n.AddUser(bot.Owner, bot.Telegram_Id)
}
wg.Add(1)
go n.Listen()
n.Broadcast(tag,
fmt.Sprintf("Crypto Flash v%s initialized. Update: \n%s",
version, update))
} else {
n = nil
}
ftx := exchange.NewFTX("", "", "")
rs := character.NewResTrend(ftx, n)
fra := character.NewFRArb(ftx, n)
if config.Mode == "trade" {
for _, bot := range config.Bots {
if bot.Key == "" || bot.Secret == "" {
continue
}
ftx := exchange.NewFTX(bot.Key, bot.Secret, bot.SubAccount)
trader := character.NewTrader(bot.Owner, ftx, n)
signalChan := make(chan *util.Signal)
rs.SubSignal(signalChan)
wg.Add(1)
go trader.Start(signalChan)
}
wg.Add(2)
go rs.Start()
go fra.Start()
} else if config.Mode == "notify" {
wg.Add(2)
go rs.Start()
go fra.Start()
} else if config.Mode == "backtest" {
//endTime, _ := time.Parse(time.RFC3339, "2019-12-01T05:00:00+00:00")
endTime := time.Now()
d := util.Duration{ Day: -30 }
startTime := endTime.Add(d.GetTimeDuration())
roi := rs.Backtest(startTime.Unix(), endTime.Unix())
annual := util.CalcAnnualFromROI(roi, -d.GetTimeDuration().Seconds())
fmt.Printf("Annual: %.2f%%", annual * 100)
}
wg.Wait()
}