{ "cells": [ { "cell_type": "markdown", "id": "2b725ab1", "metadata": {}, "source": [ "
" ] }, { "cell_type": "markdown", "id": "c686b10c", "metadata": {}, "source": [ "This code calculates the expectancy ratio of a series of trades. The expectancy ratio measures the average expected return per trade by considering the win rate, loss rate, and average profit/loss of trades. It is useful in financial trading to evaluate the performance of a trading strategy. The input is a DataFrame of trades with profit or loss values. The output is a single expectancy ratio value." ] }, { "cell_type": "code", "execution_count": null, "id": "652ddb60", "metadata": { "lines_to_next_cell": 1 }, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np" ] }, { "cell_type": "markdown", "id": "f54264f9", "metadata": {}, "source": [ "Define a function to calculate the expectancy ratio of trades." ] }, { "cell_type": "code", "execution_count": null, "id": "d056583b", "metadata": { "lines_to_next_cell": 1 }, "outputs": [], "source": [ "def calculate_expectancy_ratio(trades):\n", " \"\"\"Calculate the expectancy ratio of trades.\n", " \n", " This function computes the average expected return for a series of trades \n", " by considering their win rate, loss rate, and average profit/loss.\n", " \n", " Parameters\n", " ----------\n", " trades : pd.DataFrame\n", " DataFrame containing trade information with a 'Profit' column.\n", " \n", " Returns\n", " -------\n", " expectancy_ratio : float\n", " The calculated expectancy ratio.\n", " \"\"\"\n", " \n", " # Calculate the number of trades\n", " num_trades = len(trades)\n", " \n", " # Separate winning and losing trades\n", " winners = trades[trades['Profit'] > 0]\n", " losers = trades[trades['Profit'] <= 0]\n", " \n", " # Calculate win rate and loss rate\n", " win_rate = len(winners) / num_trades\n", " loss_rate = len(losers) / num_trades\n", " \n", " # Calculate average profit for winning trades and average loss for losing trades\n", " avg_win = winners['Profit'].mean()\n", " avg_loss = losers['Profit'].mean()\n", " \n", " # Compute the expectancy ratio\n", " expectancy_ratio = (win_rate * avg_win) + (loss_rate * avg_loss)\n", " \n", " return expectancy_ratio" ] }, { "cell_type": "markdown", "id": "2611f9e5", "metadata": {}, "source": [ "Create a dictionary with trade data including trade numbers and corresponding profits/losses." ] }, { "cell_type": "code", "execution_count": null, "id": "7c26a120", "metadata": {}, "outputs": [], "source": [ "trade_data = {\n", " 'Trade': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],\n", " 'Profit': [100, -50, 200, -100, 300, -150, 400, -200, 500, -250]\n", "}" ] }, { "cell_type": "markdown", "id": "b84b5061", "metadata": {}, "source": [ "Convert the trade data dictionary into a pandas DataFrame." ] }, { "cell_type": "code", "execution_count": null, "id": "3f057971", "metadata": {}, "outputs": [], "source": [ "trades = pd.DataFrame(trade_data)" ] }, { "cell_type": "markdown", "id": "4ce371e5", "metadata": {}, "source": [ "Calculate the expectancy ratio using the defined function and print the result." ] }, { "cell_type": "code", "execution_count": null, "id": "4086f225", "metadata": {}, "outputs": [], "source": [ "expectancy_ratio = calculate_expectancy_ratio(trades)\n", "print(f\"Expectancy Ratio: {expectancy_ratio}\")" ] }, { "cell_type": "markdown", "id": "49299264", "metadata": {}, "source": [ "PyQuant News is where finance practitioners level up with Python for quant finance, algorithmic trading, and market data analysis. Looking to get started? Check out the fastest growing, top-selling course to get started with Python for quant finance. For educational purposes. Not investment advise. Use at your own risk." ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "main_language": "python", "notebook_metadata_filter": "-all" } }, "nbformat": 4, "nbformat_minor": 5 }