| Overall Statistics |
|
Total Trades 64 Average Win 0.18% Average Loss 0% Compounding Annual Return -1.001% Drawdown 7.800% Expectancy 0 Net Profit -2.408% Sharpe Ratio -0.1 Loss Rate 0% Win Rate 100% Profit-Loss Ratio 0 Alpha 0.027 Beta -0.404 Annual Standard Deviation 0.073 Annual Variance 0.005 Information Ratio -0.462 Tracking Error 0.2 Treynor Ratio 0.018 Total Fees $64.00 |
using System;
using System.Collections.Generic;
using System.Linq;
using QuantConnect.Data;
using QuantConnect.Orders;
namespace QuantConnect.Algorithm.CSharp
{
/*
* QuantConnect University: Full Basic Template:
*
* The underlying QCAlgorithm class is full of helper methods which enable you to use QuantConnect.
* We have explained some of these here, but the full algorithm can be found at:
* https://github.com/QuantConnect/QCAlgorithm/blob/master/QuantConnect.Algorithm/QCAlgorithm.cs
*/
public class BasicTemplateAlgorithm : QCAlgorithm
{
decimal alpha;
decimal lastClose;
private const string Symbol = "SPY";
private readonly List<OrderTicket> _openOrders = new List<OrderTicket>();
//Initialize the data and resolution you require for your strategy:
public override void Initialize()
{
alpha = new Decimal(1.05);
lastClose = new Decimal(1);
//Start and End Date range for the backtest:
SetStartDate(2014, 1, 1);
SetEndDate(DateTime.Now.Date.AddDays(-1));
//Cash allocation
SetCash(25000);
//Add as many securities as you like. All the data will be passed into the event handler:
AddSecurity(SecurityType.Equity, "SPY", Resolution.Hour);
}
private bool CheckPairOrdersForFills(OrderTicket longOrder, OrderTicket shortOrder)
{
if (longOrder.Status == OrderStatus.Filled)
{
Log(shortOrder.OrderType + ": Cancelling short order, long order is filled.");
shortOrder.Cancel("Long filled.");
return true;
}
if (shortOrder.Status == OrderStatus.Filled)
{
Log(longOrder.OrderType + ": Cancelling long order, short order is filled.");
longOrder.Cancel("Short filled");
return true;
}
return false;
}
private bool TimeIs(int hour1, int hour2, int hour3)
{
return Time.Hour == hour1 || Time.Hour == hour2 || Time.Hour == hour3;
}
public void OnData(TradeBars data)
{
decimal close = data["SPY"].Close;
decimal cash = Portfolio.Cash;
int quantity = (int)Math.Floor(cash / (10*close));
if(TimeIs(10,13,15) && ((cash/10) < Portfolio.GetBuyingPower(Symbol, OrderDirection.Hold))){
if(!Portfolio.HoldStock){
OrderTicket longTicket = LimitOrder(Symbol, quantity, close*.99m);
_openOrders.Add(longTicket);
OrderTicket shortTicket = LimitOrder(Symbol, -quantity, close*1.01m);
_openOrders.Add(shortTicket);
if (_openOrders.Count > 0){
Debug("There are more than 1 open orders");
OrderTicket longOrder = _openOrders[0];
OrderTicket shortOrder = _openOrders[1];
if(CheckPairOrdersForFills(longOrder,shortOrder)){
//Check for Submitted Orders
_openOrders.Clear();
return;
}
var newLongLimit = longOrder.Get(OrderField.LimitPrice) + 0.01m;
var newShortLimit = shortOrder.Get(OrderField.LimitPrice) - 0.01m;
Log("Updating limits - Long: " + newLongLimit.ToString("0.00") + " Short: " + newShortLimit.ToString("0.00"));
longOrder.Update(new UpdateOrderFields
{
// we could change the quantity, but need to specify it
//Quantity =
LimitPrice = newLongLimit,
Tag = "Update #" + (longOrder.UpdateRequests.Count + 1)
});
shortOrder.Update(new UpdateOrderFields
{
LimitPrice = newShortLimit,
Tag = "Update #" + (shortOrder.UpdateRequests.Count + 1)
});
}
}
}
}
}
}