public FuturesButterflyTTAPI(string u, string p)
        {

            m_username = u;
            m_password = p;

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

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

            AutoSpreaderList = new List<ttapiUtils.AutoSpreader>();
            AutoSpreaderDictionary = new Dictionary<string, ttapiUtils.AutoSpreader>();
            AliasDictionary = new Dictionary<string, string>();
            TagDictionary = new Dictionary<string, string>();

            PriceTableSymbolsReceived = new List<string>();
            StrategyPositionDictionary = new Dictionary<string, DataTable>();

            TTAPISubs = new ttapiUtils.Subscription(m_username, m_password);
            SpreadList = new List<string>();

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

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

            string DirectoryName = TA.DirectoryNames.GetDirectoryName(ext: "ta");
            string DirectoryExtension = TA.DirectoryNames.GetDirectoryExtension(directoryDate: ReportDate);

            DataSet FollowupOutput = IOUtilities.ExcelDataReader.LoadFile(DirectoryName + "/" + DirectoryExtension + "/followup.xlsx");
            DataTable ButterfliesFollowupSheet = FollowupOutput.Tables["butterflies"];

            PriceTable = new DataTable();
            PriceTable.Columns.Add("Ticker", typeof(string));
            PriceTable.Columns.Add("TickerHead", typeof(string));
            PriceTable.Columns.Add("IsAutoSpreaderQ", typeof(bool));
            PriceTable.Columns.Add("BidPrice", typeof(double));
            PriceTable.Columns.Add("AskPrice", typeof(double));

            HistoricPriceTable = GetPrice.GetFuturesPrice.getFuturesPrice4Ticker(tickerHeadList: ContractUtilities.ContractMetaInfo.FuturesButterflyTickerheadList, 
                dateTimeFrom: ReportDate, dateTimeTo: ReportDate, conn: conn);

            DataRow [] StopList = ButterfliesFollowupSheet.Select("Recommendation='STOP'");

            if (StopList.Length>0)
            {
                ButterfliesStopSheet = StopList.CopyToDataTable();
                ButterfliesStopSheet.Columns.Add("PnlMid",typeof(double));
                ButterfliesStopSheet.Columns.Add("PnlWorst", typeof(double));

                for (int i = 0; i < StopList.Length; i++)
                {
                    string Alias = StopList[i].Field<string>("Alias");
                    StrategyUtilities.PositionManagerOutput PositionManagerOut = PositionManager.GetFuturesButterflyPosition(alias: Alias, asOfDate: TodayDate, conn: conn);
                    DataTable StrategyPosition = PositionManagerOut.SortedPosition;

                    StrategyPosition.Columns.Add("ClosePrice", typeof(double));

                    for (int j = 0; j < StrategyPosition.Rows.Count; j++)
                    {
                        DataRow SelectedHistRow = HistoricPriceTable.Select("ticker='" + StrategyPosition.Rows[j].Field<string>("Ticker") + "'")[0];
                        StrategyPosition.Rows[j]["ClosePrice"] = SelectedHistRow.Field<decimal>("close_price");
                    }


                    List<string> TickerList = new List<string> { StrategyPosition.Rows[0].Field<string>("Ticker") + "-" + StrategyPosition.Rows[1].Field<string>("Ticker"),
                                                             StrategyPosition.Rows[1].Field<string>("Ticker") + "-" + StrategyPosition.Rows[2].Field<string>("Ticker")};
                    StrategyPositionDictionary.Add(Alias, StrategyPosition);

                    SpreadList.AddRange(TickerList);

                    if (PositionManagerOut.CorrectPositionQ)
                    {
                        bool wuhu = TickerList[0].Contains("-");
                        string[] TickerListAux = TickerList[0].Split('-');

                        ttapiUtils.AutoSpreader As = new ttapiUtils.AutoSpreader(dbTickerList: TickerList, payUpTicks: 2);

                        AutoSpreaderList.Add(As);
                        AutoSpreaderDictionary.Add(As.AutoSpreaderName, As);
                        AliasDictionary.Add(As.AutoSpreaderName, Alias);
                        TagDictionary.Add(As.AutoSpreaderName, "fut_but_fol" + i);


                        DataRow PriceRow = PriceTable.NewRow();
                        PriceRow["Ticker"] = As.AutoSpreaderName;
                        PriceRow["TickerHead"] = StrategyPosition.Rows[0].Field<string>("TickerHead");
                        PriceRow["IsAutoSpreaderQ"] = true;
                        PriceTable.Rows.Add(PriceRow);

                    }
                    else
                    {
                        ButterflyLogger.Log("Check " + Alias + " ! Position may be incorrect.");
                    }
                }


                SpreadList = SpreadList.Distinct().ToList();

                SpreadTable = new DataTable();
                SpreadTable.Columns.Add("Ticker", typeof(string));
                SpreadTable.Columns.Add("IsSpreadQ", typeof(bool));

                for (int i = 0; i < SpreadList.Count; i++)
                {
                    DataRow SpreadRow = SpreadTable.NewRow();
                    SpreadRow["Ticker"] = SpreadList[i];
                    SpreadRow["IsSpreadQ"] = true;
                    SpreadTable.Rows.Add(SpreadRow);

                    DataRow PriceRow = PriceTable.NewRow();
                    PriceRow["Ticker"] = SpreadList[i];
                    string[] SpreadAsArray = SpreadList[i].Split(new char[] { '-' });
                    PriceRow["TickerHead"] = ContractUtilities.ContractMetaInfo.GetContractSpecs(SpreadAsArray[0]).tickerHead;
                    PriceRow["IsAutoSpreaderQ"] = false;
                    PriceTable.Rows.Add(PriceRow);
                }
    
            }

            

            

            PriceTableSymbols = DataAnalysis.DataTableFunctions.GetColumnAsList<string>(dataTableInput: PriceTable, columnName: "Ticker");


            TTAPISubs.TickerTable = SpreadTable;
            TTAPISubs.AutoSpreaderList = AutoSpreaderList;
            TTAPISubs.AsuUpdateList = new List<EventHandler<AuthenticationStatusUpdateEventArgs>> { TTAPISubs.StartASESubscriptions,TTAPISubs.startInstrumentLookupSubscriptionsFromDataTable };
            //TTAPISubs.AsuUpdateList = new List<EventHandler<AuthenticationStatusUpdateEventArgs>> {TTAPISubs.startInstrumentLookupSubscriptionsFromDataTable };
            TTAPISubs.ilsUpdateList = new List<EventHandler<InstrumentLookupSubscriptionEventArgs>> { TTAPISubs.startPriceSubscriptions, TTAPISubs.startTradeSubscriptions };
            //TTAPISubs.ilsUpdateList = new List<EventHandler<InstrumentLookupSubscriptionEventArgs>> { TTAPISubs.startPriceSubscriptions};
            TTAPISubs.priceUpdatedEventHandler = m_ps_FieldsUpdated;
            TTAPISubs.orderFilledEventHandler = m_ts_OrderFilled;

        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
           

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

            //            DataTable PriceTable = GetPrice.GetFuturesPrice.getFuturesPrice4Ticker(tickerHead: "ED", conn: conn,
            //              dateTimeFrom: new DateTime(2016, 8, 2), dateTimeTo: new DateTime(2016, 8, 2));

            //        DateTime DateTimeFrom = new DateTime(2016,10,1);
            //  DateTime DateTimeTo = new DateTime(2017,2,1);

            // Worked perfectly on September 12th and 13th
            

            




           

            DataTable DeltaStrategyTable = (from x in StrategyTable.AsEnumerable()
                                             where x.Field<string>("StrategyClass").Contains("delta")
                                             select x).CopyToDataTable();
            




            string alias = "LCG17Z16VCS";

            List<string>HedgeNotes = TA.StrategyHedger.HedgeStrategyAgainstDelta(alias: alias, deltaAlias: "delta_jul16", conn: conn);

            



            


           



           
            

            
         

            


                string instrument = "options";

       

                Console.WriteLine("Emre");
            


        }
Ejemplo n.º 3
0
        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;

        }