Back

External data source for real-time trading

How can I use my own streaming subscription of a specific external data source and implement it in my code here at QC such that I can trade live with it?
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.



MichaelH has an example of using a custom data source for BitCoin in the tutorials. He will probably give you the link. There is another acting on weather data. Basically, you create a custom data object you can react to in your algorithm.


public class MyCustomData : BaseData
{
// It is ok to call into a route on an MVC application.
private const string DATA_SOURCE_URI = @"http://55.169.46.205/getdata";

private List urls;


///
/// Field for the named column
///

public DateTime date { get; set; }
///
/// Field for the named column
///

public string symbol { get; set; }
///
/// Field for the named column
///

public decimal close { get; set; }
///
/// Field for the named column
///

public int kno { get; set; }
///
/// Field for the named column
///

public string w { get; set; }


///
/// 1. DEFAULT CONSTRUCTOR: Custom data types need a default constructor.
/// We search for a default constructor so please provide one here.
/// It won't be used for data, just to generate the "Factory".
///

public MyCustomData()
{
Symbol = "MYSYMBOL";
urls = new List();
}

///
/// 2. RETURN THE STRING URL SOURCE LOCATION FOR YOUR DATA:
/// This is a powerful and dynamic select source file method.
/// If you have a large dataset, 10+mb we recommend you break it into smaller files.
/// E.g. One zip per year.
/// We can accept raw text or ZIP files. We read the file extension to determine if it is a zip file.
///

/// Configuration object
/// Date of this source file
/// true if we're in live mode, false for backtesting mode
/// String URL of source file.
public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime currentDate, bool isLiveMode)
{
if (isLiveMode)
{
return new SubscriptionDataSource(string.Format(DATA_SOURCE_URI, currentDate.ToShortDateString(), currentDate.ToShortDateString()), SubscriptionTransportMedium.RemoteFile);
}

// OR simply return a fixed small data file. Large files will slow down your backtest

var source = new SubscriptionDataSource(uri, SubscriptionTransportMedium.RemoteFile);
return source;
// OR return new SubscriptionDataSource(fi._dataSourceUri, SubscriptionTransportMedium.LocalFile);

}

///
/// 3. READER METHOD: Read 1 line from data source and convert it into Object.
/// Each line of the CSV File is presented in here. The backend downloads your file, loads it into memory and then line by line
/// feeds it into your algorithm
///

/// string line from the data source file submitted above
/// Subscription data, symbol name, data type
/// Current date we're requesting. This allows you to break up the data source into daily files.
/// true if we're in live mode, false for backtesting mode
/// New Object which extends BaseData.
public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime currentDate, bool isLiveMode)
{
var instruction = new MyCustomData();
if (isLiveMode)
{
try
{
MyCustomDataParser dp = new MyCustomDataParser();
instruction = dp.ParceRowIntoObject(line);
}
catch { /* Do nothing, possible error in json decoding.
warning, you may not want to "swallow" this exception */ }
return instruction;
}
}


In your Initialize AddData to your algorithm

AddData("MYDATA", Resolution.Daily);

This custom data object's GetSource method is called once per OnData cycle. So if your AddSecurity in initialize is Resolution.Daily, your url will be called once per day. Once QC has gotten your data from the url, it will call the Reader method you override to create an Object from the data you get from the url. For example, JSON -> object. Once that is done OC will fire an OnData(MyCustomData data) event

Then in your algorithm, you add a handler for the event which works just like the regular OnData(TradeBars tradebars) event. If your data source returns nothing, the exception will be thrown. It is better to include a header line with the field names and skip it when parsing.

public void OnData(AbhiGoalCustomData data)
{
if (data.symbol == "NKE")
{
// do something for the symbol
}
}


I hope this helps.

@bizcad
2

@NicholasStein Very helpful thank you! This is exactly what I was looking for.
0

This link contains the live/backtest Bitcoin data object from the open source project.
0

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.


Update Backtest





0

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.


Loading...

This discussion is closed