forked from robcarver17/pysystemtrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport_system_dynamic_optimised.py
90 lines (71 loc) · 2.55 KB
/
report_system_dynamic_optimised.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import datetime
import pandas as pd
from syscore.objects import header, table, body_text
from sysdata.data_blob import dataBlob
from sysobjects.production.backtest_storage import interactiveBacktest
from sysproduction.strategy_code.report_system_classic import (
report_system_classic_no_header_or_footer,
)
from sysproduction.data.positions import dataOptimalPositions
def report_system_dynamic(data: dataBlob, backtest: interactiveBacktest):
format_output = []
strategy_name = backtest.strategy_name
timestamp = backtest.timestamp
optimal_positions_df = get_optimal_positions_table_as_df(
data=data, strategy_name=backtest.strategy_name
)
optimal_positions_table = table("Optimal positions", optimal_positions_df)
format_output.append(optimal_positions_table)
report_header = header(
"Strategy report for %s backtest timestamp %s produced at %s"
% (strategy_name, timestamp, str(datetime.datetime.now()))
)
format_output.append(report_header)
format_output = report_system_classic_no_header_or_footer(
data, backtest=backtest, format_output=format_output
)
format_output.append(body_text("End of report for %s" % strategy_name))
return format_output
def get_optimal_positions_table_as_df(
data: dataBlob, strategy_name: str
) -> pd.DataFrame:
data_optimal_positions = dataOptimalPositions(data)
list_of_positions = (
data_optimal_positions.get_list_of_current_optimal_positions_for_strategy_name(
strategy_name
)
)
as_verbose_pd = list_of_positions.as_verbose_pd()
subset_of_pd = as_verbose_pd[
[
"dont_trade",
"reduce_only",
"optimal_position",
"position_limit_contracts",
"previous_position",
"weight_per_contract",
"position_limit_weight",
"optimum_weight",
"start_weight",
"maximum_weight",
"minimum_weight",
"previous_weight",
"optimised_weight",
"optimised_position",
]
]
things_to_round = [
"optimal_position",
"weight_per_contract",
"position_limit_weight",
"optimum_weight",
"start_weight",
"maximum_weight",
"minimum_weight",
"previous_weight",
"optimised_weight",
]
for column_name in things_to_round:
subset_of_pd[column_name] = subset_of_pd[column_name].round(2)
subset_of_pd = subset_of_pd.sort_values("optimum_weight")
return subset_of_pd