CFD trading simulation for back- and forward testing different trading strategies.
⭐ Please star this repository — it motivates a lot!
Stocks and CFD's are a great way to start trading and earn some money. But what strategy should I use to actually make money 💵. This is where this simulator comes in. You can use predefined strategies or implement your own strategy and test how well it would perform long-term on any CFD listed by yfinance.
- Python >=3.13
Note
Note that the virtual environment always has to be active when trying to run simulations.
- Clone repository:
git clone https://github.com/engag1ng/stock-simulation.git - Create virtual environment:
python -m venv .venv - Activate virtual environment:
.venv\Scripts\activate - Install dependencies:
pip install -r requirements.txt
- Replace the parameters in
MAIN.py, to your liking. - Run
python MAIN.py
Before adding new features, indicators or strategies, it is important to understand, what each one is intended for.
Features: provide the ability to calculate values (columns) of data, that help make informed decisions for trading or just help to calcuate other values.
Indicators: typically returns a value between -10 and 10 which indicates, if we should buy (positive) or sell (negative) and how much, where 0 indicates holding, 10 indicates going all-in bullish and -10 indicates all-in bearish.
Strategies: Combining features and indicators to create an algorithm, that makes trading decisions, like buying / selling and setting stop-loss/take-profit.
Location: features/
- Create a new file in the
features/directory. - Define your feature function, for example:
def feature_daily_return(df):
return df['Close'].pct_change().fillna(0)Location: indicators/
- Create a new file or add to an existing one in the
indicators/directory. - Use the
@register_indicatordecorator to register it automatically:
from indicators import register_indicator
@register_indicator
def indicator_my_custom(df):
return df['Close'].rolling(window=10).mean()Location: strategies/
- Create a new file or add to an existing one in the
strategies/directory. - Subclass the
Strategybase class and use the@Strategy.registerdecorator with a unique name:
from strategies.base import Strategy
@Strategy.register("my_custom_strategy")
class MyCustomStrategy(Strategy):
def execute(self, df, owned_stocks, price, capital):
# Your buy/sell logic here
return 0.1, [price * 0.95, 0.1], [price * 1.05, 0.1]- Implement new strategies
- Add extensive documentation
- Improve simulation sequencing
- Improve UI
Community contributions are very welcome. Please see below how you can contribute...
- Find bugs and report them.
- Make recommendations.
- Make improvements to the code base.
- Make improvements to the documentation.
This project is GNU GPLv3 licensed. Please have a look at the LICENSE.md for more information.