Integrating C# Algorithms with Python

I'm sharing some example code that may be interesting to both the C# and Python algorithm developer communities. Attached is a sample algorithm pattern that utilizes C# for core execution functionality and calls a Python module for evaluating trade entries/exit.  

Why would you want to do this?  In my opinion, this pattern can take advantage of the particular strengths of both technologies.  C# is better at high-performance, type-safe, reusable code.  Python is better at community-driven data analytics modules and predictive modeling.  So we can potentially build models in Python using packages like Scikit-Learn, XGBoost, and Tensorflow, and then call them for individual trade decisions from our high-performance C# trade execution framework.

The example Python code is hosted on Dropbox and lazy-loaded by the attached algo at run-time.  Note there is no model or unique trade strategy provided here.  I hope you still find it useful.

from clr import AddReference
from System import *
from QuantConnect import *
from QuantConnect.Data.Market import TradeBar, QuoteBar
from QuantConnect.Indicators import *
from datetime import datetime, timedelta
import pandas as pd
import numpy as np
from sklearn import linear_model

# Initialize your Python model here.
def initialize_model():
print "Hello World"

# Example Python function to return entry/exit decisions.
def scan_entry(price):
if int(price) % 2 == 0:
return 0
return 1;
Update Backtest



Update Backtest


The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


This discussion is closed