2
0

strategy.go 2.9 KB

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