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
AddReference("System")
AddReference("QuantConnect.Common")
AddReference("QuantConnect.Indicators")
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
else:
return 1;