Trading Order Lines free

by reyx in category Other at 22/03/2020
Description

Add target/stop colored shapes on pending orders similar to Trading View Tool

Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgocTrader
´╗┐using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TradingOrderLines : Indicator
    {
        [Parameter("TP Color", DefaultValue = "8065FE66")]
        public string TPColor { get; set; }

        [Parameter("SL Color", DefaultValue = "80FF999A")]
        public string SLColor { get; set; }

        [Output("Main")]
        public IndicatorDataSeries Result { get; set; }

        protected override void Initialize()
        {
            PendingOrders.Cancelled += OnPendingOrdersCancelled;
            PendingOrders.Created += OnPendingOrdersCreated;
            PendingOrders.Filled += OnPendingOrdersFilled;
            PendingOrders.Modified += OnPendingOrdersModified;

            foreach (var order in PendingOrders)
            {
                DrawShapes(order);
            }
        }

        void OnPendingOrdersCancelled(PendingOrderCancelledEventArgs obj)
        {
            RemoveShapes(obj.PendingOrder);
        }

        void OnPendingOrdersCreated(PendingOrderCreatedEventArgs obj)
        {
            DrawShapes(obj.PendingOrder);
        }

        void OnPendingOrdersFilled(PendingOrderFilledEventArgs obj)
        {
            RemoveShapes(obj.PendingOrder);
        }

        void OnPendingOrdersModified(PendingOrderModifiedEventArgs obj)
        {
            DrawShapes(obj.PendingOrder);
        }

        void DrawShapes(PendingOrder order)
        {
            var start = Bars.ClosePrices.Count - 1;
            var end = start + 20;

            if (order.TakeProfit.HasValue)
            {
                var rect = Chart.DrawRectangle("TP" + order.Id, start, order.TargetPrice, end, order.TakeProfit.Value, Color.FromHex(TPColor));
                rect.IsFilled = true;
            }

            if (order.StopLoss.HasValue)
            {
                var rect = Chart.DrawRectangle("SL" + order.Id, start, order.TargetPrice, end, order.StopLoss.Value, Color.FromHex(SLColor));
                rect.IsFilled = true;
            }
        }

        void RemoveShapes(PendingOrder order)
        {
            Chart.RemoveObject("TP" + order.Id);
            Chart.RemoveObject("SL" + order.Id);
        }

        public override void Calculate(int index)
        {
        }
    }
}
Comments
0