Ejemplo n.º 1
0
        private async void button1_Click(object sender, EventArgs e)
        {
            // TestAccountEncryptionDecryption();
            // TestCalcLimit();

            TradingEnvironment.Initialize();

            try
            {
                var sinaQuote = await SinaStockQuoteInterface.GetQuote("000001");

                ShowQuote(sinaQuote);

                string[] sinaCodes = new string[]
                {
                    "000001",
                    "000002",
                    "000003",
                    "000004"
                };

                var sinaQuotes = await SinaStockQuoteInterface.GetQuote(sinaCodes);

                ShowQuote(sinaQuotes);

                using (var client = new StockTrading.Utility.TradingClient())
                {
                    string error;

                    int logonFailureCount = 0;
                    while (logonFailureCount < 5)
                    {
                        if (!client.LogOn("wt5.foundersc.com", 7708, "6.19", 1, "13003470", 9, "13003470", "789012", string.Empty, out error))
                        {
                            textBox1.AppendText("\n");
                            textBox1.AppendText(string.Format("Log on failed: {0}", error));

                            logonFailureCount++;
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (!client.IsLoggedOn())
                    {
                        return;
                    }

                    textBox1.AppendText("\n");
                    textBox1.AppendText(string.Format("Logged on, client id = {0}", client.ClientId));

                    TabulateData result;

                    client.QueryData(DataCategory.Capital, out result, out error);
                    WriteOutput(result, error);

                    client.QueryData(DataCategory.OrderSubmittedToday, out result, out error);
                    WriteOutput(result, error);

                    client.QueryData(DataCategory.OrderSucceededToday, out result, out error);
                    WriteOutput(result, error);

                    client.QueryData(DataCategory.Stock, out result, out error);
                    WriteOutput(result, error);

                    client.QueryData(DataCategory.ShareholderRegistryCode, out result, out error);
                    WriteOutput(result, error);

                    client.QueryData(DataCategory.CancellableOrder, out result, out error);
                    WriteOutput(result, error);

                    client.QueryData(DataCategory.FinancingBalance, out result, out error);
                    WriteOutput(result, error);

                    client.QueryData(DataCategory.MarginBalance, out result, out error);
                    WriteOutput(result, error);

                    client.QueryData(DataCategory.MarginableSecurity, out result, out error);
                    WriteOutput(result, error);

                    textBox1.AppendText("\n");
                    textBox1.AppendText(">>>>>>> test array api");

                    TabulateData[] results;
                    string[]       errors;
                    DataCategory[] categories = new DataCategory[]
                    {
                        DataCategory.Capital,
                        DataCategory.OrderSubmittedToday,
                        DataCategory.OrderSucceededToday,
                        DataCategory.Stock,
                        DataCategory.ShareholderRegistryCode,
                        DataCategory.CancellableOrder,
                    };


                    bool[] succeeds = client.QueryData(categories, out results, out errors);
                    for (int i = 0; i < categories.Length; ++i)
                    {
                        WriteOutput(results[i], errors[i]);
                    }


                    string[] codes = new string[] { "000001", "000004", "601398" };

                    for (int k = 0; k < 5; k++)
                    {
                        succeeds = client.GetQuote(codes, out results, out errors);
                        if (k == 0)
                        {
                            WriteOutput(results[0], errors[0]);
                        }

                        for (int i = 0; i < codes.Length; ++i)
                        {
                            if (succeeds[i])
                            {
                                ShowQuote(results[i], DateTime.Now);
                            }
                            else
                            {
                                textBox1.AppendText("\n");

                                textBox1.AppendText(string.Format("error: {0}", errors[i]));
                            }
                        }

                        Thread.Sleep(3000);

                        //DateTime now = DateTime.Now;
                        //if (now.TimeOfDay > new TimeSpan(9, 25, 1))
                        //{
                        //    break;
                        //}
                    }

                    bool succeeded;

                    //DateTime time;

                    //for (; ; )
                    //{
                    //    textBox1.AppendText("Please input code.");
                    //    string line = Console.ReadLine();

                    //    time = DateTime.Now;
                    //    string code = line.Trim();

                    //    if (code == "exit")
                    //    {
                    //        break;
                    //    }

                    //    if (client.GetQuote(code, out result, out error))
                    //    {
                    //        ShowQuote(result, time);
                    //    }
                    //    else
                    //    {
                    //        textBox1.AppendText("error: {0}", error);
                    //    }
                    //}

                    //string icbcCode = "601398";

                    //time = DateTime.Now;
                    //if (!client.GetQuote(icbcCode, out result, out error))
                    //{
                    //    textBox1.AppendText("error: {0}", error);
                    //}

                    //ShowQuote(result, time);

                    //var quotes = FiveLevelQuote.ExtractFrom(result, time);
                    //if (quotes.Count() == 0)
                    //{
                    //    textBox1.AppendText("unable to get quote for {0}", icbcCode);
                    //}
                    //else
                    //{
                    //    var quote = quotes.First();

                    //    float yesterdayClosePrice = quote.YesterdayClosePrice;
                    //    float downLimit = PriceHelper.CalcDownLimit(yesterdayClosePrice);

                    //    textBox1.AppendText("try to send order to buy 601398 at price {0:0.000}", downLimit);

                    //    bool succeeded = client.SendOrder(OrderCategory.Buy, OrderPriceType.LimitPrice, icbcCode, downLimit, 100, out result, out error);
                    //    WriteOutput(result, error);

                    //    if (succeeded)
                    //    {
                    //        var sendOrderResults = SendOrderResult.ExtractFrom(result);
                    //        if (sendOrderResults.Count() == 0)
                    //        {
                    //            textBox1.AppendText("Failed to get result for SendOrder()");
                    //        }
                    //        else
                    //        {
                    //            var sendOrderResult = sendOrderResults.First();

                    //            Thread.Sleep(5000);

                    //            textBox1.AppendText("try to cancel order {0}", sendOrderResult.OrderNo);

                    //            succeeded = client.CancelOrder(Exchange.GetTradeableExchangeForSecurity(icbcCode), sendOrderResult.OrderNo, out result, out error);

                    //            WriteOutput(result, error);
                    //        }
                    //    }
                    //}

                    succeeded = client.QueryData(DataCategory.OrderSubmittedToday, out result, out error);
                    if (succeeded)
                    {
                        var orders = QueryGeneralOrderResult.ExtractFrom(result);

                        foreach (var order in orders)
                        {
                            textBox1.AppendText("\n");

                            textBox1.AppendText(
                                string.Format(
                                    "{0} {1} {2} {3} {4:0.000} {5} {6} {7}",
                                    order.OrderNo,
                                    order.SubmissionTime,
                                    order.BuySellFlag,
                                    order.StatusString,
                                    order.SubmissionPrice,
                                    order.SubmissionVolume,
                                    order.SubmissionType,
                                    order.PricingType));
                        }
                    }

                    WriteOutput(result, error);
                }
            }
            catch (Exception ex)
            {
                textBox1.AppendText("\n");

                textBox1.AppendText(string.Format("{0}", ex));
            }
            finally
            {
                TradingEnvironment.UnInitialize();
            }
        }
Ejemplo n.º 2
0
        private void GetQuote(object state)
        {
            if (!Monitor.TryEnter(_publisherLockObj))
            {
                // ignore this refresh because previous refreshing is still on going.
                return;
            }

            if (_isStopped)
            {
                return;
            }

            if (_client == null || !_client.IsLoggedOn())
            {
                return;
            }

            try
            {
                // get a duplicate quote list to avoid lock quote list too long
                List <string> codes = null;

                lock (_codeListLockObj)
                {
                    if (_codeList.Count == 0)
                    {
                        return;
                    }

                    codes = new List <string>(_codeList);
                }

                System.Diagnostics.Debug.Assert(codes.Count > 0);

                List <string[]> subsets = new List <string[]>();
                for (int index = 0; index < codes.Count; index += MaxBatchSize)
                {
                    int count = Math.Min(codes.Count - index, MaxBatchSize);
                    if (count == 0)
                    {
                        break;
                    }

                    var subset = codes.GetRange(index, count).ToArray();

                    subsets.Add(subset);
                }

                Parallel.ForEach(
                    subsets,
                    async subset =>
                {
                    List <SinaStockQuote> sinaQuotes = await SinaStockQuoteInterface.GetQuote(subset);

                    string[] errors;
                    FiveLevelQuote[] quotes = _client.GetQuote(subset, out errors);

                    if (quotes.Length != sinaQuotes.Count)
                    {
                        throw new InvalidOperationException("The count of sina quote does not match tdx quote");
                    }

                    for (int i = 0; i < quotes.Length; ++i)
                    {
                        if (quotes[i] != null)
                        {
                            quotes[i].DealAmount       = sinaQuotes[i].DealAmount;
                            quotes[i].DealVolumeInHand = sinaQuotes[i].DealVolumeInHand;
                        }
                    }

                    PublishQuotes(quotes, errors);
                });
            }
            catch (Exception ex)
            {
                ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
                if (logger != null)
                {
                    logger.ErrorFormat("Exception in getting quote: {0}", ex);
                }
            }
            finally
            {
                Monitor.Exit(_publisherLockObj);
            }
        }