Overall Statistics |
Total Trades 34 Average Win 0.18% Average Loss -0.12% Compounding Annual Return -0.097% Drawdown 1.400% Expectancy -0.282 Net Profit -0.591% Sharpe Ratio -0.322 Probabilistic Sharpe Ratio 0.015% Loss Rate 71% Win Rate 29% Profit-Loss Ratio 1.44 Alpha -0.001 Beta -0.001 Annual Standard Deviation 0.002 Annual Variance 0 Information Ratio -0.716 Tracking Error 0.165 Treynor Ratio 1.118 Total Fees $9.23 |
using QuantConnect.Securities; namespace QuantConnect { public partial class Base ///ONDATA { public class Alpha_SYMBOL_DOWN_S2 : AlphaModel { private readonly Symbol _symbol; private string SYMBOL = "GBPUSD" ; public Alpha_SYMBOL_DOWN_S2(Symbol symbol) { _symbol = symbol; } public override IEnumerable<Insight> Update(QCAlgorithm algorithm, Slice data) { var open_orders = algorithm.Transactions.GetOpenOrders(x=>x.Status == OrderStatus.Submitted); if (!open_orders.Any()) { algorithm.Liquidate(); } if (!open_orders.Any(x=>x.Tag.Contains("SYMBOL_DOWN_S2_OCO1"))) { Base.SYMBOL_OCO1_DOWN_S2_IsReady = true; } if (open_orders.Any(x=>x.Tag.Contains("SYMBOL_DOWN_S2"))) { foreach (var order in open_orders) { if (Base.SYMBOL_OCO1_EO1_DOWN_S2) Base.SYMBOL_OCO1_EO1_DOWN_S2_ID = order.Id; if (algorithm.UtcTime - order.CreatedTime >= TimeSpan.FromMinutes(Base.SYMBOL_DOWN_S2_OCO_DELAY_7DAY)) { int SYMBOL_OCO1S1_DOWN_S2 = 0; for (; SYMBOL_OCO1S1_DOWN_S2 < Base.SYMBOL_DOWN_S2_OCO1Sets1.Count; SYMBOL_OCO1S1_DOWN_S2++) if (Base.SYMBOL_DOWN_S2_OCO1Sets1[SYMBOL_OCO1S1_DOWN_S2].InOrders(order.Id)) { Base.SYMBOL_DOWN_S2_OCO1Sets1[SYMBOL_OCO1S1_DOWN_S2].TimeOut(); yield return Insight.Price(SYMBOL, TimeSpan.FromMilliseconds(1001), InsightDirection.Up, null, null, null, 1); algorithm.MarketOrder(SYMBOL, order.Quantity, false, "SYMBOL_DOWN_S2_Entry_1__Time out_OCO1"); Base.SYMBOL_DOWN_S2_OCO1Sets1.Clear(); SYMBOL_OCO1S1_DOWN_S2 = 0; break; } } } } var dat_ = from MINUTE in DATA_MINUTE.Values from DAY in DATA_DAY.Values from ICHI_WEEK in DATA_ICHI_WEEK.Values from ICHI_DAY in DATA_ICHI_DAY.Values select new {MINUTE, DAY, ICHI_WEEK, ICHI_DAY} ; foreach (var d_ in dat_) { if (d_.MINUTE.Symbol.Equals(SYMBOL) && d_.DAY.Symbol.Equals(SYMBOL) && d_.ICHI_WEEK.Symbol.Equals(SYMBOL) && d_.ICHI_DAY.Symbol.Equals(SYMBOL)) { if ( d_.ICHI_WEEK.BQIsReady && d_.ICHI_DAY.BQIsReady && d_.MINUTE.BQIsReady && d_.DAY.BQIsReady && d_.ICHI_WEEK.WasJustUpdated(algorithm.Time) && d_.ICHI_DAY.WasJustUpdated(algorithm.Time) && d_.MINUTE.WasJustUpdated(algorithm.Time) && d_.DAY.WasJustUpdated(algorithm.Time) ) { DOWM_SYMBOL_DOWN_S2 = (algorithm.UtcTime.DayOfWeek == DayOfWeek.Monday); DOWT_SYMBOL_DOWN_S2 = (algorithm.UtcTime.DayOfWeek == DayOfWeek.Tuesday); DOWW_SYMBOL_DOWN_S2 = (algorithm.UtcTime.DayOfWeek == DayOfWeek.Wednesday); DOWTH_SYMBOL_DOWN_S2 = (algorithm.UtcTime.DayOfWeek == DayOfWeek.Thursday); DOWR_SYMBOL_DOWN_S2 = (DOWM_SYMBOL_DOWN_S2 || DOWT_SYMBOL_DOWN_S2 || DOWW_SYMBOL_DOWN_S2 || DOWTH_SYMBOL_DOWN_S2 || algorithm.UtcTime.DayOfWeek == DayOfWeek.Friday); if(d_.ICHI_DAY.Kijun[0] != 0) algorithm.Debug("Mi : " + d_.MINUTE.B[0].High + " " + "MO : " + d_.DAY.B[0].High + " " + "iW : " + d_.ICHI_WEEK.Kijun[0] + " " + "iM : " + d_.ICHI_DAY.Kijun[0]); if (Base.SYMBOL_OCO1_DOWN_S2_IsReady == false) { Base.SYMBOL_OCO1_DOWN_S2_Monthly_IsReady = false; } if (algorithm.Time == d_.DAY.B[0].EndTime) { Base.SYMBOL_OCO1_DOWN_S2_Monthly_IsReady = true; } Signal1: if ( d_.DAY.B[0].Low < d_.ICHI_WEEK.Kijun[0] && d_.DAY.B[1].Low > d_.ICHI_WEEK.Kijun[0] && d_.DAY.B[1].Open - d_.DAY.B[1].Close > d_.MINUTE.B[0].Close * 0.001m && d_.DAY.B[1].Open - d_.DAY.B[1].Close < d_.MINUTE.B[0].Close * 0.01m && d_.DAY.B[0].Open - d_.DAY.B[0].Close > 0 && d_.DAY.B[0].High - d_.DAY.B[0].Low > (d_.DAY.B[1].High - d_.DAY.B[1].Low) ) { SYMBOL_Signal_Condition_DOWN_1 = true ; SYMBOL_DOWN_S2_SL_1 = d_.DAY.B[0].High - d_.DAY.B[0].Low; SYMBOL_DOWN_S2_TP1_1 = SYMBOL_DOWN_S2_SL_1 * 2m ; } if ( d_.DAY.B[1].Open - d_.DAY.B[1].Close < d_.MINUTE.B[0].Close * 0.001m ) { SYMBOL_Signal_Condition_DOWN_1 = false; } if (SYMBOL_Signal_Condition_DOWN_1 && DOWR_SYMBOL_DOWN_S2 && Base.SYMBOL_OCO1_DOWN_S2_IsReady == true && Base.SYMBOL_OCO1_DOWN_S2_Monthly_IsReady == true) { if (SYMBOL_DOWN_S2_SL_1 != 0) { SYMBOL_quantity_DOWN_S2_1 = -(int)Math.Floor(algorithm.Portfolio.GetBuyingPower(d_.MINUTE.Symbol, 0))/10; } SYMBOL_quantity_SL_DOWN_S2_1 = SYMBOL_quantity_DOWN_S2_1; SYMBOL_quantity_TP1_DOWN_S2_1 = (int)Math.Floor(SYMBOL_quantity_DOWN_S2_1 * 1m); Base.SYMBOL_OCO1_EO1_DOWN_S2 = true; yield return Insight.Price(d_.MINUTE.Symbol, TimeSpan.FromMinutes(5), InsightDirection.Down, null, null, null, 0.1); SYMBOL_OCO1_MO1_DOWN_S2 = algorithm.MarketOrder(d_.MINUTE.Symbol, SYMBOL_quantity_TP1_DOWN_S2_1, false,"SYMBOL_DOWN_S2_Entry_TP1_OCO1"); Base.SYMBOL_OCO1_EO1_DOWN_S2 = false; Base.SYMBOL_OCO1_DOWN_S2_IsReady = false; if (Base.SYMBOL_OCO1_DOWN_S2_IsReady == false) break; } yield break; } } } } } } }
using System; using System.Collections.Generic; using System.Linq; using QuantConnect.Data; using QuantConnect.Data.Consolidators; using QuantConnect.Data.Market; using QuantConnect.Indicators; using QuantConnect.Parameters; using QuantConnect.Orders; using QuantConnect.Algorithm.Framework.Alphas; using QuantConnect.Algorithm.Framework.Execution; using QuantConnect.Algorithm.Framework.Portfolio; using QuantConnect.Algorithm.Framework.Selection; namespace QuantConnect { public partial class Base : QCAlgorithm { public static TimeSpan Minute = TimeSpan.FromMinutes(1); public static TimeSpan Daily = TimeSpan.FromMinutes(1440); public static int Rol_minute = 3; public static int Rol_daily = 3; public static Dictionary<string, ICHI_WEEK> DATA_ICHI_WEEK = new Dictionary<string, ICHI_WEEK>(); public static Dictionary<string, ICHI_DAY> DATA_ICHI_DAY = new Dictionary<string, ICHI_DAY>(); public static Dictionary<string, MINUTE> DATA_MINUTE = new Dictionary<string, MINUTE>(); public static Dictionary<string, DAY> DATA_DAY = new Dictionary<string, DAY>(); private SYMBOL_UP_S2_OnOrderEvent up_OnOrderEvent; private SYMBOL_DOWN_S2_OnOrderEvent down_OnOrderEvent; public static string symbol = "GBPUSD"; public override void Initialize() { SetStartDate(2015, 1, 1); SetEndDate(DateTime.Now); SetCash(1000000); Settings.RebalancePortfolioOnInsightChanges = false; Settings.RebalancePortfolioOnSecurityChanges = false; SetBrokerageModel(BrokerageName.AlphaStreams); SetWarmUp(TimeSpan.FromMinutes(1440*113), Resolution.Minute); SetWarmUp(TimeSpan.FromMinutes(1440*113), Resolution.Daily); var Forex = AddForex(symbol, Resolution.Minute, Market.FXCM).Symbol; // var hichiw = History<QuoteBar>(symbol, 113, Resolution.Daily); DATA_ICHI_WEEK.Add(symbol, new ICHI_WEEK(Forex, Daily, Rol_daily)); // var hichid = History<QuoteBar>(symbol, 113, Resolution.Daily); DATA_ICHI_DAY.Add(symbol, new ICHI_DAY(Forex, Daily, Rol_daily)); // var hm = History<QuoteBar>(symbol, TimeSpan.FromDays(113), Resolution.Minute); DATA_MINUTE.Add(symbol, new MINUTE(Forex, Minute, Rol_minute)); // var hd = History<QuoteBar>(symbol, 113, Resolution.Daily); DATA_DAY.Add(symbol, new DAY(Forex, Daily, Rol_daily)); Consolidate("GBPUSD", CalendarType.Weekly, CalendarQuoteBarHandlerWeekly); Consolidate("GBPUSD", Daily, CalendarQuoteBarHandlerDaily); up_OnOrderEvent = new SYMBOL_UP_S2_OnOrderEvent(this); down_OnOrderEvent = new SYMBOL_DOWN_S2_OnOrderEvent(this); foreach (var kvp in DATA_ICHI_WEEK) { var ICHI_WEEK = kvp.Value; var cons_ichiweek = (IDataConsolidator) new QuoteBarConsolidator(CalendarTypee.Weekly); ICHI_WEEK.ichi = new IchimokuKinkoHyo(CreateIndicatorName(ICHI_WEEK.Symbol, "ICHIW", Resolution.Daily), Rol_daily); cons_ichiweek.DataConsolidated += (sender, baseData) => { var bar = (QuoteBar)baseData; ICHI_WEEK.ichi.Update(bar); ICHI_WEEK.Kijun.Add(ICHI_WEEK.ichi.Kijun.Current.Value); }; SubscriptionManager.AddConsolidator(ICHI_WEEK.Symbol, cons_ichiweek); } foreach (var kvp in DATA_ICHI_DAY) { var ICHI_DAY = kvp.Value; var data_ichi_day = (IDataConsolidator) new QuoteBarConsolidator(Daily); ICHI_DAY.ichi = new IchimokuKinkoHyo(CreateIndicatorName(ICHI_DAY.Symbol, "ICHIM", Resolution.Daily), Rol_daily); data_ichi_day.DataConsolidated += (sender, baseData) => { var bar = (QuoteBar)baseData; ICHI_DAY.ichi.Update(bar); ICHI_DAY.Kijun.Add(ICHI_DAY.ichi.Kijun.Current.Value); }; SubscriptionManager.AddConsolidator(ICHI_DAY.Symbol, data_ichi_day); } foreach (var kvp in DATA_MINUTE) { var MINUTE = kvp.Value; var bar_minute = (IDataConsolidator)new QuoteBarConsolidator(Minute); bar_minute.DataConsolidated += (sender, baseData) => { var bar = (QuoteBar)baseData; MINUTE.B.Add(bar); }; SubscriptionManager.AddConsolidator(MINUTE.Symbol, bar_minute); } foreach (var kvp in DATA_DAY) { var DAY = kvp.Value; var bar_day = (IDataConsolidator) new QuoteBarConsolidator(Daily); bar_day.DataConsolidated += (sender, baseData) => { var bar = (QuoteBar)baseData; DAY.B.Add(bar); }; SubscriptionManager.AddConsolidator(DAY.Symbol, bar_day); } foreach (var ICHI_DAY in DATA_ICHI_DAY.Values) { foreach (var ICHI_WEEK in DATA_ICHI_WEEK.Values) { PlotIndicator("ichi", ICHI_WEEK.ichi, ICHI_DAY.ichi); } } //AddAlpha(new Alpha_SYMBOL_UP_S2(_SYMBOL)); AddAlpha(new Alpha_SYMBOL_DOWN_S2(_SYMBOL)); } //////////// this permit to warmup ///////////// public void CalendarQuoteBarHandlerWeekly(QuoteBar quoteBar) { Debug($"{Time} :: {quoteBar.Time} {quoteBar.Close}"); foreach (var ICHI_WEEK in DATA_ICHI_WEEK.Values) { var bar = quoteBar; ICHI_WEEK.ichi.Update(bar); ICHI_WEEK.Kijun.Add(ICHI_WEEK.ichi.Kijun.Current.Value); } } public void CalendarQuoteBarHandlerDaily(QuoteBar quoteBar) { Debug($"{Time} :: {quoteBar.Time} {quoteBar.Close}"); foreach (var ICHI_DAY in DATA_ICHI_DAY.Values) { var bar = quoteBar; ICHI_DAY.ichi.Update(bar); ICHI_DAY.Kijun.Add(ICHI_DAY.ichi.Kijun.Current.Value); } } public override void OnOrderEvent(OrderEvent orderEvent) { up_OnOrderEvent.OnOrderEvent(this, orderEvent); down_OnOrderEvent.OnOrderEvent(this, orderEvent); } public void OnInsightsGeneratedVerifier(IAlgorithm algorithm, GeneratedInsightsCollection insightsCollection) { if ( insightsCollection.Insights.Count(insight => insight.Symbol.Value.Equals("GBPNZD")) != 1 ) { throw new Exception("Unexpected insights were emitted"); } } } public class ICHI_WEEK { public readonly Symbol Symbol; public readonly TimeSpan Daily; public IchimokuKinkoHyo ichi; public readonly RollingWindow<decimal> Kijun; public ICHI_WEEK(Symbol symbol, TimeSpan daily, int Rol_daily) { Symbol = symbol; Daily = daily; Kijun = new RollingWindow<decimal>(Rol_daily); } public bool BQIsReady { get { return ichi.Kijun.IsReady && Kijun.IsReady ;} } public bool WasJustUpdated(DateTime current) { return Kijun.Count > 0 ; } } public class ICHI_DAY { public readonly Symbol Symbol; public readonly TimeSpan Daily; public IchimokuKinkoHyo ichi; public readonly RollingWindow<decimal> Kijun; public ICHI_DAY(Symbol symbol, TimeSpan daily, int Rol_daily) { Symbol = symbol; Daily = daily; Kijun = new RollingWindow<decimal>(Rol_daily); } public bool BQIsReady { get { return ichi.Kijun.IsReady && Kijun.IsReady ;} } public bool WasJustUpdated(DateTime current) { return Kijun.Count > 0 && ichi.Kijun.Current.Time > (current - TimeSpan.FromSeconds(5)) - Daily && ichi.Kijun.Current.Time < (current + TimeSpan.FromSeconds(5)) - Daily ; } } public class MINUTE { public readonly Symbol Symbol; public readonly RollingWindow<QuoteBar> B; public readonly TimeSpan Minute; public MINUTE(Symbol symbol, TimeSpan minute, int Rol_minute) { Symbol = symbol; Minute = minute; B = new RollingWindow<QuoteBar>(Rol_minute); } public bool BQIsReady { get { return B.IsReady ;} } public bool WasJustUpdated(DateTime current) { return B.Count > 0 && B[0].Time > (current - TimeSpan.FromSeconds(5)) - Minute && B[0].Time < (current + TimeSpan.FromSeconds(5)) - Minute ; } } public class DAY { public readonly Symbol Symbol; public readonly RollingWindow<QuoteBar> B; public readonly TimeSpan Daily; public DAY(Symbol symbol, TimeSpan daily, int Rol_daily) { Symbol = symbol; Daily = daily; B = new RollingWindow<QuoteBar>(Rol_daily); } public bool BQIsReady { get { return B.IsReady ;} } public bool WasJustUpdated(DateTime current) { return B.Count > 0 && B[0].Time > (current - TimeSpan.FromSeconds(5)) - Daily && B[0].Time < (current + TimeSpan.FromSeconds(5)) - Daily ; } } }
using QuantConnect.Securities; namespace QuantConnect { public partial class Base ///ONDATA { public class Alpha_SYMBOL_UP_S2 : AlphaModel { private readonly Symbol _symbol; private string SYMBOL = "GBPUSD" ; public Alpha_SYMBOL_UP_S2(Symbol symbol) { _symbol = symbol; } public override IEnumerable<Insight> Update(QCAlgorithm algorithm, Slice data) { var open_orders = algorithm.Transactions.GetOpenOrders(x=>x.Status == OrderStatus.Submitted); if (!open_orders.Any()) { algorithm.Liquidate(); } if (!open_orders.Any(x=>x.Tag.Contains("SYMBOL_UP_S2_OCO1"))) { Base.SYMBOL_OCO1_UP_S2_IsReady = true; } if (open_orders.Any(x=>x.Tag.Contains("SYMBOL_UP_S2"))) { foreach (var order in open_orders) { if (Base.SYMBOL_OCO1_EO1_UP_S2) Base.SYMBOL_OCO1_EO1_UP_S2_ID = order.Id; if (algorithm.UtcTime - order.CreatedTime >= TimeSpan.FromMinutes(Base.SYMBOL_UP_S2_OCO_DELAY_7DAY)) { int SYMBOL_OCO1S1_UP_S2 = 0; for (; SYMBOL_OCO1S1_UP_S2 < Base.SYMBOL_UP_S2_OCO1Sets1.Count; SYMBOL_OCO1S1_UP_S2++) if (Base.SYMBOL_UP_S2_OCO1Sets1[SYMBOL_OCO1S1_UP_S2].InOrders(order.Id)) { Base.SYMBOL_UP_S2_OCO1Sets1[SYMBOL_OCO1S1_UP_S2].TimeOut(); yield return Insight.Price(SYMBOL, TimeSpan.FromMilliseconds(1001), InsightDirection.Down, null, null, null, 1); algorithm.MarketOrder(SYMBOL, order.Quantity, false, "SYMBOL_UP_S2_Entry_1__Time out_OCO1"); Base.SYMBOL_UP_S2_OCO1Sets1.Clear(); SYMBOL_OCO1S1_UP_S2 = 0; break; } } } } var dat_ = from MINUTE in DATA_MINUTE.Values from DAY in DATA_DAY.Values from ICHI_WEEK in DATA_ICHI_WEEK.Values from ICHI_DAY in DATA_ICHI_DAY.Values select new {MINUTE, DAY, ICHI_WEEK, ICHI_DAY} ; foreach (var d_ in dat_) { if (d_.MINUTE.Symbol.Equals(SYMBOL) && d_.DAY.Symbol.Equals(SYMBOL) && d_.ICHI_WEEK.Symbol.Equals(SYMBOL) && d_.ICHI_DAY.Symbol.Equals(SYMBOL)) { if ( d_.ICHI_WEEK.BQIsReady && d_.ICHI_DAY.BQIsReady && d_.MINUTE.BQIsReady && d_.DAY.BQIsReady && d_.ICHI_WEEK.WasJustUpdated(algorithm.Time) && d_.ICHI_DAY.WasJustUpdated(algorithm.Time) && d_.MINUTE.WasJustUpdated(algorithm.Time) && d_.DAY.WasJustUpdated(algorithm.Time) ) { DOWM_SYMBOL_UP_S2 = (algorithm.UtcTime.DayOfWeek == DayOfWeek.Monday); DOWT_SYMBOL_UP_S2 = (algorithm.UtcTime.DayOfWeek == DayOfWeek.Tuesday); DOWW_SYMBOL_UP_S2 = (algorithm.UtcTime.DayOfWeek == DayOfWeek.Wednesday); DOWTH_SYMBOL_UP_S2 = (algorithm.UtcTime.DayOfWeek == DayOfWeek.Thursday); DOWR_SYMBOL_UP_S2 = (DOWM_SYMBOL_UP_S2 || DOWT_SYMBOL_UP_S2 || DOWW_SYMBOL_UP_S2 || DOWTH_SYMBOL_UP_S2 || algorithm.UtcTime.DayOfWeek == DayOfWeek.Friday); if (d_.MINUTE.Symbol.Equals(SYMBOL) && d_.DAY.Symbol.Equals(SYMBOL) && d_.ICHI_WEEK.Symbol.Equals(SYMBOL) && d_.ICHI_DAY.Symbol.Equals(SYMBOL)) { if (Base.SYMBOL_OCO1_UP_S2_IsReady == false) { Base.SYMBOL_OCO1_UP_S2_Monthly_IsReady = false; } if (algorithm.Time == d_.DAY.B[0].EndTime) { Base.SYMBOL_OCO1_UP_S2_Monthly_IsReady = true; } if(d_.ICHI_DAY.Kijun[0] != 0) algorithm.Debug("Mi : " + d_.MINUTE.B[0].High + " " + "MO : " + d_.DAY.B[0].High + " " + "iW : " + d_.ICHI_WEEK.Kijun[0] + " " + "iM : " + d_.ICHI_DAY.Kijun[0]); Signal1: if ( d_.DAY.B[1].Close - d_.DAY.B[1].Open > d_.MINUTE.B[0].Close * 0.001m ) { SYMBOL_Signal_Condition_UP_1 = true ; SYMBOL_UP_S2_SL_1 = (d_.DAY.B[0].High - d_.DAY.B[0].Low); SYMBOL_UP_S2_TP1_1 = SYMBOL_UP_S2_SL_1 * 4m ; } if ( d_.DAY.B[1].Close - d_.DAY.B[1].Open < d_.MINUTE.B[0].Close * 0.001m ) { SYMBOL_Signal_Condition_UP_1 = false; } if (SYMBOL_Signal_Condition_UP_1 && DOWR_SYMBOL_UP_S2 && Base.SYMBOL_OCO1_UP_S2_IsReady == true && Base.SYMBOL_OCO1_UP_S2_Monthly_IsReady == true) { if (SYMBOL_UP_S2_SL_1 != 0) { SYMBOL_quantity_UP_S2_1 = (int)Math.Floor(algorithm.Portfolio.GetBuyingPower(d_.MINUTE.Symbol, 0))/10; } SYMBOL_quantity_SL_UP_S2_1 = SYMBOL_quantity_UP_S2_1; SYMBOL_quantity_TP1_UP_S2_1 = (int)Math.Floor(SYMBOL_quantity_UP_S2_1 * 1m); Base.SYMBOL_OCO1_EO1_UP_S2 = true; yield return Insight.Price(d_.MINUTE.Symbol, TimeSpan.FromMinutes(5), InsightDirection.Up, null, null, null, 0.1); SYMBOL_OCO1_MO1_UP_S2 = algorithm.MarketOrder(d_.MINUTE.Symbol, SYMBOL_quantity_TP1_UP_S2_1, false,"SYMBOL_UP_S2_Entry_TP1_OCO1"); Base.SYMBOL_OCO1_EO1_UP_S2 = false; Base.SYMBOL_OCO1_UP_S2_IsReady = false; if (Base.SYMBOL_OCO1_UP_S2_IsReady == false) break; } yield break; } } } } } } } }
using System; using System.Collections.Generic; using System.Linq; using QuantConnect.Data; using QuantConnect.Data.Consolidators; using QuantConnect.Data.Market; using QuantConnect.Indicators; using QuantConnect.Indicators; using QuantConnect.Orders; using QuantConnect.Algorithm.Framework.Alphas; using QuantConnect.Algorithm.Framework.Execution; using QuantConnect.Algorithm.Framework.Portfolio; using QuantConnect.Algorithm.Framework.Selection; namespace QuantConnect { public partial class Base { public static Symbol _SYMBOL; ///oco1 up public static OrderTicket SYMBOL_OCO1_MO1_UP_S2; public static int SYMBOL_OCO1_EO1_UP_S2_ID ; public static bool SYMBOL_OCO1_EO1_UP_S2 ; public static Func<QCAlgorithm, string, decimal, decimal, SYMBOL_OCO1_UP_S2> SYMBOL_OOFE_OCO1_UP_S2_1 ; public static SYMBOL_OCO1_UP_S2 SYMBOL_PFO_OCO1_UP_S2_1 ; public static List<SYMBOL_OCO1_UP_S2> SYMBOL_UP_S2_OCO1Sets1 = new List<SYMBOL_OCO1_UP_S2>(); public static bool SYMBOL_OCO1_UP_S2_IsReady = true; public static bool SYMBOL_OCO1_UP_S2_Monthly_IsReady = true; //oco1 down public static OrderTicket SYMBOL_OCO1_MO1_DOWN_S2 ; public static int SYMBOL_OCO1_EO1_DOWN_S2_ID ; public static bool SYMBOL_OCO1_EO1_DOWN_S2 ; public static Func<QCAlgorithm, string, decimal, decimal, SYMBOL_OCO1_DOWN_S2> SYMBOL_OOFE_OCO1_DOWN_S2_1 ; public static SYMBOL_OCO1_DOWN_S2 SYMBOL_PFO_OCO1_DOWN_S2_1 ; public static List<SYMBOL_OCO1_DOWN_S2> SYMBOL_DOWN_S2_OCO1Sets1 = new List<SYMBOL_OCO1_DOWN_S2>(); public static bool SYMBOL_OCO1_DOWN_S2_IsReady = true; public static bool SYMBOL_OCO1_DOWN_S2_Monthly_IsReady = true; //////////////////////////// public static decimal SYMBOL_UP_S2_SL_1 {get; set;} public static decimal SYMBOL_UP_S2_TP1_1 {get; set;} public static decimal SYMBOL_DOWN_S2_SL_1 {get; set;} public static decimal SYMBOL_DOWN_S2_TP1_1 {get; set;} public static bool SYMBOL_Signal_Condition_UP_1 ; public static bool SYMBOL_Signal_Condition_DOWN_1 ; public static int SYMBOL_UP_S2_OCO_DELAY_7DAY = 43200; public static int SYMBOL_DOWN_S2_OCO_DELAY_7DAY = 43200; public static bool DOWR_SYMBOL_UP_S2 ; public static bool DOWM_SYMBOL_UP_S2 ; public static bool DOWT_SYMBOL_UP_S2 ; public static bool DOWW_SYMBOL_UP_S2 ; public static bool DOWTH_SYMBOL_UP_S2 ; public static bool DOWR_SYMBOL_DOWN_S2 ; public static bool DOWM_SYMBOL_DOWN_S2 ; public static bool DOWT_SYMBOL_DOWN_S2 ; public static bool DOWW_SYMBOL_DOWN_S2 ; public static bool DOWTH_SYMBOL_DOWN_S2 ; public static decimal SYMBOL_quantity_UP_S2_1 ; public static decimal SYMBOL_quantity_SL_UP_S2_1 ; public static decimal SYMBOL_quantity_TP1_UP_S2_1 ; public static decimal SYMBOL_quantity_DOWN_S2_1 ; public static decimal SYMBOL_quantity_SL_DOWN_S2_1 ; public static decimal SYMBOL_quantity_TP1_DOWN_S2_1 ; } }
using System; using System.Collections.Generic; using System.Linq; using QuantConnect.Data; using QuantConnect.Data.Consolidators; using QuantConnect.Data.Market; using QuantConnect.Indicators; using QuantConnect.Orders; using QuantConnect.Algorithm.Framework.Alphas; using QuantConnect.Algorithm.Framework.Execution; using QuantConnect.Algorithm.Framework.Portfolio; using QuantConnect.Algorithm.Framework.Selection; namespace QuantConnect { public struct SYMBOL_UP_S2_OnOrderEvent { private QCAlgorithm _algorithm; public SYMBOL_UP_S2_OnOrderEvent(QCAlgorithm algorithm) { _algorithm = algorithm; } public void OnOrderEvent(QCAlgorithm _algorithm, OrderEvent orderEvent) { var OrderID = _algorithm.Transactions.GetOrderById(orderEvent.OrderId); if (orderEvent.Status == OrderStatus.Filled) { if (OrderID.Tag.StartsWith("SYMBOL OCO1 CANCEL 1 UP S2") || OrderID.Tag.StartsWith("SYMBOL OCO1 CANCEL 2 UP S2")) { _algorithm.MarketOrder(OrderID.Symbol, -OrderID.Quantity, false, "OrderID rescue SYMBOL S2"); } } if (Base.SYMBOL_OCO1_EO1_UP_S2) Base.SYMBOL_OCO1_EO1_UP_S2_ID = orderEvent.OrderId; if (orderEvent.Status == OrderStatus.Filled || orderEvent.Status == OrderStatus.PartiallyFilled) { if (orderEvent.OrderId == Base.SYMBOL_OCO1_EO1_UP_S2_ID) { Base.SYMBOL_OOFE_OCO1_UP_S2_1 = (algo1, symbol, filledPrice, fillQ) => { return new SYMBOL_OCO1_UP_S2( algo1.LimitOrder(orderEvent.Symbol, -orderEvent.Quantity, orderEvent.FillPrice + Base.SYMBOL_UP_S2_TP1_1,"TP1_SYMBOL_UP_S2_OCO1"), algo1.StopMarketOrder(orderEvent.Symbol, -orderEvent.Quantity, orderEvent.FillPrice - Base.SYMBOL_UP_S2_SL_1,"SL_TP1_SYMBOL_UP_S2_OCO1")); }; Base.SYMBOL_UP_S2_OCO1Sets1.Add( Base.SYMBOL_OOFE_OCO1_UP_S2_1(_algorithm, orderEvent.Symbol, orderEvent.FillPrice, orderEvent.Quantity)); Base.SYMBOL_OCO1_EO1_UP_S2_ID = -1; } int SYMBOL_OCO1S1_UP_S2 = 0; for (; SYMBOL_OCO1S1_UP_S2 < Base.SYMBOL_UP_S2_OCO1Sets1.Count; SYMBOL_OCO1S1_UP_S2++) if (Base.SYMBOL_UP_S2_OCO1Sets1[SYMBOL_OCO1S1_UP_S2].InOrders(orderEvent.OrderId)) { Base.SYMBOL_UP_S2_OCO1Sets1[SYMBOL_OCO1S1_UP_S2].Filled(); Base.SYMBOL_UP_S2_OCO1Sets1.RemoveAt(SYMBOL_OCO1S1_UP_S2); SYMBOL_OCO1S1_UP_S2 = 0; break; } } } } }
using System.Linq; using System.Collections.Generic; using QuantConnect.Orders; using NodaTime; using NodaTime.TimeZones; namespace QuantConnect { public struct SYMBOL_OCO1_DOWN_S2 { public SYMBOL_OCO1_DOWN_S2(params OrderTicket[] orderTickets) { this.OrderTickets = orderTickets; } private OrderTicket[] OrderTickets { get; set; } public void Filled() { // Cancel all the outstanding tickets. foreach (var orderTicket in this.OrderTickets) { if (orderTicket.Status == OrderStatus.Submitted) { if(orderTicket.Tag.StartsWith("TP1_SYMBOL_DOWN_S2_OCO1" ) || orderTicket.Tag.StartsWith("SL_TP1_SYMBOL_DOWN_S2_OCO1")) { orderTicket.Cancel("SYMBOL OCO1 CANCEL 1 DOWN S2"); } if(orderTicket.Tag.StartsWith("TP2_SYMBOL_DOWN_S2_OCO1" ) || orderTicket.Tag.StartsWith("SL_TP2_SYMBOL_DOWN_S2_OCO1")) { orderTicket.Cancel("SYMBOL OCO1 CANCEL 2 DOWN S2"); } if(orderTicket.Tag.StartsWith("TP3_SYMBOL_DOWN_S2_OCO1" ) || orderTicket.Tag.StartsWith("SL_TP3_SYMBOL_DOWN_S2_OCO1")) { orderTicket.Cancel("SYMBOL OCO1 CANCEL 3 DOWN S2"); } if(orderTicket.Tag.StartsWith("TP4_SYMBOL_DOWN_S2_OCO1" ) || orderTicket.Tag.StartsWith("SL_TP4_SYMBOL_DOWN_S2_OCO1")) { orderTicket.Cancel("SYMBOL OCO1 CANCEL 4 DOWN S2"); } } } } public void TimeOut() { // Cancel all the outstanding tickets. foreach (var orderTicket in this.OrderTickets) { if (orderTicket.Status == OrderStatus.Submitted) { if(orderTicket.Tag.StartsWith("TP1_SYMBOL_DOWN_S2_OCO1" ) || orderTicket.Tag.StartsWith("SL_TP1_SYMBOL_DOWN_S2_OCO1")) { orderTicket.Cancel("SYMBOL OCO1 CANCEL 1 DOWN S2 TIMEOUT"); } if(orderTicket.Tag.StartsWith("TP2_SYMBOL_DOWN_S2_OCO1" ) || orderTicket.Tag.StartsWith("SL_TP2_SYMBOL_DOWN_S2_OCO1")) { orderTicket.Cancel("SYMBOL OCO1 CANCEL 2 DOWN S2 TIMEOUT"); } if(orderTicket.Tag.StartsWith("TP3_SYMBOL_DOWN_S2_OCO1" ) || orderTicket.Tag.StartsWith("SL_TP3_SYMBOL_DOWN_S2_OCO1")) { orderTicket.Cancel("SYMBOL OCO1 CANCEL 3 DOWN S2 TIMEOUT"); } if(orderTicket.Tag.StartsWith("TP4_SYMBOL_DOWN_S2_OCO1" ) || orderTicket.Tag.StartsWith("SL_TP4_SYMBOL_DOWN_S2_OCO1")) { orderTicket.Cancel("SYMBOL OCO1 CANCEL 4 DOWN S2 TIMEOUT"); } } } } public bool InOrders(int orderId) { foreach (var orderTicket in this.OrderTickets) if (orderTicket.OrderId == orderId) return true; return false; } } public struct SYMBOL_OCO1_UP_S2 { public SYMBOL_OCO1_UP_S2(params OrderTicket[] orderTicketsUp) { this.OrderTicketsUp = orderTicketsUp; } private OrderTicket[] OrderTicketsUp { get; set; } public void Filled() { // Cancel all the outstanding tickets. foreach (var orderTicket in this.OrderTicketsUp) { if (orderTicket.Status == OrderStatus.Submitted) { if(orderTicket.Tag.StartsWith("TP1_SYMBOL_UP_S2_OCO1" ) || orderTicket.Tag.StartsWith("SL_TP1_SYMBOL_UP_S2_OCO1")) { orderTicket.Cancel("SYMBOL OCO1 CANCEL 1 UP S2"); } if(orderTicket.Tag.StartsWith("TP2_SYMBOL_UP_S2_OCO1" ) || orderTicket.Tag.StartsWith("SL_TP2_SYMBOL_UP_S2_OCO1")) { orderTicket.Cancel("SYMBOL OCO1 CANCEL 2 UP S2"); } if(orderTicket.Tag.StartsWith("TP3_SYMBOL_UP_S2_OCO1" ) || orderTicket.Tag.StartsWith("SL_TP3_SYMBOL_UP_S2_OCO1")) { orderTicket.Cancel("SYMBOL OCO1 CANCEL 3 UP S2"); } if(orderTicket.Tag.StartsWith("TP4_SYMBOL_UP_S2_OCO1" ) || orderTicket.Tag.StartsWith("SL_TP4_SYMBOL_UP_S2_OCO1")) { orderTicket.Cancel("SYMBOL OCO1 CANCEL 4 UP S2"); } } } } public void TimeOut() { // Cancel all the outstanding tickets. foreach (var orderTicket in this.OrderTicketsUp) { if (orderTicket.Status == OrderStatus.Submitted) { if(orderTicket.Tag.StartsWith("TP1_SYMBOL_UP_S2_OCO1" ) || orderTicket.Tag.StartsWith("SL_TP1_SYMBOL_UP_S2_OCO1")) { orderTicket.Cancel("SYMBOL OCO1 CANCEL 1 UP S2 TIMEOUT"); } if(orderTicket.Tag.StartsWith("TP2_SYMBOL_UP_S2_OCO1" ) || orderTicket.Tag.StartsWith("SL_TP2_SYMBOL_UP_S2_OCO1")) { orderTicket.Cancel("SYMBOL OCO1 CANCEL 2 UP S2 TIMEOUT"); } if(orderTicket.Tag.StartsWith("TP3_SYMBOL_UP_S2_OCO1" ) || orderTicket.Tag.StartsWith("SL_TP3_SYMBOL_UP_S2_OCO1")) { orderTicket.Cancel("SYMBOL OCO1 CANCEL 3 UP S2 TIMEOUT"); } if(orderTicket.Tag.StartsWith("TP4_SYMBOL_UP_S2_OCO1" ) || orderTicket.Tag.StartsWith("SL_TP4_SYMBOL_UP_S2_OCO1")) { orderTicket.Cancel("SYMBOL OCO1 CANCEL 4 UP S2 TIMEOUT"); } } } } public bool InOrders(int orderId) { foreach (var orderTicket in this.OrderTicketsUp) if (orderTicket.OrderId == orderId) return true; return false; } } public struct SYMBOL_OCO2_DOWN_S2 { public SYMBOL_OCO2_DOWN_S2(params OrderTicket[] orderTicketsDown) { this.OrderTicketsDown = orderTicketsDown; } private OrderTicket[] OrderTicketsDown { get; set; } public void Filled() { // Cancel all the outstanding tickets. foreach (var orderTicket in this.OrderTicketsDown) { if (orderTicket.Status == OrderStatus.Submitted) { if(orderTicket.Tag.StartsWith("TP1_SYMBOL_DOWN_S2_OCO2" ) || orderTicket.Tag.StartsWith("SL_TP1_SYMBOL_DOWN_S2_OCO2")) { orderTicket.Cancel("SYMBOL OCO2 CANCEL 1 DOWN S2"); } if(orderTicket.Tag.StartsWith("TP2_SYMBOL_DOWN_S2_OCO2" ) || orderTicket.Tag.StartsWith("SL_TP2_SYMBOL_DOWN_S2_OCO2")) { orderTicket.Cancel("SYMBOL OCO2 CANCEL 2 DOWN S2"); } if(orderTicket.Tag.StartsWith("TP3_SYMBOL_DOWN_S2_OCO2" ) || orderTicket.Tag.StartsWith("SL_TP3_SYMBOL_DOWN_S2_OCO2")) { orderTicket.Cancel("SYMBOL OCO2 CANCEL 3 DOWN S2"); } if(orderTicket.Tag.StartsWith("TP4_SYMBOL_DOWN_S2_OCO2" ) || orderTicket.Tag.StartsWith("SL_TP4_SYMBOL_DOWN_S2_OCO2")) { orderTicket.Cancel("SYMBOL OCO2 CANCEL 4 DOWN S2"); } } } } public void TimeOut() { // Cancel all the outstanding tickets. foreach (var orderTicket in this.OrderTicketsDown) { if (orderTicket.Status == OrderStatus.Submitted) { if(orderTicket.Tag.StartsWith("TP1_SYMBOL_DOWN_S2_OCO2" ) || orderTicket.Tag.StartsWith("SL_TP1_SYMBOL_DOWN_S2_OCO2")) { orderTicket.Cancel("SYMBOL OCO1 CANCEL 1 DOWN S2 TIMEOUT"); } if(orderTicket.Tag.StartsWith("TP2_SYMBOL_DOWN_S2_OCO2" ) || orderTicket.Tag.StartsWith("SL_TP2_SYMBOL_DOWN_S2_OCO2")) { orderTicket.Cancel("SYMBOL OCO1 CANCEL 2 DOWN S2 TIMEOUT"); } if(orderTicket.Tag.StartsWith("TP3_SYMBOL_DOWN_S2_OCO2" ) || orderTicket.Tag.StartsWith("SL_TP3_SYMBOL_DOWN_S2_OCO2")) { orderTicket.Cancel("SYMBOL OCO1 CANCEL 3 DOWN S2 TIMEOUT"); } if(orderTicket.Tag.StartsWith("TP4_SYMBOL_DOWN_S2_OCO2" ) || orderTicket.Tag.StartsWith("SL_TP4_SYMBOL_DOWN_S2_OCO2")) { orderTicket.Cancel("SYMBOL OCO1 CANCEL 4 DOWN S2 TIMEOUT"); } } } } public bool InOrders(int orderId) { foreach (var orderTicket in this.OrderTicketsDown) if (orderTicket.OrderId == orderId) return true; return false; } } public struct SYMBOL_OCO2_UP_S2 { public SYMBOL_OCO2_UP_S2(params OrderTicket[] orderTicketsUp) { this.OrderTicketsUp = orderTicketsUp; } private OrderTicket[] OrderTicketsUp { get; set; } public void Filled() { // Cancel all the outstanding tickets. foreach (var orderTicket in this.OrderTicketsUp) { if (orderTicket.Status == OrderStatus.Submitted) { if(orderTicket.Tag.StartsWith("TP1_SYMBOL_UP_S2_OCO2" ) || orderTicket.Tag.StartsWith("SL_TP1_SYMBOL_UP_S2_OCO2")) { orderTicket.Cancel("SYMBOL OCO2 CANCEL 1 UP S2"); } if(orderTicket.Tag.StartsWith("TP2_SYMBOL_UP_S2_OCO2" ) || orderTicket.Tag.StartsWith("SL_TP2_SYMBOL_UP_S2_OCO2")) { orderTicket.Cancel("SYMBOL OCO2 CANCEL 2 UP S2"); } if(orderTicket.Tag.StartsWith("TP3_SYMBOL_UP_S2_OCO2" ) || orderTicket.Tag.StartsWith("SL_TP3_SYMBOL_UP_S2_OCO2")) { orderTicket.Cancel("SYMBOL OCO2 CANCEL 3 UP S2"); } if(orderTicket.Tag.StartsWith("TP4_SYMBOL_UP_S2_OCO2" ) || orderTicket.Tag.StartsWith("SL_TP4_SYMBOL_UP_S2_OCO2")) { orderTicket.Cancel("SYMBOL OCO2 CANCEL 4 UP S2"); } } } } public void TimeOut() { // Cancel all the outstanding tickets. foreach (var orderTicket in this.OrderTicketsUp) { if (orderTicket.Status == OrderStatus.Submitted) { if(orderTicket.Tag.StartsWith("TP1_SYMBOL_UP_S2_OCO2" ) || orderTicket.Tag.StartsWith("SL_TP1_SYMBOL_UP_S2_OCO2")) { orderTicket.Cancel("SYMBOL OCO2 CANCEL 1 UP S2 TIMEOUT"); } if(orderTicket.Tag.StartsWith("TP2_SYMBOL_UP_S2_OCO2" ) || orderTicket.Tag.StartsWith("SL_TP2_SYMBOL_UP_S2_OCO2")) { orderTicket.Cancel("SYMBOL OCO2 CANCEL 2 UP S2 TIMEOUT"); } if(orderTicket.Tag.StartsWith("TP3_SYMBOL_UP_S2_OCO2" ) || orderTicket.Tag.StartsWith("SL_TP3_SYMBOL_UP_S2_OCO2")) { orderTicket.Cancel("SYMBOL OCO2 CANCEL 3 UP S2 TIMEOUT"); } if(orderTicket.Tag.StartsWith("TP4_SYMBOL_UP_S2_OCO2" ) || orderTicket.Tag.StartsWith("SL_TP4_SYMBOL_UP_S2_OCO2")) { orderTicket.Cancel("SYMBOL OCO2 CANCEL 4 UP S2 TIMEOUT"); } } } } public bool InOrders(int orderId) { foreach (var orderTicket in this.OrderTicketsUp) if (orderTicket.OrderId == orderId) return true; return false; } } public struct SYMBOL_OCO3_DOWN_S2 { public SYMBOL_OCO3_DOWN_S2(params OrderTicket[] orderTicketsDown) { this.OrderTicketsDown = orderTicketsDown; } private OrderTicket[] OrderTicketsDown { get; set; } public void Filled() { // Cancel all the outstanding tickets. foreach (var orderTicket in this.OrderTicketsDown) { if (orderTicket.Status == OrderStatus.Submitted) { if(orderTicket.Tag.StartsWith("TP1_SYMBOL_DOWN_S2_OCO3" ) || orderTicket.Tag.StartsWith("SL_TP1_SYMBOL_DOWN_S2_OCO3")) { orderTicket.Cancel("SYMBOL OCO3 CANCEL 1 DOWN S2"); } if(orderTicket.Tag.StartsWith("TP2_SYMBOL_DOWN_S2_OCO3" ) || orderTicket.Tag.StartsWith("SL_TP2_SYMBOL_DOWN_S2_OCO3")) { orderTicket.Cancel("SYMBOL OCO3 CANCEL 2 DOWN S2"); } if(orderTicket.Tag.StartsWith("TP3_SYMBOL_DOWN_S2_OCO3" ) || orderTicket.Tag.StartsWith("SL_TP3_SYMBOL_DOWN_S2_OCO3")) { orderTicket.Cancel("SYMBOL OCO3 CANCEL 3 DOWN S2"); } if(orderTicket.Tag.StartsWith("TP4_SYMBOL_DOWN_S2_OCO3" ) || orderTicket.Tag.StartsWith("SL_TP4_SYMBOL_DOWN_S2_OCO3")) { orderTicket.Cancel("SYMBOL OCO3 CANCEL 4 DOWN S2"); } } } } public void TimeOut() { // Cancel all the outstanding tickets. foreach (var orderTicket in this.OrderTicketsDown) { if (orderTicket.Status == OrderStatus.Submitted) { if(orderTicket.Tag.StartsWith("TP1_SYMBOL_DOWN_S2_OCO3" ) || orderTicket.Tag.StartsWith("SL_TP1_SYMBOL_DOWN_S2_OCO3")) { orderTicket.Cancel("SYMBOL OCO3 CANCEL 1 DOWN S2 TIMEOUT"); } if(orderTicket.Tag.StartsWith("TP2_SYMBOL_DOWN_S2_OCO3" ) || orderTicket.Tag.StartsWith("SL_TP2_SYMBOL_DOWN_S2_OCO3")) { orderTicket.Cancel("SYMBOL OCO3 CANCEL 2 DOWN S2 TIMEOUT"); } if(orderTicket.Tag.StartsWith("TP3_SYMBOL_DOWN_S2_OCO3" ) || orderTicket.Tag.StartsWith("SL_TP3_SYMBOL_DOWN_S2_OCO3")) { orderTicket.Cancel("SYMBOL OCO3 CANCEL 3 DOWN S2 TIMEOUT"); } if(orderTicket.Tag.StartsWith("TP4_SYMBOL_DOWN_S2_OCO3" ) || orderTicket.Tag.StartsWith("SL_TP4_SYMBOL_DOWN_S2_OCO3")) { orderTicket.Cancel("SYMBOL OCO3 CANCEL 4 DOWN S2 TIMEOUT"); } } } } public bool InOrders(int orderId) { foreach (var orderTicket in this.OrderTicketsDown) if (orderTicket.OrderId == orderId) return true; return false; } } public struct SYMBOL_OCO3_UP_S2 { public SYMBOL_OCO3_UP_S2(params OrderTicket[] orderTicketsUp) { this.OrderTicketsUp = orderTicketsUp; } private OrderTicket[] OrderTicketsUp { get; set; } public void Filled() { // Cancel all the outstanding tickets. foreach (var orderTicket in this.OrderTicketsUp) { if (orderTicket.Status == OrderStatus.Submitted) { if(orderTicket.Tag.StartsWith("TP1_SYMBOL_UP_S2_OCO3" ) || orderTicket.Tag.StartsWith("SL_TP1_SYMBOL_UP_S2_OCO3")) { orderTicket.Cancel("SYMBOL OCO3 CANCEL 1 UP S2"); } if(orderTicket.Tag.StartsWith("TP2_SYMBOL_UP_S2_OCO3" ) || orderTicket.Tag.StartsWith("SL_TP2_SYMBOL_UP_S2_OCO3")) { orderTicket.Cancel("SYMBOL OCO3 CANCEL 2 UP S2"); } if(orderTicket.Tag.StartsWith("TP3_SYMBOL_UP_S2_OCO3" ) || orderTicket.Tag.StartsWith("SL_TP3_SYMBOL_UP_S2_OCO3")) { orderTicket.Cancel("SYMBOL OCO3 CANCEL 3 UP S2"); } if(orderTicket.Tag.StartsWith("TP4_SYMBOL_UP_S2_OCO3" ) || orderTicket.Tag.StartsWith("SL_TP4_SYMBOL_UP_S2_OCO3")) { orderTicket.Cancel("SYMBOL OCO3 CANCEL 4 UP S2"); } } } } public void TimeOut() { // Cancel all the outstanding tickets. foreach (var orderTicket in this.OrderTicketsUp) { if (orderTicket.Status == OrderStatus.Submitted) { if(orderTicket.Tag.StartsWith("TP1_SYMBOL_UP_S2_OCO3" ) || orderTicket.Tag.StartsWith("SL_TP1_SYMBOL_UP_S2_OCO3")) { orderTicket.Cancel("SYMBOL OCO3 CANCEL 1 UP S2 TIMEOUT"); } if(orderTicket.Tag.StartsWith("TP2_SYMBOL_UP_S2_OCO3" ) || orderTicket.Tag.StartsWith("SL_TP2_SYMBOL_UP_S2_OCO3")) { orderTicket.Cancel("SYMBOL OCO3 CANCEL 2 UP S2 TIMEOUT"); } if(orderTicket.Tag.StartsWith("TP3_SYMBOL_UP_S2_OCO3" ) || orderTicket.Tag.StartsWith("SL_TP3_SYMBOL_UP_S2_OCO3")) { orderTicket.Cancel("SYMBOL OCO3 CANCEL 3 UP S2 TIMEOUT"); } if(orderTicket.Tag.StartsWith("TP4_SYMBOL_UP_S2_OCO3" ) || orderTicket.Tag.StartsWith("SL_TP4_SYMBOL_UP_S2_OCO3")) { orderTicket.Cancel("SYMBOL OCO3 CANCEL 4 UP S2 TIMEOUT"); } } } } public bool InOrders(int orderId) { foreach (var orderTicket in this.OrderTicketsUp) if (orderTicket.OrderId == orderId) return true; return false; } } public struct SYMBOL_OCO4_DOWN_S2 { public SYMBOL_OCO4_DOWN_S2(params OrderTicket[] orderTicketsDown) { this.OrderTicketsDown = orderTicketsDown; } private OrderTicket[] OrderTicketsDown { get; set; } public void Filled() { // Cancel all the outstanding tickets. foreach (var orderTicket in this.OrderTicketsDown) { if (orderTicket.Status == OrderStatus.Submitted) { if(orderTicket.Tag.StartsWith("TP1_SYMBOL_DOWN_S2_OCO4" ) || orderTicket.Tag.StartsWith("SL_TP1_SYMBOL_DOWN_S2_OCO4")) { orderTicket.Cancel("SYMBOL OCO4 CANCEL 1 DOWN S2"); } if(orderTicket.Tag.StartsWith("TP2_SYMBOL_DOWN_S2_OCO4" ) || orderTicket.Tag.StartsWith("SL_TP2_SYMBOL_DOWN_S2_OCO4")) { orderTicket.Cancel("SYMBOL OCO4 CANCEL 2 DOWN S2"); } if(orderTicket.Tag.StartsWith("TP3_SYMBOL_DOWN_S2_OCO4" ) || orderTicket.Tag.StartsWith("SL_TP3_SYMBOL_DOWN_S2_OCO4")) { orderTicket.Cancel("SYMBOL OCO4 CANCEL 3 DOWN S2"); } if(orderTicket.Tag.StartsWith("TP4_SYMBOL_DOWN_S2_OCO4" ) || orderTicket.Tag.StartsWith("SL_TP4_SYMBOL_DOWN_S2_OCO4")) { orderTicket.Cancel("SYMBOL OCO4 CANCEL 4 DOWN S2"); } } } } public void TimeOut() { // Cancel all the outstanding tickets. foreach (var orderTicket in this.OrderTicketsDown) { if (orderTicket.Status == OrderStatus.Submitted) { if(orderTicket.Tag.StartsWith("TP1_SYMBOL_DOWN_S2_OCO4" ) || orderTicket.Tag.StartsWith("SL_TP1_SYMBOL_DOWN_S2_OCO4")) { orderTicket.Cancel("SYMBOL OCO4 CANCEL 1 DOWN S2 TIMEOUT"); } if(orderTicket.Tag.StartsWith("TP2_SYMBOL_DOWN_S2_OCO4" ) || orderTicket.Tag.StartsWith("SL_TP2_SYMBOL_DOWN_S2_OCO4")) { orderTicket.Cancel("SYMBOL OCO4 CANCEL 2 DOWN S2 TIMEOUT"); } if(orderTicket.Tag.StartsWith("TP3_SYMBOL_DOWN_S2_OCO4" ) || orderTicket.Tag.StartsWith("SL_TP3_SYMBOL_DOWN_S2_OCO4")) { orderTicket.Cancel("SYMBOL OCO4 CANCEL 3 DOWN S2 TIMEOUT"); } if(orderTicket.Tag.StartsWith("TP4_SYMBOL_DOWN_S2_OCO4" ) || orderTicket.Tag.StartsWith("SL_TP4_SYMBOL_DOWN_S2_OCO4")) { orderTicket.Cancel("SYMBOL OCO4 CANCEL 4 DOWN S2 TIMEOUT"); } } } } public bool InOrders(int orderId) { foreach (var orderTicket in this.OrderTicketsDown) if (orderTicket.OrderId == orderId) return true; return false; } } public struct SYMBOL_OCO4_UP_S2 { public SYMBOL_OCO4_UP_S2(params OrderTicket[] orderTicketsUp) { this.OrderTicketsUp = orderTicketsUp; } private OrderTicket[] OrderTicketsUp { get; set; } public void Filled() { // Cancel all the outstanding tickets. foreach (var orderTicket in this.OrderTicketsUp) { if (orderTicket.Status == OrderStatus.Submitted) { if(orderTicket.Tag.StartsWith("TP1_SYMBOL_UP_S2_OCO4" ) || orderTicket.Tag.StartsWith("SL_TP1_SYMBOL_UP_S2_OCO4")) { orderTicket.Cancel("SYMBOL OCO4 CANCEL 1 UP S2"); } if(orderTicket.Tag.StartsWith("TP2_SYMBOL_UP_S2_OCO4" ) || orderTicket.Tag.StartsWith("SL_TP2_SYMBOL_UP_S2_OCO4")) { orderTicket.Cancel("SYMBOL OCO4 CANCEL 2 UP S2"); } if(orderTicket.Tag.StartsWith("TP3_SYMBOL_UP_S2_OCO4" ) || orderTicket.Tag.StartsWith("SL_TP3_SYMBOL_UP_S2_OCO4")) { orderTicket.Cancel("SYMBOL OCO4 CANCEL 3 UP S2"); } if(orderTicket.Tag.StartsWith("TP4_SYMBOL_UP_S2_OCO4" ) || orderTicket.Tag.StartsWith("SL_TP4_SYMBOL_UP_S2_OCO4")) { orderTicket.Cancel("SYMBOL OCO4 CANCEL 4 UP S2"); } } } } public void TimeOut() { // Cancel all the outstanding tickets. foreach (var orderTicket in this.OrderTicketsUp) { if (orderTicket.Status == OrderStatus.Submitted) { if(orderTicket.Tag.StartsWith("TP1_SYMBOL_UP_S2_OCO4" ) || orderTicket.Tag.StartsWith("SL_TP1_SYMBOL_UP_S2_OCO4")) { orderTicket.Cancel("SYMBOL OCO4 CANCEL 1 UP S2 TIMEOUT"); } if(orderTicket.Tag.StartsWith("TP2_SYMBOL_UP_S2_OCO4" ) || orderTicket.Tag.StartsWith("SL_TP2_SYMBOL_UP_S2_OCO4")) { orderTicket.Cancel("SYMBOL OCO4 CANCEL 2 UP S2 TIMEOUT"); } if(orderTicket.Tag.StartsWith("TP3_SYMBOL_UP_S2_OCO4" ) || orderTicket.Tag.StartsWith("SL_TP3_SYMBOL_UP_S2_OCO4")) { orderTicket.Cancel("SYMBOL OCO4 CANCEL 3 UP S2 TIMEOUT"); } if(orderTicket.Tag.StartsWith("TP4_SYMBOL_UP_S2_OCO4" ) || orderTicket.Tag.StartsWith("SL_TP4_SYMBOL_UP_S2_OCO4")) { orderTicket.Cancel("SYMBOL OCO4 CANCEL 4 UP S2 TIMEOUT"); } } } } public bool InOrders(int orderId) { foreach (var orderTicket in this.OrderTicketsUp) if (orderTicket.OrderId == orderId) return true; return false; } } }
using System; using System.Collections.Generic; using System.Linq; using QuantConnect.Data; using QuantConnect.Data.Consolidators; using QuantConnect.Data.Market; using QuantConnect.Indicators; using QuantConnect.Orders; using QuantConnect.Algorithm.Framework.Alphas; using QuantConnect.Algorithm.Framework.Execution; using QuantConnect.Algorithm.Framework.Portfolio; using QuantConnect.Algorithm.Framework.Selection; namespace QuantConnect { public struct SYMBOL_DOWN_S2_OnOrderEvent { private QCAlgorithm _algorithm; public SYMBOL_DOWN_S2_OnOrderEvent(QCAlgorithm algorithm) { _algorithm = algorithm; } public void OnOrderEvent(QCAlgorithm _algorithm, OrderEvent orderEvent) { var OrderID = _algorithm.Transactions.GetOrderById(orderEvent.OrderId); if (orderEvent.Status == OrderStatus.Filled) { if (OrderID.Tag.StartsWith("SYMBOL OCO1 CANCEL 1 DOWN S2") || OrderID.Tag.StartsWith("SYMBOL OCO1 CANCEL 2 DOWN S2")) { _algorithm.MarketOrder(OrderID.Symbol, -OrderID.Quantity, false, "OrderID rescue SYMBOL S2"); } } if (Base.SYMBOL_OCO1_EO1_DOWN_S2) Base.SYMBOL_OCO1_EO1_DOWN_S2_ID = orderEvent.OrderId; if (orderEvent.Status == OrderStatus.Filled || orderEvent.Status == OrderStatus.PartiallyFilled) { if (orderEvent.OrderId == Base.SYMBOL_OCO1_EO1_DOWN_S2_ID) { Base.SYMBOL_OOFE_OCO1_DOWN_S2_1 = (algo1, symbol, filledPrice, fillQ) => { return new SYMBOL_OCO1_DOWN_S2( algo1.StopMarketOrder(orderEvent.Symbol, -orderEvent.Quantity, orderEvent.FillPrice + Base.SYMBOL_DOWN_S2_SL_1,"SL_TP1_SYMBOL_DOWN_S2_OCO1"), algo1.LimitOrder(orderEvent.Symbol, -orderEvent.Quantity, orderEvent.FillPrice - Base.SYMBOL_DOWN_S2_TP1_1,"TP1_SYMBOL_DOWN_S2_OCO1")); }; Base.SYMBOL_DOWN_S2_OCO1Sets1.Add( Base.SYMBOL_OOFE_OCO1_DOWN_S2_1(_algorithm, orderEvent.Symbol, orderEvent.FillPrice, orderEvent.Quantity)); Base.SYMBOL_OCO1_EO1_DOWN_S2_ID = -1; } int SYMBOL_OCO1S1_DOWN_S2 = 0; for (; SYMBOL_OCO1S1_DOWN_S2 < Base.SYMBOL_DOWN_S2_OCO1Sets1.Count; SYMBOL_OCO1S1_DOWN_S2++) if (Base.SYMBOL_DOWN_S2_OCO1Sets1[SYMBOL_OCO1S1_DOWN_S2].InOrders(orderEvent.OrderId)) { Base.SYMBOL_DOWN_S2_OCO1Sets1[SYMBOL_OCO1S1_DOWN_S2].Filled(); Base.SYMBOL_DOWN_S2_OCO1Sets1.RemoveAt(SYMBOL_OCO1S1_DOWN_S2); SYMBOL_OCO1S1_DOWN_S2 = 0; break; } } } } }
using System; namespace QuantConnect.Data.Consolidators { [Obsolete("CalendarType is obsolete, please use Calendar instead")] public static class CalendarTypee { /// <summary> /// Computes the start of week (previous Monday) of given date/time /// </summary> public static Func<DateTime, CalendarInfo> Weekly => Calendar.Weekly; /// <summary> /// Computes the start of month (1st of the current month) of given date/time /// </summary> public static Func<DateTime, CalendarInfo> Monthly => Calendar.Monthly; public static Func<DateTime, CalendarInfo> Yearly => Calendar.Yearly; public static Func<DateTime, CalendarInfo> Quarterly => Calendar.Quarterly; } }