2
0

strategy.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. if orders[i].GetSymbol() == strategy.PositionSymbols()[sentio.LONG] && sentio.Action_SELL == turn.Action {
  65. shouldClose = true
  66. }
  67. if orders[i].GetSymbol() == strategy.PositionSymbols()[sentio.BASE] &&
  68. sentio.LONG == orders[i].Intent() && sentio.Action_SELL == turn.Action {
  69. shouldClose = true
  70. }
  71. if orders[i].GetSymbol() == strategy.PositionSymbols()[sentio.SHORT] && sentio.Action_BUY == turn.Action {
  72. shouldClose = true
  73. }
  74. if orders[i].GetSymbol() == strategy.PositionSymbols()[sentio.BASE] &&
  75. sentio.SHORT == orders[i].Intent() && sentio.Action_BUY == turn.Action {
  76. shouldClose = true
  77. }
  78. if shouldClose {
  79. if _, err = market.CloseOrder(orders[i], nil); err != nil {
  80. return err
  81. }
  82. hasClosedOrders = true
  83. continue
  84. }
  85. if orders[i].GetCreatedAt().Round(time.Minute).Add(strategy.MaxTradeDuration()).Before(time.Now()) {
  86. if _, err = market.CloseOrder(orders[i], nil); err != nil {
  87. return err
  88. }
  89. hasClosedOrders = true
  90. continue
  91. }
  92. opts := sentio.OrderUpdateOptions{
  93. TakeProfit: rs.TakeProfit(orders[i].GetSymbol(), quotes[orders[i].GetSymbol()].BidPrice),
  94. StopLoss: rs.StopLoss(orders[i].GetSymbol(), quotes[orders[i].GetSymbol()].BidPrice),
  95. EnableStopLoss: strategy.EnableStopLoss(),
  96. }
  97. if err = market.UpdateOrder(orders[i].GetId(), opts); err != nil {
  98. return err
  99. }
  100. }
  101. // Prevent new orders if we just closed one
  102. if hasClosedOrders || len(orders) > 0 {
  103. return nil
  104. }
  105. // Prevent BUYs on closing market
  106. if now.Hour() == 15 && now.Minute() > 45 {
  107. return nil
  108. }
  109. var opts sentio.OrderCreateOptions
  110. if opts, err = util.NewOrder(market, strategy, turn.Action, quotes, rs); err != nil {
  111. return err
  112. }
  113. // Disable StopLoss
  114. if !strategy.EnableStopLoss() {
  115. opts.StopLoss = nil
  116. }
  117. return market.CreateOrder(opts)
  118. }