strategy.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 [TQQQ : 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.BASE: "QQQ",
  19. sentio.LONG: "TQQQ",
  20. sentio.SHORT: "SQQQ",
  21. }
  22. }
  23. func (s alpacaQQQ) Handle(market sentio.Market, proba float64) ([]sentio.StrategyOrder, error) {
  24. if !market.IsMarketOpened() {
  25. return nil, sentio.ErrMarketClosed
  26. }
  27. var (
  28. portfolio sentio.Portfolio
  29. orders []sentio.StrategyOrder
  30. ok bool
  31. err error
  32. )
  33. if portfolio, err = market.Portfolio(); err != nil {
  34. return nil, err
  35. }
  36. for side, symbol := range s.PositionSymbols() {
  37. if sentio.BASE == side {
  38. continue
  39. }
  40. var (
  41. position sentio.Position
  42. t = market.Time().Now()
  43. )
  44. if portfolio != nil {
  45. position, ok = portfolio.Get(symbol)
  46. ok = ok && position.GetSize() != 0
  47. } else {
  48. ok = false
  49. }
  50. // Close positions before market closed
  51. if ok && t.Hour() == 19 && t.Minute() > 30 {
  52. orders = append(orders, sentio.StrategyOrder{
  53. Symbol: symbol,
  54. Action: sentio.OrderSell,
  55. Ratio: 1,
  56. })
  57. continue
  58. }
  59. if proba < 0 {
  60. continue
  61. }
  62. // Close LONG position
  63. if ok && sentio.LONG == side && proba < 1.00009 {
  64. orders = append(orders, sentio.StrategyOrder{
  65. Symbol: symbol,
  66. Action: sentio.OrderSell,
  67. Ratio: 1,
  68. })
  69. }
  70. if ok && sentio.SHORT == side && proba > .9999 {
  71. orders = append(orders, sentio.StrategyOrder{
  72. Symbol: symbol,
  73. Action: sentio.OrderSell,
  74. Ratio: 1,
  75. })
  76. }
  77. if t.Hour() == 19 && t.Minute() > 29 {
  78. continue
  79. }
  80. if sentio.LONG == side && proba > 1.00119 {
  81. if ok && position != nil && position.GetCurrentPrice() > position.GetAvgPrice() {
  82. continue
  83. }
  84. orders = append(orders, sentio.StrategyOrder{
  85. Symbol: symbol,
  86. Action: sentio.OrderBuy,
  87. Ratio: func() float64 {
  88. if t.Hour() >= 18 {
  89. return .3
  90. }
  91. if ok {
  92. return .4
  93. } else {
  94. return .6
  95. }
  96. }(),
  97. })
  98. continue
  99. }
  100. if sentio.SHORT == side && proba < .99955 {
  101. // prevent extra orders if our position is cheaper
  102. if ok && position != nil && position.GetCurrentPrice() > position.GetAvgPrice() {
  103. continue
  104. }
  105. orders = append(orders, sentio.StrategyOrder{
  106. Symbol: symbol,
  107. Action: sentio.OrderBuy,
  108. Ratio: func() float64 {
  109. if t.Hour() >= 18 {
  110. return .3
  111. }
  112. if ok {
  113. return .4
  114. } else {
  115. return .6
  116. }
  117. }(),
  118. })
  119. continue
  120. }
  121. }
  122. return orders, nil
  123. }