| Overall Statistics |
|
Total Trades 0 Average Win 0% Average Loss 0% Compounding Annual Return 0% Drawdown 0% Expectancy 0 Net Profit 0% Sharpe Ratio NaN Loss Rate 0% Win Rate 0% Profit-Loss Ratio 0 Alpha NaN Beta NaN Annual Standard Deviation NaN Annual Variance NaN Information Ratio NaN Tracking Error NaN Treynor Ratio NaN |
using System;
using System.Linq;
using System.Collections.Generic;
using QuantConnect.Securities;
using QuantConnect.Models;
namespace QuantConnect
{
// Name your algorithm class anything, as long as it inherits QCAlgorithm
public class BasicTemplateAlgorithm : QCAlgorithm
{
decimal open = 0m;
decimal close = 0m;
private Consolidator oneDayConsol;
//Initialize the data and resolution you require for your strategy:
public override void Initialize()
{
SetStartDate(2014, 12, 01);
SetEndDate(2014, 12, 03);
SetCash(1000);
AddSecurity(SecurityType.Equity, "SPY", Resolution.Tick);
AddSecurity(SecurityType.Equity, "SPY", Resolution.Second);
oneDayConsol = new Consolidator(TimeSpan.FromDays(1));
Log("ACTUAL VALUES:");
Log("12/01/14: Open: 206.40, Close: 205.76");
Log("12/02/14: Open: 205.81, Close: 207.09");
//Log("12/03/14: Open: 207.30, Close: 207.89");
Log("====================================");
Log("EVALUATED VALUES:");
}
Tick last;
Tick first;
public void OnData(Ticks ticks)
{
List<Tick> tick;
if (ticks.TryGetValue("SPY", out tick))
{
// im not sure how the ticks are ordered in the list,
// sadly DateTime doesn't provide decent precision so we don't know
// who was actually the first
if (first == null)
{
first = tick.First();
}
last = tick.Last();
}
}
//Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol.
public void OnData(TradeBars data)
{
TradeBar SPY = data["SPY"];
if (SPY.Time.Hour == 9 && SPY.Time.Minute == 30 && SPY.Time.Second == 00) {
open = Math.Round(SPY.Price,2);
}
else if (SPY.Time.Hour == 15 && SPY.Time.Minute == 59 && SPY.Time.Second == 59) {
close = Math.Round(SPY.Price,2);
Log("TIME: Open: " + open.ToString() + " Close: " + close.ToString());
}
if (oneDayConsol.Update(SPY))
{
Log("CONSOL: Open: " + Math.Round(oneDayConsol.Bar.Open,2).ToString() + " Close: " + Math.Round(oneDayConsol.Bar.Close,2).ToString());
Log("------------------------------------");
}
}
public override void OnEndOfDay()
{
Log("TICK: Open: " + first.Value.ToString(".00") + " Close: " + last.Value.ToString(".00"));
first = null;
}
}
}using System;
using System.Collections;
using System.Collections.Generic;
using QuantConnect.Securities;
using QuantConnect.Models;
namespace QuantConnect
{
/*
* TimeSpanConsolidator Helper Routine: Assemble generic timespan bar lengths: e.g. 10 minutes:
*
* 1. Setup the new Consolidator class with the timespan period:
* var _consolidator = new Consolidator(TimeSpan.FromMinutes(10));
*
* 2. Add in the data with the update routine. It will return true when bar ready
* if (_consolidator.Update(data["MSFT"])) { UseBar }
*/
public class Consolidator
{
private TradeBar _resultBar;
private TradeBar _workingBar;
private DateTime _start;
private TimeSpan _period;
//Result:
public TradeBar Bar
{
get
{
return _resultBar;
}
}
//Constructor: Set the period we'd like to scan
public Consolidator(TimeSpan span)
{
this._period = span;
this._resultBar = new TradeBar();
this._workingBar = new TradeBar(new DateTime(), "", Decimal.Zero, Decimal.MinValue, Decimal.MaxValue, 0, 0);
}
//Submit this bar, return true if we've started a new one.
public bool Update(TradeBar newBar)
{
//Intialize:
if (_start == new DateTime())
{
_start = newBar.Time;
}
//While we're less than end date, keep adding to this bar:
if (newBar.Time < (_start + _period))
{
//Building bar:
AddToBar(newBar);
return false;
}
else
{
//Completed bar: start new one:
_resultBar = _workingBar;
//Create a new bar:
_workingBar = new TradeBar(newBar.Time, newBar.Symbol, Decimal.Zero, Decimal.MinValue, Decimal.MaxValue, 0, 0);
//Start of this bar:
_start = newBar.Time;
AddToBar(newBar);
return true;
}
}
//Add to a tradebar
private void AddToBar(TradeBar newBar)
{
//Add this data to working bar:
if (_workingBar.Time == new DateTime()) _workingBar.Time = newBar.Time;
if (_workingBar.Symbol == "") _workingBar.Symbol = newBar.Symbol;
if (_workingBar.Open == Decimal.Zero) _workingBar.Open = newBar.Open;
if (newBar.High > _workingBar.High) _workingBar.High = newBar.High;
if (newBar.Low < _workingBar.Low) _workingBar.Low = newBar.Low;
_workingBar.Close = newBar.Close;
_workingBar.Volume = newBar.Volume;
}
}
}