Hi,
Is there a guideline to add a new futures market for local backtesting? In my case I'm trying to add Shanghai Futures Exchange (market/exchange name “SHFE”), and the underlying ticker is “ag” (silver). What I have done:
- Add SHFE field and its key/value pair in Market.cs.
public const string SHFE = "shfe";
private static readonly IEnumerable<Tuple<string, int>> HardcodedMarkets = new List<Tuple<string, int>>
{
...
Tuple.Create(SHFE, 555),
...
}
2. Add SHFE field in Exchange.cs
public static Exchange SHFE { get; }
= new("SHFE", "SHFE", "Shanghai Futures Exchange", "shfe", SecurityType.Future);
3. Add code in Global.GetPrimaryExchange method so it returns the correct SHFE exchange
else if (securityType == SecurityType.Future || securityType == SecurityType.FutureOption)
{
switch (exchange.LazyToUpper())
{
...
case "SHFE":
return Exchange.SHFE;
...
}
}
4. Make sure the symbol properties and market hours for “ag” are added, also the expiry calculation setup in my TestAlgorithm.Initialize:
MarketHoursDatabase.SetEntry(Market.SHFE, "es", SecurityType.Future, hours);
var symProps = Securities.SymbolProperties("Argent", Currencies.CNH, 10, 1, 1, "");
SymbolPropertiesDatabase.SetEntry(Market.SHFE, "ag", SecurityType.Future, symProps);
Securities.Future.FuturesExpiryFunctions.FuturesExpiryDictionary[Symbol.Create("ag", SecurityType.Future, Market.SHFE)] = time => Securities.Future.FuturesExpiryUtilityFunctions.NthLastBusinessDay(time,3);
5. Organise the minute data including trade/quote/open_interest in zip files and put them in the folder defined in config.json/data-folder. The final folder structure follows the engine's rules: data/future/shfe/minute/ag.
6. Put mapping files and factor files in data/future/shfe/map_files/ag.csv and data/future/shfe/factor_files /ag.csv respectively, and in the mapping files I have done the symbol mappings like:
18991230,ag,SHFE
20130831,ag vmrhkn2nn1zl,SHFE,1
20130915,ag vmrhkn2nn1zl,SHFE,2
20130919,ag vmrhkn2nn1zl,SHFE,0
Since I'm not trading it so I didn't change the fee model. After the above, I could get the future contract list:
public override void Initialize() {
SetStartDate(2013, 10, 6);
SetEndDate(2013, 10, 8);
SetCash(1000000);
... MHDB/SPDB initialization code
var ag = AddFuture("ag", Resolution.Minute, Market.SHFE);
var cs = FutureChainProvider.GetFutureContractList(ag.Symbol, new System.DateTime(2013, 10, 6));
foreach(var c in cs) {
Log($"{c}: {c.ID.Date}"); // Correctly lists the symbol as well as the expiry date
}
}
public override void OnData(Slice slice) {
Log($"{Time}");
}
But TestAlgorithm.OnData doesn't get called - there is no output at all. What else I should do to add a new futures market?
Laye
I figured it out. I didn't stamp the correct dates in my mapping files. Please ignore my post above.
Alexandre Catarino
Hi Laye ,
Great work! In fact, there are no guidelines for adding a new market at the moment, and we usually recommend developers look at pull requests to Lean (e.g.: NYSE Liffe) for examples.
Laye
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!