Skip to content

Instantly share code, notes, and snippets.

@ExaByt3s
Last active December 13, 2022 21:33
Show Gist options
  • Save ExaByt3s/c9a75eedd6aed16f28a3903e31d252bb to your computer and use it in GitHub Desktop.
Save ExaByt3s/c9a75eedd6aed16f28a3903e31d252bb to your computer and use it in GitHub Desktop.
metodología de backtesting
import json
import os
from functools import reduce
from pathlib import Path
from typing_extensions import Self
import copy
import joblib
import logging
from sqlalchemy import null

from multiprocessing import Pool, cpu_count
from pathlib import Path
from tabulate import tabulate
from IPython.display import HTML, display
import nest_asyncio
nest_asyncio.apply()

log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
ft_path = ""

def getFiles():
    path =  os.path.split(os.environ['VIRTUAL_ENV'])[0]
    os.chdir( path )
    return os.getcwd()
getFiles()




ft_path =  getFiles()
from user_data.strategies.x7aBackTesting import x7aBackTesting
monthly_20220701 = f"{ft_path}" + "/configs/backtesting/config.small.x7a.dev.json"
monthly_20220701 = Configuration.from_files(files=[monthly_20220701])

backtest = x7aBackTesting(monthly_20220701)
backtest.setup_config("20220901-20221001")
strategy = monthly_20220701['strategy']
# ok primero  realizamos un backtest para  recopilar los datos necesarios para el análisis posteriores

resuls, canldes = backtest.backtest_candles_range()
backtest.show_console_results()


"""
ok ya tenemos acceso a los datos recogidos por el backtesting 
y una vista general, vamos a procesar  2 monedas del resultado

=== BACKTESTING REPORT ==========================================================================================================================
|          Pair |   Entries |   Avg Profit % |   Cum Profit % |   Tot Profit USDT |   Tot Profit % |     Avg Duration |   Win  Draw  Loss  Win% |
|---------------+-----------+----------------+----------------+-------------------+----------------+------------------+-------------------------|
|     ATOM/USDT |         4 |           1.50 |           6.01 |             2.154 |           0.22 | 3 days, 17:54:00 |     3     0     1  75.0 |
|      ENS/USDT |         4 |          -1.26 |          -5.04 |            -1.771 |          -0.18 |          8:06:00 |     1     0     3  25.0 |

"""

analysed_trades_dict = {
    strategy: {}
}
coin_one = 'ATOM/USDT'
coin_two = 'ENS/USDT'
analysed_trades_dict[strategy][coin_one] = backtest.process_pair_signals(strategy, coin_one)


# """
# ok ahora que tenemos los datos resultados de los procesados  vamos a agrupar  las señales de compra y venta y  de esa forma
# tener una idea  cuales  fueron las entrada ganadoras y las perdeador
# """
# backtest.analyze_trade_groups(analysed_trades_dict,coin_one)

# backtest.process_pair_data(monthly_20220701['strategy'], 'ATOM/USDT')
# pairs = resuls['results']['pair']





=== BACKTESTING REPORT ==========================================================================================================================
|          Pair |   Entries |   Avg Profit % |   Cum Profit % |   Tot Profit USDT |   Tot Profit % |     Avg Duration |   Win  Draw  Loss  Win% |
|---------------+-----------+----------------+----------------+-------------------+----------------+------------------+-------------------------|
|     ATOM/USDT |         4 |           1.50 |           6.01 |             2.154 |           0.22 | 3 days, 17:54:00 |     3     0     1  75.0 |
| FOOTBALL/USDT |         1 |           4.47 |           4.47 |             1.343 |           0.13 |          1:55:00 |     1     0     0   100 |
|     COMP/USDT |         2 |           0.74 |           1.48 |             0.540 |           0.05 |   1 day, 4:52:00 |     1     0     1  50.0 |
|      UNI/USDT |         3 |           0.64 |           1.91 |             0.474 |           0.05 |         12:15:00 |     1     0     2  33.3 |
|      SOL/USDT |         2 |           0.19 |           0.39 |             0.124 |           0.01 |         11:48:00 |     1     0     1  50.0 |
|      ETC/USDT |         0 |           0.00 |           0.00 |             0.000 |           0.00 |             0:00 |     0     0     0     0 |
|     AAVE/USDT |         4 |          -0.63 |          -2.51 |            -0.715 |          -0.07 |          7:09:00 |     1     0     3  25.0 |
|     UNFI/USDT |         3 |          -0.77 |          -2.31 |            -0.841 |          -0.08 |          6:45:00 |     1     0     2  33.3 |
|     FLOW/USDT |         4 |          -0.89 |          -3.55 |            -1.292 |          -0.13 |          8:22:00 |     1     0     3  25.0 |
|      ENS/USDT |         4 |          -1.26 |          -5.04 |            -1.771 |          -0.18 |          8:06:00 |     1     0     3  25.0 |
|         TOTAL |        27 |           0.03 |           0.84 |             0.014 |           0.00 |         22:01:00 |    11     0    16  40.7 |
"""
ok ahora que tenemos los datos resultados de los procesados  vamos a agrupar  las señales de compra y venta y  de esa forma
tener una idea  cuales  fueron las entrada ganadoras y las perdeador
"""

backtest.analyze_trade_groups(analysed_trades_dict,coin_one)
+-----------+----------------+--------------------------------------+------------+------------------+---------------------+-------------------+---------------------+-------------------+--------------------+
| pair      |   enter_reason | exit_reason                          |   num_buys |   profit_abs_sum |   profit_abs_median |   profit_abs_mean |   median_profit_pct |   mean_profit_pct |   total_profit_pct |
|-----------+----------------+--------------------------------------+------------+------------------+---------------------+-------------------+---------------------+-------------------+--------------------|
| ATOM/USDT |             1  | exit_normal_bull_stoploss_doom ( 1 ) |          1 |         -1.47581 |           -1.47581  |         -1.47581  |            -4.07598 |          -4.07598 |           -4.07598 |
| ATOM/USDT |             1  | sell_profit_q_3 ( 1 )                |          1 |          1.69323 |            1.69323  |          1.69323  |             4.69712 |           4.69712 |            4.69712 |
| ATOM/USDT |             1  | sell_profit_q_2 ( 1 )                |          2 |          1.93694 |            0.968471 |          0.968471 |             5.39141 |           2.6957  |            2.6957  |
+-----------+----------------+--------------------------------------+------------+------------------+---------------------+-------------------+---------------------+-------------------+--------------------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment