| Overall Statistics |
|
Total Trades 4 Average Win 0.03% Average Loss 0% Compounding Annual Return -13.712% Drawdown 0.200% Expectancy 0 Net Profit -0.202% Sharpe Ratio -3.815 Loss Rate 0% Win Rate 100% Profit-Loss Ratio 0 Alpha -0.077 Beta 0.026 Annual Standard Deviation 0.022 Annual Variance 0 Information Ratio 2.581 Tracking Error 0.073 Treynor Ratio -3.207 Total Fees $4.00 |
namespace QuantConnect
{
public class dictionaryPractice : QCAlgorithm
{
private readonly List<string> StockPool = new List<string>
{
"NFLX",
"AAPL",
"GOOG",
"FB",
"TXN",
};
private Dictionary<string, decimal> symbolData = new Dictionary<string, decimal>();
private int today = 0;
private int nextTradeDate = 0;
private int quantity;
private int uninvestedCount;
private decimal dailyOpen;
private decimal allotment;
public override void Initialize()
{
SetStartDate(2016, 06, 06);
SetEndDate(DateTime.Now.Date.AddDays(-1));
SetCash(50000);
foreach (var symbol in StockPool)
{
AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);
}
// Schedule an event to fire every morning at market open to capture opening prices of each Stock
// and build the dictionary "symbolData" with the values.
Schedule.On(DateRules.EveryDay(), TimeRules.At(09, 30, 15), ()=>
{
today++;
uninvestedCount = 0;
foreach (var symbol in StockPool)
{
dailyOpen = Securities[symbol].Open;
// Log("Daily Open For " + symbol + " = " + dailyOpen);
symbolData.Add(symbol, dailyOpen);
}
});
}
public void OnData(TradeBars data)
{
// Every one minute tradebar, see how many stocks currently have no open position.
foreach (var symbol in StockPool)
{
if (!Portfolio[symbol].HoldStock)
{
uninvestedCount++;
}
}
// Log("Uninvested Count = " + uninvestedCount);
// Log("Portfolio Cash = " + Portfolio.Cash);
// Calculate how much available cash to allot to each potential new buy.
allotment = Portfolio.Cash / uninvestedCount;
// Log("Available Cash = " + availCash);
// Use closing price of each tradebar to determine buys for uninvested stocks
// and sells for invested stocks.
foreach( KeyValuePair<string, decimal> symbol in symbolData )
{
if (Portfolio[symbol.Key].HoldStock &&
data[symbol.Key].Close > (Portfolio[symbol.Key].AveragePrice + 0.75m))
{
Liquidate(symbol.Key);
nextTradeDate = today + 3;
}
if (!Portfolio[symbol.Key].HoldStock && today > nextTradeDate &&
data[symbol.Key].Close < symbol.Value - 0.50m)
{
quantity = (int) (Math.Floor(allotment / data[symbol.Key].Close));
Order(symbol.Key, quantity);
}
}
// At 3:59 PM (market time) clear all dictionary entries to allow rebuilding the dictionary the
// next day with new opening prices.
if (Time.TimeOfDay.TotalHours > 15.985)
{
// First print to Log the entries in the dictionary to verify that the opening price values
// for each stock are still correct.
foreach( KeyValuePair<string, decimal> symbol in symbolData )
{
Console.WriteLine("Key = {0}, Value = {1}",
symbol.Key, symbol.Value);
}
// Add a Log entry for the count of the entries in the dictionary. Should be 5.
Console.WriteLine("Count: {0}", symbolData.Count);
// Clear all entries in the dictionary.
symbolData.Clear();
// Another log entry for the dictionary entries count just to verify. Should be 0.
Console.WriteLine("Count: {0}", symbolData.Count);
}
}
}
}