strategy.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package main
  2. import (
  3. "git.beejay.kim/Gshopper/sentio"
  4. )
  5. var Strategy = alpacaQQQ{}
  6. type alpacaQQQ struct{}
  7. func (s alpacaQQQ) Name() string {
  8. return "Alpaca: QQQ / SQQQ"
  9. }
  10. func (s alpacaQQQ) Model() string {
  11. return "qqq001"
  12. }
  13. func (s alpacaQQQ) MarketId() string {
  14. return "alpaca"
  15. }
  16. func (s alpacaQQQ) PositionSymbols() map[sentio.Side]string {
  17. return map[sentio.Side]string{
  18. sentio.LONG: "QQQ",
  19. sentio.SHORT: "SQQQ",
  20. }
  21. }
  22. func (s alpacaQQQ) Handle(market sentio.Market, proba float64) ([]sentio.StrategyOrder, error) {
  23. if !market.IsMarketOpened() {
  24. return nil, sentio.ErrMarketClosed
  25. }
  26. var (
  27. portfolio sentio.Portfolio
  28. orders []sentio.StrategyOrder
  29. ok bool
  30. err error
  31. )
  32. if portfolio, err = market.Portfolio(); err != nil {
  33. return nil, err
  34. }
  35. for side, symbol := range s.PositionSymbols() {
  36. var (
  37. position sentio.Position
  38. t = market.Time().Now()
  39. )
  40. if portfolio != nil {
  41. position, ok = portfolio.Get(symbol)
  42. ok = ok && position.GetSize() != 0
  43. } else {
  44. ok = false
  45. }
  46. // Close positions before market closed
  47. if ok && t.Hour() >= 19 && t.Minute() >= 30 {
  48. orders = append(orders, sentio.StrategyOrder{
  49. Symbol: symbol,
  50. Action: sentio.OrderSell,
  51. Ratio: 1,
  52. })
  53. continue
  54. }
  55. if proba < 0 {
  56. continue
  57. }
  58. // Close LONG position
  59. if ok && sentio.LONG == side && proba < 1 {
  60. orders = append(orders, sentio.StrategyOrder{
  61. Symbol: symbol,
  62. Action: sentio.OrderSell,
  63. Ratio: 1,
  64. })
  65. continue
  66. }
  67. if ok && sentio.SHORT == side && proba > 1 {
  68. orders = append(orders, sentio.StrategyOrder{
  69. Symbol: symbol,
  70. Action: sentio.OrderSell,
  71. Ratio: 1,
  72. })
  73. continue
  74. }
  75. if t.Hour() == 19 && t.Minute() >= 30 {
  76. continue
  77. }
  78. if sentio.LONG == side && proba > 1.001 {
  79. if ok && position != nil && position.GetCurrentPrice() > position.GetAvgPrice() {
  80. continue
  81. }
  82. orders = append(orders, sentio.StrategyOrder{
  83. Symbol: symbol,
  84. Action: sentio.OrderBuy,
  85. Ratio: func() float64 {
  86. if ok {
  87. return .3
  88. } else {
  89. return .6
  90. }
  91. }(),
  92. })
  93. continue
  94. }
  95. if sentio.SHORT == side && proba < .9998 {
  96. if ok && position != nil && position.GetCurrentPrice() > position.GetAvgPrice() {
  97. continue
  98. }
  99. orders = append(orders, sentio.StrategyOrder{
  100. Symbol: symbol,
  101. Action: sentio.OrderBuy,
  102. Ratio: func() float64 {
  103. if ok {
  104. return .3
  105. } else {
  106. return .8
  107. }
  108. }(),
  109. })
  110. continue
  111. }
  112. }
  113. return orders, nil
  114. }