Drawing lines from robots and indicators

Spotware's avatar

Spotware since: 23 Sep 2013;

  29 Sep 2014, 12:24
Drawing lines from robots and indicators

We recommend you to post a job in Jobs section or contact one of cAlgo consultants.

TRADERS FIRSTâ„¢ Vote for your favorite features: https://ctrader.com/forum/suggestions

Abhi since: 31 Aug 2014;

  01 Oct 2014, 12:13
RE:

Spotware said:

We recommend you to post a job in Jobs section or contact one of cAlgo consultants.

no worries, I guess that was a very polite hint. :-)

never mind, I sorted it out.

bachapk's avatar

bachapk since: 20 Mar 2014;

  25 Dec 2014, 03:17
RE:

Hi

Can we get index-10th bar value of Upper line in this example? If yes then how? As I didn't see any example of how to retrieve value from a line specially trend line.

Looking forward

Regards

Bacha

cAlgo_Development said:

Indicator drawing trend-lines automatically using specified period (minimum distance between high and low extremums):

Source code:

using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true)]
    public class TrendLines : Indicator
    {
    	[Parameter(DefaultValue = 30, MinValue = 1)]
        public int Period { get; set; }	
    
        protected override void Initialize()
        {
            RedrawLines();
        }
    
        public override void Calculate(int index)
        {
            if (IsRealTime)
                RedrawLines();
        }
        
        private void RedrawLines()
        {
            int count = MarketSeries.Close.Count;
        
            int maxIndex1 = FindNextLocalExtremum(MarketSeries.High, count - 1, true);
            int maxIndex2 = FindNextLocalExtremum(MarketSeries.High, maxIndex1 - Period, true);
        	
            int minIndex1 = FindNextLocalExtremum(MarketSeries.Low, count - 1, false);
            int minIndex2 = FindNextLocalExtremum(MarketSeries.Low, minIndex1 - Period, false);
            
            int startIndex = Math.Min(maxIndex2, minIndex2) - 100;
            int endIndex = count + 100;
            
            DrawTrendLine("high", startIndex, endIndex, maxIndex1, MarketSeries.High[maxIndex1], 
                maxIndex2, MarketSeries.High[maxIndex2]);

            DrawTrendLine("low", startIndex, endIndex, minIndex1, MarketSeries.Low[minIndex1], 
                minIndex2, MarketSeries.Low[minIndex2]);
        }
        
        private void DrawTrendLine(string lineName, int startIndex, 
            int endIndex, int index1, double value1, int index2, double value2)
        {
            double gradient = (value2 - value1) / (index2 - index1);
            
            double startValue = value1 + (startIndex - index1) * gradient;
            double endValue = value1 + (endIndex - index1) * gradient;
            
            ChartObjects.DrawLine(lineName, startIndex, startValue, endIndex, endValue, Colors.Gray);
            ChartObjects.DrawLine(lineName+"_red", index1, value1, index2, value2, Colors.Red);
        }
        
        private int FindNextLocalExtremum(DataSeries series, int maxIndex, bool findMax)
        {
            for (int index = maxIndex; index >= 0; index --)
            {
                if (IsLocalExtremum(series, index, findMax))
                {
                    return index;
                }
            }
            return 0;
        }
        
        private bool IsLocalExtremum(DataSeries series, int index, bool findMax)
        {	
        	int end = Math.Min(index + Period, series.Count - 1);
        	int start = Math.Max(index - Period, 0);
        	
        	double value = series[index];
        
        	for (int i = start; i < end; i++)
        	{
        		if (findMax && value < series[i])
        			return false;
        			
    			if (!findMax && value > series[i])
        			return false;
        	}
        	return true;
        }
    }
}
http://sites.google.com/site/youralgo/

Anka Software since: 06 Nov 2014;

  28 Dec 2014, 16:28
RE: RE:

Can you provide examples / links of Robots using DrawLine?

Regards

Vivek

Spotware's avatar

Spotware since: 23 Sep 2013;

  31 Dec 2014, 11:20

Dear Traders,

Currently it is not possible to access trend lines or other chart objects from cAlgo API. We plan to include such functionality in the future.

TRADERS FIRSTâ„¢ Vote for your favorite features: https://ctrader.com/forum/suggestions

crank since: 11 Nov 2014;

  04 Jan 2015, 15:38
RE:

How would I display just the numerical value of the linear regression slope of these two trendlines on the chart? I don't need to graph anything, just the numerical values of the two slopes in the top left corner of the existing price chart.

cAlgo_Development said:

Indicator drawing trend-lines automatically using specified period (minimum distance between high and low extremums):

Source code:

using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true)]
    public class TrendLines : Indicator
    {
    	[Parameter(DefaultValue = 30, MinValue = 1)]
        public int Period { get; set; }	
    
        protected override void Initialize()
        {
            RedrawLines();
        }
    
        public override void Calculate(int index)
        {
            if (IsRealTime)
                RedrawLines();
        }
        
        private void RedrawLines()
        {
            int count = MarketSeries.Close.Count;
        
            int maxIndex1 = FindNextLocalExtremum(MarketSeries.High, count - 1, true);
            int maxIndex2 = FindNextLocalExtremum(MarketSeries.High, maxIndex1 - Period, true);
        	
            int minIndex1 = FindNextLocalExtremum(MarketSeries.Low, count - 1, false);
            int minIndex2 = FindNextLocalExtremum(MarketSeries.Low, minIndex1 - Period, false);
            
            int startIndex = Math.Min(maxIndex2, minIndex2) - 100;
            int endIndex = count + 100;
            
            DrawTrendLine("high", startIndex, endIndex, maxIndex1, MarketSeries.High[maxIndex1], 
                maxIndex2, MarketSeries.High[maxIndex2]);

            DrawTrendLine("low", startIndex, endIndex, minIndex1, MarketSeries.Low[minIndex1], 
                minIndex2, MarketSeries.Low[minIndex2]);
        }
        
        private void DrawTrendLine(string lineName, int startIndex, 
            int endIndex, int index1, double value1, int index2, double value2)
        {
            double gradient = (value2 - value1) / (index2 - index1);
            
            double startValue = value1 + (startIndex - index1) * gradient;
            double endValue = value1 + (endIndex - index1) * gradient;
            
            ChartObjects.DrawLine(lineName, startIndex, startValue, endIndex, endValue, Colors.Gray);
            ChartObjects.DrawLine(lineName+"_red", index1, value1, index2, value2, Colors.Red);
        }
        
        private int FindNextLocalExtremum(DataSeries series, int maxIndex, bool findMax)
        {
            for (int index = maxIndex; index >= 0; index --)
            {
                if (IsLocalExtremum(series, index, findMax))
                {
                    return index;
                }
            }
            return 0;
        }
        
        private bool IsLocalExtremum(DataSeries series, int index, bool findMax)
        {	
        	int end = Math.Min(index + Period, series.Count - 1);
        	int start = Math.Max(index - Period, 0);
        	
        	double value = series[index];
        
        	for (int i = start; i < end; i++)
        	{
        		if (findMax && value < series[i])
        			return false;
        			
    			if (!findMax && value > series[i])
        			return false;
        	}
        	return true;
        }
    }
}

hiba7rain since: 20 Jul 2014;

  08 Feb 2015, 08:14
RE:

Thanks for any new updates but i just have a question, is the chart objects on both indicators and robots will have a readable value?

for example as trader if i draw a line that i see as a resistance or support line or a trend line it would be help full for me if i am able to use it to send me email notifications or even to trade the line (object) once the price hit the line, currently yes it is possible for placing the orders on same horizontal lines that we may draw but it is not possible for trend lines that we draw on charts hope you consider such function and make it available even without the need for us to create indicators or robots to use the new functions

thanks

cAlgo_Development said:

Three new methods will be introduced in ChartObjects object next release:

1. Generic line:

ChartObjects.DrawLine(objectName, date1, y1, date2, y2, color, [optional] thickness, [optional] style))
ChartObjects.DrawLine(objectName, index1, y1, index2, y2, color, [optional] thickness, [optional] style)

2. Horizontal line:

ChartObjects.DrawHorizontalLine(objectName, y, color, [optional] thickness, [optional] style)

3. Vertical line:

ChartObjects.DrawVerticalLine(objectName, date, color, [optional] thickness, [optional] style)
ChartObjects.DrawVerticalLine(objectName, index, color, [optional] thickness, [optional] style)


The logic of adding, updating and removing chart objects is the same as we already have in DrawText  method: objectName identifiers unique created object, if you draw second object with the same name, old one is removed (updated). So if you want to have e.g. several lines at the same time you have to use different object names.


All methods are available in both robots and indicators.


Here we will post several examples of usage.

Rod_Sands since: 18 Oct 2014;

  08 Feb 2015, 08:51

Is there (planned) a method DrawRectangle, DrawTriangle  etc?

Rod_Sands since: 18 Oct 2014;

  08 Feb 2015, 09:16
RE: RE:

Seems that Objects are still not accessible  

"....Currently it is not possible to access trend lines or other chart objects from cAlgo API. We plan to include such functionality in the future"

cAlgo is a lame duck without this functionality

hiba7rain said:

Thanks for any new updates but i just have a question, is the chart objects on both indicators and robots will have a readable value?

for example as trader if i draw a line that i see as a resistance or support line or a trend line it would be help full for me if i am able to use it to send me email notifications or even to trade the line (object) once the price hit the line, currently yes it is possible for placing the orders on same horizontal lines that we may draw but it is not possible for trend lines that we draw on charts hope you consider such function and make it available even without the need for us to create indicators or robots to use the new functions

thanks

cAlgo_Development said:

Three new methods will be introduced in ChartObjects object next release:

1. Generic line:

ChartObjects.DrawLine(objectName, date1, y1, date2, y2, color, [optional] thickness, [optional] style))
ChartObjects.DrawLine(objectName, index1, y1, index2, y2, color, [optional] thickness, [optional] style)

2. Horizontal line:

ChartObjects.DrawHorizontalLine(objectName, y, color, [optional] thickness, [optional] style)

3. Vertical line:

ChartObjects.DrawVerticalLine(objectName, date, color, [optional] thickness, [optional] style)
ChartObjects.DrawVerticalLine(objectName, index, color, [optional] thickness, [optional] style)


The logic of adding, updating and removing chart objects is the same as we already have in DrawText  method: objectName identifiers unique created object, if you draw second object with the same name, old one is removed (updated). So if you want to have e.g. several lines at the same time you have to use different object names.


All methods are available in both robots and indicators.


Here we will post several examples of usage.

magforex since: 06 Oct 2015;

  30 Oct 2015, 15:54
RE:

cAlgo_Fanatic said:

Daily High and Low Indicator. Displays horizontal lines that correspond to the daily high and low.

Source Code:

using System;
using cAlgo.API;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true)]
    public class DailyHighLow : Indicator
    {
        public override void Calculate(int index)
        {
            DateTime today = MarketSeries.OpenTime[index].Date;
            DateTime tomorrow = today.AddDays(1);

            double high = MarketSeries.High.LastValue;
            double low = MarketSeries.Low.LastValue;

            for (int i = MarketSeries.Close.Count - 1; i > 0; i--)
            {
                if (MarketSeries.OpenTime[i].Date < today)
                    break;

                high = Math.Max(high, MarketSeries.High[i]);
                low = Math.Min(low, MarketSeries.Low[i]);
            }

            ChartObjects.DrawLine("high " + today, today, high, tomorrow, high, Colors.Pink);
            ChartObjects.DrawLine("low " + today, today, low, tomorrow, low, Colors.Pink);
        }
    }
}


 

Hi Spotware, how do I control if I only want the current day or maybe only 2 days high and low only? Can you give me a hint? thanks!