plotguy 2.3.3__tar.gz → 2.3.4__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: plotguy
3
- Version: 2.3.3
3
+ Version: 2.3.4
4
4
  Summary: Plotguy
5
5
  Home-page: https://pypi.org/project/plotguy/
6
6
  Author: Plotguy Team
@@ -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
- df_daily = df
162
-
163
- ########### price result ###############
164
- df_daily['running_max'] = df_daily['price'].cummax()
165
- df_daily['dd_dollar'] = df_daily['running_max'] - df_daily['price']
166
- df_daily['dd_pct'] = df_daily['dd_dollar'] / df_daily['running_max'] * 100
167
- price_mdd_dollar = df_daily['dd_dollar'].max()
168
- price_mdd_pct = df_daily['dd_pct'].max()
169
-
170
- price_pct_series = df_daily['price'].pct_change().dropna()
171
- price_net_profit = df_daily.at[df_daily.index[-1], 'price'] - df_daily.at[df_daily.index[0], 'price']
172
- holding_period_day = (df_daily.index[-1].date() - df_daily.index[0].date()).days
173
-
174
- price_return_on_capital = price_net_profit / df_daily.at[df_daily.index[0], 'price']
175
- price_annualized_return = (np.sign(1 + price_return_on_capital) * np.abs(1 + price_return_on_capital)) ** (
176
- 365 / holding_period_day) - 1
177
- price_annualized_std = price_pct_series.std() * math.sqrt(365)
178
- price_annualized_sr = (
179
- price_annualized_return - risk_free_rate / 100) / price_annualized_std if price_annualized_std != 0 else 0
180
- price_net_profit_to_mdd = price_net_profit / price_mdd_dollar if price_mdd_dollar != 0 else 0
181
-
182
- price_return_on_capital = round(100 * price_return_on_capital, 2)
183
- price_annualized_return = round(100 * price_annualized_return, 2)
184
- price_annualized_std = round(100 * price_annualized_std, 2)
185
- price_annualized_sr = round(price_annualized_sr, 2)
186
- price_net_profit_to_mdd = round(100 * price_net_profit_to_mdd, 2)
187
-
188
- last_action = df['action'].iloc[-1]
189
- if last_action == 'open':
190
- df = df.drop(df.index[-1])
191
- df_count = df[(df['trd_side'] == 'BUY') | (df['trd_side'] == 'SELL_SHORT')].copy()
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
@@ -210,8 +222,12 @@ def save_backtest_result(df, para_combination_item):
210
222
  cov_count = 0
211
223
  total_commission = 0
212
224
 
213
- start_year = df.index[0].year
214
- end_year = df.index[-1].year
225
+ if len(df) > 0:
226
+ start_year = df.index[0].year
227
+ end_year = df.index[-1].year
228
+ else:
229
+ start_year = int(para_combination['start_date'][0:4])
230
+ end_year = int(para_combination['end_date'][0:4])
215
231
 
216
232
  yearly_stats_string = ''
217
233
  for year in range(start_year, end_year + 1):
@@ -321,17 +337,18 @@ def save_backtest_result(df, para_combination_item):
321
337
 
322
338
  correlation = df['equity_return'].corr(df['price_return'])
323
339
 
324
- if long_short == 'short':
325
- return_on_capital_diff = equity_annualized_return - (-1) * price_annualized_return
326
- correlation *= -1
327
- else:
328
- ### consider both long and long_short ###
329
- return_on_capital_diff = equity_annualized_return - price_annualized_return
340
+ if long_short == 'short':
341
+ return_on_capital_diff = equity_annualized_return - (-1) * price_annualized_return
342
+ correlation *= -1
343
+ else:
344
+ ### consider both long and long_short ###
345
+ return_on_capital_diff = equity_annualized_return - price_annualized_return
346
+
347
+ if intraday:
348
+ df = df[['price', 'equity_value']]
349
+ elif not intraday:
350
+ df = df[['price', 'action', 'trd_side', 'equity_value']]
330
351
 
331
- if intraday:
332
- df = df[['price', 'equity_value']]
333
- elif not intraday:
334
- df = df[['price', 'action', 'trd_side', 'equity_value']]
335
352
  df.to_parquet(eqiuty_curve_save_path)
336
353
  print('backtest is runnung...', eqiuty_curve_save_path)
337
354
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: plotguy
3
- Version: 2.3.3
3
+ Version: 2.3.4
4
4
  Summary: Plotguy
5
5
  Home-page: https://pypi.org/project/plotguy/
6
6
  Author: Plotguy Team
@@ -5,7 +5,7 @@ with open("README.md", "r") as fh:
5
5
 
6
6
  setuptools.setup(
7
7
  name="plotguy",
8
- version="2.3.3",
8
+ version="2.3.4",
9
9
  author="Plotguy Team",
10
10
  author_email="plotguy.info@gmail.com",
11
11
  description="Plotguy",
File without changes
File without changes
File without changes
File without changes
File without changes