Skip to content

Commit

Permalink
Merge pull request #5 from creack/cleanup
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
creack committed Nov 12, 2014
2 parents 8dd2b08 + 8a3c060 commit cb27d03
Show file tree
Hide file tree
Showing 42 changed files with 1,700 additions and 2,241 deletions.
20 changes: 11 additions & 9 deletions advisor_account_manager.go
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
)

// AccountSummary Enum
const (
AccountSummaryTagAccountType = "AccountType"
AccountSummaryTagNetLiquidation = "NetLiquidation"
Expand Down Expand Up @@ -36,7 +37,7 @@ const (
AccountSummaryTagLeverage = "Leverage"
)

var allTags [29]string = [29]string{
var allTags = [...]string{
AccountSummaryTagAccountType,
AccountSummaryTagNetLiquidation,
AccountSummaryTagTotalCashValue,
Expand Down Expand Up @@ -78,24 +79,25 @@ type AdvisorAccountManager struct {
portfolio map[PositionKey]Position
}

// NewAdvisorAccountManager creates a new AdvisorAccountManager
func NewAdvisorAccountManager(e *Engine) (*AdvisorAccountManager, error) {
am, err := NewAbstractManager(e)
if err != nil {
return nil, err
}

a := &AdvisorAccountManager{AbstractManager: *am,
id: UnmatchedReplyId,
values: make(map[AccountSummaryKey]AccountSummary),
portfolio: make(map[PositionKey]Position),
id: UnmatchedReplyID,
values: map[AccountSummaryKey]AccountSummary{},
portfolio: map[PositionKey]Position{},
}

go a.startMainLoop(a.preLoop, a.receive, a.preDestroy)
return a, nil
}

func (a *AdvisorAccountManager) preLoop() error {
a.id = a.eng.NextRequestId()
a.id = a.eng.NextRequestID()
a.eng.Subscribe(a.rc, a.id)

var tags bytes.Buffer
Expand All @@ -105,14 +107,14 @@ func (a *AdvisorAccountManager) preLoop() error {
}

reqAs := &RequestAccountSummary{}
reqAs.SetId(a.id)
reqAs.SetID(a.id)
reqAs.Group = "All"
reqAs.Tags = tags.String()
if err := a.eng.Send(reqAs); err != nil {
return err
}

a.eng.Subscribe(a.rc, UnmatchedReplyId)
a.eng.Subscribe(a.rc, UnmatchedReplyID)
reqPos := &RequestPositions{}
return a.eng.Send(reqPos)
}
Expand Down Expand Up @@ -146,10 +148,10 @@ func (a *AdvisorAccountManager) receive(r Reply) (UpdateStatus, error) {

func (a *AdvisorAccountManager) preDestroy() {
a.eng.Unsubscribe(a.rc, a.id)
a.eng.Unsubscribe(a.rc, UnmatchedReplyId)
a.eng.Unsubscribe(a.rc, UnmatchedReplyID)

canAs := &CancelAccountSummary{}
canAs.SetId(a.id)
canAs.SetID(a.id)
a.eng.Send(canAs)

canPos := &CancelPositions{}
Expand Down
7 changes: 3 additions & 4 deletions advisor_account_manager_test.go
Expand Up @@ -17,17 +17,16 @@ func TestAdvisorAccountManager(t *testing.T) {

defer aam.Close()

var m Manager = aam
SinkManagerTest(t, &m, 15*time.Second, 1)
SinkManagerTest(t, aam, 15*time.Second, 1)

if len(aam.Values()) < 3 {
t.Fatal("Insufficient account values %v", aam.Values())
t.Fatalf("Insufficient account values %v", aam.Values())
}

// demo accounts have no portfolio, so this just tests the accessor
aam.Portfolio()

if b, ok := <-aam.Refresh(); ok {
t.Fatal("Expected the refresh channel to be closed, but got %v", b)
t.Fatalf("Expected the refresh channel to be closed, but got %t", b)
}
}
11 changes: 7 additions & 4 deletions chain_manager.go
Expand Up @@ -5,13 +5,15 @@ import (
"time"
)

// ChainManager .
type ChainManager struct {
AbstractManager
id int64
c Contract
chains OptionChains
}

// NewChainManager .
func NewChainManager(e *Engine, c Contract) (*ChainManager, error) {
am, err := NewAbstractManager(e)
if err != nil {
Expand All @@ -21,19 +23,19 @@ func NewChainManager(e *Engine, c Contract) (*ChainManager, error) {
m := &ChainManager{
AbstractManager: *am,
c: c,
chains: make(OptionChains),
chains: OptionChains{},
}

go m.startMainLoop(m.preLoop, m.receive, m.preDestroy)
return m, nil
}

func (c *ChainManager) preLoop() error {
c.id = c.eng.NextRequestId()
c.id = c.eng.NextRequestID()
req := &RequestContractData{Contract: c.c}
req.Contract.SecurityType = "OPT"
req.Contract.LocalSymbol = ""
req.SetId(c.id)
req.SetID(c.id)
c.eng.Subscribe(c.rc, c.id)
return c.eng.Send(req)
}
Expand All @@ -59,7 +61,7 @@ func (c *ChainManager) receive(r Reply) (UpdateStatus, error) {
if _, ok := c.chains[expiry]; !ok {
c.chains[expiry] = &OptionChain{
Expiry: expiry,
Strikes: make(map[float64]*OptionStrike),
Strikes: map[float64]*OptionStrike{},
}
}
c.chains[expiry].update(r)
Expand All @@ -70,6 +72,7 @@ func (c *ChainManager) receive(r Reply) (UpdateStatus, error) {
return UpdateFalse, fmt.Errorf("Unexpected type %v", r)
}

// Chains .
func (c *ChainManager) Chains() map[time.Time]*OptionChain {
c.rwm.RLock()
defer c.rwm.RUnlock()
Expand Down
3 changes: 1 addition & 2 deletions chain_manager_test.go
Expand Up @@ -24,8 +24,7 @@ func TestChainManager(t *testing.T) {

defer m.Close()

var mgr Manager = m
SinkManagerTest(t, &mgr, 15*time.Second, 1)
SinkManagerTest(t, m, 15*time.Second, 1)

if len(m.Chains()) < 2 {
t.Fatalf("expected a chain to be returned (length was %d)", len(m.Chains()))
Expand Down
14 changes: 9 additions & 5 deletions combo_leg.go
Expand Up @@ -2,21 +2,25 @@ package ib

// This file ports IB API ComboLeg.java. Please preserve declaration order.

// LegOpenClose .
type LegOpenClose int64

// LegShortSaleSlot .
type LegShortSaleSlot int64

// Enum .
const (
kPosSame LegOpenClose = 0
kPosOpen = 1
kPosClose = 2
kPosUnknown = 3
posSame LegOpenClose = 0
posOpen = 1
posClose = 2
posUnknown = 3
LegShortSaleSlotClearingBroker LegShortSaleSlot = 1
LegShortSaleSlotThirdParty = 2
)

// ComboLeg .
type ComboLeg struct {
ContractId int64 // m_conId
ContractID int64 // m_conId
Ratio int64
Action string
Exchange string
Expand Down
33 changes: 32 additions & 1 deletion commission_report.go
@@ -1,12 +1,43 @@
package ib

import "bufio"

// This file ports IB API CommissionReport.java. Please preserve declaration order.

// CommissionReport .
type CommissionReport struct {
ExecutionId string
ExecutionID string
Commission float64
Currency string
RealizedPNL float64
Yield float64
YieldRedemptionDate int64
}

func (c *CommissionReport) code() IncomingMessageID {
return mCommissionReport
}

func (c *CommissionReport) read(b *bufio.Reader) error {
var err error

if c.ExecutionID, err = readString(b); err != nil {
return err
}
if c.Commission, err = readFloat(b); err != nil {
return err
}
if c.Currency, err = readString(b); err != nil {
return err
}
if c.RealizedPNL, err = readFloat(b); err != nil {
return err
}
if c.Yield, err = readFloat(b); err != nil {
return err
}
if c.YieldRedemptionDate, err = readInt(b); err != nil {
return err
}
return nil
}
7 changes: 4 additions & 3 deletions contract.go
Expand Up @@ -2,8 +2,9 @@ package ib

// This file ports IB API Contract.java. Please preserve declaration order.

// Contract .
type Contract struct {
ContractId int64
ContractID int64
Symbol string
SecurityType string
Expiry string
Expand All @@ -16,8 +17,8 @@ type Contract struct {
TradingClass string
PrimaryExchange string
IncludeExpired bool
SecIdType string
SecId string
SecIDType string
SecID string
ComboLegsDescription string
ComboLegs []ComboLeg
UnderComp *UnderComp
Expand Down
10 changes: 6 additions & 4 deletions contract_details.go
Expand Up @@ -3,27 +3,29 @@ package ib
// This file ports IB API ContractDetails.java. Please preserve declaration order.
// We have separated the Java code into the two natural structs they should be.

// ContractDetails .
type ContractDetails struct {
Summary Contract
MarketName string
MinTick float64
PriceMagnifier int64
OrderTypes string
ValidExchanges string
UnderContractId int64
UnderContractID int64
LongName string
ContractMonth string
Industry string
Category string
Subcategory string
TimezoneId string
TimezoneID string
TradingHours string
LiquidHours string
EVRule string
EVMultiplier float64
SecIdList []TagValue
SecIDList []TagValue
}

// BondContractDetails .
type BondContractDetails struct {
Summary Contract
MarketName string
Expand All @@ -49,5 +51,5 @@ type BondContractDetails struct {
Notes string
EVRule string
EVMultiplier float64
SecIdList []TagValue
SecIDList []TagValue
}
7 changes: 4 additions & 3 deletions current_time_manager.go
Expand Up @@ -2,20 +2,21 @@ package ib

import "time"

// Provides a Manager to access the IB current time on the server side
// CurrentTimeManager provides a Manager to access the IB current time on the server side
type CurrentTimeManager struct {
AbstractManager
id int64
t time.Time
}

// NewCurrentTimeManager .
func NewCurrentTimeManager(e *Engine) (*CurrentTimeManager, error) {
am, err := NewAbstractManager(e)
if err != nil {
return nil, err
}

m := &CurrentTimeManager{AbstractManager: *am, id: UnmatchedReplyId}
m := &CurrentTimeManager{AbstractManager: *am, id: UnmatchedReplyID}

go m.startMainLoop(m.preLoop, m.receive, m.preDestroy)
return m, nil
Expand Down Expand Up @@ -48,7 +49,7 @@ func (m *CurrentTimeManager) preDestroy() {
m.eng.Unsubscribe(m.rc, m.id)
}

// Returns the current server time.
// Time returns the current server time.
func (m *CurrentTimeManager) Time() time.Time {
m.rwm.RLock()
defer m.rwm.RUnlock()
Expand Down
7 changes: 3 additions & 4 deletions current_time_manager_test.go
Expand Up @@ -17,8 +17,7 @@ func TestCurrentTimeManager(t *testing.T) {

defer ctm.Close()

var m Manager = ctm
SinkManagerTest(t, &m, 15*time.Second, 1)
SinkManagerTest(t, ctm, 15*time.Second, 1)

ctmTime := ctm.Time()
t.Logf("got time: %s\n", ctmTime.String())
Expand All @@ -28,11 +27,11 @@ func TestCurrentTimeManager(t *testing.T) {
}

if ctmTime.Before(engine.serverTime) {
t.Fatal("Expected time to be later than serverTime of %s, got: %s", engine.serverTime.String(), ctmTime.String())
t.Fatalf("Expected time to be later than serverTime of %s, got: %s", engine.serverTime, ctmTime)
}

if b, ok := <-ctm.Refresh(); ok {
t.Fatal("Expected the refresh channel to be closed, but got %v", b)
t.Fatalf("Expected the refresh channel to be closed, but got %t", b)
}

}

0 comments on commit cb27d03

Please sign in to comment.