strategy.go 4.0 KB

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