Future Trading Python Backtest Step By Step

Trading & Investment Strategies
3 min readMar 30, 2023

Backtesting trading strategies is a crucial step in developing and testing trading algorithms. Python is a popular language for backtesting trading strategies due to its simplicity and flexibility. In this article, we will walk through the steps to create a simple backtesting script using the provided code.

Step 1: Import Required Libraries

Before starting the code, we need to import the necessary libraries. For this example, we will be using pandas for data manipulation.

import pandas as pd

Step 2: Load Data

The first step is to load the historical price data into our script. We can use pandas to read a CSV file containing our data.

df = pd.read_csv('data.csv')

Step 3: Define Strategy and Variables

Next, we need to define the variables that we will use in our backtesting script.

df = df.dropna()
df.replace([np.inf, -np.inf], np.nan, inplace=True)
signals.append(0)
for i in range(1, len(df)-1):
# Your trading strategy goes here
if df.iloc[i]['STORSI'] > 0.2 :
signals.append(-1)
elif df.iloc[i]['STORSI'] < 0.8 :
signals.append(1)
else :
signals.append(0)…

--

--

Trading & Investment Strategies

I'm a French engineer who work with passion on diferent subjects. I'm curious about new technologies https://medium.com/@cemanahuacstrategies/membership