strategy.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package main
  2. import (
  3. "fmt"
  4. "git.beejay.kim/Gshopper/sentio"
  5. "git.beejay.kim/Gshopper/sentio/indicator"
  6. "time"
  7. )
  8. const (
  9. ATR_MULTIPLIER = 1.5
  10. ATR_PERIOD = 14
  11. ATR_HHV = 10
  12. ATR_STOPLOSS_THRESHOLD = 1
  13. EXTRA_POSITION_THRESHOLD = 1.002
  14. )
  15. var Strategy = alpacaQQQ{}
  16. type alpacaQQQ struct{}
  17. func (s alpacaQQQ) Name() string {
  18. return "Alpaca: QQQ [TQQQ : SQQQ]"
  19. }
  20. func (s alpacaQQQ) Model() string {
  21. return "qqq15"
  22. }
  23. func (s alpacaQQQ) MarketId() string {
  24. return "alpaca"
  25. }
  26. func (s alpacaQQQ) PositionSymbols() map[sentio.Side]string {
  27. return map[sentio.Side]string{
  28. sentio.BASE: "QQQ",
  29. sentio.LONG: "TQQQ",
  30. sentio.SHORT: "SQQQ",
  31. }
  32. }
  33. func (s alpacaQQQ) Interval() uint8 {
  34. return 5
  35. }
  36. func (s alpacaQQQ) Cooldown(periods uint8) time.Duration {
  37. return time.Minute * time.Duration(s.Interval()*periods)
  38. }
  39. func (s alpacaQQQ) Handle(market sentio.Market, ts time.Time, proba float64) ([]sentio.StrategyOrder, error) {
  40. var (
  41. portfolio sentio.Portfolio
  42. orders []sentio.StrategyOrder
  43. now = market.Time().Now()
  44. err error
  45. )
  46. // skip too early trades
  47. if now.Hour() == 9 && now.Minute() < 45 {
  48. return orders, nil
  49. }
  50. if portfolio, err = market.Portfolio(); err != nil {
  51. return nil, err
  52. }
  53. for side, symbol := range s.PositionSymbols() {
  54. var (
  55. position sentio.Position
  56. quote *sentio.Quote
  57. bars []sentio.Bar
  58. stoploss float64
  59. uptrending bool
  60. ok bool
  61. )
  62. // no need to trade BASE quote
  63. if sentio.BASE == side {
  64. continue
  65. }
  66. if quote, err = market.Quote(symbol); err != nil {
  67. return nil, err
  68. }
  69. if bars, err = market.HistoricalBars(symbol, time.Minute, nil); err == nil && len(bars) > 0 {
  70. h := make([]float64, len(bars))
  71. l := make([]float64, len(bars))
  72. c := make([]float64, len(bars))
  73. for i := range bars {
  74. h[i] = bars[i].High
  75. l[i] = bars[i].Low
  76. c[i] = bars[i].Close
  77. }
  78. trailing := indicator.AtrTrailingStopLoss(h, l, c, ATR_PERIOD, ATR_MULTIPLIER, ATR_HHV)
  79. stoploss = trailing[len(trailing)-1]
  80. uptrending = trailing[len(trailing)-1] > trailing[len(trailing)-2]
  81. fmt.Printf("ATR Trailing StopLoss: [%s: %f; uptrading: %v]\n", symbol, stoploss, uptrending)
  82. }
  83. if portfolio != nil {
  84. position, ok = portfolio.Get(symbol)
  85. ok = ok && position != nil && position.GetSize() != 0
  86. } else {
  87. ok = false
  88. }
  89. // Close positions before market closed
  90. if ok && now.Hour() == 15 && now.Minute() > 55 {
  91. return []sentio.StrategyOrder{{
  92. Symbol: symbol,
  93. Action: sentio.OrderSell,
  94. Ratio: 1,
  95. }}, nil
  96. }
  97. // Close position if BidPrice less than StopLoss
  98. if ok && !uptrending && quote.BidPrice/stoploss < ATR_STOPLOSS_THRESHOLD {
  99. return []sentio.StrategyOrder{{
  100. Symbol: symbol,
  101. Action: sentio.OrderSell,
  102. Ratio: 1,
  103. }}, nil
  104. }
  105. if proba < 0 {
  106. continue
  107. }
  108. // Close LONG position
  109. if ok && sentio.LONG == side && proba < 1.0008 {
  110. return []sentio.StrategyOrder{{
  111. Symbol: symbol,
  112. Action: sentio.OrderSell,
  113. Ratio: 1,
  114. }}, nil
  115. }
  116. // Close SHORT position
  117. if ok && sentio.SHORT == side && proba > .9998 {
  118. return []sentio.StrategyOrder{{
  119. Symbol: symbol,
  120. Action: sentio.OrderSell,
  121. Ratio: 1,
  122. }}, nil
  123. }
  124. // Prevent BUYs on closing market
  125. if now.Hour() == 15 && now.Minute() > 46 {
  126. continue
  127. }
  128. if sentio.LONG == side && proba > 1 {
  129. size := float64(0)
  130. if ok && position.GetAvgPrice()/position.GetCurrentPrice() > EXTRA_POSITION_THRESHOLD {
  131. // extra position with cheaper price
  132. size = .5
  133. } else if !ok && proba > 1.0008 {
  134. // new trade position
  135. size = .7
  136. }
  137. if size > 0 {
  138. orders = append(orders, sentio.StrategyOrder{
  139. Symbol: symbol,
  140. Action: sentio.OrderBuy,
  141. Ratio: size,
  142. })
  143. }
  144. continue
  145. }
  146. if sentio.SHORT == side && proba < 1 {
  147. size := float64(0)
  148. if ok && position.GetAvgPrice()/position.GetCurrentPrice() > EXTRA_POSITION_THRESHOLD {
  149. // extra position with cheaper price
  150. size = .5
  151. } else if !ok && proba < .9990 {
  152. // new trade position
  153. size = .7
  154. }
  155. if size > 0 {
  156. orders = append(orders, sentio.StrategyOrder{
  157. Symbol: symbol,
  158. Action: sentio.OrderBuy,
  159. Ratio: size,
  160. })
  161. }
  162. continue
  163. }
  164. }
  165. return orders, nil
  166. }