2
0

strategy.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. package main
  2. import (
  3. "errors"
  4. "git.beejay.kim/Gshopper/sentio"
  5. "git.beejay.kim/Gshopper/sentio/indicator"
  6. "math"
  7. "time"
  8. )
  9. var Strategy = qqq{}
  10. type qqq struct{}
  11. func (strategy qqq) Name() string {
  12. return "Alpaca: QQQ [TQQQ : SQQQ]"
  13. }
  14. func (strategy qqq) Model() string {
  15. return "qqq15"
  16. }
  17. func (strategy qqq) MarketId() string {
  18. return "alpaca"
  19. }
  20. func (strategy qqq) Interval() uint8 {
  21. return 5
  22. }
  23. func (strategy qqq) Cooldown(periods uint8) time.Duration {
  24. return time.Minute * time.Duration(strategy.Interval()*periods)
  25. }
  26. func (strategy qqq) PositionSymbols() map[sentio.Side]string {
  27. return map[sentio.Side]string{
  28. sentio.BASE: "QQQ",
  29. sentio.LONG: "TQQQ",
  30. sentio.SHORT: "SQQQ",
  31. }
  32. }
  33. func (strategy qqq) PositionProbabilities() map[sentio.Side]float64 {
  34. return map[sentio.Side]float64{
  35. sentio.BASE: -1,
  36. sentio.LONG: 1.0008,
  37. sentio.SHORT: .9990,
  38. }
  39. }
  40. func (strategy qqq) Handle(market sentio.Market, probability sentio.Probability) error {
  41. var (
  42. now = market.Clock().Now()
  43. symbols = strategy.Symbols()
  44. stoplosses map[string]float64
  45. quotes map[string]sentio.Quote
  46. orders []sentio.Order
  47. portfolio sentio.Portfolio
  48. err error
  49. )
  50. // skip too early orders
  51. if now.Hour() == 9 && now.Minute() < 45 {
  52. return nil
  53. }
  54. // close all orders before market close
  55. if now.Hour() == 15 && now.Minute() > 50 {
  56. return strategy.CloseAllOrders(market)
  57. }
  58. // retrieve running orders
  59. if orders, err = market.Orders(sentio.OrderListCriteria{
  60. Status: "open",
  61. Symbols: symbols,
  62. Nested: true,
  63. }); err != nil {
  64. return err
  65. }
  66. if stoplosses, err = strategy.AtrStopLoss(market, symbols...); err != nil {
  67. return err
  68. }
  69. // update stoplosses or close running orders
  70. for i := range orders {
  71. if strategy.PositionSymbols()[sentio.LONG] == orders[i].GetSymbol() &&
  72. probability.Value < 1.0008 {
  73. if _, err = market.CloseOrder(orders[i].GetId()); err != nil {
  74. return err
  75. }
  76. continue
  77. }
  78. if strategy.PositionSymbols()[sentio.SHORT] == orders[i].GetSymbol() &&
  79. probability.Value > .9998 {
  80. if _, err = market.CloseOrder(orders[i].GetId()); err != nil {
  81. return err
  82. }
  83. continue
  84. }
  85. if f, ok := stoplosses[orders[i].GetId()]; ok && f > 0 {
  86. if err = market.UpdateOrder(orders[i].GetId(), f); err != nil {
  87. return err
  88. }
  89. continue
  90. }
  91. }
  92. //if probability.TS.Add(time.Minute * time.Duration(int64(strategy.Interval()/2))).Before(now.Round(time.Minute)) {
  93. // return nil
  94. //}
  95. // Prevent BUYs on closing market
  96. if now.Hour() == 15 && now.Minute() > 46 {
  97. return nil
  98. }
  99. // Prevent Market.CreateOrder while probability is not clear
  100. if probability.Value == 1 || probability.Value < 0 {
  101. return nil
  102. }
  103. if portfolio, err = market.Portfolio(); err != nil {
  104. return err
  105. }
  106. if quotes, err = market.Quotes(symbols...); err != nil {
  107. return err
  108. }
  109. var t = sentio.SHORT
  110. if probability.Value > 1 {
  111. t = sentio.LONG
  112. }
  113. return strategy.CreateOrder(market, t, probability, portfolio, quotes, stoplosses)
  114. }
  115. func (strategy qqq) Symbols() []string {
  116. var symbols []string
  117. for side, s := range strategy.PositionSymbols() {
  118. if sentio.BASE == side {
  119. continue
  120. }
  121. symbols = append(symbols, s)
  122. }
  123. return symbols
  124. }
  125. func (strategy qqq) CloseAllOrders(market sentio.Market) error {
  126. var (
  127. symbols = strategy.Symbols()
  128. orders []sentio.Order
  129. err error
  130. )
  131. if orders, err = market.Orders(sentio.OrderListCriteria{
  132. Status: "open",
  133. Symbols: symbols,
  134. Nested: true,
  135. }); err != nil {
  136. return err
  137. }
  138. for i := range orders {
  139. if _, err = market.CloseOrder(orders[i].GetId()); err != nil {
  140. return err
  141. }
  142. }
  143. return nil
  144. }
  145. func (strategy qqq) AtrStopLoss(market sentio.Market, symbols ...string) (map[string]float64, error) {
  146. var (
  147. stoplosses map[string]float64
  148. bars map[string][]sentio.Bar
  149. err error
  150. )
  151. if bars, err = market.HistoricalBars(symbols, time.Minute, nil); err != nil {
  152. return nil, err
  153. }
  154. if bars == nil || len(bars) < sentio.ATR_PERIOD {
  155. return nil, errors.New("AtrStopLoss: could not calculate stoploss for too short timeseries")
  156. }
  157. stoplosses = make(map[string]float64)
  158. for s := range bars {
  159. h := make([]float64, len(bars[s]))
  160. l := make([]float64, len(bars[s]))
  161. c := make([]float64, len(bars[s]))
  162. for i := range bars[s] {
  163. h[i] = bars[s][i].High
  164. l[i] = bars[s][i].Low
  165. c[i] = bars[s][i].Close
  166. }
  167. trailing := indicator.AtrTrailingStopLoss(h, l, c, sentio.ATR_PERIOD, sentio.ATR_MULTIPLIER, sentio.ATR_HHV)
  168. stoplosses[s] = trailing[len(trailing)-1]
  169. }
  170. return stoplosses, nil
  171. }
  172. func (strategy qqq) CreateOrder(m sentio.Market, t sentio.Side, proba sentio.Probability, p sentio.Portfolio, q map[string]sentio.Quote, sl map[string]float64) error {
  173. var (
  174. symbol = strategy.PositionSymbols()[t]
  175. position sentio.Position
  176. account sentio.MarketAccount
  177. has = false
  178. threshold float64
  179. size uint
  180. ok bool
  181. err error
  182. )
  183. // ensure portfolio
  184. position, has = p.Get(symbol)
  185. // define threshold
  186. if threshold, ok = strategy.PositionProbabilities()[t]; !ok {
  187. threshold = -1
  188. }
  189. if threshold == -1 || symbol == "" {
  190. return nil
  191. }
  192. // Prevent Market.CreateOrder when BidPrice less than ATR_STOPLOSS_THRESHOLD
  193. if q[symbol].BidPrice/sl[symbol] < sentio.ATR_STOPLOSS_THRESHOLD {
  194. return nil
  195. }
  196. if account, err = m.Account(); err != nil {
  197. return err
  198. }
  199. if account.GetCash() < q[symbol].BidPrice {
  200. return sentio.ErrTooSmallOrder
  201. }
  202. if !has && proba.Value > threshold {
  203. // create a new order
  204. if size = uint(math.Floor(account.GetCash() * .7 / q[symbol].BidPrice)); size < 1 {
  205. return sentio.ErrTooSmallOrder
  206. }
  207. _, err = m.CreateOrder(symbol, size, sl[symbol])
  208. return err
  209. } else if has && position.GetAvgPrice()/position.GetCurrentPrice() > sentio.EXTRA_POSITION_THRESHOLD {
  210. // create an extra position
  211. if size = uint(math.Floor(account.GetCash() * .5 / q[symbol].BidPrice)); size < 1 {
  212. return sentio.ErrTooSmallOrder
  213. }
  214. _, err = m.CreateOrder(symbol, size, sl[symbol])
  215. return err
  216. }
  217. return nil
  218. }