strategy.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package main
  2. import (
  3. "git.beejay.kim/Gshopper/sentio"
  4. "git.beejay.kim/Gshopper/sentio/indicator"
  5. "git.beejay.kim/Gshopper/sentio/util"
  6. "time"
  7. )
  8. const (
  9. ATR_MULTIPLIER = 3
  10. ATR_PERIOD = 21
  11. ATR_HHV = 10
  12. )
  13. var Strategy = qqq{}
  14. type qqq struct{}
  15. func (strategy qqq) Name() string {
  16. return "Alpaca: QQQ [TQQQ : SQQQ]"
  17. }
  18. func (strategy qqq) Model() string {
  19. return "qqq15"
  20. }
  21. func (strategy qqq) MarketId() string {
  22. return "alpaca"
  23. }
  24. func (strategy qqq) Interval() uint8 {
  25. return 5
  26. }
  27. func (strategy qqq) PositionSymbols() map[sentio.Side]string {
  28. return map[sentio.Side]string{
  29. sentio.BASE: "QQQ",
  30. sentio.LONG: "TQQQ",
  31. sentio.SHORT: "SQQQ",
  32. }
  33. }
  34. func (strategy qqq) PositionProbabilities() map[sentio.Side]float64 {
  35. return map[sentio.Side]float64{
  36. sentio.BASE: -1,
  37. sentio.LONG: 1.0008,
  38. sentio.SHORT: .9990,
  39. }
  40. }
  41. func (strategy qqq) Handle(market sentio.Market, probability sentio.Probability) error {
  42. var (
  43. now = market.Clock().Now()
  44. symbols = util.Symbols(strategy)
  45. stoplosses map[string]float64
  46. quotes map[string]sentio.Quote
  47. orders []sentio.Order
  48. err error
  49. )
  50. // skip too early orders
  51. if now.Hour() == 9 && now.Minute() < 45 {
  52. return nil
  53. }
  54. // close all orders before market close
  55. if now.Hour() == 15 && now.Minute() > 50 {
  56. return util.CloseAllOrders(market, strategy)
  57. }
  58. // retrieve running orders
  59. if orders, err = market.Orders(sentio.OrderListCriteria{
  60. Status: "open",
  61. Symbols: symbols,
  62. }); err != nil {
  63. return err
  64. }
  65. if stoplosses, err = indicator.AtrTrailingStopLoss(market, 1, ATR_PERIOD, ATR_MULTIPLIER, ATR_HHV, symbols...); err != nil {
  66. return err
  67. }
  68. // update stoplosses or close running orders
  69. hasClosedOrders := false
  70. for i := range orders {
  71. if strategy.PositionSymbols()[sentio.LONG] == orders[i].GetSymbol() &&
  72. probability.Value < 1.0008 {
  73. if _, err = market.CloseOrder(orders[i]); err != nil {
  74. return err
  75. }
  76. hasClosedOrders = true
  77. continue
  78. }
  79. if strategy.PositionSymbols()[sentio.SHORT] == orders[i].GetSymbol() &&
  80. probability.Value > .9998 {
  81. if _, err = market.CloseOrder(orders[i]); err != nil {
  82. return err
  83. }
  84. hasClosedOrders = true
  85. continue
  86. }
  87. if f, ok := stoplosses[orders[i].GetSymbol()]; ok && f > 0 {
  88. if err = market.UpdateOrder(orders[i].GetId(), f); err != nil {
  89. return err
  90. }
  91. continue
  92. }
  93. }
  94. // Prevent new orders if we just closed one
  95. if hasClosedOrders {
  96. return nil
  97. }
  98. if probability.TS.Add(time.Minute * time.Duration(int64(strategy.Interval()/2))).Before(now.Round(time.Minute)) {
  99. return nil
  100. }
  101. // Prevent BUYs on closing market
  102. if now.Hour() == 15 && now.Minute() > 46 {
  103. return nil
  104. }
  105. // Prevent Market.CreateOrder while probability is not clear
  106. if probability.Value == 1 || probability.Value < 0 {
  107. return nil
  108. }
  109. if quotes, err = market.Quotes(symbols...); err != nil {
  110. return err
  111. }
  112. var t = sentio.SHORT
  113. if probability.Value > 1 {
  114. t = sentio.LONG
  115. }
  116. return util.CreateOrder(market, strategy, t, probability, quotes, stoplosses)
  117. }