strategy.go 2.3 KB

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