qqq.go 2.7 KB

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