2
0

strategy.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 for delayed probas
  87. if ts.Add(time.Minute * time.Duration(int64(s.Interval()/2))).Before(now) {
  88. continue
  89. }
  90. // Prevent BUYs on closing market
  91. if now.Hour() == 15 && now.Minute() > 46 {
  92. continue
  93. }
  94. if sentio.LONG == side && proba > 1 {
  95. size := float64(0)
  96. if ok && position.GetAvgPrice()/position.GetCurrentPrice() > 1.002 {
  97. // extra position with cheaper price
  98. size = .4
  99. } else if !ok && proba > 1.0008 {
  100. // new trade position
  101. size = .6
  102. }
  103. if size > 0 {
  104. orders = append(orders, sentio.StrategyOrder{
  105. Symbol: symbol,
  106. Action: sentio.OrderBuy,
  107. Ratio: size,
  108. })
  109. }
  110. continue
  111. }
  112. if sentio.SHORT == side && proba < 1 {
  113. size := float64(0)
  114. if ok && position.GetAvgPrice()/position.GetCurrentPrice() > 1.002 {
  115. // extra position with cheaper price
  116. size = .4
  117. } else if !ok && proba < .9990 {
  118. // new trade position
  119. size = .6
  120. }
  121. if size > 0 {
  122. orders = append(orders, sentio.StrategyOrder{
  123. Symbol: symbol,
  124. Action: sentio.OrderBuy,
  125. Ratio: size,
  126. })
  127. }
  128. continue
  129. }
  130. }
  131. return orders, nil
  132. }