/// <summary> /// CONVERT STRING TO OPEN TRADE , USING DATA FROM ORDER ADMIN /// </summary> /// <param name="subParameter"></param> private Business.OpenTrade ConvertStringToOpenTrade(string[] subParameter) { Business.OpenTrade newOpenTrade = new OpenTrade(); double closePrice = 0; DateTime closeTime; DateTime timeExp; int ID = 0; int investorID = 0; double OpenPrice = 0; DateTime openTime; double size = 0; double stopLoss = 0; //Symbol double takeProfit = 0; int TypeID = 0; double profit = 0; double swap = 0; double commission = 0; double margin = 0; double taxes = 0; double.TryParse(subParameter[0], out closePrice); DateTime.TryParse(subParameter[1], out closeTime); DateTime.TryParse(subParameter[2], out timeExp); int.TryParse(subParameter[3], out ID); int.TryParse(subParameter[4], out investorID); double.TryParse(subParameter[5], out OpenPrice); DateTime.TryParse(subParameter[6], out openTime); double.TryParse(subParameter[7], out size); double.TryParse(subParameter[8], out stopLoss); //Symbol double.TryParse(subParameter[10], out takeProfit); int.TryParse(subParameter[11], out TypeID); double.TryParse(subParameter[12], out profit); double.TryParse(subParameter[13], out swap); double.TryParse(subParameter[14], out commission); double.TryParse(subParameter[15], out margin); double.TryParse(subParameter[16], out taxes); newOpenTrade = TradingServer.Facade.FacadeFillInstanceOpenTrade(investorID, subParameter[9], TypeID); newOpenTrade.ClosePrice = closePrice; newOpenTrade.CloseTime = closeTime; newOpenTrade.Commission = commission; newOpenTrade.ExpTime = timeExp; newOpenTrade.ID = ID; newOpenTrade.Margin = margin; newOpenTrade.OpenPrice = OpenPrice; newOpenTrade.OpenTime = openTime; newOpenTrade.Profit = profit; newOpenTrade.Size = size; newOpenTrade.StopLoss = stopLoss; newOpenTrade.Swap = swap; newOpenTrade.TakeProfit = takeProfit; newOpenTrade.Taxes = taxes; newOpenTrade.SpreaDifferenceInOpenTrade = Model.CommandFramework.CommandFrameworkInstance.GetSpreadDifference(newOpenTrade.Symbol.SecurityID, newOpenTrade.Investor.InvestorGroupInstance.InvestorGroupID); newOpenTrade.CalculatorMarginCommand(newOpenTrade); newOpenTrade.Profit = newOpenTrade.Symbol.ConvertCurrencyToUSD(newOpenTrade.Symbol.Currency, newOpenTrade.Profit, false, newOpenTrade.SpreaDifferenceInOpenTrade, newOpenTrade.Symbol.Digit); return newOpenTrade; }
/// <summary> /// /// </summary> /// <param name="Command"></param> public void AddCommand(OpenTrade Command) { string comment = string.Empty; string mode = string.Empty; string content = string.Empty; #region Set Property IsBuy And CommandType Send To Client bool IsBuy = false; string CommandType = "SellFuture"; switch (Command.Type.ID) { case 11: IsBuy = true; CommandType = "BuyFuture"; break; case 12: IsBuy = false; CommandType = "SellFuture"; break; case 17: IsBuy = true; CommandType = "BuyStopFutureCommand"; break; case 18: IsBuy = false; CommandType = "SellStopFutureCommand"; break; case 19: IsBuy = true; CommandType = "BuyLimitFutureCommand"; break; case 20: IsBuy = false; CommandType = "SellLimitFutureCommand"; break; } #endregion int Result = -1; Command.IsClose = false; if (!Command.IsReOpen) Command.OpenTime = DateTime.Now; Command.CloseTime = Command.OpenTime; Command.Taxes = 0; if (string.IsNullOrEmpty(Command.Comment)) Command.Comment = "[future command]"; #region Find Price Close Of Symbol switch (Command.Type.ID) { case 11: Command.ClosePrice = Command.Symbol.TickValue.Bid; comment = "[future buy]"; mode = "buy future"; break; case 12: Command.ClosePrice = Command.Symbol.TickValue.Ask; comment = "[future sell]"; mode = "sell future"; break; case 17: //BUY STOP FUTURE COMMAND Command.ClosePrice = Command.Symbol.TickValue.Bid; comment = "[buy stop future command]"; mode = "buy stop future"; break; case 18: //SELL STOP FUTURE COMMAND Command.ClosePrice = Command.Symbol.TickValue.Ask; comment = "[sell stop future command]"; mode = "sell stop future"; break; case 19: //BUY LIMIT FUTURE COMMAND Command.ClosePrice = Command.Symbol.TickValue.Bid; comment = "[buy limit future command]"; mode = "buy limit future"; break; case 20: //SELL LIMIT FUTURE COMMAND Command.ClosePrice = Command.Symbol.TickValue.Ask; comment = "[sell limit future command]"; mode = "sell limit future"; break; } #endregion #region INSERT SYSTEM LOG EVENT MAKE COMMAND string size = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Size.ToString(), 2); string openPrice = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.OpenPrice.ToString(), Command.Symbol.Digit); string takeProfit = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.TakeProfit.ToString(), Command.Symbol.Digit); string stopLoss = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.StopLoss.ToString(), Command.Symbol.Digit); string bid = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Symbol.TickValue.Bid.ToString(), Command.Symbol.Digit); string ask = TradingServer.Model.TradingCalculate.Instance.BuildStringWithDigit(Command.Symbol.TickValue.Ask.ToString(), Command.Symbol.Digit); #endregion #region CHECK ISCLOSE ONLY OF FUTURE MARKET AREA if (Command.Symbol.isCloseOnlyFuture) { #region INSERT SYSTEM LOG content = "'" + Command.Investor.Code + "': " + mode + " " + size + " " + Command.Symbol.Name + " at " + openPrice + " sl: " + stopLoss + " tp: " + takeProfit + " (" + bid + "/" + ask + ") unsuccessful [symbol close only]"; comment = "[symbol close only]"; TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, Command.Investor.IpAddress, Command.Investor.Code); #endregion #region CHECK SYMBOL CLOSE ONLY if (Command.IsServer) { string Message = "AddCommandByManager$False,AFC00001," + 0 + "," + Command.Investor.InvestorID + "," + Command.Symbol.Name + "," + Command.Size + "," + IsBuy + "," + Command.OpenTime + "," + Command.OpenPrice + "," + Command.StopLoss + "," + Command.TakeProfit + "," + Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," + Command.Profit + "," + "Comment," + Command.ID + "," + CommandType + "," + 1 + "," + Command.ExpTime + "," + Command.ClientCode + "," + 0 + "," + Command.IsHedged + "," + Command.Type.ID + "," + Command.Margin + ",Open"; if (Command.Investor.ClientCommandQueue == null) Command.Investor.ClientCommandQueue = new List<string>(); Command.Investor.ClientCommandQueue.Add(Message); return; } else { string Message = "AddCommand$False,AFC00001," + 0 + "," + Command.Investor.InvestorID + "," + Command.Symbol.Name + "," + Command.Size + "," + IsBuy + "," + Command.OpenTime + "," + Command.OpenPrice + "," + Command.StopLoss + "," + Command.TakeProfit + "," + Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," + Command.Profit + "," + "Comment," + Command.ID + "," + CommandType + "," + 1 + "," + Command.ExpTime + "," + Command.ClientCode + "," + 0 + "," + Command.IsHedged + "," + Command.Type.ID + "," + Command.Margin + ",Open"; if (Command.Investor.ClientCommandQueue == null) Command.Investor.ClientCommandQueue = new List<string>(); Command.Investor.ClientCommandQueue.Add(Message); return; } #endregion } #endregion #region GET TIME EXP AND COMPARE WITH TIME OPEN COMMAND //Set Exp Time if (Command.Symbol.ParameterItems != null) { int countParameter = Command.Symbol.ParameterItems.Count; for (int i = 0; i < countParameter; i++) { if (Command.Symbol.ParameterItems[i].Code == "S045") { if (Command.Symbol.ParameterItems[i].DateValue < DateTime.Now) { #region INSERT SYSTEM LOG IF MAKE FUTURE COMMAND UNCOMPLETE content = "'" + Command.Investor.Code + "': " + mode + " " + size + " " + Command.Symbol.Name + " at " + openPrice + " sl: " + stopLoss + " tp: " + takeProfit + " (" + bid + "/" + ask + ") unsuccessful [symbol exp time]"; comment = "[symbol exp time]"; TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, Command.Investor.IpAddress, Command.Investor.Code); #endregion #region COMPARE TIME EXP WITH TIME CURRENT if (Command.IsServer) { string Message = "AddCommandByManager$False,AFC00002," + 0 + "," + Command.Investor.InvestorID + "," + Command.Symbol.Name + "," + Command.Size + "," + IsBuy + "," + Command.OpenTime + "," + Command.OpenPrice + "," + Command.StopLoss + "," + Command.TakeProfit + "," + Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," + Command.Profit + "," + "Comment," + Command.ID + "," + CommandType + "," + 1 + "," + Command.ExpTime + "," + Command.ClientCode + "," + 0 + "," + Command.IsHedged + "," + Command.Type.ID + "," + Command.Margin + ",Open"; if (Command.Investor.ClientCommandQueue == null) Command.Investor.ClientCommandQueue = new List<string>(); Command.Investor.ClientCommandQueue.Add(Message); return; } else { string Message = "AddCommand$False,AFC00002," + 0 + "," + Command.Investor.InvestorID + "," + Command.Symbol.Name + "," + Command.Size + "," + IsBuy + "," + Command.OpenTime + "," + Command.OpenPrice + "," + Command.StopLoss + "," + Command.TakeProfit + "," + Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," + Command.Profit + "," + "Comment," + Command.ID + "," + CommandType + "," + 1 + "," + Command.ExpTime + "," + Command.ClientCode + "," + 0 + "," + Command.IsHedged + "," + Command.Type.ID + "," + Command.Margin + ",Open"; if (Command.Investor.ClientCommandQueue == null) Command.Investor.ClientCommandQueue = new List<string>(); Command.Investor.ClientCommandQueue.Add(Message); return; } #endregion } else { Command.ExpTime = Command.Symbol.ParameterItems[i].DateValue; } break; } } } #endregion if (Command.Type.ID == 11 || Command.Type.ID == 12) { //Calculator Profit Of Command Command.CalculatorProfitCommand(Command); Command.Profit = Command.Symbol.ConvertCurrencyToUSD(Command.Symbol.Currency, Command.Profit, false, Command.SpreaDifferenceInOpenTrade, Command.Symbol.Digit); #region CALCULATION MARGIN FOR COMMAND //Call Function Calculator Margin Of Command Command.CalculatorMarginCommand(Command); #endregion Command.Commission = Model.CalculationFormular.Instance.CalculationCommission(Command); } string CommandCode = string.Empty; #region REND CLIENT CODE IF CLIENT CODE == EMPTY if (string.IsNullOrEmpty(Command.ClientCode)) { string tempCode = string.Empty; Random ran = new Random(); int tempRan = ran.Next(0000000, 9999999); Command.ClientCode = Command.Investor.InvestorID + "_" + tempRan; bool isOK = false; while (!isOK) { if (Command.Investor.CommandList != null && Command.Investor.CommandList.Count > 0) { int count = Command.Investor.CommandList.Count; for (int i = 0; i < count; i++) { if (Command.Investor.CommandList[i].ClientCode == Command.ClientCode) { Command.ClientCode = Command.Investor.InvestorID + "_" + ran.Next(0000000, 9999999); break; } } isOK = true; } else { isOK = true; } } } #endregion #region Add Command To Database And Build Command Code //Add Command To Database // Result = TradingServer.Facade.FacadeAddNewOpenTrade(Command); Command.ID = Result; //Call Function Update Command Code Of Command CommandCode = TradingServer.Model.TradingCalculate.Instance.BuildCommandCode(Result.ToString()); TradingServer.Facade.FacadeUpdateCommandCode(Result, CommandCode); #endregion if (Result > 0) { double spreaDifferenceInCommand = Model.CommandFramework.CommandFrameworkInstance.GetSpreadDifference(Command.Symbol.SecurityID, Command.Investor.InvestorGroupInstance.InvestorGroupID); #region Build Command And Add Command To CommandList In Symbol And Command List In Investor //Build Two Instance OpenTrade //One Instance For Investor //One Instance For Symbol And MarketArea #region Build Instance OpenTrade For Investor Business.OpenTrade newOpenTradeInvestor = new OpenTrade(); newOpenTradeInvestor.ID = Result; newOpenTradeInvestor.ClientCode = Command.ClientCode; newOpenTradeInvestor.ClosePrice = Command.ClosePrice; newOpenTradeInvestor.CloseTime = Command.CloseTime; newOpenTradeInvestor.CommandCode = CommandCode; newOpenTradeInvestor.Commission = Command.Commission; newOpenTradeInvestor.ExpTime = Command.ExpTime; newOpenTradeInvestor.Investor = Command.Investor; newOpenTradeInvestor.IsClose = Command.IsClose; newOpenTradeInvestor.OpenPrice = Command.OpenPrice; newOpenTradeInvestor.OpenTime = Command.OpenTime; newOpenTradeInvestor.Profit = Command.Profit; newOpenTradeInvestor.Size = Command.Size; newOpenTradeInvestor.StopLoss = Command.StopLoss; newOpenTradeInvestor.Swap = Command.Swap; newOpenTradeInvestor.Symbol = Command.Symbol; newOpenTradeInvestor.TakeProfit = Command.TakeProfit; newOpenTradeInvestor.Type = new TradeType(); newOpenTradeInvestor.Type.ID = Command.Type.ID; newOpenTradeInvestor.Type.Name = Command.Type.Name; newOpenTradeInvestor.Margin = Command.Margin; newOpenTradeInvestor.IGroupSecurity = Command.IGroupSecurity; newOpenTradeInvestor.Commission = Command.Commission; newOpenTradeInvestor.IsHedged = Command.IsHedged; newOpenTradeInvestor.SpreaDifferenceInOpenTrade = spreaDifferenceInCommand; newOpenTradeInvestor.AgentCommission = Command.AgentCommission; #endregion #region Find Investor In Investor List And Add Command To Investor List //Find Investor In Investor List And Add Command To Investor List if (Business.Market.InvestorList != null) { int countInvestor = Business.Market.InvestorList.Count; for (int n = 0; n < countInvestor; n++) { if (Business.Market.InvestorList[n].InvestorID == newOpenTradeInvestor.Investor.InvestorID) { if (Business.Market.InvestorList[n].CommandList != null) { Business.Market.InvestorList[n].CommandList.Add(newOpenTradeInvestor); } else { Business.Market.InvestorList[n].CommandList = new List<OpenTrade>(); Business.Market.InvestorList[n].CommandList.Add(newOpenTradeInvestor); } break; } } } #endregion #region Build Instance Open Trade For Symbol Business.OpenTrade newOpenTradeSymbol = new OpenTrade(); newOpenTradeSymbol.ID = Result; newOpenTradeSymbol.ClientCode = Command.ClientCode; newOpenTradeSymbol.ClosePrice = Command.ClosePrice; newOpenTradeSymbol.CloseTime = Command.CloseTime; newOpenTradeSymbol.CommandCode = CommandCode; newOpenTradeSymbol.Commission = Command.Commission; newOpenTradeSymbol.ExpTime = Command.ExpTime; newOpenTradeSymbol.Investor = Command.Investor; newOpenTradeSymbol.IsClose = false; newOpenTradeSymbol.OpenPrice = Command.OpenPrice; newOpenTradeSymbol.OpenTime = Command.OpenTime; newOpenTradeSymbol.Profit = Command.Profit; newOpenTradeSymbol.Size = Command.Size; newOpenTradeSymbol.StopLoss = Command.StopLoss; newOpenTradeSymbol.Swap = Command.Swap; newOpenTradeSymbol.Symbol = Command.Symbol; newOpenTradeSymbol.TakeProfit = Command.TakeProfit; newOpenTradeSymbol.Type = new TradeType(); newOpenTradeSymbol.Type.ID = Command.Type.ID; newOpenTradeSymbol.Type.Name = Command.Type.Name; newOpenTradeSymbol.Margin = Command.Margin; newOpenTradeSymbol.IGroupSecurity = Command.IGroupSecurity; newOpenTradeSymbol.Commission = Command.Commission; newOpenTradeSymbol.IsHedged = Command.IsHedged; newOpenTradeSymbol.SpreaDifferenceInOpenTrade = spreaDifferenceInCommand; newOpenTradeSymbol.AgentCommission = Command.AgentCommission; #endregion #region Find Symbol In Market And Add Command To Market Area And List Symbol //Find Symbol In Market And Add Command To Market Area And List Symbol if (Business.Market.SymbolList != null) { int countSymbol = Business.Market.SymbolList.Count; for (int i = 0; i < countSymbol; i++) { if (Business.Market.SymbolList[i].SymbolID == newOpenTradeSymbol.Symbol.SymbolID) { if (Business.Market.SymbolList[i].CommandList != null) { Business.Market.SymbolList[i].CommandList.Add(newOpenTradeSymbol); } else { Business.Market.SymbolList[i].CommandList = new List<OpenTrade>(); Business.Market.SymbolList[i].CommandList.Add(newOpenTradeSymbol); } break; } } } #endregion #region Build Instance Open Trade For Command Executor Business.OpenTrade newOpenTradeExe = new OpenTrade(); newOpenTradeExe.ID = Result; newOpenTradeExe.ClientCode = Command.ClientCode; newOpenTradeExe.ClosePrice = Command.ClosePrice; newOpenTradeExe.CloseTime = Command.CloseTime; newOpenTradeExe.CommandCode = CommandCode; newOpenTradeExe.Commission = Command.Commission; newOpenTradeExe.ExpTime = Command.ExpTime; newOpenTradeExe.Investor = Command.Investor; newOpenTradeExe.IsClose = false; newOpenTradeExe.OpenPrice = Command.OpenPrice; newOpenTradeExe.OpenTime = Command.OpenTime; newOpenTradeExe.Profit = Command.Profit; newOpenTradeExe.Size = Command.Size; newOpenTradeExe.StopLoss = Command.StopLoss; newOpenTradeExe.Swap = Command.Swap; newOpenTradeExe.Symbol = Command.Symbol; newOpenTradeExe.TakeProfit = Command.TakeProfit; newOpenTradeExe.Type = new TradeType(); newOpenTradeExe.Type.ID = Command.Type.ID; newOpenTradeExe.Type.Name = Command.Type.Name; newOpenTradeExe.Margin = Command.Margin; newOpenTradeExe.IGroupSecurity = Command.IGroupSecurity; newOpenTradeExe.Commission = Command.Commission; newOpenTradeExe.IsHedged = Command.IsHedged; newOpenTradeExe.SpreaDifferenceInOpenTrade = spreaDifferenceInCommand; newOpenTradeExe.AgentCommission = Command.AgentCommission; #endregion Business.Market.CommandExecutor.Add(newOpenTradeExe); //If Client Add New Command Complete Then Add Message To Client Queue if (Command.Investor.ClientCommandQueue == null) Command.Investor.ClientCommandQueue = new List<string>(); #region Map Command Server To Client if (Command.IsServer) { string Message = "AddCommandByManager$True,Add New Command Complete," + Result + "," + Command.Investor.InvestorID + "," + Command.Symbol.Name + "," + Command.Size + "," + IsBuy + "," + Command.OpenTime + "," + Command.OpenPrice + "," + Command.StopLoss + "," + Command.TakeProfit + "," + Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," + Command.Profit + "," + "Comment," + Command.ID + "," + CommandType + "," + 1 + "," + Command.ExpTime + "," + Command.ClientCode + "," + CommandCode + "," + Command.IsHedged + "," + Command.Type.ID + "," + Command.Margin + ",Open"; if (Command.Investor.ClientCommandQueue == null) Command.Investor.ClientCommandQueue = new List<string>(); Command.Investor.ClientCommandQueue.Add(Message); } else { string Message = "AddCommand$True,Add New Command Complete," + Result + "," + Command.Investor.InvestorID + "," + Command.Symbol.Name + "," + Command.Size + "," + IsBuy + "," + Command.OpenTime + "," + Command.OpenPrice + "," + Command.StopLoss + "," + Command.TakeProfit + "," + Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," + Command.Profit + "," + "Comment," + Command.ID + "," + CommandType + "," + 1 + "," + Command.ExpTime + "," + Command.ClientCode + "," + CommandCode + "," + Command.IsHedged + "," + Command.Type.ID + "," + Command.Margin + ",Open"; if (Command.Investor.ClientCommandQueue == null) Command.Investor.ClientCommandQueue = new List<string>(); Command.Investor.ClientCommandQueue.Add(Message); } #endregion Command.IsHedged = Command.Symbol.IsHedged; //Command.Investor.UpdateCommand(Command); Business.Margin newMargin = new Margin(); newMargin = Command.Symbol.CalculationTotalMargin(Command.Investor.CommandList); Command.Investor.Margin = newMargin.TotalMargin; Command.Investor.FreezeMargin = newMargin.TotalFreezeMargin; //Business.RequestDealer Request = new RequestDealer(); //NOTIFY INVESTOR TO MANAGER TradingServer.Facade.FacadeSendNotifyManagerRequest(3, Command.Investor); //SEND NOTIFY TO MANAGER THEN ADD NEW ACCOUNT TradingServer.Facade.FacadeSendNoticeManagerRequest(1, Command); #endregion #region INSERT SYSTEM LOG IF MAKE FUTURE COMMAND COMPLETE content = "'" + Command.Investor.Code + "': future order #" + CommandCode + " " + mode + " " + size + " " + Command.Symbol.Name + " at " + openPrice + " commission: " + Command.Commission; TradingServer.Facade.FacadeAddNewSystemLog(5, content, comment, Command.Investor.IpAddress, Command.Investor.Code); #endregion } else { #region Return Error Can't Insert Command To Database For Client //Add Result To Client Command Queue Of Investor string Message = "AddCommand$False,Can't Insert Database," + Result + "," + Command.Investor.InvestorID + "," + Command.Symbol.Name + "," + Command.Size + "," + IsBuy + "," + Command.OpenTime + "," + Command.OpenPrice + "," + Command.StopLoss + "," + Command.TakeProfit + "," + Command.ClosePrice + "," + Command.Commission + "," + Command.Swap + "," + Command.Profit + "," + "Comment," + Command.ID + "," + CommandType + "," + 1 + "," + Command.ExpTime + "," + Command.ClientCode + "," + "0000000," + Command.IsHedged + "," + Command.Type.ID + "," + Command.Margin + ",Open"; if (Command.Investor.ClientCommandQueue == null) Command.Investor.ClientCommandQueue = new List<string>(); Command.Investor.ClientCommandQueue.Add(Message); #endregion } }