Exemple #1
0
        public HedgeTTAPI(string u, string p)
        {
            m_username = u;
            m_password = p;

            PricesReceivedQ = false;

            connection = new mysql();
            conn       = connection.conn;

            UnderlyingTickerTable = Underlying.GetUnderlying2QueryTable(conn: conn);

            List <string> TickerHeadList = DataAnalysis.DataTableFunctions.GetColumnAsList <string>(dataTableInput: UnderlyingTickerTable, columnName: "TickerHead", uniqueQ: true);

            DateTo = CalendarUtilities.BusinessDays.GetBusinessDayShifted(shiftInDays: -1);
            DateTime TodayDate = DateTime.Now.Date;
            DateTime DateFrom  = CalendarUtilities.BusinessDays.GetBusinessDayShifted(shiftInDays: -10);

            DataTable HistoricPriceTable = GetPrice.GetFuturesPrice.getFuturesPrice4Ticker(tickerHeadList: TickerHeadList, dateTimeFrom: DateFrom, dateTimeTo: DateTo, conn: conn);

            SortedHistoricPriceTable = HistoricPriceTable.AsEnumerable()
                                       .OrderBy(r => r.Field <string>("ticker"))
                                       .ThenBy(r => r.Field <DateTime>("price_date"))
                                       .CopyToDataTable();

            TTAPISubs             = new ttapiUtils.Subscription(m_username, m_password);
            IlsDictionary         = TTAPISubs.IlsDictionary;
            TTAPISubs.TickerTable = UnderlyingTickerTable;
            ilsUpdateList         = new List <EventHandler <InstrumentLookupSubscriptionEventArgs> > {
                TTAPISubs.startPriceSubscriptions, TTAPISubs.startTradeSubscriptions
            };
            TTAPISubs.ilsUpdateList = ilsUpdateList;
            TTAPISubs.AsuUpdateList = new List <EventHandler <AuthenticationStatusUpdateEventArgs> > {
                TTAPISubs.startInstrumentLookupSubscriptionsFromDataTable, TTAPISubs.Subscribe2OrderFills
            };
            TTAPISubs.priceUpdatedEventHandler = GetBidAsk;
            TTAPISubs.FillAddedEventHandler    = OrderFilledEventHandler;

            PriceData = new DataTable();
            PriceData.Columns.Add("Ticker", typeof(string));
            PriceData.Columns.Add("TickerHead", typeof(string));
            PriceData.Columns.Add("TickerClass", typeof(string));
            PriceData.Columns.Add("ContINDX", typeof(int));
            PriceData.Columns.Add("IsSpreadQ", typeof(bool));

            PriceData.Columns.Add("BidPrice", typeof(decimal));
            PriceData.Columns.Add("AskPrice", typeof(decimal));
            PriceData.Columns.Add("MidPrice", typeof(decimal));
            PriceData.Columns.Add("SpreadCost", typeof(decimal));

            foreach (DataRow item in UnderlyingTickerTable.Rows)
            {
                DataRow Row = PriceData.NewRow();
                Row["Ticker"]      = item.Field <string>("Ticker");
                Row["TickerHead"]  = item.Field <string>("TickerHead");
                Row["TickerClass"] = item.Field <string>("TickerClass");
                Row["ContINDX"]    = item.Field <int>("ContINDX");
                Row["IsSpreadQ"]   = item.Field <bool>("IsSpreadQ");
                PriceData.Rows.Add(Row);
            }

            PriceData            = DataAnalysis.DataTableFunctions.Sort(dataTableInput: PriceData, columnList: new string[] { "TickerHead", "ContINDX" });
            UnderlyingTickerList = DataAnalysis.DataTableFunctions.GetColumnAsList <string>(dataTableInput: PriceData, columnName: "Ticker", uniqueQ: true);

            string       OutputFolder = TA.DirectoryNames.GetDirectoryName("daily");
            StreamWriter LogFile      = new StreamWriter(OutputFolder + "/Delta.txt", true);

            DeltaLogger = new Logger(LogFile);

            DeltaLogger.SW.WriteLine();
            DeltaLogger.Log("NOTES FOR " + TodayDate.ToString("MM/dd/yyyy"));
            DeltaLogger.Log(new String('-', 20));

            DeltaStrategyAlias = HedgeStrategies.GetActiveDeltaStrategy(conn: conn);
        }
        public static DataTable GetUnderlying2QueryTable(MySqlConnection conn)
        {
            List <string> StrategyList = HedgeStrategies.GetStrategyList2Hedge(conn: conn);

            StrategyList.Add(HedgeStrategies.GetActiveDeltaStrategy(conn: conn));
            DataTable MergedPosition = null;

            for (int i = 0; i < StrategyList.Count; i++)
            {
                if (i == 0)
                {
                    MergedPosition = TA.Strategy.GetNetPosition(alias: StrategyList[i], conn: conn);
                }
                else
                {
                    MergedPosition.Merge(TA.Strategy.GetNetPosition(alias: StrategyList[i], conn: conn));
                }
            }

            DataTable FuturesPosition = MergedPosition.Select("Instrument='F'").CopyToDataTable();
            DataTable OptionsPosition = MergedPosition.Select("Instrument='O'").CopyToDataTable();

            List <string> OptionTickerList = OptionsPosition.AsEnumerable()
                                             .Select(r => r.Field <string>("Ticker")).Distinct()
                                             .ToList();

            List <string> FuturesTickerList = FuturesPosition.AsEnumerable()
                                              .Select(r => r.Field <string>("Ticker")).Distinct()
                                              .ToList();

            List <string> UnderlyingTickerList = new List <string>();

            foreach (string item in OptionTickerList)
            {
                UnderlyingTickerList.Add(OptionModels.Utils.GetOptionUnderlying(ticker: item));
            }

            List <string> UniqueList = UnderlyingTickerList.Union(FuturesTickerList).ToList();

            DataTable UnderlyingTable = new DataTable();

            UnderlyingTable.Columns.Add("Ticker", typeof(string));
            UnderlyingTable.Columns.Add("TickerHead", typeof(string));
            UnderlyingTable.Columns.Add("TickerClass", typeof(string));
            UnderlyingTable.Columns.Add("ContINDX", typeof(int));

            for (int i = 0; i < UniqueList.Count; i++)
            {
                DataRow UnderlyingRow = UnderlyingTable.NewRow();
                UnderlyingRow["Ticker"] = UniqueList[i];
                ContractUtilities.ContractSpecs Cs = ContractUtilities.ContractMetaInfo.GetContractSpecs(ticker: UniqueList[i]);
                UnderlyingRow["TickerHead"]  = Cs.tickerHead;
                UnderlyingRow["TickerClass"] = Cs.tickerClass;
                UnderlyingRow["ContINDX"]    = Cs.contINDX;
                UnderlyingTable.Rows.Add(UnderlyingRow);
            }

            DataRow[] FlatCurveUnderlyings = (from x in UnderlyingTable.AsEnumerable()
                                              where FlatCurveTickerClassList.Any(b => x.Field <string>("TickerClass").Contains(b))
                                              select x).ToArray();

            DataRow[] NonFlatCurveUnderlyings = (from x in UnderlyingTable.AsEnumerable()
                                                 where !FlatCurveTickerClassList.Any(b => x.Field <string>("TickerClass").Contains(b))
                                                 select x).ToArray();

            DataTable FlatCurveQueryTable = new DataTable();

            FlatCurveQueryTable.Columns.Add("Ticker", typeof(string));
            FlatCurveQueryTable.Columns.Add("TickerHead", typeof(string));
            FlatCurveQueryTable.Columns.Add("TickerClass", typeof(string));
            FlatCurveQueryTable.Columns.Add("ContINDX", typeof(int));
            FlatCurveQueryTable.Columns.Add("IsSpreadQ", typeof(bool));

            DataTable NonFlatCurveQueryTable = new DataTable();

            NonFlatCurveQueryTable.Columns.Add("Ticker", typeof(string));
            NonFlatCurveQueryTable.Columns.Add("TickerHead", typeof(string));
            NonFlatCurveQueryTable.Columns.Add("TickerClass", typeof(string));
            NonFlatCurveQueryTable.Columns.Add("ContINDX", typeof(int));
            NonFlatCurveQueryTable.Columns.Add("IsSpreadQ", typeof(bool));

            if (FlatCurveUnderlyings.Count() > 0)
            {
                DataTable FlatCurveUnderlyingTable = FlatCurveUnderlyings.CopyToDataTable();
                ContractUtilities.ContractList liquidContractList = new ContractUtilities.ContractList(FlatCurveUnderlyingTable.AsEnumerable().Select(s => s.Field <string>("TickerHead")).Distinct().ToArray());

                for (int i = 0; i < liquidContractList.dbTickerList.Count; i++)
                {
                    if (!(FlatCurveUnderlyingTable.AsEnumerable().Any(row => liquidContractList.dbTickerList[i] == row.Field <String>("Ticker"))))
                    {
                        DataRow UnderlyingRow = FlatCurveUnderlyingTable.NewRow();
                        UnderlyingRow["Ticker"] = liquidContractList.dbTickerList[i];
                        ContractUtilities.ContractSpecs Cs = ContractUtilities.ContractMetaInfo.GetContractSpecs(ticker: liquidContractList.dbTickerList[i]);
                        UnderlyingRow["TickerHead"]  = Cs.tickerHead;
                        UnderlyingRow["TickerClass"] = Cs.tickerClass;
                        UnderlyingRow["ContINDX"]    = Cs.contINDX;
                        FlatCurveUnderlyingTable.Rows.Add(UnderlyingRow);
                    }
                }

                for (int i = 0; i < FlatCurveUnderlyingTable.Rows.Count; i++)
                {
                    DataRow Row = FlatCurveQueryTable.NewRow();
                    Row["Ticker"]      = FlatCurveUnderlyingTable.Rows[i].Field <string>("Ticker");
                    Row["TickerHead"]  = FlatCurveUnderlyingTable.Rows[i].Field <string>("TickerHead");
                    Row["TickerClass"] = FlatCurveUnderlyingTable.Rows[i].Field <string>("TickerClass");
                    Row["ContINDX"]    = FlatCurveUnderlyingTable.Rows[i].Field <int>("ContINDX");
                    Row["IsSpreadQ"]   = false;
                    FlatCurveQueryTable.Rows.Add(Row);
                }
            }

            if (NonFlatCurveUnderlyings.Count() > 0)
            {
                DataTable SortedNonFlatCurveUnderlyings = NonFlatCurveUnderlyings.AsEnumerable()
                                                          .OrderBy(r => r.Field <string>("TickerHead"))
                                                          .ThenBy(r => r.Field <int>("contINDX"))
                                                          .CopyToDataTable();

                List <string> TickerHeadList = SortedNonFlatCurveUnderlyings.AsEnumerable().Select(s => s.Field <string>("TickerHead")).Distinct().ToList();

                for (int i = 0; i < TickerHeadList.Count(); i++)
                {
                    DataTable TickerHeadPositions = SortedNonFlatCurveUnderlyings.Select("TickerHead='" + TickerHeadList[i] + "'").CopyToDataTable();

                    for (int j = 0; j < TickerHeadPositions.Rows.Count; j++)
                    {
                        DataRow Row = NonFlatCurveQueryTable.NewRow();
                        Row["Ticker"]      = TickerHeadPositions.Rows[j].Field <string>("Ticker");
                        Row["TickerHead"]  = TickerHeadPositions.Rows[j].Field <string>("TickerHead");
                        Row["TickerClass"] = TickerHeadPositions.Rows[j].Field <string>("TickerClass");
                        Row["ContINDX"]    = TickerHeadPositions.Rows[j].Field <int>("ContINDX");
                        Row["IsSpreadQ"]   = false;
                        NonFlatCurveQueryTable.Rows.Add(Row);
                    }

                    for (int j = 0; j < TickerHeadPositions.Rows.Count - 1; j++)
                    {
                        for (int k = j + 1; k < TickerHeadPositions.Rows.Count; k++)
                        {
                            DataRow Row2 = NonFlatCurveQueryTable.NewRow();
                            Row2["Ticker"]      = TickerHeadPositions.Rows[j].Field <string>("Ticker") + "-" + TickerHeadPositions.Rows[k].Field <string>("Ticker");
                            Row2["TickerHead"]  = TickerHeadPositions.Rows[j].Field <string>("TickerHead");
                            Row2["TickerClass"] = TickerHeadPositions.Rows[j].Field <string>("TickerClass");
                            Row2["ContINDX"]    = TickerHeadPositions.Rows[j].Field <int>("ContINDX");
                            Row2["IsSpreadQ"]   = true;
                            NonFlatCurveQueryTable.Rows.Add(Row2);
                        }
                    }
                }
            }
            FlatCurveQueryTable.Merge(NonFlatCurveQueryTable);
            return(FlatCurveQueryTable);
        }