1
+ __author__ = 'saeedamen' # Saeed Amen
2
+
1
3
#
2
4
# Copyright 2016-2020 Cuemacro - https://www.cuemacro.com / @cuemacro
3
5
#
4
- # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
5
- # License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ # Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
+ # use this file except in compliance with the License. You may obtain a copy of
8
+ # the License at http://www.apache.org/licenses/LICENSE-2.0
6
9
#
7
- # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
8
- # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ # Unless required by applicable law or agreed to in writing, software distributed
11
+ # under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12
+ # CONDITIONS OF ANY KIND, either express or implied.
9
13
#
10
- # See the License for the specific language governing permissions and limitations under the License.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
11
16
#
12
17
13
-
14
18
import numpy as np
15
19
import pandas as pd
16
20
from finmarketpy .util .marketconstants import MarketConstants
20
24
from finmarketpy .backtest .backtestengine import TradingModel
21
25
from chartpy import Chart , Style , ChartConstants
22
26
23
- class BacktestComparison (object ):
24
27
28
+ class BacktestComparison (object ):
25
29
DEFAULT_PLOT_ENGINE = ChartConstants ().chartfactory_default_engine
26
30
SCALE_FACTOR = ChartConstants ().chartfactory_scale_factor
27
31
CHART_SOURCE = ChartConstants ().chartfactory_source
@@ -34,16 +38,19 @@ def __init__(self, models, ref_index=0,
34
38
:param models: iterable of TradingModel instances.
35
39
:param ref_index: index of the reference model in the list (for difference).
36
40
"""
37
- if hasattr (models , "__iter__" ) and all ([isinstance (x , TradingModel ) for x in models ]):
41
+ if hasattr (models , "__iter__" ) and all (
42
+ [isinstance (x , TradingModel ) for x in models ]):
38
43
self .models = models
39
44
self .ref_index = ref_index
40
45
else :
41
- raise AttributeError ("Models need to be an iterable of TradingModel instances." )
46
+ raise AttributeError (
47
+ "Models need to be an iterable of TradingModel instances." )
42
48
43
49
self .labels = labels
44
50
45
51
def plot_pnl (self , diff = True , silent_plot = False , reduce_plot = True ):
46
- style = self .models [self .ref_index ]._create_style ("" , "Strategy PnL" , reduce_plot = reduce_plot )
52
+ style = self .models [self .ref_index ]._create_style ("" , "Strategy PnL" ,
53
+ reduce_plot = reduce_plot )
47
54
48
55
models = self .models
49
56
ref = self .ref_index
@@ -57,14 +64,16 @@ def plot_pnl(self, diff=True, silent_plot=False, reduce_plot=True):
57
64
if self .labels is not None :
58
65
df .columns = self .labels
59
66
60
- chart = Chart (df , engine = self .DEFAULT_PLOT_ENGINE , chart_type = 'line' , style = style )
67
+ chart = Chart (df , engine = self .DEFAULT_PLOT_ENGINE , chart_type = 'line' ,
68
+ style = style )
61
69
if not silent_plot :
62
70
chart .plot ()
63
71
64
72
return chart
65
73
66
74
def plot_sharpe (self , silent_plot = False , reduce_plot = True ):
67
- style = self .models [self .ref_index ]._create_style ("" , "Sharpe Curve" , reduce_plot = reduce_plot )
75
+ style = self .models [self .ref_index ]._create_style ("" , "Sharpe Curve" ,
76
+ reduce_plot = reduce_plot )
68
77
69
78
models = self .models
70
79
ref = self .ref_index
@@ -76,18 +85,23 @@ def plot_sharpe(self, silent_plot=False, reduce_plot=True):
76
85
77
86
df = pd .concat (annualized_sharpe , axis = 1 )
78
87
79
- chart = Chart (df , engine = self .DEFAULT_PLOT_ENGINE , chart_type = 'bar' , style = style )
88
+ chart = Chart (df , engine = self .DEFAULT_PLOT_ENGINE , chart_type = 'bar' ,
89
+ style = style )
80
90
if not silent_plot :
81
91
chart .plot ()
82
92
return chart
83
93
84
- def plot_strategy_trade_notional (self , diff = True , silent_plot = False , reduce_plot = True ):
85
- style = self .models [self .ref_index ]._create_style ("" , "Trades (Scaled by Notional)" , reduce_plot = reduce_plot )
94
+ def plot_strategy_trade_notional (self , diff = True , silent_plot = False ,
95
+ reduce_plot = True ):
96
+ style = self .models [self .ref_index ]._create_style ("" ,
97
+ "Trades (Scaled by Notional)" ,
98
+ reduce_plot = reduce_plot )
86
99
87
100
models = self .models
88
101
ref = self .ref_index
89
102
90
- strategy_trade_notional = [model ._strategy_trade_notional for model in models ]
103
+ strategy_trade_notional = [model ._strategy_trade_notional for model in
104
+ models ]
91
105
92
106
df = pd .concat (strategy_trade_notional , axis = 1 )
93
107
@@ -96,7 +110,8 @@ def plot_strategy_trade_notional(self, diff=True, silent_plot=False, reduce_plot
96
110
if self .labels is not None :
97
111
df .columns = self .labels
98
112
99
- chart = Chart (df , engine = self .DEFAULT_PLOT_ENGINE , chart_type = 'bar' , style = style )
113
+ chart = Chart (df , engine = self .DEFAULT_PLOT_ENGINE , chart_type = 'bar' ,
114
+ style = style )
100
115
if not silent_plot :
101
116
chart .plot ()
102
117
0 commit comments