Package: infertrade
InferTradeinfertradeis an open source trading and investment strategy library designed for accessibility and compatibility.Theinfertradepackage seeks to achieve three objectives:Simplicity: a simplepandastopandasinterface 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 withscikit-learnfunctionality for prediction and calibration. (fit, transform, predict, pipelines, gridsearch) andscikit-learncompatible libraries, likefeature-engine.The best open source trading strategies: wrapping functionality to allow strategies from any open source Python libraries with compatible licences, such astato be used with theinfertradeinterface.The project is licenced under theApache 2.0licence.Connection to InferTrade.comMany thanks for looking into theinfertradepackage!I createdInferTrade.comto provide cutting edge statistical analysis in an accessible free interface. The intention was to help individuals and small firms have access to the same quality of analysis as large institutions for systematic trading and to allow more time to be spent on creating good signals rather than backtesting and strategy verification. If someone has done the hard work of gaining insights into markets I wanted them to be able to compete in a landscape of increasingly automated statistically-driven market participants. A huge amount of effort has been made by the trading and AI/ML communities to create open source packages withpowerful diagnostic functionality, which means you do not need to build a large and complex in-house analytics library to be able to support your investment decisions with solid statistical machine learning. However there remain educational and technical barriers to using this community-created wealth if you are not an experience programmer or do not have mathematical training. I wantInferTrade.comto allow everyone trading in markets to have access without barriers - cost, training or time - to be competitive, with an easy to use interface that both provides direct analysis and education insights to support your trading.The initial impetus for the creation of this open source package,infertradewas to ensure any of our users finding an attractive strategy on InferTrade.com could easily implement the rule in Python and have full access to the code to fully understand every aspect of how it works. By adding wrapper for existing libraries we hope to support further independent backtesting by users with their own preferred choice of trading libraries. We at InferStat heavily use open source in delivering InferTrade.com's functionality and we also wanted to give something back to the trading and data science community. The Apache 2.0 licence is a permissive licence, so that you can use or build uponinfertradefor your personal, community or commercial projects.Theinfertradepackage and InferTrade.com will be adding functionality each week, and we are continually seeking to improve the experience and support the package and website provides for traders, portfolio managers and other users. Gaining feedback on new features is extremely helpful for us to improve our UX and design, as are any ideas for enhancements that would help you to trade better. If you would like to assist me in turning InferTrade into the leading open source trading platform we can offer participation in our Beta Testing programme (sign up link). You can also fork this repository and make direct improvements to the package.Best,
Tom OliverInferStat Founder and CEOhttps://github.com/ta-oliverhttps://www.linkedin.com/in/thomas-a-oliver/Contact UsThis wasInferStat'sfirst open source project and we welcome your thoughts for improvements to code structure, documentation or any changes that would support your use of the library.If you would like assistance with using theinfertradeyou can email us atsupport@infertrade.comorbook a video callIf you would like to contribute to the package, e.g. to add support for an additional package or library, please see ourcontributinginformation.If you want guidance on infertrade API then please see ourAPI Guidanceinformation.QuickstartPlease note the project requires Python 3.7 or higher due to dependent libraries used.SeeWindowsorLinuxguides for installation details.My First InferTrade Ruleimport 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()Basic usage with community functions"Community" functions are those declared in this repository, not retrieved from an external package. They are all exposed atinfertrade.algos.community.frominfertrade.algos.communityimportnormalised_close,scikit_signal_factoryfrominfertrade.data.simulate_dataimportsimulated_market_data_4_years_gensignal_transformer=scikit_signal_factory(normalised_close)signal_transformer.fit_transform(simulated_market_data_4_years_gen())Usage with TAfrominfertrade.algos.communityimportscikit_signal_factoryfrominfertrade.data.simulate_dataimportsimulated_market_data_4_years_genfrominfertrade.algosimportta_adaptorfromta.trendimportAroonIndicatoradapted_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())Calculate positions with simple position functionfrominfertrade.algos.community.allocationsimportconstant_allocation_sizefrominfertrade.utilities.operationsimportscikit_allocation_factoryfrominfertrade.data.simulate_dataimportsimulated_market_data_4_years_genposition_transformer=scikit_allocation_factory(constant_allocation_size)position_transformer.fit_transform(simulated_market_data_4_years_gen())Example of position calculation via kelly just based on signal generationfrominfertrade.algos.communityimportscikit_signal_factoryfrominfertrade.data.simulate_dataimportsimulated_market_data_4_years_genfrominfertrade.utilities.operationsimportPositionsFromPricePrediction,PricePredictionFromSignalRegressionfromsklearn.pipelineimportmake_pipelinefrominfertrade.algosimportta_adaptorfromta.trendimportAroonIndicatoradapted_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())Creating simulated data for testingFor convenience, theinfertrade.datamodule 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()Exporting portfolio performance to a CSV fileThe "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.frominfertrade.apiimportApiApi.export_to_csv(dataframe="MarketData",rule_name="weighted_moving_averages")"""Resulting CSV file would contain portfolio performance of supplied MarketDataafter 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 MarketData2after trading using weighted moving averages and calculating the change relationship"""Calculate multiple combinations of relationships between supplied data and export to CSVBesides 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 combinationfrominfertrade.apiimportApiApi.export_cross_prediction(listOfDataframes)""" The result of this would be CSV files of every possible combination of supplied datawith 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 dataframesalong 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 """### Using the InferTrade APIThe"api_automation"modulecontainsthe"execute_it_api_request"function,bysupplyingthefunctionwitharequestnamefromtheAPI_GUIDANCE.mdfileandyourAPIkeyitisabletoexecuteanycallmentionedintheguidance.```pythonfrominfertrade.utilities.api_automationimportexecute_it_api_requestexecute_it_api_request(request_name="Get trading rule metadata",api_key="YourApiKey")Calls that contain data inside of lists ("[]") need you to provide the specified
data.In this example, the API request ("Get available time series simulation models")
contains two lists and those are : "research_1" and "price"
To supply this data we simply pass the lists inside a dictionary as
"additional_data"frominfertrade.utilities.api_automationimportexecute_it_api_requestadditional_data={"price":[0,1,2,3,4,5,6,7,8,9],"research_1":[0,1,2,3,4,5,6,7,8,9]}execute_it_api_request(request_name="Get available time series simulation models",api_key="YourApiKey",additional_data=additional_data)The passed data does not have to replace data inside a list, you can replace any
key listed in the JSON body of the request by using the same feature as before.If you wish to use your own body or header you can do that by passing them to
the function:frominfertrade.utilities.api_automationimportexecute_it_api_requestexecute_it_api_request(request_name="Get available time series simulation models",api_key="YourApiKey",request_body="YourRequestBody",header="YourHeader")The default headers are set to:headers={'Content-Type':'application/json','x-api-key':'YourApiKey'}You can also pass a specific Content Type to the function:frominfertrade.utilities.api_automationimportexecute_it_api_requestexecute_it_api_request(request_name="Get trading rule metadata",api_key="YourApiKey",Content_Type="YourContentType")The default request are executed using the "request" module but if you prefer
using the "http.client" you can use the "selected_module" argument inside
the function callfrominfertrade.utilities.api_automationimportexecute_it_api_requestexecute_it_api_request(request_name="Get trading rule metadata",api_key="YourApiKey",selected_module="http.client")You can also use the "parse_to_csv" function to read data from a csv file either
located on your computer or the InferTrade package:frominfertrade.utilities.api_automationimportexecute_it_api_request,parse_csv_filedata=parse_csv_file(file_name="File_Name")additional={"trailing_stop_loss_maximum_daily_loss":"value","price":data["Column_Name"],"research_1":data["Column_Name"]}response=execute_it_api_request(request_name="Get available time series simulation models",api_key="YourApiKey",additional_data=additional,)print(response.txt)If you are only providing the file name, the function presumes that it is located in
"/infertrade/".The same functions can be used alongside postman to generate request bodies,
if you set "execute_request" to false in the function parameters it will return
the request body with additional data:frominfertrade.utilities.api_automationimportexecute_it_api_request,parse_csv_filedata=parse_csv_file(file_location="File_Location")additional={"trailing_stop_loss_maximum_daily_loss":"value","price":data["Column_Name"],"research_1":data["Column_Name"]}response=execute_it_api_request(request_name="Get available time series simulation models",api_key="YourApiKey",additional_data=additional,execute_request=False)print(response)The result of this will be the request body with "price", "research_1" and
"trailing_stop_loss_maximum_daily_loss" set to provided data.