2
0

strategy.go 3.0 KB

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