| Overall Statistics |
|
Total Orders 2 Average Win 0% Average Loss 0% Compounding Annual Return 3.469% Drawdown 0.100% Expectancy 0 Start Equity 100000 End Equity 100043.61 Net Profit 0.044% Sharpe Ratio 3.734 Sortino Ratio 0 Probabilistic Sharpe Ratio 67.466% Loss Rate 0% Win Rate 0% Profit-Loss Ratio 0 Alpha -0.031 Beta 0.027 Annual Standard Deviation 0.006 Annual Variance 0 Information Ratio -9.02 Tracking Error 0.217 Treynor Ratio 0.831 Total Fees $2.00 Estimated Strategy Capacity $85000000.00 Lowest Capacity Asset SPY R735QTJ8XC9X Portfolio Turnover 0.55% Drawdown Recovery 3 |
#region imports
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
using System.Drawing;
using QuantConnect;
using QuantConnect.Algorithm.Framework;
using QuantConnect.Algorithm.Framework.Selection;
using QuantConnect.Algorithm.Framework.Alphas;
using QuantConnect.Algorithm.Framework.Portfolio;
using QuantConnect.Algorithm.Framework.Portfolio.SignalExports;
using QuantConnect.Algorithm.Framework.Execution;
using QuantConnect.Algorithm.Framework.Risk;
using QuantConnect.Algorithm.Selection;
using QuantConnect.Api;
using QuantConnect.Parameters;
using QuantConnect.Benchmarks;
using QuantConnect.Brokerages;
using QuantConnect.Commands;
using QuantConnect.Configuration;
using QuantConnect.Util;
using QuantConnect.Interfaces;
using QuantConnect.Algorithm;
using QuantConnect.Indicators;
using QuantConnect.Data;
using QuantConnect.Data.Auxiliary;
using QuantConnect.Data.Consolidators;
using QuantConnect.Data.Custom;
using QuantConnect.Data.Custom.IconicTypes;
using QuantConnect.DataSource;
using QuantConnect.Data.Fundamental;
using QuantConnect.Data.Market;
using QuantConnect.Data.Shortable;
using QuantConnect.Data.UniverseSelection;
using QuantConnect.Notifications;
using QuantConnect.Orders;
using QuantConnect.Orders.Fees;
using QuantConnect.Orders.Fills;
using QuantConnect.Orders.OptionExercise;
using QuantConnect.Orders.Slippage;
using QuantConnect.Orders.TimeInForces;
using QuantConnect.Python;
using QuantConnect.Scheduling;
using QuantConnect.Securities;
using QuantConnect.Securities.Equity;
using QuantConnect.Securities.Future;
using QuantConnect.Securities.Option;
using QuantConnect.Securities.Positions;
using QuantConnect.Securities.Forex;
using QuantConnect.Securities.Crypto;
using QuantConnect.Securities.CryptoFuture;
using QuantConnect.Securities.IndexOption;
using QuantConnect.Securities.Interfaces;
using QuantConnect.Securities.Volatility;
using QuantConnect.Storage;
using QuantConnect.Statistics;
using QCAlgorithmFramework = QuantConnect.Algorithm.QCAlgorithm;
using QCAlgorithmFrameworkBridge = QuantConnect.Algorithm.QCAlgorithm;
using Calendar = QuantConnect.Data.Consolidators.Calendar;
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using QuantConnect.Data;
using QuantConnect.Orders;
namespace QuantConnect.Algorithm.CSharp
{
public class OrderTicketIssueAlgorithm : QCAlgorithm
{
string _symbolName = "SPY";
TimeSpan _marketOrderFillTimeout = TimeSpan.FromSeconds(5);
public override void Initialize()
{
SetStartDate(2013, 10, 7);
SetEndDate(2013, 10, 11);
SetCash(100000);
AddSecurity(SecurityType.Equity, _symbolName, Resolution.Minute);
}
public override void OnData(Slice data)
{
TestMarketOrders();
}
private void TestMarketOrders()
{
if (Time.Day == 7 && Time.Hour == 9 && Time.Minute == 35)
{
Debug("Submitting MarketOrder (synchronous)");
// submit a market order using synchronous processing
var marketOrderTicket = MarketOrder(symbol:_symbolName, quantity:10, asynchronous: false, tag:"m1");
if (marketOrderTicket.Status != OrderStatus.Filled)
{
Debug("ERROR: Synchronous market order was not filled!");
Quit();
}
Debug("Submitting MarketOrder (asynchronous)");
marketOrderTicket = MarketOrder(symbol:_symbolName, quantity:10, asynchronous: true, tag:"m2");
// Wait for async order to be filled...
// In backtest, this should be filled immediately, but in live mode, this can be delayed...
for (var waitRetry=0; waitRetry<6; waitRetry++)
{
if (marketOrderTicket.Status != OrderStatus.Filled)
{
if (waitRetry>0) Debug($"{Time}: Market order not yet filled. Waiting: retry={waitRetry}...");
// Wait for fill timeout if not yet filled...
// Use OrderTicket.OrderClosed WaitHandle to wait with timeout for the Filled notification...
if (! marketOrderTicket.OrderClosed.WaitOne(_marketOrderFillTimeout))
{
Debug($"{Time}: WARNING: Market order not yet filled after waiting for {_marketOrderFillTimeout.TotalSeconds} seconds: Status={marketOrderTicket.Status}...");
}
}
if (marketOrderTicket.Status == OrderStatus.Filled)
{
Debug($"{Time}: Async market order was filled");
break;
}
}
if (marketOrderTicket.Status != OrderStatus.Filled)
{
Debug($"{Time}: WARNING (ERROR in backtest mode): Async market order was not filled: Status={marketOrderTicket.Status}");
}
}
}
public override void OnOrderEvent(OrderEvent orderEvent)
{
bool symbolFound = false;
Debug($"{Time.ToString("o")}: OnOrderEvent: {orderEvent.ToString()}");
string orderEventSymbolValue = orderEvent.Symbol.Value;
Debug($"{Time}: OnOrderEvent: symbol={orderEvent.Symbol}");
Debug($"{Time}: OnOrderEvent: symbolValue={orderEventSymbolValue}");
if (orderEventSymbolValue == _symbolName)
{
symbolFound = true;
Debug($"{Time}: OnOrderEvent: Symbol={orderEventSymbolValue} found");
}
if (!symbolFound)
{
Debug($"{Time}: OnOrderEvent: INFO: Symbol={orderEventSymbolValue} not found");
}
switch (orderEvent.Status)
{
case OrderStatus.New:
case OrderStatus.None:
case OrderStatus.Submitted:
case OrderStatus.Invalid:
break;
case OrderStatus.PartiallyFilled:
Debug($"{Time}: OnOrderEvent: Symbol={orderEventSymbolValue}: OrderId={orderEvent.OrderId}: Status={orderEvent.Status}");
Debug($"{Time}: OnOrderEvent: OrderFee={orderEvent.OrderFee} Message={orderEvent.Message} IsAssignment={orderEvent.IsAssignment} Direction={orderEvent.Direction} Portfolio.Invested={Portfolio.Invested}");
Debug($"{Time}: OnOrderEvent: PartiallyFilled - waiting for Filled order event");
break;
case OrderStatus.Filled:
Debug($"{Time}: OnOrderEvent: Symbol={orderEventSymbolValue}: OrderId={orderEvent.OrderId}: Status={orderEvent.Status}: FillQuantity={orderEvent.FillQuantity} FillPrice={orderEvent.FillPrice}");
Debug($"{Time}: OnOrderEvent: OrderFee={orderEvent.OrderFee} Message={orderEvent.Message} IsAssignment={orderEvent.IsAssignment} Direction={orderEvent.Direction} Portfolio.Invested={Portfolio.Invested}");
break;
case OrderStatus.Canceled:
Debug($"{Time}: OnOrderEvent: Symbol={orderEventSymbolValue}: OrderId={orderEvent.OrderId}: Status={orderEvent.Status}");
break;
default:
Debug($"{Time}: OnOrderEvent: Symbol={orderEventSymbolValue}: OrderId={orderEvent.OrderId}: Status={orderEvent.Status}");
break;
}
}
}
}