strategy.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package main
  2. import (
  3. "git.beejay.kim/Gshopper/sentio"
  4. )
  5. var Strategy = alpacaQQQ{}
  6. type alpacaQQQ struct{}
  7. func (s alpacaQQQ) Name() string {
  8. return "Alpaca: QQQ / SQQQ"
  9. }
  10. func (s alpacaQQQ) Model() string {
  11. return "qqq06f"
  12. }
  13. func (s alpacaQQQ) MarketId() string {
  14. return "alpaca"
  15. }
  16. func (s alpacaQQQ) PositionSymbols() map[sentio.Side]string {
  17. return map[sentio.Side]string{
  18. sentio.LONG: "QQQ",
  19. sentio.SHORT: "SQQQ",
  20. }
  21. }
  22. func (s alpacaQQQ) Handle(market sentio.Market, proba float64) ([]sentio.StrategyOrder, error) {
  23. if !market.IsMarketOpened() {
  24. return nil, sentio.ErrMarketClosed
  25. }
  26. var (
  27. portfolio sentio.Portfolio
  28. orders []sentio.StrategyOrder
  29. ok bool
  30. err error
  31. )
  32. if portfolio, err = market.Portfolio(); err != nil {
  33. return nil, err
  34. }
  35. for side, symbol := range s.PositionSymbols() {
  36. var (
  37. position sentio.Position
  38. t = market.Time().Now()
  39. )
  40. if portfolio != nil {
  41. position, ok = portfolio.Get(symbol)
  42. ok = ok && position.GetSize() != 0
  43. } else {
  44. ok = false
  45. }
  46. // Close positions before market closed
  47. if ok && t.Hour() == 19 && t.Minute() > 30 {
  48. orders = append(orders, sentio.StrategyOrder{
  49. Symbol: symbol,
  50. Action: sentio.OrderSell,
  51. Ratio: 1,
  52. })
  53. continue
  54. }
  55. if proba < 0 {
  56. continue
  57. }
  58. // Close LONG position
  59. if ok && sentio.LONG == side && proba < 1.00009 {
  60. orders = append(orders, sentio.StrategyOrder{
  61. Symbol: symbol,
  62. Action: sentio.OrderSell,
  63. Ratio: 1,
  64. })
  65. }
  66. if ok && sentio.SHORT == side && proba > .9999 {
  67. orders = append(orders, sentio.StrategyOrder{
  68. Symbol: symbol,
  69. Action: sentio.OrderSell,
  70. Ratio: 1,
  71. })
  72. }
  73. if t.Hour() == 19 && t.Minute() > 29 {
  74. continue
  75. }
  76. if sentio.LONG == side && proba > 1.00109 {
  77. var (
  78. dema []float64
  79. lb *sentio.Bar
  80. )
  81. if ok && position != nil && position.GetCurrentPrice() > position.GetAvgPrice() {
  82. continue
  83. }
  84. if dema = market.DEMA(s.PositionSymbols()[sentio.LONG], 15, 8, 1); dema == nil || len(dema) < 1 {
  85. continue
  86. }
  87. if lb, err = market.LatestBar(s.PositionSymbols()[sentio.LONG]); err != nil {
  88. continue
  89. }
  90. // prevent LONG positions if current price is greater DEMA(8)
  91. if lb.Close/dema[0] > .999 {
  92. continue
  93. }
  94. orders = append(orders, sentio.StrategyOrder{
  95. Symbol: symbol,
  96. Action: sentio.OrderBuy,
  97. Ratio: func() float64 {
  98. if t.Hour() >= 16 {
  99. return .3
  100. }
  101. if ok {
  102. return .4
  103. } else {
  104. return .6
  105. }
  106. }(),
  107. })
  108. continue
  109. }
  110. if sentio.SHORT == side && proba < .9997 {
  111. var (
  112. sma []float64
  113. lb *sentio.Bar
  114. )
  115. // prevent extra orders if our position is cheaper
  116. if ok && position != nil && position.GetCurrentPrice() > position.GetAvgPrice() {
  117. continue
  118. }
  119. if sma = market.SMA(s.PositionSymbols()[sentio.LONG], 15, 8, 1); sma == nil || len(sma) < 1 {
  120. continue
  121. }
  122. if lb, err = market.LatestBar(s.PositionSymbols()[sentio.LONG]); err != nil {
  123. continue
  124. }
  125. // prevent SHORTing if current price is under SMA(8)
  126. if lb.Close/sma[0] < 1.002 {
  127. continue
  128. }
  129. orders = append(orders, sentio.StrategyOrder{
  130. Symbol: symbol,
  131. Action: sentio.OrderBuy,
  132. Ratio: func() float64 {
  133. if t.Hour() >= 16 {
  134. return .3
  135. }
  136. if ok {
  137. return .4
  138. } else {
  139. return .6
  140. }
  141. }(),
  142. })
  143. continue
  144. }
  145. }
  146. return orders, nil
  147. }