예제 #1
0
        private void tb_Trade_Click(object sender, EventArgs e)
        {
            if (!chartComponent.ConnectedServer)
            {
                if (this.marketTrader == null)
                {
                    //弹出交易账号选择界面
                    FormAccount  formAccount = new FormAccount(DataCenter.Default.AccountManager, FormAccount.PATH_MOCK);
                    DialogResult result      = formAccount.ShowDialog();
                    if (result == DialogResult.OK)
                    {
                        chartComponent.Account = formAccount.SelectedAccount;
                        IAccount account = chartComponent.Account;
                        if (account.Time > 0)
                        {
                            string code = null;
                            if (account.CurrentTradeInfo.Count > 0)
                            {
                                code = account.CurrentTradeInfo[account.CurrentTradeInfo.Count - 1].InstrumentID;
                            }
                            if (code != null)
                            {
                                chartComponent.Controller.Change(code, account.Time);
                            }
                            else
                            {
                                chartComponent.Controller.Change(account.Time);
                            }
                        }
                        account.BindRealTimeReader(this.chartComponent.Controller.CurrentRealTimeDataReader);
                        this.marketTrader = new Plugin_MarketTrader_Simu(account, this.chartComponent.DataCenter.AccountManager, formAccount.SelectedAccountPath, formAccount.SelectedAccountName);
                    }
                    else
                    {
                        return;
                    }
                }
            }
            FormTrade formTrade = new FormTrade(marketTrader, chartComponent.Controller.CurrentNavigater);

            formTrade.TopMost = true;
            formTrade.Show();
        }
예제 #2
0
        // public void AddContract(string instrumentID) {
        //     TradeContractList.Add(instrumentID);
        // }
        public void ITick(FormTrade formTrade, ref CThostFtdcDepthMarketDataField pDepthMarketData)
        {
            if (which == pDepthMarketData.InstrumentID)
            {
                Console.WriteLine("strategy tick tradestrategy[0] = " + tradestrategy[0]);
                if (tradestrategy[0] == 1)
                {
                    tickprice[3] = tickprice[2];
                    tickprice[2] = tickprice[1];
                    tickprice[1] = tickprice[0];
                    tickprice[0] = pDepthMarketData.LastPrice;

                    //Console.WriteLine(tickprice[0] + "," + tickprice[1] + "," + tickprice[2]);

                    if (tickprice[0] > tickprice[1] && tickprice[1] > tickprice[2] &&
                        tickprice[2] > tickprice[3])
                    {
                        if (TradeHave[0] == 0)
                        {
                            //3 TICK up,buy
                            formTrade.tradeApi.OrderInsert(pDepthMarketData.InstrumentID, EnumOffsetFlagType.Open, EnumDirectionType.Buy,
                                                           pDepthMarketData.LastPrice + 10, //价格
                                                           1);
                            buyprice[0]  = pDepthMarketData.LastPrice;
                            TradeHave[0] = 1;
                        }
                    }
                    else if (tickprice[0] < tickprice[1] && tickprice[1] < tickprice[2] &&
                             tickprice[2] < tickprice[3])
                    {
                        //3 TICK down,Sell
                        if (TradeHave[0] == 1)
                        {
                            formTrade.tradeApi.OrderInsert(pDepthMarketData.InstrumentID, EnumOffsetFlagType.Open, EnumDirectionType.Sell,
                                                           pDepthMarketData.LastPrice - 10, //价格
                                                           1);

                            TradeHave[0] = 0;
                        }
                    }
                }
                else if (tradestrategy[0] == 2)
                {
                    List <CtpInfo> lists = formTrade.dbConn.QueryMarketInfo(which, 1);

                    //Console.WriteLine("database: " + lists.Count);

                    CtpInfo ctpInfo = lists[0];
                    if (lists.Count == 0)
                    {
                        return;
                    }
                    double v1 = ctpInfo.high - ctpInfo.close, v2 = ctpInfo.close - ctpInfo.low;
                    double mx     = Math.Max(v1, v2);
                    double offset = mx * 0.7;

                    double todayOpen = pDepthMarketData.OpenPrice;

                    //Console.WriteLine("LastPrice = " + pDepthMarketData.LastPrice + ", " + todayOpen + "+" + offset);

                    if (TradeHave[0] == 0)
                    {
                        if (pDepthMarketData.LastPrice > todayOpen + offset)
                        { // buy
                            buyprice[0] = pDepthMarketData.LastPrice;
                            formTrade.tradeApi.OrderInsert(pDepthMarketData.InstrumentID, EnumOffsetFlagType.Open, EnumDirectionType.Buy,
                                                           pDepthMarketData.LastPrice + 10, //价格
                                                           1);
                            TradeHave[0] = 1;
                        }
                    }
                    else if (TradeHave[0] == 1)
                    {
                        if (pDepthMarketData.LastPrice < todayOpen - offset)
                        {
                            formTrade.tradeApi.OrderInsert(pDepthMarketData.InstrumentID, EnumOffsetFlagType.Open, EnumDirectionType.Sell,
                                                           pDepthMarketData.LastPrice - 10, //价格
                                                           1);
                            TradeHave[0] = 0;
                        }
                        else if (pDepthMarketData.LastPrice > buyprice[0] + winstoparr[0])   // 盈利大于预设
                        {
                            formTrade.tradeApi.OrderInsert(pDepthMarketData.InstrumentID, EnumOffsetFlagType.Open, EnumDirectionType.Sell,
                                                           pDepthMarketData.LastPrice - 10, //价格
                                                           1);
                            TradeHave[0] = 0;
                        }
                    }
                }
            }
        }