MCPcopy
hub / github.com/microsoft/qlib / test_case2

Method test_case2

tests/ops/test_special_ops.py:23–88  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

21 print(df)
22
23 def test_case2(self):
24 def test_case(instruments, queries, note=None):
25 if note:
26 print(note)
27 print(f"checking {instruments} with queries {queries}")
28 df = D.features(instruments, queries)
29 print(df)
30 return df
31
32 test_case(["SH600519"], ["ChangeInstrument('SH000300', $close)"], "get market index close")
33 test_case(
34 ["SH600519"],
35 ["ChangeInstrument('SH000300', Feature('close')/Ref(Feature('close'),1) -1)"],
36 "get market index return with Feature",
37 )
38 test_case(
39 ["SH600519"],
40 ["ChangeInstrument('SH000300', $close/Ref($close,1) -1)"],
41 "get market index return with expression",
42 )
43 test_case(
44 ["SH600519"],
45 ["($close/Ref($close,1) -1) - ChangeInstrument('SH000300', $close/Ref($close,1) -1)"],
46 "get excess return with expression with beta=1",
47 )
48
49 ret = "Feature('close') / Ref(Feature('close'), 1) - 1"
50 benchmark = "SH000300"
51 n_period = 252
52 marketRet = f"ChangeInstrument('{benchmark}', Feature('close') / Ref(Feature('close'), 1) - 1)"
53 marketVar = f"ChangeInstrument('{benchmark}', Var({marketRet}, {n_period}))"
54 beta = f"Cov({ret}, {marketRet}, {n_period}) / {marketVar}"
55 excess_return = f"{ret} - {beta}*({marketRet})"
56 fields = [
57 "Feature('close')",
58 f"ChangeInstrument('{benchmark}', Feature('close'))",
59 ret,
60 marketRet,
61 beta,
62 excess_return,
63 ]
64 test_case(["SH600519"], fields[5:], "get market beta and excess_return with estimated beta")
65
66 instrument = "sh600519"
67 ret = Feature("close") / Ref(Feature("close"), 1) - 1
68 benchmark = "sh000300"
69 n_period = 252
70 marketRet = ChangeInstrument(benchmark, Feature("close") / Ref(Feature("close"), 1) - 1)
71 marketVar = ChangeInstrument(benchmark, Var(marketRet, n_period))
72 beta = Cov(ret, marketRet, n_period) / marketVar
73 fields = [
74 Feature("close"),
75 ChangeInstrument(benchmark, Feature("close")),
76 ret,
77 marketRet,
78 beta,
79 ret - beta * marketRet,
80 ]

Callers

nothing calls this directly

Calls 7

RefClass · 0.90
ChangeInstrumentClass · 0.90
VarClass · 0.90
CovClass · 0.90
QlibDataLoaderClass · 0.90
FeatureClass · 0.85
loadMethod · 0.45

Tested by

no test coverage detected