2
0

strategy.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package main
  2. import (
  3. "git.beejay.kim/Gshopper/sentio"
  4. )
  5. var Strategy = alpacaQQQ{}
  6. type alpacaQQQ struct{}
  7. func (s alpacaQQQ) Name() string {
  8. return "Alpaca: QQQ / SQQQ"
  9. }
  10. func (s alpacaQQQ) Model() string {
  11. return "qqq07f"
  12. }
  13. func (s alpacaQQQ) MarketId() string {
  14. return "alpaca"
  15. }
  16. func (s alpacaQQQ) PositionSymbols() map[sentio.Side]string {
  17. return map[sentio.Side]string{
  18. sentio.LONG: "QQQ",
  19. sentio.SHORT: "SQQQ",
  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. portfolio sentio.Portfolio
  28. orders []sentio.StrategyOrder
  29. ok bool
  30. err error
  31. )
  32. if portfolio, err = market.Portfolio(); err != nil {
  33. return nil, err
  34. }
  35. for side, symbol := range s.PositionSymbols() {
  36. var (
  37. position sentio.Position
  38. t = market.Time().Now()
  39. )
  40. if portfolio != nil {
  41. position, ok = portfolio.Get(symbol)
  42. ok = ok && position.GetSize() != 0
  43. } else {
  44. ok = false
  45. }
  46. // Close positions before market closed
  47. if ok && t.Hour() == 19 && t.Minute() > 30 {
  48. orders = append(orders, sentio.StrategyOrder{
  49. Symbol: symbol,
  50. Action: sentio.OrderSell,
  51. Ratio: 1,
  52. })
  53. continue
  54. }
  55. if proba < 0 {
  56. continue
  57. }
  58. // Close LONG position
  59. if ok && sentio.LONG == side && proba < 1.00009 {
  60. orders = append(orders, sentio.StrategyOrder{
  61. Symbol: symbol,
  62. Action: sentio.OrderSell,
  63. Ratio: 1,
  64. })
  65. }
  66. if ok && sentio.SHORT == side && proba > .9999 {
  67. orders = append(orders, sentio.StrategyOrder{
  68. Symbol: symbol,
  69. Action: sentio.OrderSell,
  70. Ratio: 1,
  71. })
  72. }
  73. if t.Hour() == 19 && t.Minute() > 29 {
  74. continue
  75. }
  76. if sentio.LONG == side && proba > 1.00109 {
  77. if ok && position != nil && position.GetCurrentPrice() > position.GetAvgPrice() {
  78. continue
  79. }
  80. orders = append(orders, sentio.StrategyOrder{
  81. Symbol: symbol,
  82. Action: sentio.OrderBuy,
  83. Ratio: func() float64 {
  84. if t.Hour() >= 16 {
  85. return .3
  86. }
  87. if ok {
  88. return .4
  89. } else {
  90. return .6
  91. }
  92. }(),
  93. })
  94. continue
  95. }
  96. if sentio.SHORT == side && proba < .9998 {
  97. // prevent extra orders if our position is cheaper
  98. if ok && position != nil && position.GetCurrentPrice() > position.GetAvgPrice() {
  99. continue
  100. }
  101. orders = append(orders, sentio.StrategyOrder{
  102. Symbol: symbol,
  103. Action: sentio.OrderBuy,
  104. Ratio: func() float64 {
  105. if t.Hour() >= 16 {
  106. return .3
  107. }
  108. if ok {
  109. return .4
  110. } else {
  111. return .6
  112. }
  113. }(),
  114. })
  115. continue
  116. }
  117. }
  118. return orders, nil
  119. }