Skip to content

IamSTEINI/crypto-strategy-playground

Folders and files

NameName
Last commit message
Last commit date

Latest commit

d89dc03 · · Dec 17, 2025

History

17 Commits
Dec 2, 2025
Dec 14, 2025
Dec 2, 2025
Dec 2, 2025
Dec 6, 2025
Dec 2, 2025
Dec 14, 2025
Dec 14, 2025
Dec 14, 2025
Dec 7, 2025
Dec 5, 2025
Dec 2, 2025
Dec 5, 2025
Dec 17, 2025
Dec 13, 2025
Dec 7, 2025
Dec 13, 2025

Repository files navigation

PYTHON STRATEGY BACKTESTING

A cool Python backtesting tool.

The idea

You load CSV chart data into the library, build your strategy on top of it and SIMULATE it! 🥳

Installation

pip install strategy_backtesting

Install it with PIP and then USE IT!!

Keep in mind that all the CSV data need to follow this format:

timestamp,open,high,low,close,volume

where timestamp is in milliseconds and ordered from OLDEST to NEWEST.

Example

To start, let's import the required libraries:

import strategy_backtesting as sb

import pandas as pd

After that we will define the settings for our simulation

timespan - The number of data points (e.g., 1000)

buy_amount - What the simulator will buy if triggered

sell_amount - What the simulator will sell if triggered

settings = sb.Settings(timespan=1000, buy_amount=1, sell_amount=1)

Then we will create our chart object and apply the settings and CSV file (in this case the ETHUSDT chart data)

eth_chart = sb.ChartManager()

eth_chart.set_chart_settings(settings=settings)

eth_chart.set_chart_data(pd.read_csv("ETHUSDT_1h.csv").iloc[::-1])

Then I will add my strategy, for example: Buying it only if the RSI is in cricial area

buy_data = []

sell_data = []

oversold = 30

overbought = 70

for _, row in experiment_chart.iterrows():
     if not pd.notna(row["RSI"]):
         continue
     rsi = row["RSI"]
     if rsi  oversold:
         buy_data.append([row["timestamp"], row.get("close")])
     elif rsi >gt; overbought:
         sell_data.append([row["timestamp"], row.get("close")])
overbought: sell_data.append([row["timestamp"], row.get("close")])nd([row["timestamp"], row.get("close")])se")])" tabindex="0" role="button">

Adding the strategy and simulating it

strategy = sb.Strategy(name="RSI Strategy")

strategy.set_strategy_buys(buys=buy_data)

strategy.set_strategy_sells(sells=sell_data)


simulation = sb.Simulation(chart=experiment_chart, settings=settings, strategy=strategy)

portfolio = simulation.simulate()


print("TOTAL PnL: "+str(round((portfolio["balance"].iloc[-1])-settings.default_money, 2))+"$")


simulation.graph(rsi=True, rsi_over=[oversold, overbought], ema=True)

Now let's say we want to apply our strategy with a live chart. But we also need the websocket data So let's initialize a session with::

df = pd.DataFrame() # Used for the data displaying

url = 'wss://stream.binance.com:9443/ws/'

symbol = 'ethusdt'

stream = '@aggTrade'

long_url = url+symbol+stream

session = sb.Session(websocketURL=long_url, strategy=strategyFunction)

After we started the session with

session.start()

But we also need a function that places our buy and sell marks:

def strategyFunction(data):
     global df, simulated_assets, simulated_money
     # Here we receive price data and while its running we get the indicators so we add every entry to our strategy list
     append = {
         "timestamp": data['T'],
         "close": float(data['p']),
         "signal": None
    }
    
       new = pd.DataFrame([append])
     df = pd.concat([df, new], ignore_index=True)

      # Here you can add your own strategy and add signal: buy/sell depending on your strategy

we can define a live chart:

# Wait till connection is available with websocket

time.sleep(3)


session.live_chart(
     df_getter=lambda: df, # DATAFRAME
     interval=0, # UPDATE INTERVAL
     max_p=10000, # MAX. DATA POINTS
     show_ema=True, # EMA LINES
     show_rsi=True, # RSI
     rsi_levels=[30, 70], # RSI, 30 LOW, 70 UP
     timeframe='1m' # TIMEFRAME
)



# KEEPING IT RUNNING

while(session.running):
     time.sleep(1)

FINISH! 🥳

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published