strategy.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package main
  2. import (
  3. "git.beejay.kim/Gshopper/sentio"
  4. "git.beejay.kim/Gshopper/sentio/util"
  5. "time"
  6. )
  7. var Strategy = qqq{}
  8. type qqq struct{}
  9. func (strategy qqq) Name() string {
  10. return "Alpaca: Hybrid [TQQQ : SQQQ]"
  11. }
  12. func (strategy qqq) MarketId() string {
  13. return "alpaca"
  14. }
  15. func (strategy qqq) Interval() uint8 {
  16. return 1
  17. }
  18. func (strategy qqq) EnableStopLoss() bool {
  19. return false
  20. }
  21. func (strategy qqq) PositionSymbols() map[sentio.Side]string {
  22. return map[sentio.Side]string{
  23. sentio.BASE: "QQQ",
  24. sentio.LONG: "TQQQ",
  25. sentio.SHORT: "SQQQ",
  26. }
  27. }
  28. func (strategy qqq) MaxTradeDuration() time.Duration {
  29. return time.Duration(strategy.Interval()) * time.Minute * 5
  30. }
  31. func (strategy qqq) Handle(turn *sentio.Turn, market sentio.Market, rs sentio.RiskManager) error {
  32. if market == nil || turn == nil {
  33. return nil
  34. }
  35. var (
  36. now = market.Clock().Now()
  37. symbols = util.Symbols(strategy)
  38. quotes map[string]sentio.Quote
  39. orders []sentio.Order
  40. err error
  41. )
  42. // skip too early orders or late orders
  43. if (now.Hour() == 9 && now.Minute() < 31) || now.Hour() > 15 {
  44. return nil
  45. }
  46. // close all orders before market close
  47. if now.Hour() == 15 && now.Minute() > 50 {
  48. return util.CloseAllOrders(market, strategy)
  49. }
  50. if quotes, err = market.Quotes(); err != nil {
  51. return err
  52. }
  53. // retrieve running orders
  54. if orders, err = market.Orders(sentio.OrderListCriteria{
  55. Status: util.ToPtr("open"),
  56. Symbols: symbols,
  57. }); err != nil {
  58. return err
  59. }
  60. // update StopLosses or close running orders
  61. var hasClosedOrders = false
  62. for i := range orders {
  63. shouldClose := false
  64. side := util.SideOf(orders[i].GetIntent())
  65. if orders[i].GetSymbol() == strategy.PositionSymbols()[sentio.LONG] && sentio.Action_SELL == turn.Action {
  66. shouldClose = true
  67. }
  68. if orders[i].GetSymbol() == strategy.PositionSymbols()[sentio.BASE] &&
  69. sentio.LONG == side && sentio.Action_SELL == turn.Action {
  70. shouldClose = true
  71. }
  72. if orders[i].GetSymbol() == strategy.PositionSymbols()[sentio.SHORT] && sentio.Action_BUY == turn.Action {
  73. shouldClose = true
  74. }
  75. if orders[i].GetSymbol() == strategy.PositionSymbols()[sentio.BASE] &&
  76. sentio.SHORT == side && sentio.Action_BUY == turn.Action {
  77. shouldClose = true
  78. }
  79. if shouldClose {
  80. if _, err = market.CloseOrder(orders[i], nil); err != nil {
  81. return err
  82. }
  83. hasClosedOrders = true
  84. continue
  85. }
  86. if orders[i].GetCreatedAt().Round(time.Minute).Add(strategy.MaxTradeDuration()).Before(time.Now()) {
  87. if _, err = market.CloseOrder(orders[i], nil); err != nil {
  88. return err
  89. }
  90. hasClosedOrders = true
  91. continue
  92. }
  93. opts := sentio.OrderUpdateOptions{
  94. TakeProfit: rs.TakeProfit(orders[i].GetSymbol(), quotes[orders[i].GetSymbol()].BidPrice),
  95. StopLoss: rs.StopLoss(orders[i].GetSymbol(), quotes[orders[i].GetSymbol()].BidPrice),
  96. EnableStopLoss: strategy.EnableStopLoss(),
  97. }
  98. if err = market.UpdateOrder(orders[i].GetId(), opts); err != nil {
  99. return err
  100. }
  101. }
  102. // Prevent new orders if we just closed one
  103. if hasClosedOrders || len(orders) > 0 {
  104. return nil
  105. }
  106. // Prevent BUYs on closing market
  107. if now.Hour() == 15 && now.Minute() > 45 {
  108. return nil
  109. }
  110. var opts sentio.OrderCreateOptions
  111. if opts, err = util.NewOrder(market, strategy, turn.Action, quotes, rs); err != nil {
  112. return err
  113. }
  114. // Disable StopLoss
  115. if !strategy.EnableStopLoss() {
  116. opts.StopLoss = nil
  117. }
  118. return market.CreateOrder(opts)
  119. }