Пример #1
0
        public void Stop()
        {
            _timer.Dispose();
            _timer = null;

            lock (_dispatcherLockObj)
            {
                _isStopped = true;

                _client = null;
            }
        }
Пример #2
0
        public OrderDispatcher(TradingClient client, int refreshingIntervalInMillisecond)
        {
            if (client == null)
            {
                throw new ArgumentException();
            }

            _client = client;
            _refreshingIntervalInMillisecond = refreshingIntervalInMillisecond;

            _timer = new Timer(QueryOrderStatus, null, 0, _refreshingIntervalInMillisecond);
        }
Пример #3
0
        public QuotePublisher(TradingClient client, int refreshingIntervalInMillisecond)
        {
            if (client == null)
            {
                throw new ArgumentNullException();
            }

            if (refreshingIntervalInMillisecond <= 0)
            {
                throw new ArgumentOutOfRangeException("Refreshing interval must be greater than 0");
            }

            _client = client;
            _refreshingIntervalInMillisecond = refreshingIntervalInMillisecond;

            _timer = new Timer(GetQuote, null, 0, refreshingIntervalInMillisecond);
        }
Пример #4
0
        public bool Initialize(
            string address,
            short port,
            string protocolVersion,
            short yybId,
            string accountNo,
            short accountType,
            string tradeAccount,
            string tradePassword,
            string communicationPassword,
            out string error)
        {
            if (!_initialized)
            {
                lock (_initializationLockObj)
                {
                    if (!_initialized)
                    {
                        TradingEnvironment.Initialize();

                        _client = new TradingClient();

                        if (!_client.LogOn(address, port, protocolVersion, yybId, accountNo, accountType, tradeAccount, tradePassword, communicationPassword, out error))
                        {
                            _client.Dispose();
                            _client = null;

                            return(false);
                        }

                        _quotePublisher  = new QuotePublisher(_client, QuoteRefreshingInterval);
                        _orderDispatcher = new OrderDispatcher(_client, OrderRefreshingInterval);

                        _initialized = true;

                        return(true);
                    }
                }
            }

            error = "CtpSimulator has been initialized already, can't be reinitialized";
            return(false);
        }
Пример #5
0
        public void UnInitialize()
        {
            if (_initialized)
            {
                lock (_initializationLockObj)
                {
                    if (_initialized)
                    {
                        _quotePublisher.Stop();
                        _quotePublisher = null;

                        _orderDispatcher.Stop();
                        _orderDispatcher = null;

                        _client.LogOff();
                        _client = null;

                        TradingEnvironment.UnInitialize();

                        _initialized = false;
                    }
                }
            }
        }
Пример #6
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();
            }
        }