Vertical trendlines at mouse click  [SOLVED]

Questions about MultiCharts and user contributed studies.
Louis88
Posts: 24
Joined: 11 May 2013
Has thanked: 28 times
Been thanked: 1 time

Vertical trendlines at mouse click

Postby Louis88 » 14 Mar 2015

Hello,
I'm working on 1 minute barcharts.
I would like to:
1. plot a first vertical line on the chart by left clicking on a certain date and time
2. plot a second vertical line on the chart by left clicking on a certain date and time
3. calculate the number of bars between the two lines
4. dimension a dynamic array
5. store price data in the array
6. process and analyse the data
7. show results somewhere in some way.

Needless to say I'm stuck at point 1.

Please take a look at this code.
The original code, used to calculate % difference between two points, was found on the internet (thanks to maxmax68).
I tried to modify it as follow:
[ProcessMouseEvents = true] ;

Variables:
RecalcPersist Line1(0),
RecalcPersist Line2(0),

RecalcPersist LastBar(0),
RecalcPersist BarsAgo1(0),
RecalcPersist BarsAgo2(0),

RecalcPersist Date1(0),
RecalcPersist Date2(0),

RecalcPersist Time1(0),
RecalcPersist Time2(0),

RecalcPersist Price1(0),
RecalcPersist Price2(0),
RecalcPersist Price3(0),
RecalcPersist Price4(0),
RecalcPersist init(0),

RecalcPersist BarNumber1(0),
RecalcPersist BarNumber2(0);

if (LastBarOnChart) then begin
switch (GetAppInfo(aiCalcReason)) begin

// recalc for Left Mouse Click
case CalcReason_MouseLClick :
if (init = 0) and (BarNumber > 0) then begin
// Date1 = JulianToDate(MouseClickDateTime);
// Time1 = DateTime2elTime_s(MouseClickDateTime);
Price1 = GetAppInfo(aiLowestDispValue);
Price2 = GetAppInfo(aiHighestDispValue);
BarNumber1 = MouseClickBarNumber;
LastBar = BarNumber + MaxBarsBack;
BarsAgo1 = LastBar - BarNumber1;

Line1 = TL_New_BN(BarNumber1, Price1, BarNumber1, Price2);
TL_SetSize(Line1, 1);
TL_SetColor(Line1, RGB(051, 102, 255));

init = 1;
end
else if (init = 1) then begin
// Date2 = JulianToDate(MouseClickDateTime);
// Time2 = DateTime2elTime_s(MouseClickDateTime);
Price3 = GetAppInfo(aiLowestDispValue);
Price4 = GetAppInfo(aiHighestDispValue);
BarNumber2 = MouseClickBarNumber;
LastBar = BarNumber + MaxBarsBack;
BarsAgo2 = LastBar - BarNumber2;

Line2 = TL_New_BN(BarNumber2, Price3, BarNumber2, Price4);
TL_SetSize(Line2, 1);
TL_SetColor(Line2, RGB(051, 102, 255));

init = 0;
end;
end;

// The following code not modified yet:
// recalc for new tick arrived
{Case CalcReason_Default :
TL_Delete(value15);
value15 = Tl_New_s(date3, time3, price1, date4, time4, price2);
TL_SetSize(value15, 1);
TL_SetColor(value15, RGB(051, 102, 255));

end;}

end; Known issues:
a) with a bar spacing large enough (for me 3 or 5 pixels) I realize that if I click on a bar then the trend line is plotted six bars right;
b) if I want to plot a second trend line the first disappears. But I want them to stay there both, to delimit the area to analyse.
c) if I add the following code before the last end; Print(Close[BarsAgo2]:15:3); it prints the correct price of the clicked bar, but the vertical trend line disappears.

Any suggestion is sincerely appreciated.
Thanks in advance for your time.
Louis88

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

Re: Vertical trendlines at mouse click  [SOLVED]

Postby JoshM » 15 Mar 2015

I would like to:
1. plot a first vertical line on the chart by left clicking on a certain date and time
2. plot a second vertical line on the chart by left clicking on a certain date and time
If you store the reference to a created trend line in a variable, the variable can be used to determine whether the line has already been plotted (because it will have a value greater than 0 if the drawing succeeded). That allows for plotting only 2 trend lines after a click on the chart.

To create a vertical trend line we can plot a vertical line on the bar (for example, between high and low) and then extend it with `TL_SetExtLeft` and `TL_SetExtRight`.

To retrieve the mouse click location for the trend line we can use `MouseClickDateTime` and use `MouseClickBarNumber` to calculate the clicked-on bar number. See mouse clicks for more on mouse click processing.

The two points quoted above would then look like:

Image

Code: Select all

[ProcessMouseEvents = true];
[RecoverDrawings = false];

Variables:
IntraBarPersist firstLine(-1),
IntraBarPersist secondLine(-1),
bar_num(0);

if (GetAppInfo(aiCalcReason) = CalcReason_MouseLClick) then begin

bar_num = (CurrentBar + MaxBarsBack) - MouseClickBarNumber;

if (firstLine < 0) then begin

firstLine = TL_New_DT(MouseClickDateTime, Symbol_Low[bar_num],
MouseClickDateTime, Symbol_High[bar_num]);

TL_SetExtLeft(firstLine, true);
TL_SetExtRight(firstLine, true);
TL_SetSize(firstLine, 2);
TL_SetColor(firstLine, blue);

end
else if (secondLine < 0) then begin

secondLine = TL_New_DT(MouseClickDateTime, Symbol_Low[bar_num],
MouseClickDateTime, Symbol_High[bar_num]);

TL_SetExtLeft(secondLine, true);
TL_SetExtRight(secondLine, true);
TL_SetSize(secondLine, 2);
TL_SetColor(secondLine, blue);

end;

end;
Attachments
scr.15-03-2015 13.19.32.png
(8.19 KiB) Downloaded 547 times

Louis88
Posts: 24
Joined: 11 May 2013
Has thanked: 28 times
Been thanked: 1 time

Re: Vertical trendlines at mouse click

Postby Louis88 » 15 Mar 2015

Many thanks for your help JoshM.
I've made a mess.
I will study your code and the material on the site.
Thanks again.


Return to “MultiCharts”