Esempio n. 1
0
        public static void HedgeStrategiesAgainstDelta(MySqlConnection conn, DataTable priceTable)
        {
            List <string> HedgeStrategyList   = HedgeStrategies.GetStrategyList2Hedge(conn: conn);
            List <string> DeltaStrategyList   = HedgeStrategies.GetDeltaStrategyList(conn: conn);
            string        ActiveDeltaStrategy = DeltaStrategyList[DeltaStrategyList.Count - 1];

            Console.WriteLine("Calculating hedges for strategies...");

            foreach (string item in HedgeStrategyList)
            {
                Console.WriteLine(item);
                HedgeStrategies.HedgeStrategyAgainstDelta(alias: item, deltaAlias: ActiveDeltaStrategy, conn: conn, priceTable: priceTable);
            }
        }
Esempio n. 2
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);
        }
Esempio n. 3
0
        DataTable GenerateHedgeTable(MySqlConnection conn)
        {
            string DeltaStrategy = HedgeStrategies.GetDeltaStrategyList(conn: conn).Last();
            //string DeltaStrategy = HedgeStrategies.GetDeltaStrategyList(conn: conn).First();

            DataTable DeltaNetPosition = TA.Strategy.GetNetPosition(alias: DeltaStrategy, conn: conn);

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

            for (int i = 0; i < DeltaNetPosition.Rows.Count; i++)
            {
                ContractUtilities.ContractSpecs Cs = ContractUtilities.ContractMetaInfo.GetContractSpecs(ticker: DeltaNetPosition.Rows[i].Field <string>("Ticker"));
                DeltaNetPosition.Rows[i]["TickerHead"]  = Cs.tickerHead;
                DeltaNetPosition.Rows[i]["TickerClass"] = Cs.tickerClass;
                DeltaNetPosition.Rows[i]["ContINDX"]    = Cs.contINDX;
            }


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

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

            DataTable FlatCurveHedgeTable = new DataTable();

            FlatCurveHedgeTable.Columns.Add("Ticker", typeof(string));
            FlatCurveHedgeTable.Columns.Add("TickerHead", typeof(string));
            FlatCurveHedgeTable.Columns.Add("IsSpreadQ", typeof(bool));
            FlatCurveHedgeTable.Columns.Add("Hedge", typeof(int));

            DataTable NonFlatCurveHedgeTable = new DataTable();

            NonFlatCurveHedgeTable.Columns.Add("Ticker", typeof(string));
            NonFlatCurveHedgeTable.Columns.Add("TickerHead", typeof(string));
            NonFlatCurveHedgeTable.Columns.Add("IsSpreadQ", typeof(bool));
            NonFlatCurveHedgeTable.Columns.Add("Hedge", typeof(int));

            if (FlatCurvePositions.Count() > 0)
            {
                DataTable FlatCurveTable = FlatCurvePositions.CopyToDataTable();

                ContractUtilities.ContractList liquidContractList = new ContractUtilities.ContractList(FlatCurveTable.AsEnumerable().Select(s => s.Field <string>("TickerHead")).Distinct().ToArray());

                Dictionary <string, string> LiquidContractDictionary = new Dictionary <string, string>();

                foreach (string item in liquidContractList.dbTickerList)
                {
                    LiquidContractDictionary.Add(ContractUtilities.ContractMetaInfo.GetContractSpecs(item).tickerHead, item);
                }

                var grouped = FlatCurveTable.AsEnumerable().GroupBy(r => r["TickerHead"]).Select(w => new
                {
                    TickerHead = w.Key.ToString(),
                    Qty        = w.Sum(r => decimal.Parse(r["Qty"].ToString())),
                }).ToList();


                for (int i = 0; i < grouped.Count; i++)
                {
                    FlatCurveHedgeTable.Rows.Add();
                    FlatCurveHedgeTable.Rows[i]["Ticker"]     = LiquidContractDictionary[grouped[i].TickerHead];
                    FlatCurveHedgeTable.Rows[i]["TickerHead"] = grouped[i].TickerHead;
                    FlatCurveHedgeTable.Rows[i]["IsSpreadQ"]  = false;
                    FlatCurveHedgeTable.Rows[i]["Hedge"]      = Math.Round(-grouped[i].Qty);
                }
            }

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

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

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

                    if (TickerHeadPositions.Rows.Count == 1)
                    {
                        DataRow Row = NonFlatCurveHedgeTable.NewRow();
                        NonFlatCurveHedgeTable.Rows.Add();
                        Row["Ticker"]     = TickerHeadPositions.Rows[0].Field <string>("Ticker");
                        Row["TickerHead"] = TickerHeadList[i];
                        Row["IsSpreadQ"]  = false;
                        Row["Hedge"]      = Math.Round(-TickerHeadPositions.Rows[0].Field <double>("Qty"));
                        NonFlatCurveHedgeTable.Rows.Add(Row);
                        continue;
                    }

                    int sum = (int)Math.Round(-TickerHeadPositions.AsEnumerable().Sum(x => x.Field <double>("Qty")));

                    int MaxIndex = TickerHeadPositions.Rows.IndexOf(TickerHeadPositions.Select("Qty = MAX(Qty)")[0]);
                    int MinIndex = TickerHeadPositions.Rows.IndexOf(TickerHeadPositions.Select("Qty = MIN(Qty)")[0]);

                    if (sum > 0)
                    {
                        DataRow Row = NonFlatCurveHedgeTable.NewRow();
                        Row["Ticker"]     = TickerHeadPositions.Rows[MinIndex].Field <string>("Ticker");
                        Row["TickerHead"] = TickerHeadList[i];
                        Row["IsSpreadQ"]  = false;
                        Row["Hedge"]      = sum;
                        NonFlatCurveHedgeTable.Rows.Add(Row);
                        TickerHeadPositions.Rows[MinIndex]["Qty"] = TickerHeadPositions.Rows[MinIndex].Field <double>("Qty") + sum;
                    }
                    else if (sum < 0)
                    {
                        DataRow Row = NonFlatCurveHedgeTable.NewRow();
                        Row["Ticker"]     = TickerHeadPositions.Rows[MaxIndex].Field <string>("Ticker");
                        Row["TickerHead"] = TickerHeadList[i];
                        Row["IsSpreadQ"]  = false;
                        Row["Hedge"]      = sum;
                        NonFlatCurveHedgeTable.Rows.Add(Row);
                        TickerHeadPositions.Rows[MaxIndex]["Qty"] = TickerHeadPositions.Rows[MaxIndex].Field <double>("Qty") + sum;
                    }

                    bool PositionCleanedQ = false;

                    while (!PositionCleanedQ)
                    {
                        var OrderedRows = TickerHeadPositions.AsEnumerable().OrderByDescending(x => x["Qty"]);

                        MaxIndex = TickerHeadPositions.Rows.IndexOf(OrderedRows.First());
                        MinIndex = TickerHeadPositions.Rows.IndexOf(OrderedRows.Last());

                        PositionCleanedQ = !((TickerHeadPositions.Rows[MaxIndex].Field <double>("Qty") > 0.5) & (TickerHeadPositions.Rows[MinIndex].Field <double>("Qty") < -0.5));

                        if (PositionCleanedQ)
                        {
                            break;
                        }
                        int qty = -(int)Math.Round(Math.Min(TickerHeadPositions.Rows[MaxIndex].Field <double>("Qty"), -TickerHeadPositions.Rows[MinIndex].Field <double>("Qty")));

                        string SpreadTicker;
                        int    HedgeQty;

                        if (TickerHeadPositions.Rows[MaxIndex].Field <int>("ContINDX") < TickerHeadPositions.Rows[MinIndex].Field <int>("ContINDX"))
                        {
                            SpreadTicker = TickerHeadPositions.Rows[MaxIndex].Field <string>("Ticker") + "-" + TickerHeadPositions.Rows[MinIndex].Field <string>("Ticker");
                            HedgeQty     = qty;
                        }
                        else
                        {
                            HedgeQty     = -qty;
                            SpreadTicker = TickerHeadPositions.Rows[MinIndex].Field <string>("Ticker") + "-" + TickerHeadPositions.Rows[MaxIndex].Field <string>("Ticker");
                        }

                        DataRow Row = NonFlatCurveHedgeTable.NewRow();
                        Row["Ticker"]     = SpreadTicker;
                        Row["TickerHead"] = TickerHeadList[i];
                        Row["IsSpreadQ"]  = true;
                        Row["Hedge"]      = HedgeQty;
                        NonFlatCurveHedgeTable.Rows.Add(Row);

                        TickerHeadPositions.Rows[MaxIndex]["Qty"] = TickerHeadPositions.Rows[MaxIndex].Field <double>("Qty") + qty;
                        TickerHeadPositions.Rows[MinIndex]["Qty"] = TickerHeadPositions.Rows[MinIndex].Field <double>("Qty") - qty;
                    }
                }
            }

            FlatCurveHedgeTable.Merge(NonFlatCurveHedgeTable);
            return(FlatCurveHedgeTable);
        }
Esempio n. 4
0
        void GetBidAsk(object sender, FieldsUpdatedEventArgs e)
        {
            if (e.Error == null)
            {
                if ((string.IsNullOrEmpty(e.Fields.GetDirectBidPriceField().FormattedValue)) || (string.IsNullOrEmpty(e.Fields.GetDirectAskPriceField().FormattedValue)))
                {
                    return;
                }

                string InstrumentName = e.Fields.Instrument.Name.ToString();

                TickerDB = TA.TickerConverters.ConvertFromTTAPIFields2DB(e.Fields.Instrument.Product.ToString(), e.Fields.Instrument.Name.ToString());
                string TickerHead = TA.TickerheadConverters.ConvertFromTT2DB(e.Fields.Instrument.Product.ToString());

                UpdatePriceData(ticker: TickerDB, tickerHead: TickerHead, e: e);

                TA.ttapiTicker TTAPITicker = TA.TickerConverters.ConvertFromDbTicker2ttapiTicker(dbTicker: TickerDB, productType: e.Fields.Instrument.Product.Type.ToString());

                InstrumentKey IKey = new InstrumentKey(new ProductKey(e.Fields.Instrument.Product.Market.Key, e.Fields.Instrument.Product.Type, e.Fields.Instrument.Product.Name), TTAPITicker.SeriesKey);
                Ts = TTAPISubs.TsDictionary[e.Fields.Instrument.Key];

                if (IlsDictionary.ContainsKey(IKey))
                {
                    for (int i = 0; i < ilsUpdateList.Count; i++)
                    {
                        IlsDictionary[IKey].Update -= ilsUpdateList[i];
                    }

                    IlsDictionary[IKey].Dispose();
                    IlsDictionary[IKey] = null;
                    IlsDictionary.Remove(IKey);
                    TTAPISubs.IlsDictionary = IlsDictionary;
                }


                if ((IlsDictionary.Count == 0) & (!PricesReceivedQ))
                {
                    PricesReceivedQ = true;

                    HedgeStrategies.HedgeStrategiesAgainstDelta(conn: conn, priceTable: PriceData);

                    DataTable StdMoves   = CalculateStdMoves(priceData: PriceData);
                    DataTable HedgeTable = GenerateHedgeTable(conn: conn);
                    NetHedgeTable = CalculateUrgency(hedgeTable: HedgeTable);
                    //NetHedgeTable = NetHedgeTable.Select("IsSpreadQ=true").CopyToDataTable();

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



                    foreach (string TickerHead2Report in TickerHeads2Report)
                    {
                        DeltaLogger.Log(new String('-', 20));
                        DataRow[] SelectedRows = StdMoves.Select("TickerHead='" + TickerHead2Report + "'");

                        foreach (DataRow Row in SelectedRows)
                        {
                            DeltaLogger.Log(Row.Field <string>("Ticker") + " std change: " + Row.Field <double>("StdChange"));
                        }

                        DataRow[] SelectedHedgeRows = NetHedgeTable.Select("TickerHead='" + TickerHead2Report + "'");

                        foreach (DataRow Row in SelectedHedgeRows)
                        {
                            DeltaLogger.Log(Row.Field <int>("Hedge") + "  " + Row.Field <string>("Ticker") + " with urgency " + Row.Field <decimal>("Urgency"));
                        }
                    }

                    Console.Write("Do you agree? (Y/N): ");
                    Decision = Console.ReadLine();
                    Decision = Decision.ToUpper();


                    DataColumn WorkingOrdersColumn = new DataColumn("WorkingOrders", typeof(int));
                    WorkingOrdersColumn.DefaultValue = 0;
                    NetHedgeTable.Columns.Add(WorkingOrdersColumn);
                    NetHedgeTickerList = DataAnalysis.DataTableFunctions.GetColumnAsList <string>(dataTableInput: NetHedgeTable, columnName: "Ticker");
                }

                if ((PricesReceivedQ) && (Decision == "Y"))
                {
                    int RowIndex = NetHedgeTickerList.IndexOf(TickerDB);

                    if ((RowIndex < 0) || (NetHedgeTable.Rows[RowIndex].Field <int>("WorkingOrders") != 0))
                    {
                        return;
                    }

                    Price BidPrice = e.Fields.GetDirectBidPriceField().Value;
                    Price AskPrice = e.Fields.GetDirectAskPriceField().Value;

                    Price MidPrice = (AskPrice + BidPrice) / 2;

                    Price TradePrice;

                    NetHedgeTable.Rows[RowIndex]["WorkingOrders"] = NetHedgeTable.Rows[RowIndex].Field <int>("Hedge");

                    if (NetHedgeTable.Rows[RowIndex].Field <int>("Hedge") > 0)
                    {
                        if (NetHedgeTable.Rows[RowIndex].Field <decimal>("Urgency") > 5)
                        {
                            TradePrice = MidPrice.Round(Rounding.Up);
                        }
                        else
                        {
                            TradePrice = BidPrice;
                        }
                    }
                    else if (NetHedgeTable.Rows[RowIndex].Field <int>("Hedge") < 0)
                    {
                        if (NetHedgeTable.Rows[RowIndex].Field <decimal>("Urgency") > 5)
                        {
                            TradePrice = MidPrice.Round(Rounding.Down);
                        }
                        else
                        {
                            TradePrice = AskPrice;
                        }
                    }
                    else
                    {
                        return;
                    }

                    DeltaLogger.Log(NetHedgeTable.Rows[RowIndex].Field <int>("Hedge") + " " + TickerDB + " for " + TradePrice +
                                    ", Bid: " + e.Fields.GetDirectBidPriceField().FormattedValue +
                                    ", Ask: " + e.Fields.GetDirectAskPriceField().FormattedValue);
                    ttapiUtils.Trade.SendLimitOrder(instrument: e.Fields.Instrument, price: TradePrice,
                                                    qty: NetHedgeTable.Rows[RowIndex].Field <int>("Hedge"), ttapisubs: TTAPISubs, orderTag: "DeltaHedge");
                }
            }
            else
            {
                if (e.Error.IsRecoverableError == false)
                {
                    Console.WriteLine("Unrecoverable price subscription error: {0}", e.Error.Message);
                    //Dispose();
                }
            }
        }
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
 public static string GetActiveDeltaStrategy(MySqlConnection conn)
 {
     return(HedgeStrategies.GetDeltaStrategyList(conn: conn).Last());
 }