market.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package sentio
  2. import (
  3. "errors"
  4. "fmt"
  5. "strings"
  6. "time"
  7. )
  8. var (
  9. ErrMarketClosed = errors.New("market is closed")
  10. ErrTooSmallOrder = errors.New("too small order size")
  11. )
  12. type Market interface {
  13. Clock() Clock
  14. IsOpen() bool
  15. Cooldown() uint8
  16. Connect(done chan struct{}) (chan MarketConnection, error)
  17. Subscribe(symbols ...string) error
  18. Account() (MarketAccount, error)
  19. CreateOrder(symbol string, quantity uint, sl float64) (Order, error)
  20. UpdateOrder(orderID string, sl float64) error
  21. CloseOrder(orderID string) (Order, error)
  22. Order(orderID string, nested bool) (Order, error)
  23. Orders(criteria OrderListCriteria) ([]Order, error)
  24. Portfolio() (Portfolio, error)
  25. PortfolioHistory() ([]PortfolioRecord, error)
  26. Quotes(symbols ...string) (map[string]Quote, error)
  27. HistoricalBars(symbols []string, interval time.Duration, from *time.Time) (map[string][]Bar, error)
  28. }
  29. type OrderListCriteria struct {
  30. Status string
  31. Limit uint
  32. Symbols []string
  33. After *time.Time
  34. Until *time.Time
  35. Nested bool
  36. Side *string
  37. }
  38. func (criteria OrderListCriteria) Values() map[string]string {
  39. var values = make(map[string]string)
  40. if criteria.Limit < 1 {
  41. criteria.Limit = 500
  42. }
  43. if criteria.Symbols != nil && len(criteria.Symbols) > 0 {
  44. values["symbols"] = strings.Join(criteria.Symbols, ",")
  45. }
  46. if criteria.After == nil {
  47. t := time.Now().Add(-time.Hour * 24).Round(time.Hour * 24)
  48. criteria.After = &t
  49. }
  50. if criteria.Until != nil && !criteria.Until.IsZero() {
  51. values["until"] = criteria.Until.In(time.UTC).Format(time.RFC3339)
  52. }
  53. if criteria.Side != nil {
  54. values["side"] = *criteria.Side
  55. }
  56. values["status"] = "all"
  57. values["nested"] = "true"
  58. values["limit"] = fmt.Sprintf("%d", criteria.Limit)
  59. values["after"] = criteria.After.In(time.UTC).Format(time.RFC3339)
  60. values["direction"] = "asc"
  61. return values
  62. }