strategy.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package main
  2. import (
  3. "git.beejay.kim/Gshopper/sentio"
  4. "time"
  5. )
  6. var Strategy = alpacaQQQ{}
  7. type alpacaQQQ struct{}
  8. func (s alpacaQQQ) Name() string {
  9. return "Alpaca: QQQ [TQQQ : SQQQ]"
  10. }
  11. func (s alpacaQQQ) Model() string {
  12. return "qqq15"
  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.BASE: "QQQ",
  20. sentio.LONG: "TQQQ",
  21. sentio.SHORT: "SQQQ",
  22. }
  23. }
  24. func (s alpacaQQQ) Interval() uint8 {
  25. return 5
  26. }
  27. func (s alpacaQQQ) Cooldown(periods uint8) time.Duration {
  28. return time.Minute * time.Duration(s.Interval()*periods)
  29. }
  30. func (s alpacaQQQ) Handle(market sentio.Market, ts time.Time, proba float64) ([]sentio.StrategyOrder, error) {
  31. var (
  32. portfolio sentio.Portfolio
  33. orders []sentio.StrategyOrder
  34. now = market.Time().Now()
  35. err error
  36. )
  37. // skip too early trades
  38. if now.Hour() == 9 && now.Minute() < 45 {
  39. return orders, nil
  40. }
  41. if portfolio, err = market.Portfolio(); err != nil {
  42. return nil, err
  43. }
  44. for side, symbol := range s.PositionSymbols() {
  45. var (
  46. position sentio.Position
  47. ok bool
  48. )
  49. // no need to trade BASE quote
  50. if sentio.BASE == side {
  51. continue
  52. }
  53. if portfolio != nil {
  54. position, ok = portfolio.Get(symbol)
  55. ok = ok && position != nil && position.GetSize() != 0
  56. } else {
  57. ok = false
  58. }
  59. // Close positions before market closed
  60. if ok && now.Hour() == 15 && now.Minute() > 55 {
  61. return []sentio.StrategyOrder{{
  62. Symbol: symbol,
  63. Action: sentio.OrderSell,
  64. Ratio: 1,
  65. }}, nil
  66. }
  67. if proba < 0 {
  68. continue
  69. }
  70. // Close LONG position
  71. if ok && sentio.LONG == side && proba < 1.0008 {
  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 > .9998 {
  80. return []sentio.StrategyOrder{{
  81. Symbol: symbol,
  82. Action: sentio.OrderSell,
  83. Ratio: 1,
  84. }}, nil
  85. }
  86. // Prevent BUYs on closing market
  87. if now.Hour() == 15 && now.Minute() > 46 {
  88. continue
  89. }
  90. if sentio.LONG == side && proba > 1 {
  91. size := float64(0)
  92. if ok && position.GetAvgPrice()/position.GetCurrentPrice() > 1.002 {
  93. // extra position with cheaper price
  94. size = .4
  95. } else if !ok && proba > 1.0008 {
  96. // new trade position
  97. size = .6
  98. }
  99. if size > 0 {
  100. orders = append(orders, sentio.StrategyOrder{
  101. Symbol: symbol,
  102. Action: sentio.OrderBuy,
  103. Ratio: size,
  104. })
  105. }
  106. continue
  107. }
  108. if sentio.SHORT == side && proba < 1 {
  109. size := float64(0)
  110. if ok && position.GetAvgPrice()/position.GetCurrentPrice() > 1.002 {
  111. // extra position with cheaper price
  112. size = .4
  113. } else if !ok && proba < .9990 {
  114. // new trade position
  115. size = .6
  116. }
  117. if size > 0 {
  118. orders = append(orders, sentio.StrategyOrder{
  119. Symbol: symbol,
  120. Action: sentio.OrderBuy,
  121. Ratio: size,
  122. })
  123. }
  124. continue
  125. }
  126. }
  127. return orders, nil
  128. }