2
0

strategy.go 2.9 KB

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