def single_instrument_single_system(strategy, period, parameters, price_data):

    parameters = list(filter(None, parameters))
    initial_capital = 1000000
    trading_cost = 0.0000

    method_name = strategy
    method = eval(method_name)

    strategy_func = getattr(method, strategy)

    if period is None: period = ""

    trades, price_signal = strategy_func(price_data,
                                         *parameters,
                                         period=period)

    trade_summary_data = trade_summary.trade_summary(trades)

    trades_table = trade_summary.trade_data_table(trades)

    pnl_series, _ = pg.pnl_timeseries_monthly_rebalance(
        trades, price_data, 1000000)

    print(f"{strategy} ok!")

    return pnl_series, trade_summary_data.iloc[0]
Exemplo n.º 2
0
def rolling_12m_trade_summary(trades, price_data):
    price_monthly = tm.price_series_to_month(price_data)
    trade_summary_data = []

    for i in range(len(price_monthly) - 12):

        start_date = price_monthly.index[i]
        start_price = price_monthly["Close"].iloc[i]
        end_date = price_monthly.index[i + 12]
        end_price = price_monthly["Close"].iloc[i + 12]

        selected_trades = list(
            filter(
                lambda x:
                (x.exit_date > start_date and x.entry_date < end_date),
                trades))
        if len(selected_trades) == 0:
            trade_summary_data.append([0] * 14)
            continue
        if selected_trades[0].entry_date < start_date:
            trade_to_replace = selected_trades[0]
            selected_trades[0] = Trades(
                start_date, start_price, trade_to_replace.exit_date,
                trade_to_replace.exit_price, trade_to_replace.trade_position,
                price_data.loc[(start_date <= price_data.index) & (
                    price_data.index <= trade_to_replace.exit_date)])
        if selected_trades[-1].exit_date > end_date:
            trade_to_replace = selected_trades[-1]
            selected_trades[-1] = Trades(
                trade_to_replace.entry_date, trade_to_replace.entry_price,
                end_date, end_price, trade_to_replace.trade_position,
                price_data.loc[
                    (trade_to_replace.entry_date <= price_data.index)
                    & (price_data.index <= end_date)])

        td_summary = trade_summary.trade_summary(selected_trades).iloc[0]

        trade_summary_data.append(td_summary)

    trade_summary_data = pd.DataFrame(trade_summary_data)
    trade_summary_data.columns = [
        "Total Trades", "Profit Factor", "Total Profit", "Average Profit",
        "Max Profit", "Min Profit", "Average Duration", "Hit Ratio",
        "Profitability", "Max DD in Trade", "Max Profit in Trade",
        "Max Loss in Trade", "Max Recovery in Trade",
        "Max DD Duration in Trade"
    ]
    trade_summary_data["Months"] = price_monthly.index[12:].strftime("%b-%Y")
    trade_summary_data.set_index("Months", inplace=True)

    return trade_summary_data
def walkforward_trade_summary(trades, price_data):
    price_annual = tm.price_series_to_annual(price_data)
    trade_summary_data = []

    for i in range(len(price_annual) - 1):

        i = i + 1

        end_date = price_annual.index[i]
        end_price = price_annual["Close"].iloc[i]

        selected_trades = list(filter(lambda x: (x.entry_date < end_date), trades))
        if len(selected_trades) == 0:
            trade_summary_data.append([0] * 5)
            continue

        if selected_trades[-1].exit_date > end_date:
            trade_to_replace = selected_trades[-1]
            selected_trades[-1] = Trades(trade_to_replace.entry_date, trade_to_replace.entry_price, end_date, end_price
                                         , trade_to_replace.trade_position,
                                         price_data.loc[(trade_to_replace.entry_date <= price_data.index) &
                                                        (price_data.index <= end_date)])

        td_summary = trade_summary.trade_summary(selected_trades).iloc[0]

        trade_summary_data.append(td_summary)

    trade_summary_data = pd.DataFrame(trade_summary_data)
    trade_summary_data.columns = ["Total Trades", "Profit Factor", "Total Profit", "Average Profit", "Max Profit",
                                  "Min Profit", "Average Duration", "Hit Ratio", "Profitability", "Max DD in Trade",
                                  "Max Profit in Trade", "Max Loss in Trade", "Max Recovery in Trade",
                                  "Max DD Duration in Trade"]
    trade_summary_data["Years"] = price_annual.index[1:].year
    trade_summary_data.set_index("Years", inplace=True)

    return trade_summary_data
Exemplo n.º 4
0
    DD_distribution = {}
    pnl_series_monthly = {}
    pnl_series_annual = {}
    pnl_series_monthly_rolling_12m = {}

    period_1 = 20

    for symbol in symbols:

        print(f"Trades for {symbol}")

        trades, _ = system.wma20_macd_system(price_data[symbol],
                                             period_1,
                                             period="")

        trade_summary_data_full = trade_summary.trade_summary(trades)

        trades_table[symbol] = trade_summary.trade_data_table(trades)

        trade_summary_data.loc[symbol] = trade_summary_data_full.loc[
            "ALL Trades"]

        #pnl_series,DD_distribution = pg.pnl_timeseries_same_value_trade(trades, price_data, 1000000)

        pnl_series_full_data, DD_distribution = pg.pnl_timeseries_monthly_rebalance(
            trades, price_data[symbol], 1000000)

        pnl_series[symbol] = pnl_series_full_data["%PNL"]

        pnl_series_monthly[symbol] = pd.DataFrame(columns=["PNL"])
        pnl_series_annual[symbol] = pd.DataFrame(columns=["PNL"])
Exemplo n.º 5
0
    data_path = Path().absolute().joinpath("Data/Price_Data")
    result_path = Path().absolute().joinpath("results/periodic_reversal")
    symbols = ["NZ1 Index"]

    price_data = import_price_data_from_csv_files(data_path, symbols)

    initial_capital = 1000000
    trading_cost = 0
    PNL_daily = pd.DataFrame()
    PNL_monthly = pd.DataFrame()

    for symbol in symbols:
        for period in range(2, 51, 2):
            trades, _ = periodic_reversal_system.periodic_reversal_system(
                price_data[symbol], period)
            trade_summary_data = trade_summary.trade_summary(trades)
            trades_table = trade_summary.trade_data_table(trades)
            pnl_series, _ = pg.pnl_timeseries_monthly_rebalance(
                trades, price_data[symbol], 1000000)

            PNL_daily[str(period) + "period"] = pnl_series["%PNL"]
            PNL_monthly[str(period) +
                        "period"] = pnl_series["%PNL"].resample("M").sum()

    PNL_daily.name = "PNL_Daily"
    PNL_monthly.name = "PNL_Monthly"

    to_be_saved_as_csv = [PNL_daily, PNL_monthly]

    excel_creation(to_be_saved_as_csv, result_path, "Nifty")