strategy.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. fmt.Printf("Quotes: %s [BID: %f] [ASK: %s]\n", symbol, quote.BidPrice, quote.AskPrice)
  70. if bars, err = market.HistoricalBars(symbol, time.Minute, nil); err == nil && len(bars) > 0 {
  71. fmt.Printf("HistoricalBars: %s", bars[len(bars)-1].Datetime.Format(time.RFC3339))
  72. h := make([]float64, len(bars))
  73. l := make([]float64, len(bars))
  74. c := make([]float64, len(bars))
  75. for i := range bars {
  76. h[i] = bars[i].High
  77. l[i] = bars[i].Low
  78. c[i] = bars[i].Close
  79. }
  80. trailing := indicator.AtrTrailingStopLoss(h, l, c, ATR_PERIOD, ATR_MULTIPLIER, ATR_HHV)
  81. stoploss = trailing[len(trailing)-1]
  82. uptrending = trailing[len(trailing)-1] > trailing[len(trailing)-2]
  83. fmt.Printf("ATR Trailing StopLoss: [%s: %f; uptrading: %v]\n", symbol, stoploss, uptrending)
  84. }
  85. if portfolio != nil {
  86. position, ok = portfolio.Get(symbol)
  87. ok = ok && position != nil && position.GetSize() != 0
  88. } else {
  89. ok = false
  90. }
  91. // Close positions before market closed
  92. if ok && now.Hour() == 15 && now.Minute() > 55 {
  93. return []sentio.StrategyOrder{{
  94. Symbol: symbol,
  95. Action: sentio.OrderSell,
  96. Ratio: 1,
  97. }}, nil
  98. }
  99. // Close position if BidPrice less than StopLoss
  100. if ok && !uptrending && quote.BidPrice/stoploss < ATR_STOPLOSS_THRESHOLD {
  101. return []sentio.StrategyOrder{{
  102. Symbol: symbol,
  103. Action: sentio.OrderSell,
  104. Ratio: 1,
  105. }}, nil
  106. }
  107. if proba < 0 {
  108. continue
  109. }
  110. // Close LONG position
  111. if ok && sentio.LONG == side && proba < 1.0008 {
  112. return []sentio.StrategyOrder{{
  113. Symbol: symbol,
  114. Action: sentio.OrderSell,
  115. Ratio: 1,
  116. }}, nil
  117. }
  118. // Close SHORT position
  119. if ok && sentio.SHORT == side && proba > .9998 {
  120. return []sentio.StrategyOrder{{
  121. Symbol: symbol,
  122. Action: sentio.OrderSell,
  123. Ratio: 1,
  124. }}, nil
  125. }
  126. // Prevent BUYs for delayed probas
  127. if ts.Add(time.Minute * time.Duration(int64(s.Interval()/2))).Before(now.Round(time.Minute)) {
  128. continue
  129. }
  130. // Prevent BUYs on closing market
  131. if now.Hour() == 15 && now.Minute() > 46 {
  132. continue
  133. }
  134. if sentio.LONG == side && proba > 1 {
  135. size := float64(0)
  136. if ok && position.GetAvgPrice()/position.GetCurrentPrice() > EXTRA_POSITION_THRESHOLD {
  137. // extra position with cheaper price
  138. size = .5
  139. } else if !ok && proba > 1.0008 {
  140. // new trade position
  141. size = .7
  142. }
  143. if size > 0 {
  144. orders = append(orders, sentio.StrategyOrder{
  145. Symbol: symbol,
  146. Action: sentio.OrderBuy,
  147. Ratio: size,
  148. })
  149. }
  150. continue
  151. }
  152. if sentio.SHORT == side && proba < 1 {
  153. size := float64(0)
  154. if ok && position.GetAvgPrice()/position.GetCurrentPrice() > EXTRA_POSITION_THRESHOLD {
  155. // extra position with cheaper price
  156. size = .5
  157. } else if !ok && proba < .9990 {
  158. // new trade position
  159. size = .7
  160. }
  161. if size > 0 {
  162. orders = append(orders, sentio.StrategyOrder{
  163. Symbol: symbol,
  164. Action: sentio.OrderBuy,
  165. Ratio: size,
  166. })
  167. }
  168. continue
  169. }
  170. }
  171. return orders, nil
  172. }