2
0

strategy.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 : x]"
  11. }
  12. func (strategy qqq) Model() string {
  13. return "ppo_20250625"
  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) PositionSymbols() map[sentio.Side]string {
  22. return map[sentio.Side]string{
  23. sentio.BASE: "QQQ",
  24. sentio.LONG: "TQQQ",
  25. sentio.SHORT: "QQQ",
  26. }
  27. }
  28. func (strategy qqq) MaxTradeDuration() time.Duration {
  29. return time.Duration(strategy.Interval()) * time.Minute * 15
  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() < 45) || 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. // retrieve running orders
  51. if orders, err = market.Orders(sentio.OrderListCriteria{
  52. Status: "open",
  53. Symbols: symbols,
  54. }); err != nil {
  55. return err
  56. }
  57. // update stoplosses or close running orders
  58. var hasClosedOrders = false
  59. for i := range orders {
  60. if sentio.LONG == orders[i].Intent() &&
  61. sentio.Action_SELL == turn.Action {
  62. if _, err = market.CloseOrder(orders[i], nil); err != nil {
  63. return err
  64. }
  65. hasClosedOrders = true
  66. continue
  67. }
  68. if sentio.SHORT == orders[i].Intent() &&
  69. sentio.Action_BUY == turn.Action {
  70. if _, err = market.CloseOrder(orders[i], nil); err != nil {
  71. return err
  72. }
  73. hasClosedOrders = true
  74. continue
  75. }
  76. if orders[i].GetCreatedAt().Round(time.Minute).Add(strategy.MaxTradeDuration()).Before(time.Now()) {
  77. if _, err = market.CloseOrder(orders[i], nil); err != nil {
  78. return err
  79. }
  80. hasClosedOrders = true
  81. continue
  82. }
  83. if err = market.UpdateOrder(orders[i].GetId(), rs); err != nil {
  84. return err
  85. }
  86. }
  87. // Prevent new orders if we just closed one
  88. if hasClosedOrders || len(orders) > 0 {
  89. return nil
  90. }
  91. // Prevent BUYs on closing market
  92. if now.Hour() == 15 && now.Minute() > 45 {
  93. return nil
  94. }
  95. if quotes, err = market.Quotes(); err != nil {
  96. return err
  97. }
  98. if sentio.Action_SELL == turn.Action {
  99. return nil
  100. }
  101. return util.CreateOrder(market, strategy, turn.Action, quotes, rs)
  102. }