Simple drawing

Questions about MultiCharts .NET and user contributed studies.
Antny
Posts: 13
Joined: 31 Jul 2014
Has thanked: 3 times

Simple drawing

Postby Antny » 15 Sep 2014

I have been trying to sort out the drawing mechanism and so far it has proven to be too foreign to what I am used to. So, I figured I would just drop in here and ask if someone would have the time to help. If so, this is all I need.

A simple indicator that draws a triangle on the chart.
To keep this as simple as possible, use these criteria:
Red outline
Point 1 is the open of the most recent bar.
Point 2 is the open of 20 bars back from the most recent.
Point 3 is the open of 50 bars back from the most recent.
updates at the beginning of a new bar.

I had intended to use the DrawPolygon(Pen, PointF[]) method, but at this point, I will be happy to figure out how to get an indie to render a triangle on a chart by any means whatsoever.

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Simple drawing

Postby JoshM » 15 Sep 2014

Henry (or someone else), can you provide an example for this? I'm asking because my example for Antny, while seemingly theoretically correct as far as I know, does not work correctly on MultiCharts .NET64 Version 8.8 Release (Build 8365).

The manual says here:
Object movement is done by changing its coordinates. For a trendline object, they are Begin and End properties, and for text objects and arrows, it is Location property.
But that does not seem to work:

Image

So oddly enough, half of the points update but the other half does not. And that is while every line uses similar code:

Code: Select all

using System;
using System.Drawing;
using System.Linq;
using PowerLanguage.Function;

namespace PowerLanguage.Indicator
{
[SameAsSymbol(true), RecoverDrawings(false)]
public class Forum_TriangleOnChart : IndicatorObject
{
ITrendLineObject line1to2, line2to3, line3to1;

public Forum_TriangleOnChart(object _ctx) : base(_ctx) { }

protected override void CalcBar()
{
if (Bars.LastBarOnChart)
{
if (Bars.Status == EBarState.Open)
{
DrawTriangle();
}
}
}

private void DrawTriangle()
{
ChartPoint point1 = new ChartPoint(Bars.Time[0], Bars.Open[0]);
ChartPoint point2 = new ChartPoint(Bars.Time[20], Bars.Open[20]);
ChartPoint point3 = new ChartPoint(Bars.Time[50], Bars.Open[50]);

// Line 1
if (line1to2 == null)
line1to2 = DrwTrendLine.Create(point1, point2);
else
{
line1to2.Begin = point1;
line1to2.End = point2;
}

// Line 2
if (line2to3 == null)
line2to3 = DrwTrendLine.Create(point2, point3);
else
{
line2to3.Begin = point2;
line2to3.End = point3;
}

// Line 3
if (line3to1 == null)
line3to1 = DrwTrendLine.Create(point3, point1);
else
{
line3to1.Begin = point3;
line3to1.End = point1;
}

// Set line settings
line1to2.Color = Color.Red;
line2to3.Color = Color.RoyalBlue;
line3to1.Color = Color.ForestGreen;

line1to2.Size = 3;
line2to3.Size = 3;
line3to1.Size = 3;
}
}
}
Attachments
scr.15-09-2014 14.02.44.png
(20.49 KiB) Downloaded 1310 times

Antny
Posts: 13
Joined: 31 Jul 2014
Has thanked: 3 times

Re: Simple drawing

Postby Antny » 15 Sep 2014

Thanks JoshM! I am not terribly concerned about the technical accuracy. I primarily just needed an interface that demonstrated (very simply) how to get an object drawn on a chart. I am used to working in NinjaScript and MQL4 where all the drawing functions are very straightforward. The raw C# interface is still fairly new to me. Thanks again for the example!

Antny
Posts: 13
Joined: 31 Jul 2014
Has thanked: 3 times

Re: Simple drawing

Postby Antny » 15 Sep 2014

JoshM, I see what you are talking about in reference to not all of the points being updated on the trend lines. On line1to2, point1 does not update. On line2to3, point2 does not update. Everything else tracks as it should.

Anybody have any idea why?

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Simple drawing

Postby Henry MultiСharts » 15 Sep 2014

When trendlines are being modified you need to change the End point first, then change the Start point. Please find the fixed code attached.
Attachments
Forum_TriangleOnChart_fix.pln
(1.25 KiB) Downloaded 502 times

Antny
Posts: 13
Joined: 31 Jul 2014
Has thanked: 3 times

Re: Simple drawing

Postby Antny » 15 Sep 2014

I was toying around with the code and discovered if I update the end points of line1to2, and line2to3 first, those two lines now update as expected.

Code: Select all

// Line 1
if (line1to2 == null)
{
line1to2 = DrwTrendLine.Create(point1, point2);
}
else
{
line1to2.End = point2;
line1to2.Begin = point1;
}

// Line 2
if (line2to3 == null)
{
line2to3 = DrwTrendLine.Create(point2, point3);
}
else
{
line2to3.End = point3;
line2to3.Begin = point2;
}

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Simple drawing

Postby JoshM » 15 Sep 2014

When trendlines are being modified you need to change the End point first, then change the Start point. Please find the fixed code attached.
Thanks, with that change it works correctly. Though I do find it slightly discouraging that these things are missing from the manual and chm file; that's not good for my productivity and neither for yours (MC Support in general).

Antny
Posts: 13
Joined: 31 Jul 2014
Has thanked: 3 times

Re: Simple drawing

Postby Antny » 15 Sep 2014

I did a little further investigation and the issue with the trend lines not updating correctly appears to be unique to line1to2 and line2to3. I rearranged the code to where line3to1 updated first, and changed the begin and end for the other two lines back to the original. When I did this, line1to2 and line2to3 stopped updating correctly again. So, the issue appears to be tied directly to these first two lines for some reason.

Any ideas as to why?

User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

Re: Simple drawing

Postby JoshM » 15 Sep 2014

I did a little further investigation and the issue with the trend lines not updating correctly appears to be unique to line1to2 and line2to3. I rearranged the code to where line3to1 updated first, and changed the begin and end for the other two lines back to the original. When I did this, line1to2 and line2to3 stopped updating correctly again. So, the issue appears to be tied directly to these first two lines for some reason.

Any ideas as to why?
I have no issue anymore when I changed the code to Henry's suggestion. I now have this:

Code: Select all

private void DrawTriangle()
{
ChartPoint point1 = new ChartPoint(Bars.Time[0], Bars.Open[0]);
ChartPoint point2 = new ChartPoint(Bars.Time[20], Bars.Open[20]);
ChartPoint point3 = new ChartPoint(Bars.Time[50], Bars.Open[50]);

// Line 1
if (line1to2 == null)
line1to2 = DrwTrendLine.Create(point1, point2);
else
{
line1to2.End = point2;
line1to2.Begin = point1;
}

// Line 2
if (line2to3 == null)
line2to3 = DrwTrendLine.Create(point2, point3);
else
{
line2to3.End = point3;
line2to3.Begin = point2;
}

// Line 3
if (line3to1 == null)
line3to1 = DrwTrendLine.Create(point3, point1);
else
{
line3to1.End = point1;
line3to1.Begin = point3;
}

// Set line settings
line1to2.Color = Color.Red;
line2to3.Color = Color.RoyalBlue;
line3to1.Color = Color.ForestGreen;

line1to2.Size = 3;
line2to3.Size = 3;
line3to1.Size = 3;
}
(changing the end points before the begin points). The order in which the three lines are plotted does not seem to be relevant; it's the order in which the 'End' and 'Begin' points of each line are set that matters.

Antny
Posts: 13
Joined: 31 Jul 2014
Has thanked: 3 times

Re: Simple drawing

Postby Antny » 15 Sep 2014

Yeah, I tried that. What disturbs me is the issue is something else, and this only covers it up. This code also works perfectly fine....

Code: Select all

// Line 3
if (line3to1 == null)
{
line3to1 = DrwTrendLine.Create(point3, point1);
}
else
{
line3to1.Begin = point3;
line3to1.End = point1;
}

// Line 1
if (line1to2 == null)
{
line1to2 = DrwTrendLine.Create(point1, point2);
}
else
{
line1to2.Begin = point2;
line1to2.End = point1;
}

// Line 2
if (line2to3 == null)
{
line2to3 = DrwTrendLine.Create(point2, point3);
}
else
{
line2to3.Begin = point3;
line2to3.End = point2;
}
As you can see, according to the fix, this should cause the same problems.....but it works fine.

Antny
Posts: 13
Joined: 31 Jul 2014
Has thanked: 3 times

Re: Simple drawing

Postby Antny » 15 Sep 2014

Also, I am not trying to be difficult. I am just preparing a massive indicator for a major release on the MultiCharts.NET platform, and the only thing I have left to do is sort out the graphics. Unfortunately, the indie is complex enough that I cannot have these hidden issues lurking around, as I may never get it to work correctly this way.


Return to “MultiCharts .NET”