2
0

strategy.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package main
  2. import (
  3. "git.beejay.kim/Gshopper/sentio"
  4. "time"
  5. )
  6. var Strategy = qqq{}
  7. type qqq struct {
  8. sentio.BaseStrategy
  9. }
  10. func (strategy qqq) Name() string {
  11. return "Alpaca: QQQ [TQQQ : SQQQ]"
  12. }
  13. func (strategy qqq) Model() string {
  14. return "qqq15"
  15. }
  16. func (strategy qqq) PositionSymbols() map[sentio.Side]string {
  17. return map[sentio.Side]string{
  18. sentio.BASE: "QQQ",
  19. sentio.LONG: "TQQQ",
  20. sentio.SHORT: "SQQQ",
  21. }
  22. }
  23. func (strategy qqq) PositionProbabilities() map[sentio.Side]float64 {
  24. return map[sentio.Side]float64{
  25. sentio.BASE: -1,
  26. sentio.LONG: 1.0008,
  27. sentio.SHORT: .9990,
  28. }
  29. }
  30. func (strategy qqq) Handle(market sentio.Market, probability sentio.Probability) error {
  31. var (
  32. now = market.Clock().Now()
  33. symbols = strategy.Symbols()
  34. stoplosses map[string]float64
  35. quotes map[string]sentio.Quote
  36. orders []sentio.Order
  37. portfolio sentio.Portfolio
  38. err error
  39. )
  40. // skip too early orders
  41. if now.Hour() == 9 && now.Minute() < 45 {
  42. return nil
  43. }
  44. // close all orders before market close
  45. if now.Hour() == 15 && now.Minute() > 50 {
  46. return strategy.CloseAllOrders(market)
  47. }
  48. // retrieve running orders
  49. if orders, err = market.Orders(sentio.OrderListCriteria{
  50. Status: "open",
  51. Symbols: symbols,
  52. Nested: true,
  53. }); err != nil {
  54. return err
  55. }
  56. if stoplosses, err = strategy.AtrStopLoss(market, symbols...); err != nil {
  57. return err
  58. }
  59. // update stoplosses or close running orders
  60. for i := range orders {
  61. if strategy.PositionSymbols()[sentio.LONG] == orders[i].GetSymbol() &&
  62. probability.Value < 1.0008 {
  63. if _, err = market.CloseOrder(orders[i].GetId()); err != nil {
  64. return err
  65. }
  66. continue
  67. }
  68. if strategy.PositionSymbols()[sentio.SHORT] == orders[i].GetSymbol() &&
  69. probability.Value > .9998 {
  70. if _, err = market.CloseOrder(orders[i].GetId()); err != nil {
  71. return err
  72. }
  73. continue
  74. }
  75. if f, ok := stoplosses[orders[i].GetId()]; ok && f > 0 {
  76. if err = market.UpdateOrder(orders[i].GetId(), f); err != nil {
  77. return err
  78. }
  79. continue
  80. }
  81. }
  82. if probability.TS.Add(time.Minute * time.Duration(int64(strategy.Interval()/2))).Before(now.Round(time.Minute)) {
  83. return nil
  84. }
  85. // Prevent BUYs on closing market
  86. if now.Hour() == 15 && now.Minute() > 46 {
  87. return nil
  88. }
  89. // Prevent Market.CreateOrder while probability is not clear
  90. if probability.Value == 1 || probability.Value < 0 {
  91. return nil
  92. }
  93. if portfolio, err = market.Portfolio(); err != nil {
  94. return err
  95. }
  96. if quotes, err = market.Quotes(symbols...); err != nil {
  97. return err
  98. }
  99. var t = sentio.SHORT
  100. if probability.Value > 1 {
  101. t = sentio.LONG
  102. }
  103. return strategy.CreateOrder(market, t, probability, portfolio, quotes, stoplosses)
  104. }