Beispiel #1
0
 private static void Initialize(TradeDay tradeDay, DataRow dataRow)
 {
     tradeDay.CurrentDay = (DateTime)dataRow["TradeDay"];
     tradeDay.BeginTime  = (DateTime)dataRow["BeginTime"];
     tradeDay.EndTime    = (DateTime)dataRow["EndTime"];
     tradeDay.LastDay    = (DateTime)dataRow["LastTradeDay"];
 }
Beispiel #2
0
        private string GenerateTranCode(string orgCode, TradeDay tradeDay, string orderTypeCode)
        {
            string prefix = this.GenerateTranCodePrefix(orgCode, tradeDay, orderTypeCode);
            string suffix = this.GenerateCodeSuffix(this.GetTranSequence(prefix));

            return(prefix + suffix);
        }
Beispiel #3
0
        internal void NotifyTradeDayReset(DateTime tradeDay)
        {
            Thread thread = new Thread(delegate()
            {
                string sql      = "EXEC P_GetResetEmailNotGeneratedTradeDays";
                DataSet dataSet = DataAccess.GetData(sql, this._ConnectionString);

                bool hasTradeDay = dataSet.Tables[0].Rows.Count > 0;
                if (hasTradeDay)
                {
                    lock (this._ToBeSendEntitiesLock)
                    {
                        foreach (DataRow dataRow in dataSet.Tables[0].Rows)
                        {
                            TradeDay tradeDay2 = new TradeDay(dataRow);
                            Logger.Info(string.Format("NotifyTradeDayReset, will notify {0} if it is not equal to TradeDay = {1}", tradeDay2.Day, tradeDay));
                            if (tradeDay2.Day != tradeDay)
                            {
                                Logger.Info("NotifyTradeDayReset " + tradeDay2.Day.ToString());
                                this._ToBeSendEntities.Enqueue(new TradeDayReset(tradeDay2));
                            }
                        }
                    }
                    this._HasToBeSendEntityEvent.Set();
                }
            });

            thread.IsBackground = true;
            thread.Start();
        }
Beispiel #4
0
        private void VerifyTransaction(Transaction tran, PlaceContext context)
        {
            TradeDay tradeDay          = Settings.Setting.Default.GetTradeDay(context.TradeDay);
            var      tradePolicyDetail = tran.TradePolicyDetail(context.TradeDay);
            var      settingAccount    = tran.Owner.Setting(context.TradeDay);
            var      account           = tran.Owner;

            if (tran.OrderType != OrderType.Risk || (tran.ExecuteTime != null && tran.ExecuteTime.Value > tradeDay.BeginTime))
            {
                if (tran.Owner.IsResetFailed && tran.Type != TransactionType.Assign)
                {
                    throw new TransactionServerException(TransactionError.AccountResetFailed, string.Format("accountId = {0}, lastResetDay = {1}, tran.id = {2}, tran.Type = {3}", tran.Owner.Id, tran.Owner.LastResetDay, tran.Id, tran.Type));
                }

                if (tradePolicyDetail == null)
                {
                    Logger.InfoFormat("can't find tradePolicyDetail  tranId = {0}, tradePolicyId = {1}, instrumentId = {2}", tran.Id, tran.Owner.Setting(context.TradeDay).TradePolicyId, tran.InstrumentId);
                    throw new TransactionServerException(TransactionError.InstrumentNotInTradePolicy);
                }


                if (!tradePolicyDetail.IsTradeActive)
                {
                    throw new TransactionServerException(TransactionError.TradePolicyIsNotActive);
                }

#if PLACETEST
#else
                if (this.ShouldVerifyInstrumentCanPlaceAndTrade(tran))
                {
                    this.VerifyInstrumentCanPlaceAndTrade(tran, context);
                }
#endif
                if (settingAccount.Type == AccountType.Agent)
                {
                    if (account.HasUnassignedOvernightOrders)
                    {
                        throw new TransactionServerException(TransactionError.HasUnassignedOvernightOrders);
                    }
                }
                else
                {
                    if (tran.OrderType != OrderType.Risk && tran.Type != TransactionType.Assign)
                    {
                        if (account.IsLocked)
                        {
                            throw new TransactionServerException(TransactionError.IsLockedByAgent);
                        }
                    }
                }
            }
        }
Beispiel #5
0
        private static Price CalculateAutoClosePrice(Order order, PriceType priceType)
        {
            Debug.Assert(order.IsOpen);
            SpecialTradePolicyDetail policy = order.Owner.SpecialTradePolicyDetail();

            Settings.Instrument instrument = order.Owner.SettingInstrument();

            OrderLevelRiskBase autoCloseBase      = priceType == PriceType.Limit ? policy.AutoLimitBase : policy.AutoStopBase;
            decimal            autoCloseThreshold = priceType == PriceType.Limit ? policy.AutoLimitThreshold : policy.AutoStopThreshold;

            if (autoCloseBase == OrderLevelRiskBase.None)
            {
                return(null);
            }
            else if (autoCloseBase == OrderLevelRiskBase.Necessary)
            {
                return(CalculateForOrderLevelRiskNecessay(order, autoCloseThreshold, instrument, priceType));
            }
            else
            {
                Price basePrice = order.ExecutePrice;
                if (autoCloseBase == OrderLevelRiskBase.SettlementPrice)
                {
                    TradeDay tradeDay = Settings.Setting.Default.GetTradeDay();
                    if (order.Owner.ExecuteTime > tradeDay.BeginTime)
                    {
                        return(null);
                    }
                    else
                    {
                        basePrice = (order.IsBuy ? instrument.DayQuotation.Buy : instrument.DayQuotation.Sell);
                    }
                }
                int autoClosePips = (int)autoCloseThreshold;
                if (order.SetPriceMaxMovePips > 0 && order.SetPriceMaxMovePips < autoClosePips)
                {
                    autoClosePips = order.SetPriceMaxMovePips;
                }

                if (order.IsBuy == (priceType == PriceType.Limit))
                {
                    return(Price.Add(basePrice, autoClosePips, !instrument.IsNormal));
                }
                else
                {
                    return(Price.Subtract(basePrice, autoClosePips, !instrument.IsNormal));
                }
            }
        }
        private void ValidateTransactionSettings()
        {
            if (_owner.IsExpired)
            {
                var msg = string.Format("TranId {0} is canceled for instrument is not acceptable, ProcessBaseTime={1}", _owner.Id, Market.MarketManager.Now);
                Logger.Warn(msg);
                throw new TransactionServerException(TransactionError.TimingIsNotAcceptable);
            }

            var account = _owner.Owner;

            if (!account.Setting.IsTrading(Market.MarketManager.Now))
            {
                var msg = string.Format("TranId {0} is canceled for account is not trading, ProcessBaseTime={1}", _owner.Id, Market.MarketManager.Now);
                throw new TransactionServerException(TransactionError.AccountIsNotTrading);
            }

            TradeDay tradeDay = Settings.SettingManager.Default.Setting.GetTradeDay();

            if (_owner.OrderType != OrderType.Risk || _owner.ExecuteTime > tradeDay.BeginTime)
            {
                if (!_owner.TradePolicyDetail.IsTradeActive)
                {
                    throw new TransactionServerException(TransactionError.TradePolicyIsNotActive);
                }

                if (this.ShouldVerifyInstrumentCanPlaceAndTrade())
                {
                    this.VerifyInstrumentCanPlaceAndTrade();
                }

                if (account.Setting.Type == AccountType.Agent)
                {
                    if (account.HasUnassignedOvernightOrders)
                    {
                        throw new TransactionServerException(TransactionError.HasUnassignedOvernightOrders);
                    }
                }
                else
                {
                    if (account.IsLocked)
                    {
                        throw new TransactionServerException(TransactionError.IsLockedByAgent);
                    }
                }
            }
        }
 public SettingsProvider(string xmlNodeStr)
 {
     this._instruments = new List<Instrument>();
     XDocument doc = XDocument.Parse(xmlNodeStr);
     foreach (XElement element in doc.Element("InitDataForChart").Element("Instruments").Elements())
     {
         Instrument instrument = new Instrument(Guid.Parse(element.Attribute("Id").Value), element.Attribute("Description").Value, short.Parse(element.Attribute("Decimals").Value), bool.Parse(element.Attribute("HasVolume").Value));
         this._instruments.Add(instrument);
     }
     this._EnableTrendSheetChart = bool.Parse(doc.Element("InitDataForChart").Attribute("EnableTrendSheetChart").Value);
     this._HighBid = bool.Parse(doc.Element("InitDataForChart").Attribute("HighBid").Value);
     this._LowBid = bool.Parse(doc.Element("InitDataForChart").Attribute("LowBid").Value);
     this._Language = doc.Element("InitDataForChart").Attribute("Language").Value;
     DateTime currentDay = DateTime.Parse(doc.Element("InitDataForChart").Attribute("CurrentDay").Value);
     DateTime BeginDay = DateTime.Parse(doc.Element("InitDataForChart").Attribute("BeginTime").Value);
     DateTime EndDay = DateTime.Parse(doc.Element("InitDataForChart").Attribute("EndTime").Value);
     this._TradeDay = new TradeDay(currentDay, BeginDay, EndDay);
     this._UserSettingsDirectory = System.IO.Path.Combine("Users",doc.Element("InitDataForChart").Attribute("UserCode").Value);
     this._Language = doc.Element("InitDataForChart").Attribute("Language").Value;
 }
Beispiel #8
0
 private string GenerateOrderCodePrefix(string orgCode, TradeDay tradeDay)
 {
     return(orgCode + tradeDay.Day.ToString("yyyyMMdd"));
 }
Beispiel #9
0
 private string GenerateTranCodePrefix(string orgCode, TradeDay tradeDay, string orderTypeCode)
 {
     return(orgCode + tradeDay.Day.ToString("yyMMdd") + orderTypeCode);
 }
Beispiel #10
0
        private decimal CalculateFloatingMargin()
        {
            decimal result = 0m;

            foreach (var eachOrder in Orders)
            {
                if (!this.ShouldCalculateFloating(eachOrder))
                {
                    continue;
                }
                var     orderDayHistory  = ResetManager.Default.GetOrderDayHistory(eachOrder.Id, TradeDay.AddDays(-1));
                decimal storagePerLot    = orderDayHistory == null ? 0m : orderDayHistory.StoragePerLot;
                decimal interestPerLot   = orderDayHistory == null ? 0m : orderDayHistory.InterestPerLot;
                decimal storageFloating  = FloatingCalculator.CalculateOrderFloatRpt(storagePerLot, eachOrder.LotBalance, CurrencyRate.RateIn, CurrencyRate.RateOut, this.AccountCurrencyDecimals);
                decimal interestFloating = FloatingCalculator.CalculateOrderFloatRpt(interestPerLot, eachOrder.LotBalance, CurrencyRate.RateIn, CurrencyRate.RateOut, this.AccountCurrencyDecimals);
                Price   livePrice        = this.GetLivePrice(eachOrder.IsBuy);
                decimal tradeFloating    = FloatingCalculator.CalculateOrderFloatTrade(eachOrder.LotBalance, eachOrder.Owner.ContractSize(this.TradeDay), eachOrder.IsBuy, (int)Instrument.TradePLFormula, eachOrder.ExecutePrice, livePrice, CurrencyRate.RateIn, CurrencyRate.RateOut, this.AccountCurrencyDecimals);
                result += storageFloating + interestFloating + tradeFloating;
            }
            return(result);
        }
Beispiel #11
0
 public TradeDayReset(TradeDay tradeDay)
 {
     this.TradeDay = tradeDay;
 }