qqq_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. package qqq
  2. import (
  3. "git.beejay.kim/Gshopper/sentio"
  4. "reflect"
  5. "testing"
  6. "time"
  7. )
  8. func Test_IBKR_QQQ_IsMarketOpened(t *testing.T) {
  9. tests := []struct {
  10. name string
  11. datetime string
  12. want bool
  13. }{
  14. {
  15. name: "1s before open",
  16. datetime: "2024-09-05T13:29:59Z",
  17. want: false,
  18. },
  19. {
  20. name: "just opened",
  21. datetime: "2024-09-05T13:30:00Z",
  22. want: true,
  23. },
  24. {
  25. name: "Saturday",
  26. datetime: "2024-09-07T13:30:00Z",
  27. want: false,
  28. },
  29. {
  30. name: "Sunday",
  31. datetime: "2024-09-08T13:30:00Z",
  32. want: false,
  33. },
  34. {
  35. name: "1s before close",
  36. datetime: "2024-09-05T18:59:59Z",
  37. want: true,
  38. },
  39. {
  40. name: "just closed",
  41. datetime: "2024-09-05T20:00:00Z",
  42. want: false,
  43. },
  44. {
  45. name: "closed",
  46. datetime: "2024-09-05T00:30:00Z",
  47. want: false,
  48. },
  49. }
  50. for _, tt := range tests {
  51. var (
  52. strategy _ib_qqq
  53. dt = sentio.StubClock{}
  54. err error
  55. )
  56. if dt.Clock, err = time.ParseInLocation(time.RFC3339, tt.datetime, time.UTC); err != nil {
  57. t.Errorf("ParseInLocation() = %v", err)
  58. continue
  59. }
  60. strategy = _ib_qqq{clock: dt}
  61. t.Run(tt.name, func(t *testing.T) {
  62. if got := strategy.IsMarketOpened(); got != tt.want {
  63. t.Errorf("IsMarketOpened() = %v, want %v", got, tt.want)
  64. }
  65. })
  66. }
  67. }
  68. func Test__ib_qqq_Handle(t *testing.T) {
  69. type args struct {
  70. proba float64
  71. portfolio sentio.Portfolio
  72. time string
  73. }
  74. tests := []struct {
  75. name string
  76. args args
  77. want []sentio.StrategyOrder
  78. }{
  79. {
  80. name: "close short position: market reversal",
  81. args: args{
  82. proba: 1.00001,
  83. portfolio: sentio.NewPortfolioStub(
  84. sentio.PositionStub{
  85. Symbol: "320227571",
  86. Size: -10,
  87. },
  88. ),
  89. time: "2024-08-02T15:04:05Z",
  90. },
  91. want: []sentio.StrategyOrder{
  92. {
  93. Symbol: "320227571",
  94. Action: sentio.OrderBuy,
  95. Ratio: 1,
  96. },
  97. },
  98. },
  99. {
  100. name: "close long position: market reversal",
  101. args: args{
  102. proba: .999,
  103. portfolio: sentio.NewPortfolioStub(
  104. sentio.PositionStub{
  105. Symbol: "320227571",
  106. Size: 255,
  107. }),
  108. time: "2024-08-02T15:04:05Z",
  109. },
  110. want: []sentio.StrategyOrder{
  111. {
  112. Symbol: "320227571",
  113. Action: sentio.OrderSell,
  114. Ratio: 1,
  115. },
  116. },
  117. },
  118. {
  119. name: "close position: market closes",
  120. args: args{
  121. proba: 0.00001,
  122. portfolio: sentio.NewPortfolioStub(
  123. sentio.PositionStub{
  124. Symbol: "320227571",
  125. Size: -10,
  126. },
  127. ),
  128. time: "2024-08-02T19:34:05Z",
  129. },
  130. want: []sentio.StrategyOrder{
  131. {
  132. Symbol: "320227571",
  133. Action: sentio.OrderBuy,
  134. Ratio: 1,
  135. },
  136. },
  137. },
  138. {
  139. name: "close position: market closes",
  140. args: args{
  141. proba: 1.00201,
  142. portfolio: sentio.NewPortfolioStub(
  143. sentio.PositionStub{
  144. Symbol: "320227571",
  145. Size: -10,
  146. },
  147. ),
  148. time: "2024-08-02T19:34:05Z",
  149. },
  150. want: []sentio.StrategyOrder{
  151. {
  152. Symbol: "320227571",
  153. Action: sentio.OrderBuy,
  154. Ratio: 1,
  155. },
  156. },
  157. },
  158. {
  159. name: "close position: market closes",
  160. args: args{
  161. proba: 1.00201,
  162. portfolio: sentio.NewPortfolioStub(
  163. sentio.PositionStub{
  164. Symbol: "320227571",
  165. Size: 10,
  166. },
  167. ),
  168. time: "2024-08-02T19:34:05Z",
  169. },
  170. want: []sentio.StrategyOrder{
  171. {
  172. Symbol: "320227571",
  173. Action: sentio.OrderSell,
  174. Ratio: 1,
  175. },
  176. },
  177. },
  178. {
  179. name: "open position: long",
  180. args: args{
  181. proba: 1.00201,
  182. time: "2024-08-02T17:34:05Z",
  183. },
  184. want: []sentio.StrategyOrder{
  185. {
  186. Symbol: "320227571",
  187. Action: sentio.OrderBuy,
  188. Ratio: .6,
  189. },
  190. },
  191. },
  192. {
  193. name: "close LONG position",
  194. args: args{
  195. proba: .99,
  196. portfolio: sentio.NewPortfolioStub(
  197. sentio.PositionStub{
  198. Symbol: "320227571",
  199. Size: 10,
  200. },
  201. ),
  202. time: "2024-08-02T15:04:05Z",
  203. },
  204. want: []sentio.StrategyOrder{
  205. {
  206. Symbol: "320227571",
  207. Action: sentio.OrderSell,
  208. Ratio: 1,
  209. },
  210. },
  211. },
  212. {
  213. name: "nothing to close or open",
  214. args: args{
  215. proba: 1,
  216. portfolio: sentio.NewPortfolioStub(),
  217. time: "2024-08-02T15:04:05Z",
  218. },
  219. },
  220. {
  221. name: "nothing to close or open",
  222. args: args{
  223. proba: 1,
  224. portfolio: nil,
  225. time: "2024-08-02T15:04:05Z",
  226. },
  227. },
  228. {
  229. name: "nothing to close or open",
  230. args: args{
  231. proba: 1.002,
  232. portfolio: sentio.NewPortfolioStub(
  233. sentio.PositionStub{
  234. Symbol: "569311092",
  235. Size: 100,
  236. },
  237. sentio.PositionStub{
  238. Symbol: "521525020",
  239. Size: 200,
  240. },
  241. ),
  242. time: "2024-08-02T15:04:05Z",
  243. },
  244. },
  245. {
  246. name: "open extra LONG position",
  247. args: args{
  248. proba: 1.002000001,
  249. portfolio: sentio.NewPortfolioStub(
  250. sentio.PositionStub{
  251. Symbol: "320227571",
  252. Size: 1,
  253. }),
  254. time: "2024-08-02T18:30:00Z",
  255. },
  256. want: []sentio.StrategyOrder{
  257. {
  258. Symbol: "320227571",
  259. Action: sentio.OrderBuy,
  260. Ratio: .3,
  261. },
  262. },
  263. },
  264. }
  265. for _, tt := range tests {
  266. var (
  267. strategy _ib_qqq
  268. dt = sentio.StubClock{}
  269. err error
  270. )
  271. if dt.Clock, err = time.ParseInLocation(time.RFC3339, tt.args.time, time.UTC); err != nil {
  272. t.Errorf("ParseInLocation() = %v", err)
  273. continue
  274. }
  275. strategy = _ib_qqq{clock: dt}
  276. t.Run(tt.name, func(t *testing.T) {
  277. got := strategy.Handle(tt.args.proba, tt.args.portfolio)
  278. if !reflect.DeepEqual(got, tt.want) {
  279. t.Errorf("Handle() got = %v, want %v", got, tt.want)
  280. }
  281. })
  282. }
  283. }