strategy.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 = 1.5
  10. ATR_PERIOD = 5
  11. ATR_HHV = 4
  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) Cooldown(periods uint8) time.Duration {
  28. return time.Minute * time.Duration(strategy.Interval()*periods)
  29. }
  30. func (strategy qqq) PositionSymbols() map[sentio.Side]string {
  31. return map[sentio.Side]string{
  32. sentio.BASE: "QQQ",
  33. sentio.LONG: "TQQQ",
  34. sentio.SHORT: "SQQQ",
  35. }
  36. }
  37. func (strategy qqq) PositionProbabilities() map[sentio.Side]float64 {
  38. return map[sentio.Side]float64{
  39. sentio.BASE: -1,
  40. sentio.LONG: 1.0008,
  41. sentio.SHORT: .9990,
  42. }
  43. }
  44. func (strategy qqq) Handle(market sentio.Market, probability sentio.Probability) error {
  45. var (
  46. now = market.Clock().Now()
  47. symbols = util.Symbols(strategy)
  48. stoplosses map[string]float64
  49. quotes map[string]sentio.Quote
  50. orders []sentio.Order
  51. err error
  52. )
  53. // skip too early orders
  54. if now.Hour() == 9 && now.Minute() < 45 {
  55. return nil
  56. }
  57. // close all orders before market close
  58. if now.Hour() == 15 && now.Minute() > 50 {
  59. return util.CloseAllOrders(market, strategy)
  60. }
  61. // retrieve running orders
  62. if orders, err = market.Orders(sentio.OrderListCriteria{
  63. Status: "open",
  64. Symbols: symbols,
  65. }); err != nil {
  66. return err
  67. }
  68. if stoplosses, err = indicator.AtrTrailingStopLoss(market, 1, ATR_PERIOD, ATR_MULTIPLIER, ATR_HHV, symbols...); err != nil {
  69. return err
  70. }
  71. // update stoplosses or close running orders
  72. for i := range orders {
  73. if strategy.PositionSymbols()[sentio.LONG] == orders[i].GetSymbol() &&
  74. probability.Value < 1.0008 {
  75. if _, err = market.CloseOrder(orders[i].GetId()); err != nil {
  76. return err
  77. }
  78. continue
  79. }
  80. if strategy.PositionSymbols()[sentio.SHORT] == orders[i].GetSymbol() &&
  81. probability.Value > .9998 {
  82. if _, err = market.CloseOrder(orders[i].GetId()); err != nil {
  83. return err
  84. }
  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. if probability.TS.Add(time.Minute * time.Duration(int64(strategy.Interval()/2))).Before(now.Round(time.Minute)) {
  95. return nil
  96. }
  97. // Prevent BUYs on closing market
  98. if now.Hour() == 15 && now.Minute() > 46 {
  99. return nil
  100. }
  101. // Prevent Market.CreateOrder while probability is not clear
  102. if probability.Value == 1 || probability.Value < 0 {
  103. return nil
  104. }
  105. if quotes, err = market.Quotes(symbols...); err != nil {
  106. return err
  107. }
  108. var t = sentio.SHORT
  109. if probability.Value > 1 {
  110. t = sentio.LONG
  111. }
  112. return util.CreateOrder(market, strategy, t, probability, quotes, stoplosses)
  113. }