2
0

strategy.go 2.9 KB

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