-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontract.go
331 lines (300 loc) · 8.65 KB
/
contract.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package ibapi
import (
"fmt"
)
// LegOpenClose .
type LegOpenClose int64
const (
SAME_POS LegOpenClose = 0
OPEN_POS LegOpenClose = 1
CLOSE_POS LegOpenClose = 2
UNKNOWN_POS LegOpenClose = 3
)
// ComboLeg .
type ComboLeg struct {
ConID int64
Ratio int64
Action string // BUY/SELL/SSHORT
Exchange string
OpenClose int64
// for stock legs when doing short sale
ShortSaleSlot int64 // 1 = clearing broker, 2 = third party
DesignatedLocation string
ExemptCode int64
}
// NewComboLeg creates a default ComboLeg.
func NewComboLeg() ComboLeg {
cl := ComboLeg{}
cl.ExemptCode = -1
return cl
}
func (c ComboLeg) String() string {
return fmt.Sprintf("%d, %d, %s, %s, %d, %d, %s, %d",
c.ConID, c.Ratio, c.Action, c.Exchange, c.OpenClose, c.ShortSaleSlot, c.DesignatedLocation, c.ExemptCode)
}
// DeltaNeutralContract .
type DeltaNeutralContract struct {
ConID int64
Delta float64
Price float64
}
func NewDeltaNeutralContract() DeltaNeutralContract {
return DeltaNeutralContract{}
}
func (c DeltaNeutralContract) String() string {
return fmt.Sprintf("%d, %f, %f", c.ConID, c.Delta, c.Price)
}
// Contract describes an instrument's definition.
type Contract struct {
ConID int64
Symbol string
SecType string
LastTradeDateOrContractMonth string
LastTradeDate string
Strike float64 // float64 !!
Right string
Multiplier string
Exchange string
PrimaryExchange string // pick an actual (ie non-aggregate) exchange that the contract trades on. DO NOT SET TO SMART.
Currency string
LocalSymbol string
TradingClass string
IncludeExpired bool
SecIDType string // CUSIP;SEDOL;ISIN;RIC
SecID string
Description string
IssuerID string
// combo legs
ComboLegsDescrip string // received in open order 14 and up for all combos
ComboLegs []ComboLeg
// delta neutral contract
DeltaNeutralContract *DeltaNeutralContract
}
func NewContract() *Contract {
return &Contract{}
}
func (c *Contract) Equal(other *Contract) bool {
if c.ConID != 0 && other.ConID != 0 {
return c.ConID == other.ConID
}
if c.SecIDType != "" && other.SecIDType != "" && c.SecIDType == other.SecIDType {
return c.SecID == other.SecID
}
return c.Symbol == other.Symbol &&
c.SecType == other.SecType &&
c.Exchange == other.Exchange &&
c.Currency == other.Currency &&
c.LastTradeDate == other.LastTradeDate &&
c.Strike == other.Strike &&
c.Right == other.Right
}
func (c Contract) String() string {
s := fmt.Sprintf("%d, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %t, %s, %s, %s, %s",
c.ConID,
c.Symbol,
c.SecType,
c.LastTradeDateOrContractMonth,
c.LastTradeDate,
FloatMaxString(c.Strike),
c.Right,
c.Multiplier,
c.Exchange,
c.PrimaryExchange,
c.Currency,
c.LocalSymbol,
c.TradingClass,
c.IncludeExpired,
c.SecIDType,
c.SecID,
c.Description,
c.IssuerID,
)
if len(c.ComboLegs) > 1 {
s += ", combo:" + c.ComboLegsDescrip
for _, leg := range c.ComboLegs {
s += fmt.Sprintf("; %s", leg)
}
}
if c.DeltaNeutralContract != nil {
s += fmt.Sprintf("; %s", c.DeltaNeutralContract)
}
return s
}
// ContractDetails .
type ContractDetails struct {
Contract Contract
MarketName string
MinTick float64
OrderTypes string
ValidExchanges string
PriceMagnifier int64
UnderConID int64
LongName string
ContractMonth string
Industry string
Category string
Subcategory string
TimeZoneID string
TradingHours string
LiquidHours string
EVRule string
EVMultiplier int64
AggGroup int64
UnderSymbol string
UnderSecType string
MarketRuleIDs string
RealExpirationDate string
LastTradeTime string
StockType string
MinSize Decimal
SizeIncrement Decimal
SuggestedSizeIncrement Decimal
SecIDList []TagValue
// BOND values
Cusip string
Ratings string
DescAppend string
BondType string
CouponType string
Callable bool
Putable bool
Coupon float64
Convertible bool
Maturity string
IssueDate string
NextOptionDate string
NextOptionType string
NextOptionPartial bool
Notes string
// FUND values
FundName string
FundFamily string
FundType string
FundFrontLoad string
FundBackLoad string
FundBackLoadTimeInterval string
FundManagementFee string
FundClosed bool
FundClosedForNewInvestors bool
FundClosedForNewMoney bool
FundNotifyAmount string
FundMinimumInitialPurchase string
FundSubsequentMinimumPurchase string
FundBlueSkyStates string
FundBlueSkyTerritories string
FundDistributionPolicyIndicator FundDistributionPolicyIndicator
FundAssetType FundAssetType
IneligibilityReasonList []IneligibilityReason
}
func NewContractDetails() *ContractDetails {
cd := &ContractDetails{}
cd.MinSize = UNSET_DECIMAL
cd.SizeIncrement = UNSET_DECIMAL
cd.SuggestedSizeIncrement = UNSET_DECIMAL
return cd
}
func (c ContractDetails) String() string {
return fmt.Sprintf("%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %t, %t, %f, %t, %s, %s, %s, %s, %t, %s, %s, %s, %s",
c.Contract,
c.MarketName,
FloatMaxString(c.MinTick),
c.OrderTypes,
c.ValidExchanges,
IntMaxString(c.PriceMagnifier),
IntMaxString(c.UnderConID),
c.LongName,
c.ContractMonth,
c.Industry,
c.Category,
c.Subcategory,
c.TimeZoneID,
c.TradingHours,
c.LiquidHours,
c.EVRule,
IntMaxString(c.EVMultiplier),
c.UnderSymbol,
c.UnderSecType,
c.MarketRuleIDs,
IntMaxString(c.AggGroup),
c.SecIDList,
c.RealExpirationDate,
c.StockType,
// Bond
c.Cusip,
c.Ratings,
c.DescAppend,
c.BondType,
c.CouponType,
c.Callable,
c.Putable,
c.Coupon,
c.Convertible,
c.Maturity,
c.IssueDate,
c.NextOptionDate,
c.NextOptionType,
c.NextOptionPartial,
c.Notes,
DecimalMaxString(c.MinSize),
DecimalMaxString(c.SizeIncrement),
DecimalMaxString(c.SuggestedSizeIncrement),
)
}
// ContractDescription includes contract and DerivativeSecTypes.
type ContractDescription struct {
Contract Contract
DerivativeSecTypes []string
}
func NewContractDescription() ContractDescription {
return ContractDescription{}
}
type FundAssetType string
const (
FundAssetTypeNone FundAssetType = ""
FundAssetTypeOthers FundAssetType = "000"
FundAssetTypeMoneyMarket FundAssetType = "001"
FundAssetTypeFixedIncome FundAssetType = "002"
FundAssetTypeMultiAsset FundAssetType = "003"
FundAssetTypeEquity FundAssetType = "004"
FundAssetTypeSector FundAssetType = "005"
FundAssetTypeGuaranteed FundAssetType = "006"
FundAssetTypeAlternative FundAssetType = "007"
)
func getFundAssetType(fat string) FundAssetType {
switch fat {
case string(FundAssetTypeOthers):
return FundAssetTypeOthers
case string(FundAssetTypeMoneyMarket):
return FundAssetTypeMoneyMarket
case string(FundAssetTypeFixedIncome):
return FundAssetTypeFixedIncome
case string(FundAssetTypeMultiAsset):
return FundAssetTypeMultiAsset
case string(FundAssetTypeEquity):
return FundAssetTypeEquity
case string(FundAssetTypeSector):
return FundAssetTypeSector
case string(FundAssetTypeGuaranteed):
return FundAssetTypeGuaranteed
case string(FundAssetTypeAlternative):
return FundAssetTypeAlternative
default:
return FundAssetTypeNone
}
}
type FundDistributionPolicyIndicator string
const (
FundDistributionPolicyIndicatorNone FundDistributionPolicyIndicator = ""
FundDistributionPolicyIndicatorAccumulationFund FundDistributionPolicyIndicator = "N"
FundDistributionPolicyIndicatorIncomeFund FundDistributionPolicyIndicator = "Y"
)
func getFundDistributionPolicyIndicator(fat string) FundDistributionPolicyIndicator {
switch fat {
case string(FundDistributionPolicyIndicatorAccumulationFund):
return FundDistributionPolicyIndicatorAccumulationFund
case string(FundDistributionPolicyIndicatorIncomeFund):
return FundDistributionPolicyIndicatorIncomeFund
default:
return FundDistributionPolicyIndicatorNone
}
}