Print Bars

by Bart A in category Other at 02/10/2019
Description

Print OHLC and Tick Volume of selected bars to cBot log. Data can be copied to the clipboard from the log.

Ctrl + Click to print single bar

Ctrl + Mouse Select to print selected bars

Download
41 downloads
How to install
Warning! Executing the following cBot may result in loss of funds. Use it at your own risk.
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: cAlgo
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class PrintBars : Robot
    {
        [Parameter("Fill Color", DefaultValue = "2F68D0F7")]
        public string FillColor { get; set; }

        ChartRectangle DragRectangle;

        protected override void OnStart()
        {
            Chart.MouseDown += Chart_MouseDown;
            Chart.MouseUp += Chart_MouseUp;
            Chart.MouseMove += Chart_MouseMove;
        }

        private void Chart_MouseMove(ChartMouseEventArgs obj)
        {
            UpdateCoordincatesOnChart(obj.TimeValue);

            if (DragRectangle == null)
                return;

            DragRectangle.Time2 = obj.TimeValue;
        }

        private void Chart_MouseDown(ChartMouseEventArgs obj)
        {
            if (obj.CtrlKey == false)
                return;

            Chart.IsScrollingEnabled = false;
            DragRectangle = CreateDragRectangle(obj.TimeValue);
        }



        private void Chart_MouseUp(ChartMouseEventArgs obj)
        {
            Chart.IsScrollingEnabled = true;

            if (DragRectangle != null)
            {
                PrintBarsToLog(DragRectangle.Time1, DragRectangle.Time2);
                Chart.RemoveObject(DragRectangle.Name);
                DragRectangle = null;
            }
        }

        private ChartRectangle CreateDragRectangle(DateTime time)
        {
            var rect = Chart.DrawRectangle("DragRectangle", time, Chart.TopY, time, Chart.BottomY, FillColor);
            rect.IsFilled = true;
            return rect;
        }

        private void PrintBarsToLog(DateTime date1, DateTime date2)
        {
            var firstDate = date1 < date2 ? date1 : date2;
            var lastDate = date1 >= date2 ? date1 : date2;
            var firstBarIndex = MarketSeries.OpenTime.GetIndexByTime(firstDate);
            var lastBarIndex = MarketSeries.OpenTime.GetIndexByTime(lastDate);

            Print(" ");
            for (int i = firstBarIndex; i <= lastBarIndex; i++)
            {
                PrintBar(i);
            }
        }

        private void PrintBar(int barIndex)
        {
            var time = MarketSeries.OpenTime[barIndex].ToString("dd.MM.yy HH:mm:ss.fff");
            var open = PriceToString(MarketSeries.Open[barIndex]);
            var high = PriceToString(MarketSeries.High[barIndex]);
            var low = PriceToString(MarketSeries.Low[barIndex]);
            var close = PriceToString(MarketSeries.Close[barIndex]);
            var volume = MarketSeries.TickVolume[barIndex].ToString();
            var text = string.Format("{0} {1} {2} {3} {4} {5}", time, open, high, low, close, volume);
            Print(text);
        }

        private string PriceToString(double price)
        {
            var format = "F" + Symbol.Digits.ToString();
            return price.ToString(format);
        }

        private void UpdateCoordincatesOnChart(DateTime time)
        {
            var text = string.Format(time.ToString("dd.MM.yy HH:mm:ss.fff"));
            Chart.DrawStaticText("Coordinates", text, VerticalAlignment.Top, HorizontalAlignment.Left, Chart.ColorSettings.ForegroundColor);
        }
    }
}
Comments
0