Category: Learning CSharp

Consolidating Data to Build Bars

Consolidators are used to combine data together from finer resolutions into larger ones. This can be useful for indicators with specific data requirements or to perform long term analysis in conjunction with short term signals.

Consolidators should be constructed and setup in your Initialize() method; this ensures they are only initialized once. There are three key steps to create and register a consolidator:

  1. Create the consolidator object.
  2. Bind an event handler to handle the new bars.
  3. Register it with the subscription manager to start receiving data.
public class ConsolidatorDemoAlgorithm : QCAlgorithm
{
	public override void Initialize() 
	{
		// backtest parameters
		SetStartDate(2016, 1, 1);         
		SetEndDate(DateTime.Now);
		
		// cash allocation
		SetCash(25000);
		
		//assets or universe selection
		AddEquity("SPY", Resolution.Minute);
		
		//create a consolidator object; for tradebars; for a timespan of 30 minutes
		var thirtyMinutes = new TradeBarConsolidator(TimeSpan.FromMinutes(30));

		//bind event handler to data consolidated event.
		thirtyMinutes.DataConsolidated += OnHalfHour;
		
		//register the consolidator for data.
		SubscriptionManager.AddConsolidator("SPY", thirtyMinutes);
	}
	
	//event handler for data!
	public void OnHalfHour(object sender, TradeBar bar) {
		Debug(Time.ToString("u") + " " + bar);
	}
	
	public override void OnData(Slice data) 
	{ }
}

The LEAN API also has other consolidator types to handle working with Ticks and RenkoBars:

// From tick data sources            
var tickConsolidator = new TickConsolidator(TimeSpan.FromMinutes(30)); 

//from renko bars
var renkoConsolidator = new RenkoConsolidator(TimeSpan.FromMinutes(30)); 

There are two key points to remember:

  1. Request a smaller resolution than what you want to produce.
  2. In backtesting we only know the bar is ready on the next data point; so it may appear like daily bars are triggered at odd times. In live trading they are scanned to be triggered every second at a minimum.

The raw data of QuantConnect is provided in Tick, Minute, Second, Hour or Daily bars. Using these building blocks you can combine data together to get any other resolution of data required.

Harnessing the Twitter API for Sentiment Strategies

In this project we will be writing an application which downloads tweets from Twitter. We are continuing our journey leaning C#, as we started with our Yahoo Finance data downloader.

Twitter has a REST API that allows us to  search for tweets, users, timelines, or even post new messages. We will use an incredible C# Twitter Library called Tweetinvi. It has everything you need to start building your own program. There are other alternatives, but we found this was the easiest and most complete. To use this program, you need to have a Twitter developer account, and use your own credentials.

Continue reading

Downloading Yahoo Finance Data with C#

The following post is the first in a series by Raul Pefaur on Learning C#. Over the last month Raul has taught himself C# with a variety of projects, tutorials and books which he will describe to help others on their journey to using C# for finance. Raul has a Master of Finance and lives in Santiago, Chile.

Yahoo has a popular API which lets you download daily financial data from its enormous library. In this blog post, I will be using this API to download financial data through a C# console application. It was created in Visual Studio and is free for you to download an use, though I recommend you try to build it yourself. If you like, use mine as a reference (I know there’s a lot of improvements in my code you could make. If you do, please share!). Continue reading

© 2017 QuantConnect Blog

Democratizing algorithmic investments Up ↑