-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
107 lines (90 loc) · 2.48 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
package main
import (
"fmt"
"os"
"time"
analysis "github.com/BrianMwangi21/anti-charts.git/pkg/analysis"
cli "github.com/BrianMwangi21/anti-charts.git/pkg/cli"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/log"
"github.com/joho/godotenv"
)
func init() {
err := godotenv.Load()
if err != nil {
log.Error("Error loading .env file")
os.Exit(1)
}
analysis.BINANCE_API_KEY = os.Getenv("BINANCE_API_KEY")
analysis.BINANCE_SECRET_KEY = os.Getenv("BINANCE_SECRET_KEY")
analysis.ALPACA_API_KEY = os.Getenv("ALPACA_API_KEY")
analysis.ALPACA_SECRET_KEY = os.Getenv("ALPACA_SECRET_KEY")
analysis.ALPACA_BASE_URL = os.Getenv("ALPACA_BASE_URL")
perform_trades := os.Getenv("PERFORM_TRADES")
special_cases := os.Getenv("SPECIAL_CASES")
if analysis.BINANCE_API_KEY == "" || analysis.BINANCE_SECRET_KEY == "" {
log.Error("Error getting Binance keys")
os.Exit(1)
}
if analysis.ALPACA_API_KEY == "" || analysis.ALPACA_SECRET_KEY == "" || analysis.ALPACA_BASE_URL == "" {
log.Error("Error getting Alpaca keys")
os.Exit(1)
}
if special_cases == "True" {
analysis.SPECIAL_CASES = true
} else if special_cases == "False" {
analysis.SPECIAL_CASES = false
} else {
log.Error("Error getting Special Cases key")
os.Exit(1)
}
if perform_trades == "True" {
analysis.PERFORM_TRADES = true
} else if perform_trades == "False" {
analysis.PERFORM_TRADES = false
} else {
log.Error("Error getting Perform Trades key")
os.Exit(1)
}
}
func main() {
p := tea.NewProgram(cli.InitModel())
if model, err := p.Run(); err != nil {
log.Error("Error starting the program", "err", err)
os.Exit(1)
} else {
initModel := model.(cli.Model)
if initModel.Submitted {
analysisRequest, err := analysis.ValidateInput(initModel.Values)
if err != nil {
log.Error("Error validating input", "err", err)
os.Exit(1)
}
// Start execution at multiples of 5 for symmetry
var currentTime time.Time
log.Info("Checking time...")
for {
currentTime = time.Now()
minutes := currentTime.Minute()
seconds := currentTime.Second()
if (minutes+1)%5 == 0 && seconds > 54 {
break
}
time.Sleep(1 * time.Second)
}
log.Info(fmt.Sprintf("Time started %v", currentTime.Format(time.RFC3339)))
if analysis.PERFORM_TRADES {
ticker := time.NewTicker(10 * time.Second)
go func() {
for {
select {
case <-ticker.C:
analysis.CheckMetrics()
}
}
}()
}
analysis.StartAnalysis(analysisRequest)
}
}
}