private static string[] GetFormattedMarketOrder(MarketOrder ord, List<FastColumn> columns)
 {
     if (ord == null)
         return null;
     var result = new string[16];
     var formatString = "f" + (DalSpot.Instance.GetPrecision(ord.Symbol) + 1);
     SetFormattedStrings(ref result, columns, ord.Property(p => p.ID), ord.ID.ToString());
     SetFormattedStrings(ref result, columns, ord.Property(p => p.Volume), ord.Volume.ToStringUniformMoneyFormat());
     SetFormattedStrings(ref result, columns, ord.Property(p => p.Symbol), ord.Symbol);
     SetFormattedStrings(ref result, columns, ord.Property(p => p.Side), ord.Side > 0 ? "BUY" : "SELL");
     SetFormattedStrings(ref result, columns, ord.Property(p => p.PriceEnter), ord.PriceEnter.ToString(formatString));
     SetFormattedStrings(ref result, columns, ord.Property(p => p.TimeEnter), ord.TimeEnter.ToStringUniform());
     SetFormattedStrings(ref result, columns, ord.Property(p => p.ResultDepo),
                         ord.PriceExit.HasValue ? "" : ord.ResultDepo.ToStringUniformMoneyFormat());
     SetFormattedStrings(ref result, columns, ord.Property(p => p.ResultPoints), ord.PriceEnter == 0 ? "" : ord.ResultPoints.ToString("f0"));
     SetFormattedStrings(ref result, columns, ord.Property(p => p.StopLoss),
                         ord.StopLoss.HasValue ? ord.StopLoss.Value.ToString(formatString) : "");
     SetFormattedStrings(ref result, columns, ord.Property(p => p.TakeProfit),
                         ord.TakeProfit.HasValue ? ord.TakeProfit.Value.ToString(formatString) : "");
     SetFormattedStrings(ref result, columns, ord.Property(p => p.Trailing), GetTrailingData(ord));
     SetFormattedStrings(ref result, columns, ord.Property(p => p.Comment), ord.Comment);
     SetFormattedStrings(ref result, columns, ord.Property(p => p.ExpertComment), ord.ExpertComment);
     SetFormattedStrings(ref result, columns, ord.Property(p => p.Magic), ord.Magic.ToString());
     SetFormattedStrings(ref result, columns, ord.Property(p => p.State), EnumFriendlyName<PositionState>.GetString(ord.State));
     return result;
 }
 static AccountHistoryControl()
 {
     var blank = new MarketOrder();
     allColumns = new []
         {
             new FastColumn(blank.Property(p => p.ID), "#") { SortOrder = FastColumnSort.Ascending },
             new FastColumn(blank.Property(p => p.Volume), Localizer.GetString("TitleSum"))
                                 {
                                     formatter = v => ((int)v).ToStringUniformMoneyFormat()
                                 },
             new FastColumn(blank.Property(p => p.Symbol), Localizer.GetString("TitleSymbol")),
             new FastColumn(blank.Property(p => p.Side), Localizer.GetString("TitleType"))
             {
                 // отобразить BUY - SELL либо BalanceChangeType
                 formatter = s =>
                     (int) s == 1 ? "BUY" : (int) s == -1 ? "SELL"
                         : ((int) s / 10) == (int) BalanceChangeType.Deposit ? Localizer.GetString("TitleInvestment")
                         : ((int) s / 10) == (int) BalanceChangeType.Withdrawal ? Localizer.GetString("TitleWithdrawal")
                         : ((int) s / 10) == (int) BalanceChangeType.Profit ? Localizer.GetString("TitleDealProfit")
                         : ((int) s / 10) == (int) BalanceChangeType.Loss ? Localizer.GetString("TitleDealLoss") : "",
                         colorColumnFormatter = delegate(object cellValue, out Color? backColor, out Color? fontColor)
                 {
                     backColor = null;
                     fontColor = (int) cellValue == 1 ? Color.Green
                         : (int) cellValue == -1 ? Color.Red : Color.Black;
                 }
             },
             new FastColumn(blank.Property(p => p.PriceEnter), Localizer.GetString("TitleEnter"))
                                 {
                                     formatter = (s => (float) s > 0 ?
                                         ((float) s).ToString("f4") : "")
                                 },
             new FastColumn(blank.Property(p => p.TimeEnter), Localizer.GetString("TitleTime"))
             {
                 formatter = (s => (DateTime) s == default(DateTime) ? "" : ((DateTime) s).ToStringUniform())
             },
             new FastColumn(blank.Property(p => p.PriceExit), Localizer.GetString("TitleExit")) { FormatString = "f4" },
             new FastColumn(blank.Property(p => p.TimeExit), Localizer.GetString("TitleTime"))
             {
                 FormatString = "dd.MM.yyyy HH:mm:ss"
             },
             new FastColumn(blank.Property(p => p.ResultDepo), Localizer.GetString("TitleResult"))
             {
                 formatter = c => Math.Abs((float) c) < 0.01f ? "" : ((float) c).ToStringUniformMoneyFormat(),
                 colorColumnFormatter = delegate(object cellValue, out Color? backColor, out Color? fontColor)
                 {
                     backColor = null;
                     fontColor = null;
                     if (cellValue == null) return;
                     var resDepo = (float)cellValue;
                     fontColor = resDepo >= 0 ? Color.Green : Color.Red;
                 }
             },
             new FastColumn(blank.Property(p => p.ResultPoints), Localizer.GetString("TitlePLInPoints"))
             {
                 formatter = c => Math.Abs((float) c) < 0.02f ? "" : ((float) c).ToStringUniformMoneyFormat(),
                 colorColumnFormatter = delegate(object cellValue,
                     out Color? backColor, out Color? fontColor)
                 {
                     backColor = null;
                     fontColor = null;
                     if (cellValue == null) return;
                     var resPoints = (float) cellValue;
                     fontColor = resPoints >= 0 ? Color.Green : Color.Red;
                 }
             },
             new FastColumn(blank.Property(p => p.StopLoss), "S/L") { FormatString = "f4" },
             new FastColumn(blank.Property(p => p.TakeProfit), "T/P") { FormatString = "f4" },
             new FastColumn(blank.Property(p => p.Comment), Localizer.GetString("TitleCommentShort")),
             new FastColumn(blank.Property(p => p.ExpertComment), Localizer.GetString("TitleRobotCommentShort")),
             new FastColumn(blank.Property(p=>p.ExitReason), Localizer.GetString("TitleClosing"))
                                 {
                                     formatter = o => EnumFriendlyName<PositionExitReason>.GetString((PositionExitReason) o)
                                 }
         };
 }
        private void SetupGridMarketOrders()
        {
            var blank = new MarketOrder();
            gridOpenPos.MultiSelectEnabled = true;
            gridOpenPos.Columns.Add(new FastColumn(blank.Property(p => p.ID), "#"));
            gridOpenPos.Columns.Add(new FastColumn(blank.Property(p=>p.Volume), Localizer.GetString("TitleSum")));
            gridOpenPos.Columns.Add(new FastColumn(blank.Property(p => p.Symbol), Localizer.GetString("TitleSymbol")));
            gridOpenPos.Columns.Add(new FastColumn(blank.Property(p => p.Side), Localizer.GetString("TitleType")));
            gridOpenPos.Columns.Add(new FastColumn(blank.Property(p => p.PriceEnter), Localizer.GetString("TitleEnter")));
            gridOpenPos.Columns.Add(new FastColumn(blank.Property(p => p.TimeEnter), Localizer.GetString("TitleTime")));
            gridOpenPos.Columns.Add(new FastColumn(blank.Property(p => p.ResultDepo), Localizer.GetString("TitleOpenedPL")));
            gridOpenPos.Columns.Add(new FastColumn(blank.Property(p => p.ResultPoints), Localizer.GetString("TitlePLInPoints")));
            gridOpenPos.Columns.Add(new FastColumn(blank.Property(p => p.StopLoss), "S/L"));
            gridOpenPos.Columns.Add(new FastColumn(blank.Property(p => p.TakeProfit), "T/P"));
            gridOpenPos.Columns.Add(new FastColumn(blank.Property(p => p.Trailing), Localizer.GetString("TitleTrailing")));
            gridOpenPos.Columns.Add(new FastColumn(blank.Property(p => p.Comment), Localizer.GetString("TitleCommentShort")));
            gridOpenPos.Columns.Add(new FastColumn(blank.Property(p => p.ExpertComment), Localizer.GetString("TitleRobotCommentShort"))
                {
                    IsHyperlinkStyleColumn = true,
                    HyperlinkFontActive = fontBold,
                    HyperlinkActiveCursor = Cursors.Hand,
                    rowFormatter = value =>
                        {
                            var order = (MarketOrder) value;
                            if (string.IsNullOrEmpty(order.ExpertComment)) return string.Empty;

                            int signalCat, parentDeal;
                            if (MarketOrder.GetTradeSignalFromDeal(order, out signalCat, out parentDeal))
                                return "сигнал #" + signalCat;
                            return order.ExpertComment;
                        }
                });
            gridOpenPos.Columns.Add(new FastColumn(blank.Property(p => p.Magic), "Magic"));
            gridOpenPos.Columns.Add(new FastColumn(blank.Property(p => p.State), Localizer.GetString("TitleState")));

            gridOpenPos.UserHitCell += GridOpenPosUserHitCell;
            gridOpenPos.ColorAltCellBackground = colorAltCell;
            gridOpenPos.MinimumTableWidth = gridOpenPos.Columns.Count * DefaultColumnWidth;

            gridOpenPos.colorFormatter = delegate(object cellValue, out Color? backColor, out Color? fontColor)
                {
                    backColor = null;
                    fontColor = null;
                    var pos = (MarketOrder) cellValue;
                    if (pos.State != PositionState.Opened)
                    {
                        backColor = colorPositionIsHanged;
                        return;
                    }
                    var quote = QuoteStorage.Instance.ReceiveValue(pos.Symbol);
                    if (pos.StopLoss.HasValue && pos.StopLoss > 0)
                        if ((pos.PriceEnter <= pos.StopLoss.Value && pos.Side > 0) ||
                            (pos.PriceEnter >= pos.StopLoss.Value && pos.Side < 0))
                        {
                            backColor = colorTextPositionInProfit;
                            return;
                        }
                    if (quote == null) return;
                    if ((pos.Side > 0 && pos.PriceEnter < quote.bid) || (pos.Side < 0 && pos.PriceEnter > quote.ask))
                        backColor = colorTextPositionProtected;
                };
            gridOpenPos.rowExtraFormatter = delegate(object obj, List<FastColumn> columns)
                {
                    return GetFormattedMarketOrder(obj as MarketOrder, columns);
                };

            gridOpenPos.ColumnSettingsChanged += OnGridOpenPosColumnSettingsChange;
            // применить настройки из файла настроек
            var sets = GridSettings.EnsureSettings(GridSettings.ListAccountOpenedOrders);
            sets.ApplyToGrid(gridOpenPos);
        }
        /// <summary>
        /// Предустановка столбцов таблицы по сделкам
        /// </summary>
        public static void SetupDealsGrid(FastGrid.FastGrid grid, bool isClosedDealsTable)
        {
            var blankMarketOrder = new MarketOrder();
            grid.Columns.Add(new FastColumn(blankMarketOrder.Property(p => p.ID), "#") {ColumnWidth = 50});
            grid.Columns.Add(new FastColumn(blankMarketOrder.Property(p => p.Symbol), Localizer.GetString("TitleInstrument")) {ColumnWidth = 70});
            grid.Columns.Add(new FastColumn(blankMarketOrder.Property(p => p.Side), Localizer.GetString("TitleType"))
                {
                    ColumnWidth = 60,
                    formatter = s => (int) s < 0 ? "SELL" : "BUY"
                });
            grid.Columns.Add(new FastColumn(blankMarketOrder.Property(p => p.Volume), Localizer.GetString("TitleVolume"))
                {
                    ColumnWidth = 82,
                    formatter = value => ((int) value).ToStringUniformMoneyFormat()
                });
            grid.Columns.Add(new FastColumn(blankMarketOrder.Property(p => p.PriceEnter), Localizer.GetString("TitleEnter"))
                {
                    ColumnWidth = 64,
                    formatter = p => ((float) p).ToStringUniformPriceFormat()
                });
            grid.Columns.Add(new FastColumn(blankMarketOrder.Property(p => p.TimeEnter), Localizer.GetString("TitleEnterTime"))
                {
                    ColumnWidth = 82
                });
            grid.Columns.Add(new FastColumn(blankMarketOrder.Property(p => p.PriceExit), Localizer.GetString("TitleExit"))
                {
                    ColumnWidth = 64,
                    formatter = p => p == null ? "" : ((float) p).ToStringUniformPriceFormat()
                });
            grid.Columns.Add(new FastColumn(blankMarketOrder.Property(p => p.ResultBase), Localizer.GetString("TitleResultInUSDShort"))
                {
                    ColumnWidth = 82,
                    formatter = p => p == null ? "" : ((float) p).ToStringUniformPriceFormat()
                });
            if (isClosedDealsTable)
            {
                grid.Columns.Add(new FastColumn(blankMarketOrder.Property(p => p.TimeExit), Localizer.GetString("TitleExitTime"))
                    {
                        ColumnMinWidth = 160
                    });
                grid.Columns.Add(new FastColumn(blankMarketOrder.Property(p => p.ResultDepo), Localizer.GetString("TitleResult"))
                    {
                        ColumnMinWidth = 65,
                        formatter = value => ((float) value).ToStringUniformMoneyFormat(),
                        colorColumnFormatter = (object value, out Color? color, out Color? fontColor) =>
                            {
                                color = null;
                                fontColor = null;
                                if (value == null) return;
                                var rst = (float) value;
                                fontColor = rst < -0.9f ? Color.Red : rst < 1 ? Color.Black : Color.Blue;
                            }
                    });
            }
            else
            {
                grid.Columns.Add(new FastColumn(blankMarketOrder.Property(p => p.ResultPoints), Localizer.GetString("TitlePoints"))
                    {
                        ColumnMinWidth = 65,
                        formatter = value => ((float) value).ToStringUniformMoneyFormat(true),
                        colorColumnFormatter = (object value, out Color? color, out Color? fontColor) =>
                            {
                                color = null;
                                fontColor = null;
                                if (value == null) return;
                                var rst = (float) value;
                                fontColor = rst < -0.9f ? Color.Red : rst < 1 ? Color.Black : Color.Blue;
                            }
                    });
            }

            grid.FontAnchoredRow = new Font(grid.Font, FontStyle.Bold);
            grid.CheckSize(true);
            grid.CalcSetTableMinWidth();
            grid.ContextMenuRequested += OrdersFastGridContextMenuRequested;
        }