plotguy 2.3.3__tar.gz → 2.3.5__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {plotguy-2.3.3 → plotguy-2.3.5}/PKG-INFO +1 -1
- {plotguy-2.3.3 → plotguy-2.3.5}/plotguy/__init__.py +71 -52
- {plotguy-2.3.3 → plotguy-2.3.5}/plotguy.egg-info/PKG-INFO +1 -1
- {plotguy-2.3.3 → plotguy-2.3.5}/setup.py +1 -1
- {plotguy-2.3.3 → plotguy-2.3.5}/README.md +0 -0
- {plotguy-2.3.3 → plotguy-2.3.5}/plotguy/aggregate.py +0 -0
- {plotguy-2.3.3 → plotguy-2.3.5}/plotguy/components.py +0 -0
- {plotguy-2.3.3 → plotguy-2.3.5}/plotguy/equity_curves.py +0 -0
- {plotguy-2.3.3 → plotguy-2.3.5}/plotguy/signals.py +0 -0
- {plotguy-2.3.3 → plotguy-2.3.5}/plotguy.egg-info/SOURCES.txt +0 -0
- {plotguy-2.3.3 → plotguy-2.3.5}/plotguy.egg-info/dependency_links.txt +0 -0
- {plotguy-2.3.3 → plotguy-2.3.5}/plotguy.egg-info/requires.txt +0 -0
- {plotguy-2.3.3 → plotguy-2.3.5}/plotguy.egg-info/top_level.txt +0 -0
- {plotguy-2.3.3 → plotguy-2.3.5}/setup.cfg +0 -0
|
@@ -147,48 +147,60 @@ def save_backtest_result(df, para_combination_item):
|
|
|
147
147
|
|
|
148
148
|
eqiuty_curve_save_path = os.path.join('', equity_curve_folder, f'{ref_code}.parquet')
|
|
149
149
|
##############################################################
|
|
150
|
+
if len(df) == 0:
|
|
151
|
+
price_net_profit = 0
|
|
152
|
+
price_return_on_capital = 0
|
|
153
|
+
price_annualized_return = 0
|
|
154
|
+
price_annualized_std = 0
|
|
155
|
+
price_annualized_sr = 0
|
|
156
|
+
price_net_profit_to_mdd = 0
|
|
157
|
+
price_mdd_dollar = 0
|
|
158
|
+
price_mdd_pct = 0
|
|
159
|
+
return_on_capital_diff = 0
|
|
160
|
+
df_count = ''
|
|
150
161
|
|
|
151
|
-
if default_market_price == 'close':
|
|
152
|
-
agg_price = 'last'
|
|
153
|
-
elif default_market_price == 'open':
|
|
154
|
-
agg_price = 'first'
|
|
155
|
-
### for intraday ###
|
|
156
|
-
if intraday:
|
|
157
|
-
df_daily = df.resample('D').agg({'price': agg_price})
|
|
158
|
-
#df_daily = df.resample('D').agg({'price': agg_price, 'equity_value': agg_price})
|
|
159
|
-
df_daily = df_daily[df_daily['price'].notna()]
|
|
160
162
|
else:
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
163
|
+
if default_market_price == 'close':
|
|
164
|
+
agg_price = 'last'
|
|
165
|
+
elif default_market_price == 'open':
|
|
166
|
+
agg_price = 'first'
|
|
167
|
+
### for intraday ###
|
|
168
|
+
if intraday:
|
|
169
|
+
df_daily = df.resample('D').agg({'price': agg_price})
|
|
170
|
+
#df_daily = df.resample('D').agg({'price': agg_price, 'equity_value': agg_price})
|
|
171
|
+
df_daily = df_daily[df_daily['price'].notna()]
|
|
172
|
+
else:
|
|
173
|
+
df_daily = df
|
|
174
|
+
|
|
175
|
+
########### price result ###############
|
|
176
|
+
df_daily['running_max'] = df_daily['price'].cummax()
|
|
177
|
+
df_daily['dd_dollar'] = df_daily['running_max'] - df_daily['price']
|
|
178
|
+
df_daily['dd_pct'] = df_daily['dd_dollar'] / df_daily['running_max'] * 100
|
|
179
|
+
price_mdd_dollar = df_daily['dd_dollar'].max()
|
|
180
|
+
price_mdd_pct = df_daily['dd_pct'].max()
|
|
181
|
+
|
|
182
|
+
price_pct_series = df_daily['price'].pct_change().dropna()
|
|
183
|
+
price_net_profit = df_daily.at[df_daily.index[-1], 'price'] - df_daily.at[df_daily.index[0], 'price']
|
|
184
|
+
holding_period_day = (df_daily.index[-1].date() - df_daily.index[0].date()).days
|
|
185
|
+
|
|
186
|
+
price_return_on_capital = price_net_profit / df_daily.at[df_daily.index[0], 'price']
|
|
187
|
+
price_annualized_return = (np.sign(1 + price_return_on_capital) * np.abs(1 + price_return_on_capital)) ** (
|
|
188
|
+
365 / holding_period_day) - 1
|
|
189
|
+
price_annualized_std = price_pct_series.std() * math.sqrt(365)
|
|
190
|
+
price_annualized_sr = (
|
|
191
|
+
price_annualized_return - risk_free_rate / 100) / price_annualized_std if price_annualized_std != 0 else 0
|
|
192
|
+
price_net_profit_to_mdd = price_net_profit / price_mdd_dollar if price_mdd_dollar != 0 else 0
|
|
193
|
+
|
|
194
|
+
price_return_on_capital = round(100 * price_return_on_capital, 2)
|
|
195
|
+
price_annualized_return = round(100 * price_annualized_return, 2)
|
|
196
|
+
price_annualized_std = round(100 * price_annualized_std, 2)
|
|
197
|
+
price_annualized_sr = round(price_annualized_sr, 2)
|
|
198
|
+
price_net_profit_to_mdd = round(100 * price_net_profit_to_mdd, 2)
|
|
199
|
+
|
|
200
|
+
last_action = df['action'].iloc[-1]
|
|
201
|
+
if last_action == 'open':
|
|
202
|
+
df = df.drop(df.index[-1])
|
|
203
|
+
df_count = df[(df['trd_side'] == 'BUY') | (df['trd_side'] == 'SELL_SHORT')].copy()
|
|
192
204
|
|
|
193
205
|
if len(df_count) == 0:
|
|
194
206
|
num_of_trade = 0
|
|
@@ -201,6 +213,8 @@ def save_backtest_result(df, para_combination_item):
|
|
|
201
213
|
equity_mdd_dollar = 0
|
|
202
214
|
equity_mdd_pct = 0
|
|
203
215
|
|
|
216
|
+
return_on_capital_diff = 0
|
|
217
|
+
|
|
204
218
|
num_of_win = 0
|
|
205
219
|
num_of_trade = 0
|
|
206
220
|
win_rate = 0
|
|
@@ -210,8 +224,12 @@ def save_backtest_result(df, para_combination_item):
|
|
|
210
224
|
cov_count = 0
|
|
211
225
|
total_commission = 0
|
|
212
226
|
|
|
213
|
-
|
|
214
|
-
|
|
227
|
+
if len(df) > 0:
|
|
228
|
+
start_year = df.index[0].year
|
|
229
|
+
end_year = df.index[-1].year
|
|
230
|
+
else:
|
|
231
|
+
start_year = int(para_combination['start_date'][0:4])
|
|
232
|
+
end_year = int(para_combination['end_date'][0:4])
|
|
215
233
|
|
|
216
234
|
yearly_stats_string = ''
|
|
217
235
|
for year in range(start_year, end_year + 1):
|
|
@@ -321,17 +339,18 @@ def save_backtest_result(df, para_combination_item):
|
|
|
321
339
|
|
|
322
340
|
correlation = df['equity_return'].corr(df['price_return'])
|
|
323
341
|
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
342
|
+
if long_short == 'short':
|
|
343
|
+
return_on_capital_diff = equity_annualized_return - (-1) * price_annualized_return
|
|
344
|
+
correlation *= -1
|
|
345
|
+
else:
|
|
346
|
+
### consider both long and long_short ###
|
|
347
|
+
return_on_capital_diff = equity_annualized_return - price_annualized_return
|
|
348
|
+
|
|
349
|
+
if intraday:
|
|
350
|
+
df = df[['price', 'equity_value']]
|
|
351
|
+
elif not intraday:
|
|
352
|
+
df = df[['price', 'action', 'trd_side', 'equity_value']]
|
|
330
353
|
|
|
331
|
-
if intraday:
|
|
332
|
-
df = df[['price', 'equity_value']]
|
|
333
|
-
elif not intraday:
|
|
334
|
-
df = df[['price', 'action', 'trd_side', 'equity_value']]
|
|
335
354
|
df.to_parquet(eqiuty_curve_save_path)
|
|
336
355
|
print('backtest is runnung...', eqiuty_curve_save_path)
|
|
337
356
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|