2
0

strategy.go 2.7 KB

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