qqq.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package main
  2. import (
  3. "git.beejay.kim/Gshopper/sentio"
  4. )
  5. var Strategy = _ib_qqq{
  6. clock: sentio.ClockUTC{},
  7. }
  8. type _ib_qqq struct {
  9. clock sentio.Clock
  10. }
  11. func (strategy _ib_qqq) Name() string {
  12. return "IBKR: QQQ"
  13. }
  14. func (strategy _ib_qqq) Model() string {
  15. return "qqq400"
  16. }
  17. func (strategy _ib_qqq) MarketId() string {
  18. return "ibkr"
  19. }
  20. func (strategy _ib_qqq) PositionSymbols() map[sentio.Side]string {
  21. return map[sentio.Side]string{
  22. sentio.LONG: "320227571", // QQQ@NASDAQ
  23. }
  24. }
  25. func (strategy _ib_qqq) Handle(market sentio.Market, proba float64) ([]sentio.StrategyOrder, error) {
  26. if !market.IsMarketOpened() {
  27. return nil, sentio.ErrMarketClosed
  28. }
  29. var (
  30. utc = strategy.clock.Now()
  31. portfolio sentio.Portfolio
  32. err error
  33. )
  34. if portfolio, err = market.Portfolio(); err != nil {
  35. return nil, err
  36. }
  37. for _, symbol := range strategy.PositionSymbols() {
  38. var (
  39. position sentio.Position
  40. ok bool
  41. )
  42. if portfolio != nil {
  43. position, ok = portfolio.Get(symbol)
  44. ok = ok && position.GetSize() != 0
  45. }
  46. // Close positions before market close
  47. if ok && utc.Hour() >= 19 && utc.Minute() >= 30 {
  48. return []sentio.StrategyOrder{
  49. {
  50. Symbol: position.GetSymbol(),
  51. Action: func() sentio.OrderAction {
  52. if position.GetSize() > 0 {
  53. return sentio.OrderSell
  54. } else {
  55. return sentio.OrderBuy
  56. }
  57. }(),
  58. Ratio: 1,
  59. },
  60. }, nil
  61. }
  62. if proba < 0 {
  63. continue
  64. }
  65. // Close LONG position
  66. if ok && position.GetSize() > 0 && proba < 1 {
  67. return []sentio.StrategyOrder{
  68. {
  69. Symbol: position.GetSymbol(),
  70. Action: sentio.OrderSell,
  71. Ratio: 1,
  72. },
  73. }, nil
  74. }
  75. // Close SHORT position
  76. if ok && position.GetSize() < 0 && proba > 1 {
  77. return []sentio.StrategyOrder{
  78. {
  79. Symbol: position.GetSymbol(),
  80. Action: sentio.OrderBuy,
  81. Ratio: 1,
  82. },
  83. }, nil
  84. }
  85. if utc.Hour() == 19 && utc.Minute() >= 30 {
  86. continue
  87. }
  88. if proba > 1.002 {
  89. return []sentio.StrategyOrder{
  90. {
  91. Symbol: symbol,
  92. Action: sentio.OrderBuy,
  93. Ratio: func() float64 {
  94. if ok {
  95. return .3
  96. } else {
  97. return .6
  98. }
  99. }(),
  100. },
  101. }, nil
  102. }
  103. if proba < .998 {
  104. return []sentio.StrategyOrder{
  105. {
  106. Symbol: symbol,
  107. Action: sentio.OrderSell,
  108. Ratio: func() float64 {
  109. if ok {
  110. return .3
  111. } else {
  112. return .6
  113. }
  114. }(),
  115. },
  116. }, nil
  117. }
  118. }
  119. return nil, nil
  120. }