Page 1 of 1

Draw a text box at a fixed position on a chart

Posted: Dec 29 2013
by maxmax68
Hello,
as others on this forum, I too would like to draw some text boxes on the chart in a fixed position.
I mean a text box that always remains fixed, anchored even when the chart is moved or changed on x and y axes!
I tried using the instructions provided in other posts, and I managed quite easily to anchor the y coordinate of the text box.
Unfortunately I can not find a satisfactory method to anchor the x coordinate of the text box.
In fact, with the solution that currently I adopted in my example, by changing the time on the x axis of the chart, I get the text box moves to the right or to the left, and does not remain anchored as desired.
Would you have any suggestions ?

For the future, it would be nice and maybe a lot easier if it were possible to draw the text or trendlines directly using the coordinates x and y fixed in pixels of the screen, don't you think so !!!
Thanks and regards.
Massimo

Code: Select all

Vars:
vMostLeftDateTime(0),
vMostRightDateTime(0),
vHighestDispValue(0),
vLowestDispValue(0),
vRangeDateTime(0);
//
//
RecalcLastBarAfter(0.1);
//
//
vMostLeftDateTime = GetAppInfo(aiLeftDispDateTime);
vMostRightDateTime = GetAppInfo(aiRightDispDateTime);
vHighestDispValue = GetAppInfo(aiHighestDispValue);
vLowestDispValue = GetAppInfo(aiLowestDispValue);
//
once Value1=Text_New_Dt((vMostLeftDateTime+0.05),((vHighestDispValue-vLowestDispValue)*0.5 + vLowestDispValue),"TestBox");
Text_SetLocation_DT(Value1,(vMostLeftDateTime+0.05),((vHighestDispValue-vLowestDispValue)*0.5 + vLowestDispValue));

Re: Draw a text box at a fixed position on a chart

Posted: Dec 30 2013
by seneca
I second this. Setting RecalcLastBarAfter to 0.1 or 0.01 in order to allocate a fixed position to textboxes is more a workaround and not a fully satisfying solution.

Re: Draw a text box at a fixed position on a chart

Posted: Jan 01 2014
by maxmax68
Hi,
I am playing with Text_New, in order to create some buttons on the chart.
I'm stuck with an error I can't reconize and solve.
May you help me ?

Regards.
Massimo
[+] show the code

Code: Select all


//
// The problem I can't solve is this:
// Although vCounter1 and vCounter2 are correctly updated on Mouseclick on TextBoxes, as reported in Print debug,
// I'm unable to redraw on the chart the TextBoxes with the correct updated vCounters.
// It shows always 0 zero.
// Where I'm wrong ???
//
//

[ProcessMouseEvents = True];

Vars:
recalcpersist vValue1(0),
recalcpersist vValue2(0),
recalcpersist vMostLeftDateTime(0),
recalcpersist vMostRightDateTime(0),
recalcpersist vHighestDispValue(0),
recalcpersist vLowestDispValue(0),
recalcpersist vRangeDateTime(0),
recalcpersist vCounter1(0),
recalcpersist vCounter2(0);
//
//
//
RecalcLastBarAfter(0.5);
//
//
vMostLeftDateTime = GetAppInfo(aiLeftDispDateTime);
vMostRightDateTime = GetAppInfo(aiRightDispDateTime);
vHighestDispValue = GetAppInfo(aiHighestDispValue);
vLowestDispValue = GetAppInfo(aiLowestDispValue);
// Draws first time TextBox1
once vValue1=Text_New_Dt((vMostLeftDateTime+0.05),((vHighestDispValue-vLowestDispValue)*0.5 + vLowestDispValue),"TestBox: "+NumToStr(vCounter1,0));
// Formats TextBox1
text_setborder(vValue1,TRUE);
text_setColor(vValue1,blue);
text_setBGColor(vValue1,green);
text_lock(vValue1,TRUE);
// Redraws TextBox1 on recalc or tick
Text_SetLocation_DT(vValue1,(vMostLeftDateTime+0.05),((vHighestDispValue-vLowestDispValue)*0.5 + vLowestDispValue));
// Draws first time TextBox2
once vValue2=Text_New_Dt((vMostLeftDateTime+0.05),((vHighestDispValue-vLowestDispValue)*0.3 + vLowestDispValue),"TestBox: "+NumToStr(vCounter2,0));
// Formats TextBox2
text_setborder(vValue2,TRUE);
text_setColor(vValue2,blue);
text_setBGColor(vValue2,green);
text_lock(vValue2,TRUE);
// Redraws TextBox2 on recalc or tick
Text_SetLocation_DT(vValue2,(vMostLeftDateTime+0.05),((vHighestDispValue-vLowestDispValue)*0.3 + vLowestDispValue));
// Checks for MouseClick on TextBoxes and updates vCounters
if MouseClickShiftPressed and MC_Text_GetActive=vValue1 then begin
vCounter1=vCounter1+1;
Text_SetLocation_DT(vValue1,(vMostLeftDateTime+0.05),((vHighestDispValue-vLowestDispValue)*0.5 + vLowestDispValue));
end
else if MouseClickShiftPressed and MC_Text_GetActive=vValue2 then begin
vCounter2=vCounter2+1;
Text_SetLocation_DT(vValue2,(vMostLeftDateTime+0.05),((vHighestDispValue-vLowestDispValue)*0.3 + vLowestDispValue));
end;
// Prints Debug
print(MC_Text_GetActive:0:0," ",MouseClickShiftPressed," ",vCounter1:0:0," ",vCounter2:0:0);

Re: Draw a text box at a fixed position on a chart

Posted: Jan 01 2014
by TJ
Hi,
I am playing with Text_New, in order to create some buttons on the chart.
I'm stuck with an error I can't reconize and solve.
May you help me ?

Regards.
Massimo
::
Can you describe what are you trying to achieve?
Please post a mock up drawing of your intention?

Re: Draw a text box at a fixed position on a chart

Posted: Jan 01 2014
by maxmax68
Hi TJ,
as I wrote in my code:

Code: Select all


//
// The problem I can't solve is this:
// Although vCounter1 and vCounter2 are correctly updated on Mouseclick on TextBoxes, as reported in Print debug,
// I'm unable to redraw on the chart the TextBoxes with the correct updated vCounters.
// It shows always 0 zero.
// Where I'm wrong ???
//
//
......

Re: Draw a text box at a fixed position on a chart

Posted: Jan 04 2014
by maxmax68
I hope this image will be useful.
Any idea or suggestion ?
Thanks

Massimo

Re: Draw a text box at a fixed position on a chart

Posted: Jan 06 2014
by maxmax68
Ok, solved.
Massimo
[+] show the code

Code: Select all


[ProcessMouseEvents = True];

Vars:
recalcpersist vValue1(0),
recalcpersist vValue2(0),
recalcpersist vMostLeftDateTime(0),
recalcpersist vMostRightDateTime(0),
recalcpersist vHighestDispValue(0),
recalcpersist vLowestDispValue(0),
recalcpersist vRangeDateTime(0),
recalcpersist vCounter1(0),
recalcpersist vCounter2(0);
//
//
//
//RecalcLastBarAfter(0.5);
//
//
vMostLeftDateTime = GetAppInfo(aiLeftDispDateTime);
vMostRightDateTime = GetAppInfo(aiRightDispDateTime);
vHighestDispValue = GetAppInfo(aiHighestDispValue);
vLowestDispValue = GetAppInfo(aiLowestDispValue);
// Draws first time TextBox1
once vValue1=Text_New_Dt((vMostLeftDateTime+0.05),((vHighestDispValue-vLowestDispValue)*0.5 + vLowestDispValue),"TextBox1: "+NumToStr(vCounter1,0));
// Formats TextBox1
text_setborder(vValue1,TRUE);
text_setColor(vValue1,blue);
text_setBGColor(vValue1,yellow);
text_lock(vValue1,TRUE);
// Redraws TextBox1 on recalc or tick
Text_SetLocation_DT(vValue1,(vMostLeftDateTime+0.05),((vHighestDispValue-vLowestDispValue)*0.5 + vLowestDispValue));
// Draws first time TextBox2
once vValue2=Text_New_Dt((vMostLeftDateTime+0.05),((vHighestDispValue-vLowestDispValue)*0.3 + vLowestDispValue),"TextBox2: "+NumToStr(vCounter2,0));
// Formats TextBox2
text_setborder(vValue2,TRUE);
text_setColor(vValue2,blue);
text_setBGColor(vValue2,yellow);
text_lock(vValue2,TRUE);
// Redraws TextBox2 on recalc or tick
Text_SetLocation_DT(vValue2,(vMostLeftDateTime+0.05),((vHighestDispValue-vLowestDispValue)*0.3 + vLowestDispValue));
// Checks for MouseClick on TextBoxes and updates vCounters
if MouseClickShiftPressed and MC_Text_GetActive=vValue1 then begin
vCounter1=vCounter1+1;
// Updates string of TextBox1
text_setstring(vValue1,"TextBox1: "+NumToStr(vCounter1,0));
Text_SetLocation_DT(vValue1,(vMostLeftDateTime+0.05),((vHighestDispValue-vLowestDispValue)*0.5 + vLowestDispValue));
end
else if MouseClickShiftPressed and MC_Text_GetActive=vValue2 then begin
vCounter2=vCounter2+1;
// Updates string of TextBox2
text_setstring(vValue2,"TextBox2: "+NumToStr(vCounter2,0));
Text_SetLocation_DT(vValue2,(vMostLeftDateTime+0.05),((vHighestDispValue-vLowestDispValue)*0.3 + vLowestDispValue));
end;
// Prints Debug
print(MC_Text_GetActive:0:0," ",MouseClickShiftPressed," ",vCounter1:0:0," ",vCounter2:0:0);

Re: Draw a text box at a fixed position on a chart

Posted: Apr 10 2016
by Smoky
Great code !

did you find a workaround to anchor textbox to screen ?

too easy with MouseClickBarNumber who can give you the barnumber on screen LoL.

value1 = FirstBarOnScreen(data1);
value1 = LastBarOnScreen(data1);

would be fun for automatic redraw. (with RecalcLastBarAfter(0.5); )


thank for help

Re: Draw a text box at a fixed position on a chart

Posted: Apr 10 2016
by arjfca
Draw textbox at a fix position

Can't say if it is what your looking for
This code will put a text box at the top right corner. The data displayed is the spread and chart #

The solution is to use the GetAppInfo( aiHighestDispValue ). From it's data, you could position your text box where you want

Code: Select all

If lastbaronchart then begin
Spread = (insideask -insidebid) ;
If jpy_pair = true then spread = spread *100 else spread = spread *10000;
SpreadText ="Chart#: " + numtostr(ChartNum,0) + " Spread: " + NumtoStr(Spread ,2);
HighLocation = GetAppInfo( aiHighestDispValue ) ;
text_setstring(SpreadtextLine,Spreadtext);
text_setlocation_s(spreadTextLine,date,time_s,highLocation);
end;
2016-04-10_10h44_29.png
(94.83 KiB) Downloaded 2031 times

Re: Draw a text box at a fixed position on a chart

Posted: Apr 10 2016
by Smoky
Thanks arjfca,

But when you scroll back you chart you lose your text.

Works fine in real time because with time_s you have time of last bar on chart who is also last bar on screen ;) ....

Re: Draw a text box at a fixed position on a chart

Posted: Apr 10 2016
by arjfca
Thanks arjfca,

But when you scroll back you chart you lose your text.

Works fine in real time because with time_s you have time of last bar on chart who is also last bar on screen ;) ....

True. You may try to look at reading the left value instead of the right.

Martin

Re: Draw a text box at a fixed position on a chart

Posted: Apr 12 2016
by JoshM
But when you scroll back you chart you lose your text.
You can use `GetAppInfo(aiLeftDispDateTime)` (or `GetAppInfo(aiRightDispDateTime)`) to get the lime from the outmost left (right) bar on the chart as the location for the text box. Then, with `RecalcLastBarAfter()`, the script can be updated every so often so that the text box is moved to the then-current fixed chart location (taking the time and price axis into account).

It sounds a little cryptic if I try to explain it in this way, but if you try it you'll see that you can make a text box in a fixed position this way.

Re: Draw a text box at a fixed position on a chart

Posted: Apr 12 2016
by Smoky
Ok JosH,

I try this code :

Code: Select all

var: intrabarpersist TimeLastBar(0), intrabarpersist Counter(0);

counter=counter+1;

TimeLastBar=GetAppInfo(aiRightDispDateTime);
messagelog(numtostr(Counter,0)," Time ",TimeLastBar);

RecalcLastBarAfter(1);
you will see that time resolution is lower than Time_s and you can't use pixel resolution to choose X coordinate :(

Thanks ;)

Re: Draw a text box at a fixed position on a chart

Posted: Apr 12 2016
by TJ
Ok JosH,
I try this code :

Code: Select all

var: intrabarpersist TimeLastBar(0), intrabarpersist Counter(0);
counter=counter+1;
TimeLastBar=GetAppInfo(aiRightDispDateTime);
messagelog(numtostr(Counter,0)," Time ",TimeLastBar);
RecalcLastBarAfter(1);
you will see that time resolution is lower than Time_s and you can't use pixel resolution to choose X coordinate :(
Thanks ;)
There is a bug with the white space time scale. It has been confirmed in one of the threads.

Re: Draw a text box at a fixed position on a chart

Posted: Apr 12 2016
by TJ
Timescale inconsistency
viewtopic.php?f=1&t=49024

Re: Draw a text box at a fixed position on a chart

Posted: Apr 12 2016
by JoshM
Ok JosH,

I try this code :

Code: Select all

var: intrabarpersist TimeLastBar(0), intrabarpersist Counter(0);

counter=counter+1;

TimeLastBar=GetAppInfo(aiRightDispDateTime);
messagelog(numtostr(Counter,0)," Time ",TimeLastBar);

RecalcLastBarAfter(1);
you will see that time resolution is lower than Time_s and you can't use pixel resolution to choose X coordinate :(
Uhm, I don't understand you at all, sorry.

- I don't know get how a time resolution can be lower than `Time_s` (the bar time with seconds precision).
- I don't know what a 'pixel resolution' for the x coordinate is. If we draw a text box in PowerLanguage, we need to specify the time value but not the x pixel coordinate. (In MultiCharts .NET we can use x pixel coordinates though).
- And your code runs fine here. At least, it gives the output I was expecting to see based on the code. Can you elaborate on what is wrong or missing with this code?

If you're talking about the 'Chart Shift' option of the chart's 'Format Window' settings specified to a pixel or percentage value, then `GetAppInfo(aiRightDispDateTime)` has no influence on that. (Unless, of course, we change the 'Chart Shift' option but that makes sense).

Re: Draw a text box at a fixed position on a chart

Posted: Apr 12 2016
by Smoky
This code help you to see TimeLastBar value when you scroll back a chart, you can see a lot of bar scrolling with no value change ...

wiki :

aiRightDispDateTime .... and the fractional portion of the DateTime value specifies the fraction of the day since midnight.

only two digits for 24h :( and you will see only last digit change with many bar scrolling back ...


I ask for new EL code :

value1 = FirstBarOnScreen(data1);
value1 = LastBarOnScreen(data1);

because it's the only workaround to find barnumber on screen without use MouseClickBarNumber...

Re: Draw a text box at a fixed position on a chart

Posted: Apr 25 2016
by JoshM
I ask for new EL code :

value1 = FirstBarOnScreen(data1);
value1 = LastBarOnScreen(data1);

because it's the only workaround to find barnumber on screen without use MouseClickBarNumber...
Another workaround would be to compare the DateTime value of the left bar on the screen (`GetAppInfo(aiLeftDispDateTime)`) with the `DateTime` value returned of the recent x bars, and when they match, retrieve that bar's number.

So we can probably do this in PowerLanguage with the keywords and features that we already have, I think.

Re: Draw a text box at a fixed position on a chart

Posted: Apr 25 2016
by gilko
Yes JoshM , for precise anchor we need other EL code.

please, put this indicator on a 1 minute chart,

Code: Select all

var: intrabarpersist TimeLastBar(0), intrabarpersist Counter(0);

counter=counter+1;

TimeLastBar=GetAppInfo(aiLeftDispDateTime);
messagelog(numtostr(Counter,0)," Time ",TimeLastBar);

RecalcLastBarAfter(1);
Scroll back our chart, and you will see many bars scrolling with the same value returned by GetAppInfo !

like Smoky already said before, not any resolution on date/time returned

with this workaround you can only anchor text/box on the middle of the screen with little move ...