public override void OnStrategyStart()
        {
            base.OnStrategyStart();

            // 测试用,自定义交易时间,仿真或实盘时可删除
            base.TimeHelper = new TimeHelper(new int[] { 0, 2400 },2100, 1458);

            base.TargetPosition = 0;
            //base.DualPosition.Long.Qty = 0;
            //base.DualPosition.Long.QtyToday = 0;
            //base.DualPosition.Short.Qty = 0;
            //base.DualPosition.Short.QtyToday = 0;
            if(BrokerInfo == null)
            {
                // 使用静态的主要原因是每个实例都来取一次没有必要
                BrokerInfo = DataManager.GetBrokerInfo();   
            }
            if (BrokerInfo.Accounts.Count > 0)
            {
                BrokerAccount brokerAccount = BrokerInfo.Accounts[0];
                GetBrokerInfoHelper.Transform(brokerAccount, base.DualPosition);
            }
            

            LoadHistoricalBars(Clock.Now);

            BarSeries bars1min = GetBars(BarType.Time, BarSize);

            fastSMA = new SMA(bars1min, fastLength, Color.Red);
            slowSMA = new SMA(bars1min, slowLength, Color.Green);

            Draw(fastSMA, 0);
            Draw(slowSMA, 0);
        }
Beispiel #2
0
 protected override BrokerInfo GetBrokerInfo()
 {
   BrokerInfo brokerInfo = new BrokerInfo();
   if (!this.isConnected)
     return brokerInfo;
   try
   {
     this.rwClientById.EnterReadLock();
     foreach (string index in this.clientById.Keys)
       this.SendCommandLine("<command id=\"get_client_limits\" client=\"" + this.clientById[index].Id + "\"/>");
     foreach (string index in this.clientById.Keys)
     {
       TransaqClient transaqClient = this.clientById[index];
       BrokerAccount brokerAccount = brokerInfo.AddAccount(transaqClient.Id);
       brokerAccount.AddField("Client: Client Id", transaqClient.Id);
       brokerAccount.AddField("Client: Type", transaqClient.Type);
       brokerAccount.AddField("Client: Currency", transaqClient.Currency);
       brokerAccount.AddField("Client: Ml_intraday", transaqClient.Ml_intraday);
       brokerAccount.AddField("Client: Ml_overnight", transaqClient.Ml_overnight);
       brokerAccount.AddField("Client: Ml_restrict", transaqClient.Ml_restrict);
       brokerAccount.AddField("Client: Ml_call", transaqClient.Ml_call);
       brokerAccount.AddField("Client: Ml_close", transaqClient.Ml_close);
       Dictionary<string, TransaqMoneyPosition> dictionary1 = (Dictionary<string, TransaqMoneyPosition>) null;
       lock (this.positions.lockerMoneyPosition)
       {
         if (this.positions.MoneyPosition.TryGetValue(transaqClient.Id, out dictionary1))
         {
           foreach (string item_1 in dictionary1.Keys)
           {
             TransaqMoneyPosition local_9 = dictionary1[item_1];
             brokerAccount.AddField(string.Format("MoneyPos Register={0}: Asset", (object) local_9.Register), local_9.Asset);
             brokerAccount.AddField(string.Format("MoneyPos Register={0}: ShortName", (object) local_9.Register), local_9.ShortName);
             brokerAccount.AddField(string.Format("MoneyPos Register={0}: SaldoIn", (object) local_9.Register), local_9.SaldoIn);
             brokerAccount.AddField(string.Format("MoneyPos Register={0}: Saldo", (object) local_9.Register), local_9.Saldo);
             brokerAccount.AddField(string.Format("MoneyPos Register={0}: Bought", (object) local_9.Register), local_9.Bought);
             brokerAccount.AddField(string.Format("MoneyPos Register={0}: Sold", (object) local_9.Register), local_9.Sold);
             brokerAccount.AddField(string.Format("MoneyPos Register={0}: OrdBuy", (object) local_9.Register), local_9.OrdBuy);
             brokerAccount.AddField(string.Format("MoneyPos Register={0}: OrdBuyCond", (object) local_9.Register), local_9.OrdBuyCond);
             brokerAccount.AddField(string.Format("MoneyPos Register={0}: Comission", (object) local_9.Register), local_9.Comission);
           }
         }
       }
       TransaqFortsMoney transaqFortsMoney = (TransaqFortsMoney) null;
       lock (this.positions.lockerFortsMoney)
       {
         if (this.positions.FortsMoney.TryGetValue(transaqClient.Id, out transaqFortsMoney))
         {
           brokerAccount.AddField("FortsMoney: Current", transaqFortsMoney.Current);
           brokerAccount.AddField("FortsMoney: Blocked", transaqFortsMoney.Blocked);
           brokerAccount.AddField("FortsMoney: Free", transaqFortsMoney.Free);
           brokerAccount.AddField("FortsMoney: VarMargin", transaqFortsMoney.VarMargin);
         }
       }
       TransaqFortsCollaterals fortsCollaterals = (TransaqFortsCollaterals) null;
       lock (this.positions.lockerFortsCollaterals)
       {
         if (this.positions.FortsCollaterals.TryGetValue(transaqClient.Id, out fortsCollaterals))
         {
           brokerAccount.AddField("FortsCollaterals: Client", fortsCollaterals.Client);
           brokerAccount.AddField("FortsCollaterals: Current", fortsCollaterals.Current);
           brokerAccount.AddField("FortsCollaterals: Blocked", fortsCollaterals.Blocked);
           brokerAccount.AddField("FortsCollaterals: Free", fortsCollaterals.Free);
         }
       }
       TransaqSpotLimit transaqSpotLimit = (TransaqSpotLimit) null;
       lock (this.positions.lockerSpotLimit)
       {
         if (this.positions.SpotLimit.TryGetValue(transaqClient.Id, out transaqSpotLimit))
         {
           brokerAccount.AddField("SpotLimit: Client", transaqSpotLimit.Client);
           brokerAccount.AddField("SpotLimit: BuyLimit", transaqSpotLimit.BuyLimit);
           brokerAccount.AddField("SpotLimit: BuyLimitUsed", transaqSpotLimit.BuyLimitUsed);
         }
       }
       TransaqClientLimit transaqClientLimit = (TransaqClientLimit) null;
       try
       {
         this.rwClientLimits.EnterReadLock();
         if (this.clientLimits.TryGetValue(transaqClient.Id, out transaqClientLimit))
         {
           brokerAccount.AddField("ClientLimit: Client", transaqClientLimit.Client);
           brokerAccount.AddField("ClientLimit: CbplLimit", transaqClientLimit.CbplLimit);
           brokerAccount.AddField("ClientLimit: CbplUsed", transaqClientLimit.CbplUsed);
           brokerAccount.AddField("ClientLimit: CbplPlanned", transaqClientLimit.CbplPlanned);
           brokerAccount.AddField("ClientLimit: Fob_VarMargin", transaqClientLimit.Fob_VarMargin);
           brokerAccount.AddField("ClientLimit: Coverage", transaqClientLimit.Coverage);
           brokerAccount.AddField("ClientLimit: Liquidity_C", transaqClientLimit.Liquidity_C);
           brokerAccount.AddField("ClientLimit: Profit", transaqClientLimit.Profit);
           brokerAccount.AddField("ClientLimit: Money_Current", transaqClientLimit.Money_Current);
           brokerAccount.AddField("ClientLimit: Money_Blocked", transaqClientLimit.Money_Blocked);
           brokerAccount.AddField("ClientLimit: Money_Free", transaqClientLimit.Money_Free);
           brokerAccount.AddField("ClientLimit: Options_Premium", transaqClientLimit.Options_Premium);
           brokerAccount.AddField("ClientLimit: Exchange_Fee", transaqClientLimit.Exchange_Fee);
           brokerAccount.AddField("ClientLimit: Forts_VarMargin", transaqClientLimit.Forts_VarMargin);
           brokerAccount.AddField("ClientLimit: PclMargin", transaqClientLimit.PclMargin);
           brokerAccount.AddField("ClientLimit: Options_VM", transaqClientLimit.Options_VM);
           brokerAccount.AddField("ClientLimit: Spot_Buy_Limit", transaqClientLimit.Spot_Buy_Limit);
           brokerAccount.AddField("ClientLimit: Used_Spot_Buy_Limit", transaqClientLimit.Used_Spot_Buy_Limit);
           brokerAccount.AddField("ClientLimit: Collat_Current", transaqClientLimit.Collat_Current);
           brokerAccount.AddField("ClientLimit: Collat_Blocked", transaqClientLimit.Collat_Blocked);
           brokerAccount.AddField("ClientLimit: Collat_Free", transaqClientLimit.Collat_Free);
         }
       }
       finally
       {
         this.rwClientLimits.ExitReadLock();
       }
       if (!this.brokerAccountByClientId.ContainsKey(transaqClient.Id))
         this.brokerAccountByClientId.Add(transaqClient.Id, brokerAccount);
       Dictionary<string, TransaqSecPosition> dictionary2 = (Dictionary<string, TransaqSecPosition>) null;
       lock (this.positions.lockerSecPosition)
       {
         if (this.positions.SecPosition.TryGetValue(transaqClient.Id, out dictionary2))
         {
           foreach (string item_2 in dictionary2.Keys)
           {
             TransaqSecPosition local_16 = dictionary2[item_2];
             BrokerPosition local_17 = brokerAccount.AddPosition();
             local_17.Symbol = local_16.SecCode;
             if (local_16.Saldo > 0.0)
               local_17.LongQty = local_16.Saldo;
             else
               local_17.ShortQty = -local_16.Saldo;
             local_17.Qty = local_16.Saldo;
             local_17.AddCustomField("SecId", local_16.SecId.ToString());
             local_17.AddCustomField("Market", local_16.Market.ToString());
             local_17.AddCustomField("SecCode", local_16.SecCode);
             local_17.AddCustomField("Register", local_16.Register);
             local_17.AddCustomField("Client", local_16.Client);
             local_17.AddCustomField("ShortName", local_16.ShortName);
             local_17.AddCustomField("SaldoIn", local_16.SaldoIn);
             local_17.AddCustomField("SaldoMin", local_16.SaldoMin);
             local_17.AddCustomField("Bought", local_16.Bought.ToString());
             local_17.AddCustomField("Sold", local_16.Sold.ToString());
             local_17.AddCustomField("Saldo", local_16.Saldo.ToString());
             local_17.AddCustomField("OrderBuy", local_16.OrderBuy);
             local_17.AddCustomField("OrderSell", local_16.OrderSell);
           }
         }
       }
       Dictionary<string, TransaqFortsPosition> dictionary3 = (Dictionary<string, TransaqFortsPosition>) null;
       lock (this.positions.lockerFortsPosition)
       {
         if (this.positions.FortsPosition.TryGetValue(transaqClient.Id, out dictionary3))
         {
           foreach (string item_4 in dictionary3.Keys)
           {
             TransaqFortsPosition local_20 = dictionary3[item_4];
             BrokerPosition local_21 = brokerAccount.AddPosition();
             local_21.Symbol = local_20.SecCode;
             if (local_20.TotalNet > 0.0)
               local_21.LongQty = local_20.TotalNet;
             else
               local_21.ShortQty = -local_20.TotalNet;
             local_21.Qty = local_20.TotalNet;
             local_21.AddCustomField("SecId", local_20.SecId.ToString());
             local_21.AddCustomField("SecCode", local_20.SecCode);
             local_21.AddCustomField("Client", local_20.Client);
             local_21.AddCustomField("StartNet", local_20.StartNet);
             local_21.AddCustomField("OpenBuys", local_20.OpenBuys);
             local_21.AddCustomField("OpenSells", local_20.OpenSells);
             local_21.AddCustomField("TotalNet", local_20.TotalNet.ToString());
             local_21.AddCustomField("TodayBuy", local_20.TodayBuy);
             local_21.AddCustomField("TodaySell", local_20.TodaySell);
             local_21.AddCustomField("OptMargin", local_20.OptMargin);
             local_21.AddCustomField("VarMargin", local_20.VarMargin);
             local_21.AddCustomField("ExpirationPos", local_20.ExpirationPos);
             local_21.AddCustomField("UsedSellSpotLimit", local_20.UsedSellSpotLimit);
             local_21.AddCustomField("SellSpotLimit", local_20.SellSpotLimit);
             local_21.AddCustomField("Netto", local_20.Netto);
             local_21.AddCustomField("Kgo", local_20.Kgo);
           }
         }
       }
     }
   }
   finally
   {
     this.rwClientById.ExitReadLock();
   }
   return brokerInfo;
 }
Beispiel #3
0
        /// <summary>
        /// called by OpenQuant if
        /// * User clicks "Refresh" icon in "Broker Info" tab
        /// * Project code calls various broker related functions
        /// 
        /// the information returned is shown in the "Broker Info" Tab
        /// </summary>
        /// <returns></returns>
        protected override BrokerInfo GetBrokerInfo()
        {
            // info("GetBrokerInfo called");
            int numAccounts = 0;
            int numOpenPositions = 0;
            int numPendingOrders = 0;
            BrokerInfo brokerInfo = new BrokerInfo();
            if (!IsConnected) return brokerInfo;

            lock (private_lock)
            {
                foreach (string acctName in activeAccounts.Keys)
                {
                    if (!Accounts.IsActive(acctName)) continue; // skip inactive accounts
                    brokerInfo.AddAccount(acctName);
                    numAccounts++;

                    BrokerAccount oqAccount = brokerInfo.Accounts[acctName];
                    // Account Details
                    foreach (DataRow row in accountDetails.Rows)
                    {
                        if ((string)row["Account"] != acctName) continue;
                        string key = (string) row["Key"];
                        string value = (string) row["Value"];
                        oqAccount.AddField(key, (string) row["Currency"], value);
                        if (key == "BuyingPower")
                        {
                            double buyingPower;
                            if (!double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture,
                                                 out buyingPower))
                            {
                                error("can't parse buying Power: " + value);
                            }
                            else
                            {
                                //logger.AddLog(LoggerLevel.Detail, "*** received Buying Power: " + buyingPower.ToString());
                                // activeAccounts[e.AccountName] = buyingPower;
                                oqAccount.BuyingPower = buyingPower;
                            }
                        }
                    }
                    // Positions
                    foreach(SrBrokerPosition brpos in openPositions.Values)
                    {
                        if (brpos.Fields["Account"] != acctName) continue;
                        if ((int)Math.Round(brpos.Qty) == 0) continue; // no longer exists
                        BrokerPosition pos = oqAccount.AddPosition();
                        numOpenPositions++;
                        pos.Currency = brpos.Currency;
                        pos.Exchange = brpos.Exchange;
                        pos.InstrumentType = brpos.InstrumentType;
                        pos.LongQty = brpos.LongQty;
                        pos.Maturity = brpos.Maturity;
                        pos.PutCall = brpos.PutCall;
                        pos.Qty = brpos.Qty;
                        pos.ShortQty = brpos.ShortQty;
                        pos.Strike = brpos.Strike;
                        pos.Symbol = brpos.Symbol;
                        // Fields
                        foreach(KeyValuePair<string,string> kvp in brpos.Fields)
                        {
                            pos.AddCustomField(kvp.Key, kvp.Value);
                        }
                    }
                    // Orders
                    foreach (SrBrokerOrder srorder in pendingOrders.Values)
                    {
                        if (srorder.Fields["Account"] != acctName) continue;
                        BrokerOrder ord = oqAccount.AddOrder();
                        numPendingOrders++;
                        ord.Currency = srorder.Currency;
                        ord.Exchange = srorder.Exchange;
                        ord.InstrumentType = srorder.InstrumentType;
                        ord.OrderID = srorder.OrderId;
                        ord.Price = srorder.Price;
                        ord.Qty = srorder.Qty;
                        ord.Side = srorder.Side;
                        ord.Status = srorder.Status;
                        ord.StopPrice = srorder.StopPrice;
                        ord.Symbol = srorder.Symbol;
                        ord.Type = srorder.Type;
                        // Fields
                        foreach (KeyValuePair<string, string> kvp in srorder.Fields)
                        {
                            ord.AddCustomField(kvp.Key, kvp.Value);
                        }
                    }
                }
            }
            info("BrokerInfo: "
                + numAccounts + " active accounts, "
                + numOpenPositions + " Positions, "
                + numPendingOrders + " Orders");
            return brokerInfo;
        }