Example #1
0
        static void TestTradeApi(TradeApi tapi)
        {
            tapi.OnAccountStatus += OnAccountStatus;
            tapi.OnOrderStatus   += OnOrderStatus;
            tapi.OnOrderTrade    += OnOrderTrade;

            {
                var r = tapi.QueryAccountStatus();
                if (r.Value != null)
                {
                    foreach (var act in r.Value)
                    {
                        Console.WriteLine("account: " + act.AccountId + ","
                                          + act.Broker + "," + act.AccountId + "," + act.AccountType);
                    }
                }
                else
                {
                    Console.WriteLine("QueryAccountStatus error: " + r.Msg);
                }
            }

            {
                var r = tapi.QueryBalance("glsc");
                if (r.Value != null)
                {
                    var bal = r.Value;
                    Console.WriteLine("balance: "
                                      + bal.InitBalance + ","
                                      + bal.EnableBalance + ","
                                      + bal.Margin + ","
                                      + bal.FloatPnl + ","
                                      + bal.ClosePnl
                                      );
                }
                else
                {
                    Console.WriteLine("QueryBalance error: " + r.Msg);
                }
            }
            {
                var r = tapi.QueryPositions("glsc");
                if (r.Value != null)
                {
                    foreach (var pos in r.Value)
                    {
                        Console.WriteLine("position: "
                                          + pos.Code + ","
                                          + pos.Name + ","
                                          + pos.Side + ","
                                          + pos.InitSize + ","
                                          + pos.EnableSize + ","
                                          + pos.CurrentSize + ","
                                          + pos.TodaySize
                                          );
                    }
                }
                else
                {
                    Console.WriteLine("QueryPositions error: " + r.Msg);
                }
            }
            {
                var r = tapi.QueryOrders("glsc");
                if (r.Value != null)
                {
                    foreach (var ord in r.Value)
                    {
                        Console.WriteLine("order: "
                                          + ord.AccountId + ","
                                          + ord.Code + ","
                                          + ord.EntrustAction + ","
                                          + ord.EntrustPrice + ","
                                          + ord.EntrustSize + ","
                                          + ord.EntrustDate + ","
                                          + ord.EntrustTime + ","
                                          + ord.EntrustNo + ","
                                          + ord.FillPrice + ","
                                          + ord.FillSize + ","
                                          + ord.Status + ","
                                          + ord.StatusMsg
                                          );
                    }
                }
                else
                {
                    Console.WriteLine("QueryOrders error: " + r.Msg);
                }
            }
            {
                var r = tapi.QueryTrades("glsc");
                if (r.Value != null)
                {
                    foreach (var trade in r.Value)
                    {
                        Console.WriteLine("order: "
                                          + trade.AccountId + ","
                                          + trade.FillDate + ","
                                          + trade.FillTime + ","
                                          + trade.Code + ","
                                          + trade.EntrustAction + ","
                                          + trade.EntrustNo + ","
                                          + trade.FillPrice + ","
                                          + trade.FillSize + ","
                                          + trade.FillNo
                                          );
                    }
                }
                else
                {
                    Console.WriteLine("QueryTrades error: " + r.Msg);
                }
            }

            {
                var r = tapi.PlaceOrder("glsc", "000001.SH", 10.0, 100, "Buy");
                if (r.Value != null)
                {
                    var oid = r.Value;
                    Console.WriteLine("PlaceOrder result: " + oid.EntrustNo + "," + oid.OrderId);
                }
                else
                {
                    Console.WriteLine("PlaceOrder error: " + r.Msg);
                }

                if (r.Value != null)
                {
                    var r2 = tapi.CancelOrder("glsc", "000001.SH", r.Value.EntrustNo);
                    if (r2.Value)
                    {
                        Console.WriteLine("CancelOrder result: " + r2.Value);
                    }
                    else
                    {
                        Console.WriteLine("CancelOrder error: " + r2.Msg);
                    }
                }
            }

            while (true)
            {
                Thread.Sleep(1000);
            }
        }
Example #2
0
        void RefreshData()
        {
            TradeApi tapi = GlobalData.GetTradeApi();

            var accounts = tapi.QueryAccountStatus().Value;

            if (accounts == null)
            {
                return;
            }

            List <Position> positions = new List <Position>();

            foreach (var act in accounts)
            {
                var r = tapi.QueryPositions(act.AccountId);
                if (r.Value == null)
                {
                    MessageBox.Show(r.Msg, "Error");
                    continue;
                }
                foreach (var p in r.Value)
                {
                    positions.Add(p);
                }
            }

            listView1.BeginUpdate();
            listView1.Items.Clear();
            foreach (var pos in positions)
            {
                ListViewItem item = new ListViewItem();
                item.Text = ""; // i.ToString();
                item.SubItems.Add(pos.AccountId);
                item.SubItems.Add(pos.Code);
                item.SubItems.Add(pos.Name);
                item.SubItems.Add(pos.CurrentSize.ToString());
                item.SubItems.Add(pos.EnableSize.ToString());
                item.SubItems.Add(pos.InitSize.ToString());
                item.SubItems.Add(pos.TodaySize.ToString());
                item.SubItems.Add(pos.FrozenSize.ToString());
                item.SubItems.Add(pos.Side);
                item.SubItems.Add(pos.Cost.ToString());
                item.SubItems.Add(pos.CostPrice.ToString());
                item.SubItems.Add(pos.LastPrice.ToString());
                item.SubItems.Add(pos.FloatPnl.ToString()).ForeColor = pos.LastPrice > pos.CostPrice ? Color.Red : Color.Green;
                item.SubItems.Add(pos.ClosePnl.ToString());
                item.SubItems.Add(pos.Margin.ToString());
                item.SubItems.Add(pos.Commission.ToString());

                listView1.Items.Add(item);
            }

            foreach (ColumnHeader col in listView1.Columns)
            {
                col.Width     = -2;
                col.TextAlign = HorizontalAlignment.Right;
            }

            listView1.EndUpdate();
        }