예제 #1
0
        public void generateMarginRequest()
        {
            csvHeaders     = new string[13];
            csvHeaders[0]  = "Firm Id";
            csvHeaders[1]  = "Acct Id";
            csvHeaders[2]  = "Exchange";
            csvHeaders[3]  = "Ticker Symbol";
            csvHeaders[4]  = "Product Name";
            csvHeaders[5]  = "CC Code";
            csvHeaders[6]  = "Period Code";
            csvHeaders[7]  = "Put / Call";
            csvHeaders[8]  = "Strike";
            csvHeaders[9]  = "Underlying Period Code";
            csvHeaders[10] = "Net Positions";
            csvHeaders[11] = "Margin Type";
            csvHeaders[12] = "Client ID";



            for (int instrumentCnt = 0; instrumentCnt < instruments.Length; instrumentCnt++)
            {
                //int instrumentCnt = 2;
                //RequestType requestType = RequestType.FCMTYPE;

                foreach (RequestType requestType in (RequestType[])Enum.GetValues(typeof(RequestType)))
                {
                    Thread calcMarginCallThread = new Thread(new ParameterizedThreadStart(makeRequestCall));
                    calcMarginCallThread.IsBackground = true;

                    MarginRequestRun mrr = new MarginRequestRun();
                    mrr.instrumentId = instrumentCnt;
                    if (requestType == RequestType.FCMTYPE)
                    {
                        mrr.requestType = RequestType.FCMTYPE;
                    }
                    else
                    {
                        mrr.requestType = RequestType.MODELTYPE;
                    }

                    calcMarginCallThread.Start(mrr);
                }
            }

            //http://msdn.microsoft.com/en-us/library/t249c2y7%28v=vs.110%29.aspx

            //int instrumentCnt = 0;
            //while (instrumentCnt < instruments.Length)
            //{
            //    if (instrumentMargin.Count > 0)
            //    {
            //        int[,] instrumentInfo = instrumentMargin.Dequeue();

            //        Thread t = new Thread(new ParameterizedThreadStart(ThreadMethod));

            //        t.Start("My Parameter");

            //        instrumentCnt++;
            //    }
            //}
        }
예제 #2
0
        public void makeRequestCall(Object obj)
        {
            MarginRequestRun mrr = (MarginRequestRun)obj;

            int instrumentCnt = mrr.instrumentId;

            RequestType requestType = mrr.requestType;

            HttpClient httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Accept.Clear();
            httpClient.BaseAddress = new Uri(mainCMEMarginAPIHttp);

            httpClient.DefaultRequestHeaders.Add("username", smartClickID);
            httpClient.DefaultRequestHeaders.Add("password", pwd);

            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));

            //portfolioSpreadTotals.initialMarginTotals = 0;
            //portfolioSpreadTotals.maintenanceMarginTotals = 0;

            //portfolioSpreadTotals.initialFCM_MarginTotals = 0;
            //portfolioSpreadTotals.maintenanceFCM_MarginTotals = 0;



            List <string[]> output = new List <string[]>();

            output.Add(csvHeaders);

            if (requestType == RequestType.MODELTYPE)
            //for (int instrumentCnt = 0; instrumentCnt < instruments.Length; instrumentCnt++)
            {
                for (int i = 0; i < optionSpreadExpressionList.Count; i++)
                {
                    if (optionSpreadExpressionList[i].optionExpressionType != OPTION_EXPRESSION_TYPES.OPTION_EXPRESSION_RISK_FREE_RATE &&
                        optionSpreadExpressionList[i].spreadIdx.Count > 0 &&
                        optionSpreadExpressionList[i].legIdx.Count > 0 &&
                        optionSpreadExpressionList[i].numberOfLotsHeldForContractSummary != 0 &&
                        optionSpreadExpressionList[i].instrument.idInstrument == instruments[instrumentCnt].idInstrument)
                    {
                        string[] line = new string[12];

                        line[0] = "tml1";
                        line[1] = instruments[instrumentCnt].exchangeSymbol + "_" + requestType.ToString();
                        line[2] = optionSpreadExpressionList[i].instrument.spanExchWebAPISymbol;
                        line[3] = "";
                        line[4] = "";

                        int spreadIdx = optionSpreadExpressionList[i].spreadIdx[0];
                        int legIdx    = optionSpreadExpressionList[i].legIdx[0];

                        if (optionSpreadExpressionList[i].callPutOrFuture == OPTION_SPREAD_CONTRACT_TYPE.FUTURE)
                        {
                            line[5] = optionSpreadExpressionList[i].instrument.spanFutureCode;

                            line[6] = optionStrategies[spreadIdx]
                                      .legInfo[legIdx].contractYear.ToString()
                                      +
                                      optionStrategies[spreadIdx]
                                      .legInfo[legIdx].contractMonthInt.ToString("00");

                            line[7] = "";
                            line[8] = "";
                            line[9] = "";
                        }
                        else
                        {
                            line[5] = optionSpreadExpressionList[i].instrument.spanOptionCode;

                            line[6] = optionStrategies[spreadIdx]
                                      .legInfo[legIdx].optionYear.ToString()
                                      +
                                      optionStrategies[spreadIdx]
                                      .legInfo[legIdx].optionMonthInt.ToString("00");

                            if (optionSpreadExpressionList[i].callPutOrFuture == OPTION_SPREAD_CONTRACT_TYPE.CALL)
                            {
                                line[7] = "CALL";
                            }
                            else
                            {
                                line[7] = "PUT";
                            }

                            line[8] = CMECoreStrikeFormat(optionSpreadExpressionList[i].strikePrice,
                                                          optionSpreadExpressionList[i].instrument.idInstrument).ToString();

                            //int findFutCnt = 0;
                            //while (findFutCnt < optionStrategies[spreadIdx]
                            //        .legInfo.Length)
                            //{
                            //    if (optionStrategies[spreadIdx]
                            //        .legInfo[findFutCnt].legContractType == OPTION_SPREAD_CONTRACT_TYPE.FUTURE)
                            //    {
                            //        break;
                            //    }

                            //    findFutCnt++;
                            //}

                            line[9] = optionStrategies[spreadIdx]
                                      .legInfo[optionStrategies[spreadIdx].idxOfFutureLeg].contractYear.ToString()
                                      +
                                      optionStrategies[spreadIdx]
                                      .legInfo[optionStrategies[spreadIdx].idxOfFutureLeg].contractMonthInt.ToString("00");
                        }



                        line[10] = optionSpreadExpressionList[i].numberOfLotsHeldForContractSummary.ToString();
                        line[11] = "";

                        output.Add(line);
                    }
                }
            }
            else
            {
                for (int i = 0; i < admPositionImportWeb.Count; i++)
                {
                    int netPos = (int)admPositionImportWeb[i].Net
                                 + admPositionImportWeb[i].transNetLong
                                 - admPositionImportWeb[i].transNetShort;

                    if (netPos != 0 &&
                        admPositionImportWeb[i].instrument.idInstrument == instruments[instrumentCnt].idInstrument)
                    {
                        string[] line = new string[12];

                        line[0] = "tml1";
                        line[1] = instruments[instrumentCnt].exchangeSymbol + "_" + requestType.ToString();
                        line[2] = instruments[instrumentCnt].spanExchWebAPISymbol;
                        line[3] = "";
                        line[4] = "";

                        // int spreadIdx = optionSpreadExpressionList[i].spreadIdx[0];
                        // int legIdx = optionSpreadExpressionList[i].legIdx[0];

                        if (admPositionImportWeb[i].callPutOrFuture == OPTION_SPREAD_CONTRACT_TYPE.FUTURE)
                        {
                            line[5] = instruments[instrumentCnt].spanFutureCode;

                            line[6] = admPositionImportWeb[i].contractInfo.contractYear.ToString()
                                      +
                                      admPositionImportWeb[i].contractInfo.contractMonthInt.ToString("00");

                            line[7] = "";
                            line[8] = "";
                            line[9] = "";
                        }
                        else
                        {
                            line[5] = instruments[instrumentCnt].spanOptionCode;

                            line[6] = admPositionImportWeb[i].contractInfo.optionYear.ToString()
                                      +
                                      admPositionImportWeb[i].contractInfo.optionMonthInt.ToString("00");

                            if (admPositionImportWeb[i].callPutOrFuture == OPTION_SPREAD_CONTRACT_TYPE.CALL)
                            {
                                line[7] = "CALL";
                            }
                            else
                            {
                                line[7] = "PUT";
                            }

                            line[8] = //admPositionImportWeb[i].strikeInDecimal.ToString();
                                      CMECoreStrikeFormat(admPositionImportWeb[i].strikeInDecimal,
                                                          instruments[instrumentCnt].idInstrument).ToString();


                            line[9] = admPositionImportWeb[i].contractInfo.contractYear.ToString()
                                      +
                                      admPositionImportWeb[i].contractInfo.contractMonthInt.ToString("00");
                        }



                        line[10] = netPos.ToString();
                        line[11] = "";

                        output.Add(line);
                    }
                }
            }

            //String[] line1 = { "tml2", "2", "CME", "", "", "EC", "201409", "", "", "", "1", "" };
            //output[1] = line1;

            //OZNN4 P1230

            //String[] line2 = { "tml2", "2", "CBT", "", "", "21", "201407", "PUT", "123", "201409", "1", "" };
            //output[2] = line2;

            int length = output.Count();

            StringBuilder csvTrades = new StringBuilder();

            //stringVal.Clear();
            for (int index = 0; index < length; index++)
            {
                string lineInput;

                if (index == 0)
                {
                    lineInput = "\"" + string.Join("\",\"", output[index]) + "\"";
                }
                else
                {
                    lineInput = "\"" + string.Join("\",\"", output[index]) + "\"" + ",";
                }

                csvTrades.Append(lineInput);

                csvTrades.Append("\n");

                //stringVal.Append(",");
            }

            //String currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            if (fileOnly)
            {
                String dir = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "CSV_CORE_TRADES");

                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }

                File.WriteAllText(dir + "\\"
                                  + instruments[instrumentCnt].exchangeSymbol + "_" +
                                  requestType.ToString() + "_csvTrades" + ".csv", csvTrades.ToString());
            }
            else
            {
                RunAsync(csvTrades, instrumentCnt, requestType, httpClient);
            }
        }