strategy.go 3.0 KB

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