infertrade
is an open source trading and investment strategy library designed for accessibility and compatibility. You can install the package via pip as infertrade
.
This package seeks to achieve three objectives:
-
Simplicity: a simple
pandas
topandas
interface that those experienced in trading but new to Python can easily use. -
Gateway to data science: classes that allow rules created for the infertrade simple interface to be used with
scikit-learn
functionality for prediction and calibration. (fit, transform, predict, pipelines, gridsearch) andscikit-learn
compatible libraries, likefeature-engine
. -
The best open source trading strategies: wrapping functionality to allow strategies from any open source Python libraries with compatible licences, such as
ta
to be used with theinfertrade
interface.
The project is licenced under the Apache 2.0 licence; please feel free to utilise the code for your personal or commercial projects.
Many thanks for looking into the infertrade
package. The Apache 2.0 licence is a permissive licence, so that you can use or build upon infertrade
for your personal, community or commercial projects.
Best, Tom Oliver
This was my first public open source project and I welcome your thoughts for improvements to code structure, documentation or any changes that would support your use of the library.
If you would like to contribute to the package, e.g. to add support for an additional package or library, please see our contributing information.
Please note the project requires Python 3.7 or higher due to dependent libraries used.
See Windows or Linux guides for installation details.
import pandas as pd
import matplotlib.pyplot as plt
def my_first_infertrade_rule(df: pd.DataFrame) -> pd.DataFrame:
df["allocation"] = 0.0
df["allocation"][df.pct_change() > 0.02] = 0.5
return df
my_dataframe = pd.read_csv("example_market_data.csv")
my_dataframe_with_allocations = my_first_infertrade_rule(my_dataframe)
my_dataframe_with_allocations.plot(["close"], ["allocation"])
plt.show()
"Community" functions are those declared in this repository, not retrieved from an external package. They are all exposed at infertrade.algos.community
.
from infertrade.algos.community import normalised_close, scikit_signal_factory
from infertrade.data.simulate_data import simulated_market_data_4_years_gen
signal_transformer = scikit_signal_factory(normalised_close)
signal_transformer.fit_transform(simulated_market_data_4_years_gen())
from infertrade.algos.community import scikit_signal_factory
from infertrade.data.simulate_data import simulated_market_data_4_years_gen
from infertrade.algos import ta_adaptor
from ta.trend import AroonIndicator
adapted_aroon = ta_adaptor(AroonIndicator, "aroon_down", window=1)
signal_transformer = scikit_signal_factory(adapted_aroon)
signal_transformer.fit_transform(simulated_market_data_4_years_gen())
from infertrade.algos.community.allocations import constant_allocation_size
from infertrade.utilities.operations import scikit_allocation_factory
from infertrade.data.simulate_data import simulated_market_data_4_years_gen
position_transformer = scikit_allocation_factory(constant_allocation_size)
position_transformer.fit_transform(simulated_market_data_4_years_gen())
from infertrade.algos.community import scikit_signal_factory
from infertrade.data.simulate_data import simulated_market_data_4_years_gen
from infertrade.utilities.operations import PositionsFromPricePrediction, PricePredictionFromSignalRegression
from sklearn.pipeline import make_pipeline
from infertrade.algos import ta_adaptor
from ta.trend import AroonIndicator
adapted_aroon = ta_adaptor(AroonIndicator, "aroon_down", window=1)
pipeline = make_pipeline(scikit_signal_factory(adapted_aroon),
PricePredictionFromSignalRegression(),
PositionsFromPricePrediction()
)
pipeline.fit_transform(simulated_market_data_4_years_gen())
For convenience, the infertrade.data
module contains some basic functions for simulating market data.
import matplotlib.pyplot as plt
from infertrade.data.simulate_data import simulated_market_data_4_years_gen
simulated_market_data_4_years_gen().plot(y=["open", "close", "high", "low", "last"])
plt.show()
import matplotlib.pyplot as plt
from infertrade.data.simulate_data import simulated_correlated_equities_4_years_gen
simulated_correlated_equities_4_years_gen().plot(y=["price", "signal"])
plt.show()
The "infertrade.api" module contains an Api class with multiple useful functions including "export_to_csv" which is used to export portfolio performance as a CSV file.
The function accepts up to two dataframes containing market data, a rule name and a relationship name and the output would be a CSV file containing information about the provided rule and relationship perfomance with provided market data.
from infertrade.api import Api
Api.export_to_csv(dataframe="MarketData", rule_name="weighted_moving_averages")
"""Resulting CSV file would contain portfolio performance of supplied MarketData
after trading using weighted moving averages"""
Api.export_to_csv(dataframe="MarketData1", second_df="MarketData2", rule_name="weighted_moving_averages", relationship="change_relationship")
"""Resulting CSV file would contain portfolio performance of supplied MarketData1 and MarketData2
after trading using weighted moving averages and calculating the change relationship"""
Besides the "infertrade.api.export_to_csv" method out api module contains "infertrade.api.export_cross_prediction"
The function accepts a list of dataframes containing market data and sequentially calculates the performance of trading strategy using pairwise combination
from infertrade.api import Api
Api.export_cross_prediction(listOfDataframes)
""" The result of this would be CSV files of every possible combination of supplied data
with relationship calculations of every relationship ranked using the "percent_gain" column """
Api.export_cross_prediction(listOfDataframes,
column_to_sort="percent_gain",
export_as_csv=False)
""" If export_as_csv is set to false the return will only be ranked indexes of dataframes
along with total sum of supplied column used to sort """
Api.export_cross_prediction(listOfDataframes,
number_of_results=3,)
""" number_of_results is used to only save/return top X ranked combinations """