-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
202 lines (175 loc) · 5.35 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"bufio"
"fmt"
"os"
"strings"
"flag"
)
var lseetfTickers = map[string]bool{
"IB01": true,
"IBTA": true,
"VDTA": true,
"URNU": true,
}
func main() {
inputFilePath, outputFilePath := parseFlags()
err := convertWatchlist(inputFilePath, outputFilePath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
fmt.Printf("Conversion completed. Output saved to %s\n", outputFilePath)
}
func parseFlags() (string, string) {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s <input_file> <output_file>\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Convert TradingView watchlist to IBKR format\n")
fmt.Fprintf(os.Stderr, "\nArguments:\n")
fmt.Fprintf(os.Stderr, " input_file Path to the input TradingView watchlist file\n")
fmt.Fprintf(os.Stderr, " output_file Path to the output IBKR watchlist file\n")
fmt.Fprintf(os.Stderr, "\nFlags:\n")
flag.PrintDefaults()
}
flag.Parse()
if flag.NArg() != 2 {
flag.Usage()
os.Exit(1)
}
return flag.Arg(0), flag.Arg(1)
}
func convertWatchlist(inputFilePath, outputFilePath string) error {
inputFile, err := os.Open(inputFilePath)
if err != nil {
return fmt.Errorf("error opening input file: %v", err)
}
defer inputFile.Close()
outputFile, err := os.Create(outputFilePath)
if err != nil {
return fmt.Errorf("error creating output file: %v", err)
}
defer outputFile.Close()
outputFile.WriteString("CSVEXPORT\nCOLUMN,0\n")
scanner := bufio.NewScanner(inputFile)
for scanner.Scan() {
err := processLine(scanner.Text(), outputFile)
if err != nil {
return err
}
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("error reading input file: %v", err)
}
return nil
}
func processLine(line string, outputFile *os.File) error {
symbols := strings.Split(line, ",")
for _, symbol := range symbols {
if strings.HasPrefix(symbol, "###") {
section := strings.TrimPrefix(symbol, "###")
_, err := outputFile.WriteString(fmt.Sprintf("HED,%s\n", section))
if err != nil {
return fmt.Errorf("error writing section header: %v", err)
}
continue
}
err := processSymbol(symbol, outputFile)
if err != nil {
return err
}
}
return nil
}
func processSymbol(symbol string, outputFile *os.File) error {
parts := strings.Split(symbol, ":")
if len(parts) != 2 {
return nil // Skip invalid symbols
}
exchange := parts[0]
ticker := parts[1]
var err error
switch exchange {
case "FX", "FX_IDC":
err = handleCurrencyPair(outputFile, ticker)
case "TVC":
err = handleTVCSymbol(outputFile, ticker)
case "FRED":
_, err = outputFile.WriteString(fmt.Sprintf("DES,%s,IND,SMART,,,,,\n", ticker))
case "CBOE":
_, err = outputFile.WriteString(fmt.Sprintf("DES,%s,IND,CBOE,,,,,\n", ticker))
case "ECONOMICS":
// Skip economics indicators
default:
err = handleDefaultSymbol(outputFile, exchange, ticker)
}
if err != nil {
return fmt.Errorf("error processing symbol %s: %v", symbol, err)
}
return nil
}
func handleCurrencyPair(outputFile *os.File, ticker string) error {
currencies := strings.Split(ticker, "USD")
if len(currencies) != 2 {
return nil // Skip invalid currency pairs
}
base := currencies[0]
quote := currencies[1]
if base == "GBP" || base == "EUR" {
_, err := outputFile.WriteString(fmt.Sprintf("DES,%s,CASH,IDEALPRO,,,,,USD\n", base))
return err
}
if quote == "" {
quote = base
}
if quote == "CNY" {
quote = "HKD"
}
_, err := outputFile.WriteString(fmt.Sprintf("DES,USD,CASH,IDEALPRO,,,,,%s\n", quote))
return err
}
func handleTVCSymbol(outputFile *os.File, ticker string) error {
var output string
switch ticker {
case "DXY":
output = "DES,DX,IND,NYBOT,,,,,\n"
case "US10Y":
output = "DES,TNX,IND,CBOE,,,,,\n"
case "US05Y":
output = "DES,FVX,IND,CBOE,,,,,\n"
default:
if strings.HasPrefix(ticker, "US") && strings.HasSuffix(ticker, "Y") {
output = fmt.Sprintf("DES,%s,IND,SMART,,,,,\n", ticker)
} else {
return nil // Skip other TVC symbols
}
}
_, err := outputFile.WriteString(output)
return err
}
func handleDefaultSymbol(outputFile *os.File, exchange, ticker string) error {
if ticker == "BRK.B" {
ticker = "BRK B"
}
var output string
if isLSEETFTicker(ticker) {
output = fmt.Sprintf("DES,%s,STK,SMART/LSEETF,,,,,\n", ticker)
} else if isAmericanETForStock(exchange) {
output = fmt.Sprintf("DES,%s,STK,SMART/AMEX,,,,,\n", ticker)
} else {
output = fmt.Sprintf("DES,%s,STK,SMART,,,,,\n", ticker)
}
_, err := outputFile.WriteString(output)
return err
}
func isAmericanETForStock(exchange string) bool {
americanExchanges := []string{"NYSE", "NASDAQ", "AMEX"}
for _, e := range americanExchanges {
if e == exchange {
return true
}
}
return false
}
func isLSEETFTicker(ticker string) bool {
return lseetfTickers[ticker]
}