This repository has been archived by the owner on Jul 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
quant.go
executable file
·201 lines (168 loc) · 4.39 KB
/
quant.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
package quant
import (
"errors"
"fmt"
"quant/base"
"quant/base/bar"
"quant/base/strategy"
"quant/base/xbase"
_ "quant/canvas"
"quant/provider"
"reflect"
"time"
"github.com/milliyang/dice"
_ "quant/account"
_ "quant/base/indicator"
)
const (
debug = true
)
var (
DefaultQuant = Quant{instruments: map[string]int{}, AccountDate: time.Time{}}
ErrNoProject = errors.New("no project")
ErrNoProvider = errors.New("no provider")
)
func init() {
if debug {
fmt.Println("quant.quant.init")
}
}
type Quant struct {
Name string
Projects []*Project
instruments map[string]int
Provider provider.IProvider
AccountDate time.Time
lastBar *bar.Bar
}
func (this *Quant) addInstrument(symbol string) {
count, ok := this.instruments[symbol]
if ok {
this.instruments[symbol] = count + 1
} else {
this.instruments[symbol] = 1
}
}
func registerProject(pro *Project) {
if debug {
fmt.Println("quant.Project Add:", pro.Name)
}
DefaultQuant.Projects = append(DefaultQuant.Projects, pro)
}
func RegisterProvider(provider_ provider.IProvider) {
DefaultQuant.Provider = provider_
}
func Run() {
// check
if len(DefaultQuant.Projects) == 0 {
panic(ErrNoProject)
}
// setup
for _, oneProject := range DefaultQuant.Projects {
for _, ins := range oneProject.Instrucment {
DefaultQuant.addInstrument(ins)
for _, bs := range oneProject.basicStrategies {
t := reflect.TypeOf(bs)
v := reflect.New(t)
/*
hack to set Symbol of Strategy, but fail -_-!!
use Init(symbol) instead
*/
/*
vv := reflect.Indirect(v)
typeOfT := vv.Type()
for i := 0; i < vv.NumField(); i++ {
if typeOfT.Field(i).Name == "Symbol" {
vv.SetString(ins)
}
fmt.Printf("%d: %s %s = %v\n", i, typeOfT.Field(i).Name, vv.Type(), v.Interface())
}
*/
c := v.Interface().(strategy.IStrategy)
barseries := base.InitBarManagerWithSymbol(ins)
c.Init(ins, barseries, oneProject.Account)
oneProject.AllStrategy = append(oneProject.AllStrategy, c)
oneProject.MapStrategy[c.Key()] = c
}
}
}
// connect to bar provider
if DefaultQuant.Provider == nil {
panic(ErrNoProvider)
}
connectErr := DefaultQuant.Provider.Connect()
if connectErr != nil {
panic(connectErr)
}
symbols := []string{}
for key, _ := range DefaultQuant.instruments {
symbols = append(symbols, key)
}
requestErr := DefaultQuant.Provider.RequestInstrument(symbols)
if requestErr != nil {
panic(requestErr)
}
// request our stock bar data
if debug {
fmt.Println("request stock:", DefaultQuant.instruments)
}
datagramChan := make(chan *provider.Datagram, 100)
// start all strategy
for _, oneProject := range DefaultQuant.Projects {
for _, ss := range oneProject.AllStrategy {
ss.OnStrategyStart()
}
}
// receive datagram from other modules
go DefaultQuant.Provider.Receive(datagramChan)
for datagramPtr := range datagramChan {
DefaultQuant.handleOneBar(datagramPtr)
}
// start all strategy
for _, oneProject := range DefaultQuant.Projects {
for _, ss := range oneProject.AllStrategy {
ss.OnStrategyStop()
}
}
fmt.Println("Strategy Finish!!!")
// svgo.TestDoDrawing()
}
func (this *Quant) handleOneBar(dgram *provider.Datagram) {
if dgram.Event == provider.EventBarOpen {
// ToDo:
// not ready yet, haven't figure out how to implement it
for _, oneProject := range DefaultQuant.Projects {
for _, oneStrategy := range oneProject.AllStrategy {
return
// ToDo:
oneStrategy.OnBarOpen(bar.Bar{})
}
}
} else if dgram.Event == provider.EventBarSlice {
for _, oneProject := range DefaultQuant.Projects {
for _, oneStrategy := range oneProject.AllStrategy {
oneStrategy.OnBarSlice(1)
}
}
} else {
newBar := bar.NewBar(dgram.Time, dgram.Open, dgram.High, dgram.Low, dgram.Close, dgram.Volumn, dgram.Amount)
// Sebao Dicing Game
if dgram.DiceA > 0 {
newBar.Type = bar.Dice
newBar.Dice = dice.NewSebaoDiceRoll(dgram.DiceA, dgram.DiceB, dgram.DiceC)
}
base.StoreBarToManager(dgram.Symbol, *newBar)
// for each instrument , for each bar
for _, oneProject := range DefaultQuant.Projects {
if xbase.CasinoDicingGame {
allPendingOrder := base.GetAllPendingDicingGameOrder()
oneProject.Account.DiceHandleOrdersWithBar(allPendingOrder, newBar)
}
for _, oneStrategy := range oneProject.AllStrategy {
if oneStrategy.Match(dgram.Symbol) {
oneStrategy.OnBar(*newBar)
}
}
}
}
}