Skip to content

Instantly share code, notes, and snippets.

@joelreymont
Created September 7, 2012 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelreymont/37dada7af636e2db000d to your computer and use it in GitHub Desktop.
Save joelreymont/37dada7af636e2db000d to your computer and use it in GitHub Desktop.
package trade
type Instrument struct {
id int64
symbol string
localSymbol string
exchange string
currency string
bid float64
ask float64
last float64
engine *Engine
data chan Reply
ch chan func()
exit chan bool
hook func(Reply)
}
func NewInstrument(engine *Engine, symbol string, exchange string, currency string) *Instrument {
self := &Instrument{
id: 0,
symbol: symbol,
localSymbol: "",
exchange: exchange,
currency: currency,
engine: engine,
data: make(chan Reply),
ch: make(chan func()),
exit: make(chan bool),
}
go func() {
for {
select {
case <-self.exit:
return
case req := <-engine.ch:
req()
case v := <-self.data:
self.process(v)
}
}
}()
return self
}
func (self *Instrument) Symbol() string {
ch := make(chan string)
self.ch <- func() { ch <- self.symbol }
return <-ch
}
/*
func (self *Instrument) Exchange() string { return self.exchange }
func (self *Instrument) Currency() string { return self.currency }
func (self *Instrument) Id() int64 { return self.id }
func (self *Instrument) SetId(id int64) { self.id = id }
func (self *Instrument) LocalSymbol() string { return self.localSymbol }
func (self *Instrument) Bid() float64 { return self.bid }
func (self *Instrument) Ask() float64 { return self.ask }
func (self *Instrument) Last() float64 { return self.last }
func (self *Instrument) SetHook(hook func(Reply)) { self.hook = hook }
*/
func (self *Instrument) process(v Reply) {
switch v.(type) {
case *TickPrice:
v := v.(*TickPrice)
// we want either last or bid and ask
switch v.Type {
case TickLast:
self.last = v.Price
case TickBid:
self.bid = v.Price
case TickAsk:
self.ask = v.Price
}
}
if self.hook != nil {
self.hook(v)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment