Plot and title coding

 Plot(EMA1, "EMA1", 
 IIf(upTrend1, colorBrightGreen,
 IIf(upTrend2, colorGreen,
 IIf(dnTrend1, ColorHSB(250,300,200),
 IIf(dnTrend2 , colorRed, colorGrey40)))) , styleLine | styleThick);

Please code for the title to change colors according to this dynamic color plot code i have written.
the trends have been defined and the plot works.
Also the title names of the EMA1 should not change it should be fixed only the colors of the indicator values should change according to the indicator colors.
Thanks

@Vikas.sood, if I understood your request properly, I will code something like this (the first lines should be replaced by your own rules):

// Substitute with your rules....
// here below some fake rules only to define some variables
EMA1 = EMA( C, 21 );
upTrend1 = Ref( EMA1, -1 ) >= Ref( EMA1, -5 );
dnTrend1 = Ref( EMA1, -1 ) < Ref( EMA1, -5 );
upTrend2 = Ref( EMA1, -1 ) >= Ref( EMA1, -21 );
dnTrend2 = Ref( EMA1, -1 ) < Ref( EMA1, -21 );

// Apply your logic for colors defining a variable to hold a "colors" array
colors = IIf( upTrend1, colorBrightGreen, IIf( upTrend2, colorGreen,
              IIf( dnTrend1, ColorHSB( 250, 300, 200 ),
                   IIf( dnTrend2 , colorRed, colorGrey40 ) ) ) );

Plot( C, "Close", colorDefault, styleCandle );
// Apply the "colors" both to the plot - add styleNoTitle
Plot( EMA1, "EMA1", colors, styleLine | styleThick | styleNoTitle );
// and to the title
_N( Title = StrFormat( "{{NAME}} - " + FullName() +
                       " - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +
                       WriteVal( V, 1.0 ) + " {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) +
                       " - EMA1 " + EncodeColor( SelectedValue( colors ) ) + StrFormat( "%g", SelectedValue( EMA1 ) ) );
1 Like

Thanks for you response. your idea foe defining the color was great. But the Title code gives an error at the end. I'm sending you a screen shot.Screenshot (17)

@Vikas.sood, the code you added/changed has some wrongly placed and unbalanced parentheses.

If you click on any closing brace  )  in your formula the editor will show you, changing color, the corresponding opening brace  (  and viceversa.

For example, examining this point in your code:

StrFormat( "%g", SelectedValue( EMA301  )  +

you'll see that you are missing the closing parentheses for the StrFormat() function. You should add it before the + sign.

Anyway, here is the code you probably need (the first lines are there only to define the variables).
Please, note the addition of some extra EncodeColor() function calls to reset the color of the text title and apply your "colors" arrays only to the values.

// variable definitions
EMA301 = EMA( C, 301 );
EMA501 = EMA( C, 501 );
EMA701 = EMA( C, 701 );
EMA901 = EMA( C, 901 );
// fake values - replace wih your "colorsX01" arrays
colors301 = colorGreen;
colors501 = colorBlue;
colors701 = colorRed;
colors901 = colorOrange;

// In addition to properly balanced open and close braces, you need to add some extra EncodeColor() calls
// to reset the text to the default color as you want ONLY the values to be colored like the EMA lines
_N( Title = StrFormat( "{{NAME}} - " + FullName() +
                       " - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +
                       WriteVal( V, 1.0 ) + " {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) +
            " - EMA301 - " + EncodeColor( SelectedValue( colors301 ) ) + StrFormat( "%g", SelectedValue( EMA301 ) ) +
            EncodeColor( colorDefault ) +
            " - EMA501 - " + EncodeColor( SelectedValue( colors501 ) ) + StrFormat( "%g", SelectedValue( EMA501 ) ) +
            EncodeColor( colorDefault ) +
            " - EMA701 - " + EncodeColor( SelectedValue( colors701 ) ) + StrFormat( "%g", SelectedValue( EMA701 ) ) +
            EncodeColor( colorDefault ) +
            " - EMA901 - " + EncodeColor( SelectedValue( colors901 ) ) + StrFormat( "%g", SelectedValue( EMA901 ) ) );

Thankyou very much again . its working as I wanted it too.
I have another question. The title has 14 values ( I removed the OHLC from the title as it was for an indicator.

Title = {{NAME}} - " + _Default Name() +

            " - EMA301 - " + EncodeColor( SelectedValue( colors301 ) ) + StrFormat( "%g", SelectedValue( EMA301 ) ) +
            EncodeColor( colorDefault ) +
            " - EMA501 - " + EncodeColor( SelectedValue( colors501 ) ) + StrFormat( "%g", SelectedValue( EMA501 ) ) +
            EncodeColor( colorDefault ) +
            " - EMA701 - " + EncodeColor( SelectedValue( colors701 ) ) + StrFormat( "%g", SelectedValue( EMA701 ) ) +
            EncodeColor( colorDefault ) +
            " - EMA901 - " + EncodeColor( SelectedValue( colors901 ) ) + StrFormat( "%g", SelectedValue( EMA901 ) ) );

I used this portion of the code . and addded 5 for the next series and 5 more for another series of averages.
How do I wrap the text of the title into three separate lines of 4, 5, 5 values each.
I used SetChartOptions(2, chartWrapTitle ); this just wraps the title into 2 lines.

@Vikas.sood you should insert a "\n", a new line escape sequence.

Here is a change to my previous example to split the title over multiple lines.

_N( Title = StrFormat( "{{NAME}} - " + FullName() +
                       " - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +
                       WriteVal( V, 1.0 ) + " {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) + 
            "\n" +            
            "EMA301 - " + EncodeColor( SelectedValue( colors301 ) ) + StrFormat( "%g", SelectedValue( EMA301  ) ) +
            " - EMA501 - " + EncodeColor( SelectedValue( colors501 ) ) + StrFormat( "%g", SelectedValue( EMA501  ) ) +
            "\n" + 
            "EMA701 - " + EncodeColor( SelectedValue( colors701 ) ) + StrFormat( "%g", SelectedValue( EMA701  ) ) +
            " - EMA901 - " + EncodeColor( SelectedValue( colors901 ) ) + StrFormat( "%g", SelectedValue( EMA901 ) ) );

Actually the is no need to put in a separate line but using it in this way makes it example usage more clear.
You can write it embedded in any string like:

"\nEMA301 - " + EncodeColor( SelectedValue( colors301 ) ) + StrFormat( "%g", SelectedValue( EMA301  ) ) +
2 Likes

There isn't multiple StrFormat required. It waste of functions and looks pretty ugly.

Here is example with just single use of StrFormat.

// Code of before above

_N( Title = StrFormat( "{{NAME}} - {{FULLNAME}} - {{INTERVAL}} {{DATE}}\n"+ 
						"%sEMA301 - %g - %sEMA501 - %g\n" +
						"%sEMA701 - %g - %sEMA901 - %g\n",						
						EncodeColor(SelectedValue(colors301)), SelectedValue(EMA301), 
						EncodeColor(SelectedValue(colors501)), SelectedValue(EMA501),
						EncodeColor(SelectedValue(colors701)), SelectedValue(EMA701),
						EncodeColor(SelectedValue(colors901)), SelectedValue(EMA901) ));

Before someone asks...
%s format specifier is for EncodeColor.

3 Likes

If the color code for all the EMAs is the sane can we write a common color definition or we have to define each of them separately as I have done.

colors301 = IIf( upTrend3011, colorBrightGreen, 
            IIf( upTrend3012, colorGreen,
            IIf( dnTrend3011, ColorHSB( 250, 300, 200 ),
            IIf( dnTrend3012 , colorRed, colorGrey40 ) ) ) );     
Plot( EMA301, "EMA301", colors301, styleLine | styleThick | styleNoTitle );                   

colors501 = IIf( upTrend5011, colorBrightGreen, 
            IIf( upTrend5012, colorGreen,
            IIf( dnTrend5011, ColorHSB( 250, 300, 200 ),
            IIf( dnTrend5012 , colorRed, colorGrey40 ) ) ) );     
Plot( EMA501, "EMA501", colors501, styleLine | styleThick | styleNoTitle ); 

colors701 = IIf( upTrend7011, colorBrightGreen, 
            IIf( upTrend7012, colorGreen,
            IIf( dnTrend7011, ColorHSB( 250, 300, 200 ),
            IIf( dnTrend7012 , colorRed, colorGrey40 ) ) ) );     
Plot( EMA701, "EMA701", colors701, styleLine | styleThick | styleNoTitle );

colors901 = IIf( upTrend9011, colorBrightGreen, 
            IIf( upTrend9012, colorGreen,
            IIf( dnTrend9011, ColorHSB( 250, 300, 200 ),
            IIf( dnTrend9012 , colorRed, colorGrey40 ) ) ) );
                  
Plot( EMA901, "EMA901", colors901, styleLine | styleThick | styleNoTitle );

Vikas.sood, since you are using different variables for each "colors" array, I suppose that they are calculated using different variables.
I imagine that upTrend3011 will be different from upTrend5011 (and similar), so the resulting "colors" should be different. In such case you should keep the different variables.
If all the calculations are using the same 4 values (and in such a case, it is unclear why you assigned different variable names to them), you can avoid the repetition.

In any case, I suggest you spend a little time learning how to debug your code using an exploration, logging all the values you are using will allow you to visualize better/understand the result of your operations.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.