Trading sessions in different colors

Hello,

I wrote the following code to find the different trading sessions and their overlaps:

tn = TimeNum();
London_open = 100000;  // 03:00
London_close = 190000; // 12:00
NY_open = 150000;      // 08:00 start in HHMMSS format   
NY_close = 240000;     // 17:00 end in HHMMSS format     
Sydney_open = 010000;  // 18:00
Sydney_close = 100000; // 03:00
Tokyo_open = 020000;  // 19:00
Tokyo_close = 110000; // 04:00

NY_session = tn >= NY_open AND tn <= NY_close;
London_session  = tn >= London_open  AND tn <= London_close;
Sydney_session  = tn >= Sydney_open  AND tn <= Sydney_close;
Tokyo_session  = tn >= Tokyo_open  AND tn <= Tokyo_close;

color = IIf(NY_session,colorPaleGreen, IIf(London_session, colorPink, IIf(Sydney_session,colorAqua, colorLightYellow)));
Plot( 1, "", color, styleArea|styleOwnScale,0,1,0,-5 );

but I cannot find the overlaps of the trading sessions:
image

So, I have two questions:
1- How can I have transparent background colors to find the overlaps of the trading sessions?
2- What If I want to have the trading sessions like a ribbon at the bottom of the chart?

Best regards,

1 Like

Since AB doesn't have transparency, you would have to calculate the overlaps in your code, and add them to your plot.

To get a ribbon at the bottom, you could use PlotOHLC() in what may seem to be an unusual fashion, and use the same value (1) for the open, high, low and close. Use styleCloud | styleOwnScale | styleNoLabel | styleNoTitle options, and set the scale min to 1 and max to maybe 50. If you want the ribbon to be taller, make the max smaller. You'll have to experiment with this but I think it would work. Make sure this plot appears after the price plot in your code.

Alternately, you could plot each market using a different value, e.g. NY = 1, London = 2, etc., and plot each one separately, so you would have 4 separate ribbons at the bottom that would make the overlaps obvious to your eyes without having to calculate them.

I hope this helps,
Peter

2 Likes

Thank you so much for your help. I will try it and if it doesn't work, I will get back to you.

Hello,
@PeterD
I used the below code to get the ribbon for the trading sessions:

tn = TimeNum();
London_open = 100000;  // 03:00
London_close = 190000; // 12:00
NY_open = 150000;      // 08:00 start in HHMMSS format   
NY_close = 240000;     // 17:00 end in HHMMSS format     
Sydney_open = 010000;  // 18:00
Sydney_close = 100000; // 03:00
Tokyo_open = 020000;  // 19:00
Tokyo_close = 110000; // 04:00

NY_session = tn >= NY_open AND tn <= NY_close;
Tokyo_session  = tn >= Tokyo_open  AND tn <= Tokyo_close;
London_session  = tn >= London_open  AND tn <= London_close;
Sydney_session  = tn >= Sydney_open  AND tn <= Sydney_close;

color1 = IIf(NY_session,colorGreen, IIf(Tokyo_session, colorYellow, colorWhite));
color2 = IIf(London_session, colorRed, IIf(Sydney_session, colorAqua,colorWhite));
Plot( 1, "", color1, styleArea|styleOwnScale,-10, 150, 0, -5 );
Plot( 1, "", color2, styleArea|styleOwnScale,-12, 150, 0, -5 );

In each ribbon, you find that the inactive session has a white color but I don't like to see any color in the inactive session. What should I do?

Best regards,

Terminology in this thread is wrong.

Transparency is binary. Either something is transparent, or not. Transparent means invisible, totally passing thru everything beneath it, like glass.

Transparency in that sense is of course possible because you can of course plot hollow rectangles or refrain from plotting certain bars (by passing NULL - any Bar with NULL passed in Plot() is transparent).

But this question is not really about transparency. It is about mixing colors.

Different colors for different sessions are 100% possible as seen in the screenshot above. You just pass the color you want to color parameter (3rd argument of Plot()).
You can MIX colors if you want to achieve say "half-way mix", using colorBlend function

mix_of_colors = ColorBlend( colorRed, colorBlue, 0.5 ); // third parameter controls the mix

http://www.amibroker.com/guide/afl/colorblend.html

This way you can achieve mixtures of colors if sessions overlap. Easy.

You would do something like this:

function ColorBlendArray( array1, array2, factor )
{ 
   output = 0;
   for( i = 0; i < BarCount; i++ ) output[ i ] = ColorBlend( array1[ i ], array2[ i ], factor );
   return output; 
}

color1 = IIF( London_Session, colorPink, colorWhite );
color2 = IIF( Sydney_Session, colorBlue, colorWhite );
color = ColorBlendArray( color1, color2, 0.5 );

For more colors use more ColorBlend calls.

So everything is doable right now, right here.

Alpha blending (aka. "transluency", "fractional transparency") is another story - it is not supported by Windows GDI on per-primtive GDI object level, only supported by much slower APIs. It is being considered, but requires abandoning GDI.

But as I presented here, what you really need to do is to mix colors and it is CURRENTLY available via ColorBlend function.

1 Like

Thank you very much @Tomasz for your comprehensive response. I tried what you said and faced an error:
image

The second argument should be a number and it doesn't accept it. Could you possibly help me in this regard?

Best regards,

@simon, I think that following @PeterD indications using multiple separate PlotOHLC calls will make a chart easier to interpret.
For example, see this screenshot where I plotted the OHLC range 1-1-0-0 for Sydney, 2-2-1-1 for Tokyo, 3-3-2-2 for Milan, 4-4-3-3 for London, and 5-5-4-4 for New York - I used a scale of 0-20 (increasing the max value of the scale will result in a tinier ribbon).

image

(For the extra legend, I used some GFX low-level graphics functions that, as an alternative, can also be employed to replace the PlotOHLC calls).

4 Likes

OK, I forgot that it needs scalar. Please use this function, it works the same as ColorBlend but works with arrays:

function ColorBlendArray( array1, array2, factor )
{ 
   output = 0;
   for( i = 0; i < BarCount; i++ ) output[ i ] = ColorBlend( array1[ i ], array2[ i ], factor );
   return output; 
}

color1 = IIF( London_Session, colorPink, colorWhite );
color2 = IIF( Sydney_Session, colorBlue, colorWhite );
color = ColorBlendArray( color1, color2, 0.5 );

4 Likes

Thank you very much for your help and time @beppe

1 Like

I deeply appreciate your help @Tomasz.

Thomasz, thank you for clarifying the terminology and providing context about alpha blending.

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