/// <summary> /// Primary constructor /// </summary> public CandlePriceSaver(string u, string p) { m_username = u; m_password = p; instrumentList = ContractUtilities.ContractMetaInfo.cmeFuturesTickerheadList.Union(ContractUtilities.ContractMetaInfo.iceFuturesTickerheadList).ToArray(); liquidContractList = new ContractUtilities.ContractList(instrumentList); DateTime referanceDate = DateTime.Today; int minInterval = 10; //30 startTime = new DateTime(referanceDate.Year, referanceDate.Month, referanceDate.Day, 1, 0, 0); endTime = new DateTime(referanceDate.Year, referanceDate.Month, referanceDate.Day, 9, 0, 0); //900 candleObj = new DataAnalysis.CandleStick(startTime, endTime, liquidContractList.dbTickerList.ToArray(), minInterval); OutputFolder = TA.DirectoryNames.GetDirectoryName(ext: "overnightCandlestick") + TA.DirectoryNames.GetDirectoryExtension(DateTime.Now.Date); System.IO.Directory.CreateDirectory(OutputFolder); ttapiSubs = new ttapiUtils.Subscription(m_username, m_password); ttapiSubs.dbTickerList = liquidContractList.dbTickerList; ttapiSubs.ilsUpdateList = new List <EventHandler <InstrumentLookupSubscriptionEventArgs> > { ttapiSubs.startPriceSubscriptions }; ttapiSubs.asu_update = ttapiSubs.startInstrumentLookupSubscriptions; ttapiSubs.priceUpdatedEventHandler = m_ps_FieldsUpdated; }
/// <summary> /// Primary constructor /// </summary> public saveVolume(string u, string p) { m_username = u; m_password = p; OutputFolder = TA.DirectoryNames.GetDirectoryName(ext: "ttapiContractVolume") + TA.DirectoryNames.GetDirectoryExtension(DateTime.Now.Date); System.IO.Directory.CreateDirectory(OutputFolder); ilsUpdateList = new List <EventHandler <InstrumentLookupSubscriptionEventArgs> >(); sw = new StreamWriter(OutputFolder + "/ContractList.csv"); sw.WriteLine("{0},{1},{2},{3},{4}", "InstrumentName", "MarketKey", "ProductType", "ProductName", "Volume"); sw.Flush(); TTAPISubs = new ttapiUtils.Subscription(m_username, m_password); IlsDictionary = TTAPISubs.IlsDictionary; TTAPISubs.asu_update = TTAPISubs.startProductLookupSubscriptions; TTAPISubs.PLSEventHandler = TTAPISubs.Subscribe2InstrumentCatalogs; TTAPISubs.ICUEventHandler = TTAPISubs.StartInstrumentLookupSubscriptionsFromCatalog; ilsUpdateList.Add(TTAPISubs.startPriceSubscriptions); TTAPISubs.priceUpdatedEventHandler = WriteVolume2File; TTAPISubs.ilsUpdateList = ilsUpdateList; }
/// <summary> /// Primary constructor /// </summary> public saveVolume(string u, string p) { m_username = u; m_password = p; OutputFolder = TA.DirectoryNames.GetDirectoryName(ext: "ttapiContractVolume") + TA.DirectoryNames.GetDirectoryExtension(DateTime.Now.Date); System.IO.Directory.CreateDirectory(OutputFolder); ilsUpdateList = new List<EventHandler<InstrumentLookupSubscriptionEventArgs>>(); sw = new StreamWriter(OutputFolder + "/ContractList.csv"); sw.WriteLine("{0},{1},{2},{3},{4}", "InstrumentName", "MarketKey", "ProductType", "ProductName", "Volume"); sw.Flush(); TTAPISubs = new ttapiUtils.Subscription(m_username, m_password); IlsDictionary = TTAPISubs.IlsDictionary; TTAPISubs.asu_update = TTAPISubs.startProductLookupSubscriptions; TTAPISubs.PLSEventHandler = TTAPISubs.Subscribe2InstrumentCatalogs; TTAPISubs.ICUEventHandler = TTAPISubs.StartInstrumentLookupSubscriptionsFromCatalog; ilsUpdateList.Add(TTAPISubs.startPriceSubscriptions); TTAPISubs.priceUpdatedEventHandler = WriteVolume2File; TTAPISubs.ilsUpdateList = ilsUpdateList; }
/// <summary> /// Primary constructor /// </summary> public CandlePriceSaver(string u, string p) { m_username = u; m_password = p; instrumentList = ContractUtilities.ContractMetaInfo.cmeFuturesTickerheadList.Union(ContractUtilities.ContractMetaInfo.iceFuturesTickerheadList).ToArray(); liquidContractList = new ContractUtilities.ContractList(instrumentList); DateTime referanceDate = DateTime.Today; int minInterval = 10; //30 startTime = new DateTime(referanceDate.Year, referanceDate.Month, referanceDate.Day, 1, 0, 0); endTime = new DateTime(referanceDate.Year, referanceDate.Month, referanceDate.Day, 9, 0, 0); //900 candleObj = new DataAnalysis.CandleStick(startTime, endTime, liquidContractList.dbTickerList.ToArray(), minInterval); OutputFolder = TA.DirectoryNames.GetDirectoryName(ext: "overnightCandlestick") + TA.DirectoryNames.GetDirectoryExtension(DateTime.Now.Date); System.IO.Directory.CreateDirectory(OutputFolder); ttapiSubs = new ttapiUtils.Subscription(m_username, m_password); ttapiSubs.dbTickerList = liquidContractList.dbTickerList; ttapiSubs.ilsUpdateList = new List<EventHandler<InstrumentLookupSubscriptionEventArgs>> { ttapiSubs.startPriceSubscriptions }; ttapiSubs.asu_update = ttapiSubs.startInstrumentLookupSubscriptions; ttapiSubs.priceUpdatedEventHandler = m_ps_FieldsUpdated; }
public static void SendLimitOrder(TradingTechnologies.TTAPI.Instrument instrument, TradingTechnologies.TTAPI.Price price, int qty, Subscription ttapisubs, string orderTag="") { BuySell Direction = BuySell.Buy; string AccountName = "H1KOC"; if (qty<0) { Direction = BuySell.Sell; qty = -qty; } string tickerDB = TA.TickerConverters.ConvertFromTTAPIFields2DB(instrument.Product.ToString(), instrument.Name.ToString()); string[] TickerList = tickerDB.Split('-'); string TickerHead = ContractUtilities.ContractMetaInfo.GetContractSpecs(TickerList[0]).tickerHead; string ExchangeName = ContractUtilities.ContractMetaInfo.GetExchange4Tickerhead(TickerHead); AccountType AccType = AccountType.P2; OrderProfile op = new OrderProfile(instrument.GetValidOrderFeeds()[0], instrument); op.BuySell = Direction; op.AccountName = AccountName; if (ExchangeName == "ICE") { AccType = AccountType.G2; op.GiveUp = "5283"; } op.AccountType = AccType; op.OrderQuantity = Quantity.FromInt(instrument, qty); op.OrderType = OrderType.Limit; if (orderTag.Count()>0) { op.OrderTag = orderTag; } op.LimitPrice = price; InstrumentTradeSubscription TS = ttapisubs.TsDictionary[instrument.Key]; if (!TS.SendOrder(op)) { Console.WriteLine("Send new order failed. {0}", op.RoutingStatus.Message); ttapisubs.Dispose(); } else { Console.WriteLine("Send new order succeeded."); } }
/// <summary> /// Primary constructor /// </summary> public BreakoutTrader(string u, string p) { m_username = u; m_password = p; todayDate = DateTime.Now.Date; OutputFolder = TA.DirectoryNames.GetDirectoryName("overnightCandlestick") + TA.DirectoryNames.GetDirectoryExtension(todayDate); LogFile = new StreamWriter(OutputFolder + "/Log" + DateTime.Now.ToString("HHmmss") + ".txt", true); BreakoutLogger = new Logger(LogFile); instrumentList = ContractUtilities.ContractMetaInfo.cmeFuturesTickerheadList.Union(ContractUtilities.ContractMetaInfo.iceFuturesTickerheadList).ToArray(); liquidContractList = new ContractUtilities.ContractList(instrumentList); ttapiTickerList = liquidContractList.ttapiTickerList; dbTickerList = liquidContractList.dbTickerList; DateTimePastPnlDisplay = DateTime.MinValue; BreakoutPosition = new Portfolio.Position(fullTickerList: dbTickerList); CovMatrix = Risk.PorfolioRisk.LoadCovMatrix(); candleStickData = new DataTable(); rangeMinList = new List <double>(); rangeMaxList = new List <double>(); ilsUpdateList = new List <EventHandler <InstrumentLookupSubscriptionEventArgs> >(); candleStickData.ReadXml(OutputFolder + "/" + TA.FileNames.candlestick_signal_file); StdDict = new Dictionary <string, double>(); QtyDict = new Dictionary <string, int>(); startTime = new DateTime(todayDate.Year, todayDate.Month, todayDate.Day, 8, 30, 0); endTime = new DateTime(todayDate.Year, todayDate.Month, todayDate.Day, 9, 0, 0); //900 LastTradeEntryTime = new DateTime(todayDate.Year, todayDate.Month, todayDate.Day, 9, 20, 0); //920 selectedCandleStickData = candleStickData.Select("start>= #" + startTime.ToString() + "# AND end<= #" + endTime.ToString() + " # "); for (int i = 0; i < dbTickerList.Count; i++) { double rangeMin = double.MaxValue; double rangeMax = double.MinValue; for (int j = 0; j < selectedCandleStickData.Length; j++) { rangeMax = Math.Max(selectedCandleStickData[j].Field <Double>(dbTickerList[i] + "_high"), rangeMax); rangeMin = Math.Min(selectedCandleStickData[j].Field <Double>(dbTickerList[i] + "_low"), rangeMin); } rangeMinList.Add(rangeMin); rangeMaxList.Add(rangeMax); StdDict.Add(dbTickerList[i], Risk.PorfolioRisk.GetStd4Ticker(dbTickerList[i], CovMatrix)); QtyDict.Add(dbTickerList[i], (int)Math.Min(MaxQty4Ticker, Math.Floor(StdPerBet / StdDict[dbTickerList[i]]))); } // ttapiSubs = new ttapiUtils.Subscription(m_username, m_password); ttapiSubs.dbTickerList = dbTickerList; ilsUpdateList.Add(ttapiSubs.startPriceSubscriptions); ilsUpdateList.Add(ttapiSubs.startTradeSubscriptions); ttapiSubs.ilsUpdateList = ilsUpdateList; ttapiSubs.asu_update = ttapiSubs.startInstrumentLookupSubscriptions; ttapiSubs.priceUpdatedEventHandler = BreakoutAlgo; ttapiSubs.orderFilledEventHandler = BreakoutStopLogic; }
public Algo(string u, string p) { m_username = u; m_password = p; NumBets = 1; MaxBetSize = 900; connection = new mysql(); conn = connection.conn; string OutputFolder = TA.DirectoryNames.GetDirectoryName("daily"); StreamWriter LogFile = new StreamWriter(OutputFolder + "/IFS.txt", true); IFSLogger = new Logger(LogFile); DateTime ReportDate = CalendarUtilities.BusinessDays.GetBusinessDayShifted(shiftInDays: -1); DateTime TodayDate = DateTime.Now.Date; IFSLogger.SW.WriteLine(); IFSLogger.Log("NOTES FOR " + TodayDate.ToString("MM/dd/yyyy")); IFSLogger.Log(new String('-', 20)); string DirectoryName = TA.DirectoryNames.GetDirectoryName(ext: "ifsOutput"); string DirectoryExtension = TA.DirectoryNames.GetDirectoryExtension(directoryDate: ReportDate); DataSet PythonOutput = IOUtilities.ExcelDataReader.LoadFile(DirectoryName + "/" + DirectoryExtension + "/ifs.xlsx"); DataTable IfsSheet = PythonOutput.Tables["all"]; DataColumn ExistingPositionColumn = new DataColumn("ExistingPosition", typeof(int)); ExistingPositionColumn.DefaultValue = 0; IfsSheet.Columns.Add(ExistingPositionColumn); DataColumn ExistingPositionAbsColumn = new DataColumn("ExistingPositionAbs", typeof(int)); ExistingPositionAbsColumn.DefaultValue = 0; IfsSheet.Columns.Add(ExistingPositionAbsColumn); IfsSheet.Columns.Add("Alias", typeof(string)); AutoSpreaderList = new List<ttapiUtils.AutoSpreader>(); MarketPriceTable = new DataTable(); MarketPriceTable.Columns.Add("Ticker", typeof(string)); MarketPriceTable.Columns.Add("TickerHead", typeof(string)); MarketPriceTable.Columns.Add("Tag", typeof(string)); MarketPriceTable.Columns.Add("Alias", typeof(string)); MarketPriceTable.Columns.Add("DescriptionString", typeof(string)); MarketPriceTable.Columns.Add("Settle", typeof(decimal)); MarketPriceTable.Columns.Add("Bid", typeof(decimal)); MarketPriceTable.Columns.Add("Ask", typeof(decimal)); MarketPriceTable.Columns.Add("Mid", typeof(decimal)); MarketPriceTable.Columns.Add("MidConverted", typeof(decimal)); MarketPriceTable.Columns.Add("SuggestedPosition", typeof(int)); MarketPriceTable.Columns.Add("WorkingPosition", typeof(int)); DataColumn ExistingPositionColumn2 = new DataColumn("ExistingPosition", typeof(int)); ExistingPositionColumn2.DefaultValue = 0; MarketPriceTable.Columns.Add(ExistingPositionColumn2); DataColumn ExistingPositionTickerHeadColumn = new DataColumn("ExistingPositionTickerHead", typeof(int)); ExistingPositionTickerHeadColumn.DefaultValue = 0; MarketPriceTable.Columns.Add(ExistingPositionTickerHeadColumn); MarketPriceTable.Columns.Add("Mean", typeof(double)); MarketPriceTable.Columns.Add("Std", typeof(double)); List<string> OpenStrategyList = TA.Strategy.GetFilteredOpenStrategyList(asOfDate: TodayDate, conn: conn, strategyClass: "ifs"); for (int i = 0; i < OpenStrategyList.Count; i++) { StrategyUtilities.PositionManagerOutput PositionManagerOut = PositionManager.GetIfsPosition(alias: OpenStrategyList[i], asOfDate: TodayDate, conn: conn); if (PositionManagerOut.CorrectPositionQ) { string SelectString = "contract1='" + PositionManagerOut.SortedPosition.Rows[0].Field<string>("Ticker") + "' and contract2='" + PositionManagerOut.SortedPosition.Rows[1].Field<string>("Ticker") + "'"; if (PositionManagerOut.SortedPosition.Rows.Count==3) { SelectString = SelectString + " and contract3='" + PositionManagerOut.SortedPosition.Rows[2].Field<string>("Ticker") + "'"; } DataRow Row = IfsSheet.Select(SelectString)[0]; Row["ExistingPosition"] = Math.Round(PositionManagerOut.Scale); Row["ExistingPositionAbs"] = Math.Abs(PositionManagerOut.Scale); Row["Alias"] = OpenStrategyList[i]; } else { IFSLogger.Log("Check " + OpenStrategyList[i] + " ! Position may be incorrect."); } } DataTable newDataTable = IfsSheet.AsEnumerable() .OrderBy(r => r.Field<string>("spread_description")) .ThenByDescending(r => r.Field<double>("min_volume")) .ThenByDescending(r => r.Field<int>("ExistingPositionAbs")) .CopyToDataTable(); DataRow[] ExistingPositions = IfsSheet.Select("ExistingPositionAbs>0"); LiquidSpreads = newDataTable.AsEnumerable().GroupBy(r => r["spread_description"]).Select(w => w.First()).CopyToDataTable(); LiquidSpreads = LiquidSpreads.Select("upside<" + MaxBetSize).CopyToDataTable(); foreach (DataRow item in ExistingPositions) { LiquidSpreads.ImportRow(item); } LiquidSpreads = LiquidSpreads.AsEnumerable().GroupBy(r => r["ticker"]).Select(w => w.First()).CopyToDataTable(); for (int i = 0; i < LiquidSpreads.Rows.Count; i++) { List<string> TickerList = new List<string> { LiquidSpreads.Rows[i].Field<string>("Contract1"), LiquidSpreads.Rows[i].Field<string>("Contract2"), LiquidSpreads.Rows[i].Field<string>("Contract3") }; TickerList.RemoveAll(item => item == null); AutoSpreaderList.Add(new ttapiUtils.AutoSpreader(dbTickerList:TickerList,payUpTicks:2)); DataRow Row = MarketPriceTable.NewRow(); Row["Ticker"] = AutoSpreaderList[i].AutoSpreaderName; Row["TickerHead"] = LiquidSpreads.Rows[i].Field<string>("spread_description"); Row["Tag"] = "ifs_" + i.ToString(); if (LiquidSpreads.Rows[i].Field<int>("ExistingPositionAbs")!=0) { Row["Alias"] = LiquidSpreads.Rows[i].Field<string>("alias"); Row["ExistingPosition"] = LiquidSpreads.Rows[i].Field<int>("ExistingPosition"); } else { Row["Alias"] = LiquidSpreads.Rows[i].Field<string>("spread_description") + "_ifs_" + DateTime.Now.ToString("MMM-yyyy"); } Row["DescriptionString"] = "strategy_class=ifs&betsize=" + MaxBetSize.ToString(); Row["WorkingPosition"] = 0; Row["Settle"] = LiquidSpreads.Rows[i].Field<double>("settle"); Row["Mean"] = LiquidSpreads.Rows[i].Field<double>("mean1"); Row["Std"] = LiquidSpreads.Rows[i].Field<double>("std1"); MarketPriceTable.Rows.Add(Row); } List<string> TickerHeadList = DataAnalysis.DataTableFunctions.GetColumnAsList<string>(dataTableInput: MarketPriceTable, columnName: "TickerHead", uniqueQ: true); foreach (string item in TickerHeadList) { DataRow[] RowList = MarketPriceTable.Select("TickerHead='" + item + "'"); int TotalPosition = 0; foreach (DataRow Row in RowList) { TotalPosition += Row.Field<int>("ExistingPosition"); } foreach (DataRow Row in RowList) { Row["ExistingPositionTickerHead"] = TotalPosition; } } ASENameList = MarketPriceTable.AsEnumerable().Select(x => x.Field<string>("Ticker")).ToList(); TagList = MarketPriceTable.AsEnumerable().Select(x => x.Field<string>("Tag")).ToList(); TTAPISubs = new ttapiUtils.Subscription(m_username, m_password); TTAPISubs.AutoSpreaderList = AutoSpreaderList; TTAPISubs.AsuUpdateList = new List<EventHandler<AuthenticationStatusUpdateEventArgs>> { TTAPISubs.StartASESubscriptions }; TTAPISubs.priceUpdatedEventHandler = m_ps_FieldsUpdated; TTAPISubs.orderFilledEventHandler = m_ts_OrderFilled; }
/// <summary> /// Primary constructor /// </summary> public BreakoutTrader(string u, string p) { m_username = u; m_password = p; todayDate = DateTime.Now.Date; OutputFolder = TA.DirectoryNames.GetDirectoryName("overnightCandlestick") + TA.DirectoryNames.GetDirectoryExtension(todayDate); LogFile = new StreamWriter(OutputFolder + "/Log" + DateTime.Now.ToString("HHmmss") + ".txt", true); BreakoutLogger = new Logger(LogFile); instrumentList = ContractUtilities.ContractMetaInfo.cmeFuturesTickerheadList.Union(ContractUtilities.ContractMetaInfo.iceFuturesTickerheadList).ToArray(); liquidContractList = new ContractUtilities.ContractList(instrumentList); ttapiTickerList = liquidContractList.ttapiTickerList; dbTickerList = liquidContractList.dbTickerList; DateTimePastPnlDisplay = DateTime.MinValue; BreakoutPosition = new Portfolio.Position(fullTickerList: dbTickerList); CovMatrix = Risk.PorfolioRisk.LoadCovMatrix(); candleStickData = new DataTable(); rangeMinList = new List<double>(); rangeMaxList = new List<double>(); ilsUpdateList = new List<EventHandler<InstrumentLookupSubscriptionEventArgs>>(); candleStickData.ReadXml(OutputFolder + "/" + TA.FileNames.candlestick_signal_file); StdDict = new Dictionary<string, double>(); QtyDict = new Dictionary<string, int>(); startTime = new DateTime(todayDate.Year, todayDate.Month, todayDate.Day, 8, 30, 0); endTime = new DateTime(todayDate.Year, todayDate.Month, todayDate.Day, 9, 0, 0); //900 LastTradeEntryTime = new DateTime(todayDate.Year, todayDate.Month, todayDate.Day, 9, 20, 0); //920 selectedCandleStickData = candleStickData.Select("start>= #" + startTime.ToString() + "# AND end<= #" + endTime.ToString() + " # "); for (int i = 0; i < dbTickerList.Count; i++) { double rangeMin = double.MaxValue; double rangeMax = double.MinValue; for (int j = 0; j < selectedCandleStickData.Length; j++) { rangeMax = Math.Max(selectedCandleStickData[j].Field<Double>(dbTickerList[i] + "_high"), rangeMax); rangeMin = Math.Min(selectedCandleStickData[j].Field<Double>(dbTickerList[i] + "_low"), rangeMin); } rangeMinList.Add(rangeMin); rangeMaxList.Add(rangeMax); StdDict.Add(dbTickerList[i], Risk.PorfolioRisk.GetStd4Ticker(dbTickerList[i], CovMatrix)); QtyDict.Add(dbTickerList[i], (int)Math.Min(MaxQty4Ticker, Math.Floor(StdPerBet / StdDict[dbTickerList[i]]))); } // ttapiSubs = new ttapiUtils.Subscription(m_username, m_password); ttapiSubs.dbTickerList = dbTickerList; ilsUpdateList.Add(ttapiSubs.startPriceSubscriptions); ilsUpdateList.Add(ttapiSubs.startTradeSubscriptions); ttapiSubs.ilsUpdateList = ilsUpdateList; ttapiSubs.asu_update = ttapiSubs.startInstrumentLookupSubscriptions; ttapiSubs.priceUpdatedEventHandler = BreakoutAlgo; ttapiSubs.orderFilledEventHandler = BreakoutStopLogic; }
public Algo(string u, string p) { m_username = u; m_password = p; NumBets = 3; MaxBetSize = 700; connection = new mysql(); conn = connection.conn; string OutputFolder = TA.DirectoryNames.GetDirectoryName("daily"); StreamWriter LogFile = new StreamWriter(OutputFolder + "/IFS.txt", true); IFSLogger = new Logger(LogFile); DateTime ReportDate = CalendarUtilities.BusinessDays.GetBusinessDayShifted(shiftInDays: -1); DateTime TodayDate = DateTime.Now.Date; IFSLogger.SW.WriteLine(); IFSLogger.Log("NOTES FOR " + TodayDate.ToString("MM/dd/yyyy")); IFSLogger.Log(new String('-', 20)); string DirectoryName = TA.DirectoryNames.GetDirectoryName(ext: "ifsOutput"); string DirectoryExtension = TA.DirectoryNames.GetDirectoryExtension(directoryDate: ReportDate); DataSet PythonOutput = IOUtilities.ExcelDataReader.LoadFile(DirectoryName + "/" + DirectoryExtension + "/ifs.xlsx"); DataTable IfsSheet = PythonOutput.Tables["all"]; TradeTimer = new System.Timers.Timer(); TradeTimer.Elapsed += new ElapsedEventHandler(PeriodicCall); TradeTimer.Interval = 30000; // And start it TradeTimer.Enabled = true; DataColumn ExistingPositionColumn = new DataColumn("ExistingPosition", typeof(int)); ExistingPositionColumn.DefaultValue = 0; IfsSheet.Columns.Add(ExistingPositionColumn); DataColumn ExistingPositionAbsColumn = new DataColumn("ExistingPositionAbs", typeof(int)); ExistingPositionAbsColumn.DefaultValue = 0; IfsSheet.Columns.Add(ExistingPositionAbsColumn); IfsSheet.Columns.Add("Alias", typeof(string)); AutoSpreaderList = new List <ttapiUtils.AutoSpreader>(); AutoSpreaderDictionary = new Dictionary <string, ttapiUtils.AutoSpreader>(); SummaryTable = new DataTable(); SummaryTable.Columns.Add("Ticker", typeof(string)); SummaryTable.Columns.Add("TickerHead", typeof(string)); SummaryTable.Columns.Add("Tag", typeof(string)); SummaryTable.Columns.Add("Alias", typeof(string)); SummaryTable.Columns.Add("DescriptionString", typeof(string)); SummaryTable.Columns.Add("Settle", typeof(decimal)); SummaryTable.Columns.Add("Bid", typeof(double)); SummaryTable.Columns.Add("Ask", typeof(double)); SummaryTable.Columns.Add("Mid", typeof(double)); SummaryTable.Columns.Add("MidConverted", typeof(double)); SummaryTable.Columns.Add("SuggestedSize", typeof(int)); SummaryTable.Columns.Add("WorkingPosition", typeof(int)); DataColumn ExistingPositionColumn2 = new DataColumn("ExistingPosition", typeof(int)); ExistingPositionColumn2.DefaultValue = 0; SummaryTable.Columns.Add(ExistingPositionColumn2); DataColumn ExistingPositionTickerHeadColumn = new DataColumn("ExistingPositionTickerHead", typeof(int)); ExistingPositionTickerHeadColumn.DefaultValue = 0; SummaryTable.Columns.Add(ExistingPositionTickerHeadColumn); DataColumn ValidPriceColumn = new DataColumn("ValidPrice", typeof(bool)); ValidPriceColumn.DefaultValue = false; SummaryTable.Columns.Add(ValidPriceColumn); SummaryTable.Columns.Add("Mean", typeof(double)); SummaryTable.Columns.Add("Std", typeof(double)); List <string> OpenStrategyList = TA.Strategy.GetFilteredOpenStrategyList(asOfDate: TodayDate, conn: conn, strategyClass: "ifs"); for (int i = 0; i < OpenStrategyList.Count; i++) { StrategyUtilities.PositionManagerOutput PositionManagerOut = PositionManager.GetIfsPosition(alias: OpenStrategyList[i], asOfDate: TodayDate, conn: conn); if (PositionManagerOut.CorrectPositionQ) { string SelectString = "contract1='" + PositionManagerOut.SortedPosition.Rows[0].Field <string>("Ticker") + "' and contract2='" + PositionManagerOut.SortedPosition.Rows[1].Field <string>("Ticker") + "'"; if (PositionManagerOut.SortedPosition.Rows.Count == 3) { SelectString = SelectString + " and contract3='" + PositionManagerOut.SortedPosition.Rows[2].Field <string>("Ticker") + "'"; } DataRow Row = IfsSheet.Select(SelectString)[0]; Row["ExistingPosition"] = Math.Round(PositionManagerOut.Scale); Row["ExistingPositionAbs"] = Math.Abs(PositionManagerOut.Scale); Row["Alias"] = OpenStrategyList[i]; } else { IFSLogger.Log("Check " + OpenStrategyList[i] + " ! Position may be incorrect."); } } DataTable newDataTable = IfsSheet.AsEnumerable() .OrderBy(r => r.Field <string>("spread_description")) .ThenByDescending(r => r.Field <double>("min_volume")) .ThenByDescending(r => r.Field <int>("ExistingPositionAbs")) .CopyToDataTable(); DataRow[] ExistingPositions = IfsSheet.Select("ExistingPositionAbs>0"); LiquidSpreads = newDataTable.AsEnumerable().GroupBy(r => r["spread_description"]).Select(w => w.First()).CopyToDataTable(); LiquidSpreads = LiquidSpreads.Select("upside<" + MaxBetSize).CopyToDataTable(); LiquidSpreads = LiquidSpreads.Select("spread_description='C_W' or spread_description='W_KW' or spread_description='S_BO_SM'").CopyToDataTable(); foreach (DataRow item in ExistingPositions) { LiquidSpreads.ImportRow(item); } LiquidSpreads = LiquidSpreads.AsEnumerable().GroupBy(r => r["ticker"]).Select(w => w.First()).CopyToDataTable(); for (int i = 0; i < LiquidSpreads.Rows.Count; i++) { List <string> TickerList = new List <string> { LiquidSpreads.Rows[i].Field <string>("Contract1"), LiquidSpreads.Rows[i].Field <string>("Contract2"), LiquidSpreads.Rows[i].Field <string>("Contract3") }; TickerList.RemoveAll(item => item == null); AutoSpreaderList.Add(new ttapiUtils.AutoSpreader(dbTickerList: TickerList, payUpTicks: 2)); AutoSpreaderDictionary.Add(AutoSpreaderList[i].AutoSpreaderName, AutoSpreaderList[i]); DataRow Row = SummaryTable.NewRow(); Row["Ticker"] = AutoSpreaderList[i].AutoSpreaderName; Row["TickerHead"] = LiquidSpreads.Rows[i].Field <string>("spread_description"); Row["Tag"] = "ifs_" + i.ToString(); if (LiquidSpreads.Rows[i].Field <int>("ExistingPositionAbs") != 0) { Row["Alias"] = LiquidSpreads.Rows[i].Field <string>("alias"); Row["ExistingPosition"] = LiquidSpreads.Rows[i].Field <int>("ExistingPosition"); } else { Row["Alias"] = LiquidSpreads.Rows[i].Field <string>("spread_description") + "_ifs_" + DateTime.Now.ToString("MMM-yyyy"); } Row["DescriptionString"] = "strategy_class=ifs&betsize=" + MaxBetSize.ToString(); Row["WorkingPosition"] = 0; Row["Settle"] = LiquidSpreads.Rows[i].Field <double>("settle"); Row["Mean"] = LiquidSpreads.Rows[i].Field <double>("mean10"); Row["Std"] = LiquidSpreads.Rows[i].Field <double>("std10"); Row["SuggestedSize"] = Math.Min(10, Math.Round(2 * MaxBetSize / (LiquidSpreads.Rows[i].Field <double>("upside") + Math.Abs(LiquidSpreads.Rows[i].Field <double>("downside"))))); SummaryTable.Rows.Add(Row); } List <string> TickerHeadList = DataAnalysis.DataTableFunctions.GetColumnAsList <string>(dataTableInput: SummaryTable, columnName: "TickerHead", uniqueQ: true); foreach (string item in TickerHeadList) { DataRow[] RowList = SummaryTable.Select("TickerHead='" + item + "'"); int TotalPosition = 0; foreach (DataRow Row in RowList) { TotalPosition += Row.Field <int>("ExistingPosition"); } foreach (DataRow Row in RowList) { Row["ExistingPositionTickerHead"] = TotalPosition; } } ASENameList = SummaryTable.AsEnumerable().Select(x => x.Field <string>("Ticker")).ToList(); TagList = SummaryTable.AsEnumerable().Select(x => x.Field <string>("Tag")).ToList(); TTAPISubs = new ttapiUtils.Subscription(m_username, m_password); TTAPISubs.AutoSpreaderList = AutoSpreaderList; TTAPISubs.AsuUpdateList = new List <EventHandler <AuthenticationStatusUpdateEventArgs> > { TTAPISubs.StartASESubscriptions }; TTAPISubs.priceUpdatedEventHandler = m_ps_FieldsUpdated; TTAPISubs.orderFilledEventHandler = m_ts_OrderFilled; TTAPISubs.OrderDeletedEventHandler = OrderDeleted; }