2
0

strategy.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "qqq05f"
  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.00009 {
  60. orders = append(orders, sentio.StrategyOrder{
  61. Symbol: symbol,
  62. Action: sentio.OrderSell,
  63. Ratio: 1,
  64. })
  65. }
  66. if ok && sentio.SHORT == side && proba > .9999 {
  67. orders = append(orders, sentio.StrategyOrder{
  68. Symbol: symbol,
  69. Action: sentio.OrderSell,
  70. Ratio: 1,
  71. })
  72. }
  73. if t.Hour() == 19 && t.Minute() > 29 {
  74. continue
  75. }
  76. if sentio.LONG == side && proba > 1.00109 {
  77. if ok && position != nil && position.GetCurrentPrice() > position.GetAvgPrice() {
  78. continue
  79. }
  80. orders = append(orders, sentio.StrategyOrder{
  81. Symbol: symbol,
  82. Action: sentio.OrderBuy,
  83. Ratio: func() float64 {
  84. if ok {
  85. return .6
  86. } else {
  87. return .3
  88. }
  89. }(),
  90. })
  91. continue
  92. }
  93. if sentio.SHORT == side && proba < .9999 {
  94. if ok && position != nil && position.GetCurrentPrice() > position.GetAvgPrice() {
  95. continue
  96. }
  97. orders = append(orders, sentio.StrategyOrder{
  98. Symbol: symbol,
  99. Action: sentio.OrderBuy,
  100. Ratio: func() float64 {
  101. if ok {
  102. return .6
  103. } else {
  104. return .3
  105. }
  106. }(),
  107. })
  108. continue
  109. }
  110. }
  111. return orders, nil
  112. }