/// <summary>
 /// 
 /// </summary>
 /// <param name="leverage"></param>
 /// <param name="hedged"></param>
 /// <param name="lots"></param>
 /// <returns></returns>
 internal double CalculationMarginHedged(double leverage, double hedged, double lots, Symbol symbol)
 {
     string marginCalculation = "";
     double percentage = 0;
     int countConfig = symbol.ParameterItems.Count;
     for (int i = 0; i < countConfig; i++)
     {
         if (symbol.ParameterItems[i].Code == "S032")
         {
             marginCalculation = symbol.ParameterItems[i].StringValue;
         }
         if (symbol.ParameterItems[i].Code == "S031")
         {
             double.TryParse(symbol.ParameterItems[i].NumValue, out percentage);
         }
     }
     if (marginCalculation == "Futures [ lots * initial_margin * percentage / 100 ]")
     {
         return (lots * hedged * percentage / 100);
     }
     else
     {
         return lots * (hedged / leverage);
     }
 }
        /// <summary>
        /// delete symbol 
        /// </summary>
        /// <param name="symbol">symbol name</param>
        /// <returns></returns>
        internal string DeleteSymbol(string symbol)
        {
            if(isSymbolUpdate)
            {
                return "DSyE011";
            }

            #region check and remove symbol from market area
            int countMarketArea = Market.MarketArea.Count;
            for (int i = 0; i < countMarketArea; i++)
            {
                if (Market.MarketArea[i].ListSymbol == null || Market.MarketArea[i].ListSymbol.Count == 0)
                {
                    continue;
                }
                int countSymbol = Market.MarketArea[i].ListSymbol.Count;
                for (int j = 0; j < countSymbol; j++)
                {
                    if (Market.MarketArea[i].ListSymbol[j].Name == symbol)
                    {

                        #region delete activity
                        if (Market.MarketArea[i].ListSymbol[j].CommandList.Count != 0)
                        {
                            return "DSyE001";
                        }
                        else
                        {
                            //call DB delete function
                            //
                            int symbolID=this.GetSymbolIDByName(symbol);
                            bool isValid = false;
                            #region BUILD COMMAND REQUEST AGENT CHECK VALID DELETE SYMBOL
                            //RemoveSymbol$SymbolID
                            if (Business.Market.ListAgentConfig != null)
                            {
                                string strCmd = "RemoveSymbol$" + symbolID;
                                int countAgent = Business.Market.ListAgentConfig.Count;
                                for (int n = 0; n < countAgent; n++)
                                {
                                    if (Business.Market.IsConnectAgent)
                                    {
                                        string resultAgent = Business.Market.ListAgentConfig[n].clientAgent.StringDefaultPort(strCmd, "");
                                        if (!string.IsNullOrEmpty(resultAgent))
                                        {
                                            string[] subResult = resultAgent.Split('$');
                                            if (subResult[0] == "RemoveSymbol")
                                                isValid = bool.Parse(subResult[1]);
                                        }
                                    }
                                }
                            }
                            #endregion

                            if (isValid)
                            {
                                Symbol deleteSymbol = new Symbol();
                                bool result = deleteSymbol.DFDeleteSymbol(symbolID);
                                if (result)
                                {
                                    //delete business
                                    //
                                    this.RemoveSymbolFromMarket(symbolID);
                                    this.RemoveSymbolFromQuoteSymbol(symbol);
                                    Market.MarketArea[i].ListSymbol.RemoveAt(j);
                                    Facade.FacadeSendNoticeManagerChangeSymbol(4, symbolID);
                                    return "DSyE000";
                                }
                                else
                                {
                                    return "DSyE002";
                                }
                            }
                        }
                        #endregion
                        break;
                    }
                }
            }
            #endregion

            return "DSyE003";
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="Tick"></param>
 /// <param name="RefSymbol"></param>
 public void SetTickValueNotify(Tick Tick, Symbol RefSymbol)
 {
     throw new NotImplementedException();
 }
        /// <summary>
        /// GetAllSymbol$symbol{source{digits{description{typeSecurity{tradeMode{margin_currency{instant_max_volume{
        /// gtc_pendings{spread{long_only{stops_level{spread_balance{freeze_level{contract_size{margin_initial{
        /// margin_maintenance{margin_hedged{tick_size{tick_value{margin_mode{profit_mode{margin_hedged_strong{realtime{
        /// starting{expiration{open_hour[open_min[close_hour[close_min]|open_hour[open_min[close_hour[close_min]^
        /// open_hour[open_min[close_hour[close_min]|open_hour[open_min[close_hour[close_min]^
        /// 
        /// </summary>
        /// <param name="value"></param>
        public void ReceiveSymbolNotify(List<string> value)
        {
            if (value != null)
            {
                Business.Market.SymbolList = new List<Symbol>();
                int count = value.Count;
                for (int i = 0; i < count; i++)
                {
                    if (!string.IsNullOrEmpty(value[i]))
                    {
                        string[] subValue = value[i].Split('{');
                        Business.Symbol newSymbol = new Symbol();
                        newSymbol.MarketAreaRef = Business.Market.MarketArea[0];
                        if (Business.Market.SecurityList != null)
                        {
                            int countSecurity = Business.Market.SecurityList.Count;
                            for (int j = 0; j < countSecurity; j++)
                            {
                                if (Business.Market.SecurityList[j].SecurityID == int.Parse(subValue[4]))
                                {
                                    newSymbol.SecurityID = Business.Market.SecurityList[j].SecurityID;
                                    break;
                                }
                            }
                        }

                        newSymbol.Name = subValue[0];
                        newSymbol.IsQuote = true;
                        newSymbol.IsTrade = true;
                        newSymbol.TimeOnHold = 500000;
                        //source
                        newSymbol.Digit = int.Parse(subValue[2]);
                        //description
                        //type
                        //execution_mode
                        EnumMT4.Execution enuExecutionTrade = (EnumMT4.Execution)(Enum.Parse(typeof(EnumMT4.Execution), subValue[5]));
                        newSymbol.ExecutionTrade = enuExecutionTrade;

                        newSymbol.Currency = subValue[6];

                        EnumMT4.Trade enuTrade = (EnumMT4.Trade)(Enum.Parse(typeof(EnumMT4.Trade), subValue[7]));
                        switch (enuTrade)
                        {
                            case EnumMT4.Trade.CLOSE_ONLY:
                                newSymbol.Trade = "Close Only";
                                break;

                            case EnumMT4.Trade.FULL_ACCESS:
                                newSymbol.Trade = "Full Access";
                                break;

                            case EnumMT4.Trade.NO:
                                newSymbol.Trade = "No";
                                break;
                        }

                        //newSymbol.Trade = subValue[7];
                        //margin_currency
                        //instant_max_volume
                        //gtc_pendings
                        newSymbol.SpreadByDefault = double.Parse(subValue[11]);
                        int _isLongOnly = int.Parse(subValue[12]);
                        if (_isLongOnly == 1)
                            newSymbol.LongOnly = true;
                        else
                            newSymbol.LongOnly = false;

                        newSymbol.StopLevel = int.Parse(subValue[13]);
                        newSymbol.SpreadBalace = double.Parse(subValue[14]) - 1;
                        newSymbol.FreezeLevel = int.Parse(subValue[15]);
                        newSymbol.ContractSize = double.Parse(subValue[16]);
                        newSymbol.InitialMargin = double.Parse(subValue[17]);
                        //margin_maintenance
                        newSymbol.MarginHedged = double.Parse(subValue[19]);
                        newSymbol.TickSize = double.Parse(subValue[20]);
                        newSymbol.TickPrice = double.Parse(subValue[21]);
                        //tick_value
                        //margin_mode

                        EnumMT4.ProfitCal enuProfitCal = (EnumMT4.ProfitCal)(Enum.Parse(typeof(EnumMT4.ProfitCal), subValue[23]));
                        switch (enuProfitCal)
                        {
                            case EnumMT4.ProfitCal.CFD:
                                newSymbol.ProfitCalculation = "CFD [ (close_price - open_price) * contract_size * lots ]";
                                break;

                            case EnumMT4.ProfitCal.FOREX:
                                newSymbol.ProfitCalculation = "Forex [ (close_price - open_price) * contract_size * lots ]";
                                break;

                            case EnumMT4.ProfitCal.FUTURES:
                                newSymbol.ProfitCalculation = "Futures [ (close_price - open_price) * tick_price / tick_size * lots ]";
                                break;
                        }
                        //margin_hedged_strong
                        //realtime
                        //starting
                        //expiration

                        newSymbol.TickValue = new Tick();
                        newSymbol.TickValue.Bid = 0;
                        newSymbol.TickValue.Ask = 0;
                        newSymbol.TickValue.SymbolName = newSymbol.Name;
                        newSymbol.TickValue.Status = "down";

                        newSymbol.ParameterItems = new List<ParameterItem>();

                        Business.ParameterItem newParameterItem = new ParameterItem();
                        newParameterItem.Code = "S004";
                        newParameterItem.BoolValue = -1;
                        newParameterItem.DateValue = DateTime.Parse("1753-01-01 00:00:00.000");
                        newParameterItem.Name = "Description";
                        newParameterItem.NumValue = "-1";
                        newParameterItem.SecondParameterID = i;
                        newParameterItem.StringValue = subValue[3];
                        newSymbol.ParameterItems.Add(newParameterItem);

                        newParameterItem = new ParameterItem();
                        newParameterItem.Code = "S006";
                        newParameterItem.BoolValue = -1;
                        newParameterItem.DateValue = DateTime.Parse("1753-01-01 00:00:00.000");
                        newParameterItem.Name = "Trade Execution";
                        newParameterItem.NumValue = subValue[5];
                        newParameterItem.SecondParameterID = i;
                        newParameterItem.StringValue = "NaN";
                        newSymbol.ParameterItems.Add(newParameterItem);

                        newParameterItem = new ParameterItem();
                        newParameterItem.Code = "S048";
                        newParameterItem.BoolValue = -1;
                        newParameterItem.DateValue = DateTime.Parse("1753-01-01 00:00:00.000");
                        newParameterItem.Name = "Start Digit";
                        newParameterItem.NumValue = "0";
                        newParameterItem.SecondParameterID = i;
                        newParameterItem.StringValue = "NaN";
                        newSymbol.ParameterItems.Add(newParameterItem);

                        newParameterItem = new ParameterItem();
                        newParameterItem.Code = "S049";
                        newParameterItem.BoolValue = -1;
                        newParameterItem.DateValue = DateTime.Parse("1753-01-01 00:00:00.000");
                        newParameterItem.Name = "End Digit";
                        newParameterItem.NumValue = "0";
                        newParameterItem.SecondParameterID = i;
                        newParameterItem.StringValue = "NaN";
                        newSymbol.ParameterItems.Add(newParameterItem);

                        newParameterItem = new ParameterItem();
                        newParameterItem.Code = "S054";
                        newParameterItem.BoolValue = -1;
                        newParameterItem.DateValue = DateTime.Parse("1753-01-01 00:00:00.000");
                        newParameterItem.Name = "Description";
                        newParameterItem.NumValue = "500000";
                        newParameterItem.SecondParameterID = i;
                        newParameterItem.StringValue = "NaN";
                        newSymbol.ParameterItems.Add(newParameterItem);

                        Business.Market.SymbolList.Add(newSymbol);

                        if (Business.Market.Symbols.ContainsKey(newSymbol.Name))
                            Business.Market.Symbols[newSymbol.Name] = newSymbol;
                        else
                            Business.Market.Symbols.Add(newSymbol.Name, newSymbol);

                        #region SESSION
                        string strSession = subValue[subValue.Length - 1];
                        string[] subSession = strSession.Split('^');
                        if (subSession.Length > 0)
                        {
                            int countSession = subSession.Length;
                            for (int j = 0; j < countSession; j++)
                            {
                                if (!string.IsNullOrEmpty(subSession[j]))
                                {
                                    string[] subQuoteTrade = subSession[j].Split('|');
                                    if (subQuoteTrade.Length > 0)
                                    {
                                        string[] subQuote = subQuoteTrade[0].Split(']');
                                        if (subQuote.Length > 0)
                                        {
                                            int countSubQuote = subQuote.Length;
                                            for (int n = 0; n < countSubQuote; n++)
                                            {
                                                if (!string.IsNullOrEmpty(subQuote[n]))
                                                {
                                                    string[] subQuotePara = subQuote[n].Split('[');
                                                    //OPEN_HOUR
                                                    //OPEN_MIN
                                                    //CLOSE_HOUR
                                                    //CLOSE_MIN
                                                }
                                            }
                                        }

                                        string[] subTrade = subQuoteTrade[1].Split(']');
                                        if (subTrade.Length > 0)
                                        {
                                            int countSubTrade = subTrade.Length;
                                            for (int n = 0; n < countSubTrade; n++)
                                            {
                                                if (!string.IsNullOrEmpty(subTrade[n]))
                                                {
                                                    string[] subTradePara = subTrade[n].Split('[');
                                                    //OPEN_HOUR
                                                    //OPEN_MIN
                                                    //CLOSE_HOUR
                                                    //CLOSE_MIN
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        #endregion
                    }
                }
            }

            if (Business.Market.IsFirstStart)
            {
                Business.Market.IsProcessNotifyTick = true;
                Business.Market.ThreadProcessNotifyTickMT4 = new System.Threading.Thread(new System.Threading.ThreadStart(ProcessNotifyTickMT4));
                Business.Market.ThreadProcessNotifyTickMT4.Start();

                Business.Market.IsFirstStart = false;
            }

            //this.LoopGetCandlesManagerAPI();

            Business.Market.IsOpen = true;
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="Tick"></param>
 /// <param name="RefSymbol"></param>
 public void SetTickValueNotify(Tick Tick, Symbol RefSymbol)
 {
     return;
 }
Пример #6
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Tick"></param>
        /// <param name="RefSymbol"></param>
        public void SetTickValueNotify(Tick Tick, Symbol RefSymbol)
        {
            NumCheck++;
            if (NumCheck == 100)
                NumCheck = 0;

            #region Process Command List
            //Call Function Calculate Command 
            if (RefSymbol.CommandList != null && RefSymbol.CommandList.Count > 0)
            {
                int count = RefSymbol.CommandList.Count;
                //for (int i = 0; i < RefSymbol.CommandList.Count; i++)
                for (int i = count - 1; i >= 0; i--)
                {
                    if (RefSymbol.CommandList[i].IsClose == true)
                        continue;

                    //if (RefSymbol.CommandList[i].IsProcess)
                    //    continue;

                    #region Switch Condition Type
                    //Set Close Price For Command     
                    switch (RefSymbol.CommandList[i].Type.ID)
                    {
                        case 1: //Spot Buy Command                    
                            RefSymbol.CommandList[i].ClosePrice = Tick.Bid;
                            break;

                        case 2: //Spot Sell Command                     
                            double Ask = 0;
                            Ask = (Symbol.ConvertNumberPip(RefSymbol.CommandList[i].Symbol.Digit, RefSymbol.CommandList[i].SpreaDifferenceInOpenTrade) + Tick.Ask);
                            RefSymbol.CommandList[i].ClosePrice = Ask;
                            break;

                        case 7: //Buy Limit Command
                            double AskBuyLimit = 0;
                            AskBuyLimit = (Symbol.ConvertNumberPip(RefSymbol.CommandList[i].Symbol.Digit, RefSymbol.CommandList[i].SpreaDifferenceInOpenTrade) + Tick.Ask);
                            RefSymbol.CommandList[i].ClosePrice = AskBuyLimit;
                            break;

                        case 8: //Sell Limit Command                            
                            RefSymbol.CommandList[i].ClosePrice = Tick.Bid;
                            break;

                        case 9: //Buy Stop Command
                            double AskBuyStop = 0;
                            AskBuyStop = (Symbol.ConvertNumberPip(RefSymbol.CommandList[i].Symbol.Digit, RefSymbol.CommandList[i].SpreaDifferenceInOpenTrade) + Tick.Ask);
                            RefSymbol.CommandList[i].ClosePrice = AskBuyStop;
                            break;

                        case 10:    //Sell Stop Command                            
                            RefSymbol.CommandList[i].ClosePrice = Tick.Bid;
                            break;
                    }
                    #endregion

                    //Call Function Calculator Command
                    Business.OpenTrade newOpenTrade = new OpenTrade();
                    newOpenTrade = this.CalculateCommand(RefSymbol.CommandList[i]);

                    #region COMMENT CODE BECAUSE PENDING DON'T ACTIVE(15/07/2011)
                    //if (RefSymbol.CommandList[i].Type.ID != newOpenTrade.Type.ID)
                    //{
                    //    if (newOpenTrade.Type != null)
                    //    {
                    //        RefSymbol.CommandList[i].Type = new TradeType();
                    //        RefSymbol.CommandList[i].Type.ID = newOpenTrade.Type.ID;
                    //        RefSymbol.CommandList[i].Type.Name = newOpenTrade.Type.Name;

                    //        //RefSymbol.CommandList[i].Type = newOpenTrade.Type;
                    //    }
                    //}
                    #endregion

                    if (newOpenTrade.IsClose == true)
                        RefSymbol.CommandList[i].IsClose = true;

                    if (RefSymbol.CommandList[i].Investor.CommandList.Count > 0 && RefSymbol.CommandList[i].Investor.CommandList != null)
                    {
                        RefSymbol.CommandList[i].Investor.UpdateCommand(newOpenTrade);
                    }
                }
            } 
            #endregion    

            Facade.FacadeCalculationAlert(Tick, RefSymbol);
        }       
Пример #7
0
        /// <summary>   
        /// 
        /// </summary>
        /// <param name="symbolName"></param>
        /// <returns></returns>
        internal Business.Symbol GetSymbolConfig(string symbolName)
        {
            Business.Symbol result = new Symbol();
            int count = Business.Market.SymbolList.Count;
            for (int i = 0; i < count; i++)
            {
                if (Business.Market.SymbolList[i].Name == symbolName)
                {
                    result = Business.Market.SymbolList[i];

                    break;
                }
            }

            return result;
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="Tick"></param>
 /// <param name="RefSymbol"></param>
 public void SetTickValueNotify(Tick Tick, Symbol RefSymbol)
 {
     TradingServer.Facade.FacadeCalculationAlert(Tick, RefSymbol);
 }
Пример #9
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="Tick"></param>
 /// <param name="RefSymbol"></param>
 internal void CalculationAlert(Tick Tick, Symbol RefSymbol)
 {
     if (RefSymbol.AlertQueue == null)
     {
         RefSymbol.AlertQueue = new List<PriceAlert>();
     }
     for (int j = RefSymbol.AlertQueue.Count - 1; j >= 0; j--)
     {
         string condition = RefSymbol.AlertQueue[j].AlertCondition.ToString();
         double value = RefSymbol.AlertQueue[j].Value;
         int securityID = -1;
         int digit = RefSymbol.Digit;
         bool checkCon = false;
         RefSymbol.AlertQueue[j].TickOnline = Tick;
         switch (condition)
         {
             case "LargerBid":
                 {
                     if (Tick.Bid < value)
                     {
                         RefSymbol.AlertQueue[j].ValueApply = Tick.Bid;
                         checkCon = this.SendAlert(RefSymbol.AlertQueue[j], securityID, digit);
                     }
                     break;
                 }
             case "LargerAsk":
                 {
                     if (Tick.Ask < value)
                     {
                         securityID = RefSymbol.SecurityID;
                         RefSymbol.AlertQueue[j].ValueApply = Tick.Ask;
                         checkCon = this.SendAlert(RefSymbol.AlertQueue[j], securityID, digit);
                     }
                     break;
                 }
             case "LargerHighBid":
                 {
                     if (Tick.HighInDay < value)
                     {
                         RefSymbol.AlertQueue[j].ValueApply = Tick.HighInDay;
                         checkCon = this.SendAlert(RefSymbol.AlertQueue[j], securityID, digit);
                     }
                     break;
                 }
             case "LargerHighAsk":
                 {
                     if (Tick.HighInDay < value)
                     {
                         securityID = RefSymbol.SecurityID;
                         RefSymbol.AlertQueue[j].ValueApply = Tick.HighInDay;
                         checkCon = this.SendAlert(RefSymbol.AlertQueue[j], securityID, digit);
                     }
                     break;
                 }
             case "SmallerBid":
                 {
                     if (Tick.Bid > value)
                     {
                         RefSymbol.AlertQueue[j].ValueApply = Tick.Bid;
                         checkCon = this.SendAlert(RefSymbol.AlertQueue[j], securityID, digit);
                     }
                     break;
                 }
             case "SmallerAsk":
                 {
                     if (Tick.Ask > value)
                     {
                         securityID = RefSymbol.SecurityID;
                         RefSymbol.AlertQueue[j].ValueApply = Tick.Ask;
                         checkCon = this.SendAlert(RefSymbol.AlertQueue[j], securityID, digit);
                     }
                     break;
                 }
             case "SmallerLowBid":
                 {
                     if (Tick.LowInDay > value)
                     {
                         RefSymbol.AlertQueue[j].ValueApply = Tick.LowInDay;
                         checkCon = this.SendAlert(RefSymbol.AlertQueue[j], securityID, digit);
                     }
                     break;
                 }
             case "SmallerLowAsk":
                 {
                     if (Tick.LowInDay > value)
                     {
                         securityID = RefSymbol.SecurityID;
                         RefSymbol.AlertQueue[j].ValueApply = Tick.LowInDay;
                         checkCon = this.SendAlert(RefSymbol.AlertQueue[j], securityID, digit);
                     }
                     break;
                 }
         }
         if (checkCon)
         {
             RefSymbol.AlertQueue.RemoveAt(j);
         }
     }
 }