Working with trading entities (orders, positions and trades)

It is necessary to refer to the TradingInfo property for getting the data about trading entities.

This property includes:

  • Security - the selected instrument
  • Portfolio - the selected portfolio
  • Position - the current position
  • MyTrades - the executed trades
  • Orders - the posted orders

Besides, it is possible to redefine the following methods for receiving updates:

  • void OnNewOrders(IEnumerable orders) - new orders
  • void OnOrderChanged(Order order) - order change
  • void OnNewMyTrades(IEnumerable myTrades) - new trades
  • void OnPortfolioChanged(Portfolio portfolio) - portfolio change
  • void OnPositionChanged(Position position) - position change

Below is an example of the indicator, which displays the portfolio, position, order and trade data. The indicator also adds entries into logs when receiving new orders and new trades when the portfolio or position changes.

public class SampleTradingInfo : Indicator
    {
        public SampleTradingInfo()
        {
            EnableCustomDrawing = true;
            SubscribeToDrawingEvents(DrawingLayouts.Final);
        }

        #region Overrides of BaseIndicator

        protected override void OnCalculate(int bar, decimal value)
        {

        }

        #endregion

        protected override void OnNewOrder(Order orders)
        {
            this.LogWarn(order.ToString());
        }

        protected override void OnOrderChanged(Order order)
        {
            this.LogWarn(order.ToString());
        }

        protected override void OnNewMyTrade(MyTrade myTrade)
        {
            this.LogWarn(myTrade.ToString());
        }

        protected override void OnPortfolioChanged(Portfolio portfolio)
        {
            this.LogWarn(portfolio.ToString());
        }

        protected override void OnPositionChanged(Position position)
        {
            this.LogWarn(position.ToString());
        }

        protected override void OnRender(RenderContext context, DrawingLayouts layout)
        {
            var label = "";

            if (TradingManager.Security != null)
                label += $"Security: {TradingManager.Security}{Environment.NewLine}";

            if (TradingManager.Portfolio != null)
                label += $"Portfolio: {TradingManager.Portfolio}{Environment.NewLine}";

            if (TradingManager.Position != null)
                label += $"Position: {TradingManager.Position}{Environment.NewLine}";

            var orders = TradingManager.Orders.Where(t => t.State == OrderStates.Active);

            if (orders.Any())
            {
                label += $"{Environment.NewLine}---------------------Active orders:------------------------{Environment.NewLine}";

                foreach (var order in orders)
                {
                    label += $"{order}{Environment.NewLine}";
                }
            }

            var myTrades = TradingManager.MyTrades;

            if (myTrades.Any())
            {
                label += $"{Environment.NewLine}---------------------MyTrades:------------------------{Environment.NewLine}";

                foreach (var myTrade in myTrades)
                {
                    label += $"{myTrade}{Environment.NewLine}";
                }
            }

            var font = new RenderFont("Arial", 10);
            var size = context.MeasureString(label, font);

            context.FillRectangle(Color.DarkRed, new Rectangle(25, 25, (int)size.Width + 50, (int)size.Height + 50));
            context.DrawString(label, font, Color.Azure, 50, 50);
        }
    }

Image 13690