2
0

strategy.go 3.4 KB

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