Back

Adding Color to a Bar Chart (Bugs or User Mistakes?)

Hello All,

  I noticed a couple of curious things when plotting in python. The first is that if I try to add color to a bar chart, the type automatically changes to a line (rather than a bar) in the final chart. 

Here is how I am adding it, maybe I am doing something wrong?

# Create a custom volume chart
VolChart = Chart("Volume")
VolChart.AddSeries(Series('Buying Volume', SeriesType.Bar, Color.Green))
VolChart.AddSeries(Series('Selling Volume', SeriesType.Bar))
self.AddChart(VolChart)

and this is how I am plotting it:

if data[self.sym].Close >= data[self.sym].Open:
self.Plot('Volume', 'Buying Volume', data[self.sym].Volume)
else:
self.Plot('Volume', 'Selling Volume', data[self.sym].Volume)

In the resulting chart, the buying volume is a green line chart whilst the selling volume is an actual bar chart. 

https://i.imgur.com/hV7oYdI.png

The next thing I noticed is that if I add color to both of the series, so my code now looks like this:

VolChart = Chart("Volume")
VolChart.AddSeries(Series('Buying Volume', SeriesType.Bar, Color.Green))
VolChart.AddSeries(Series('Selling Volume', SeriesType.Bar, Color.Red))
self.AddChart(VolChart)

 

I get the following error: ( Chart series name already exists ) 

281 | 00:46:15:
During the algorithm initialization, the following exception has occurred: Exception : Chart.AddSeries(): Chart series name already exists
at QuantConnect.Chart.AddSeries (QuantConnect.Series series) [0x00031] in <8020d7f9c1ca4bb3822d930f6f8ac032>:0
at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <2e7c1c96edae44d496118948ca617c11>:0
at Initialize in main.py:line 37
Exception : Chart.AddSeries(): Chart series name already exists
at QuantConnect.Chart.AddSeries (QuantConnect.Series series) [0x00031] in <8020d7f9c1ca4bb3822d930f6f8ac032>:0
at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke(System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <2e7c1c96edae44d496118948ca617c11>:0

Finally, if I remove Color from both the lines, my volume chart appears as expected... just without the colors I want:

https://i.imgur.com/ktFBTO6.png

 

So am I missing something or making mistakes? 

Update Backtest







Thank for the detailed description! We'll look into it and see what might be causing that.

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Hi, you are getting that error because there is no constructor for the Series class with Color as third argument,
so the default Series constructor with no arguments was being called (causing the duplicate chart series error). 

This can be easily fixed by changing this line:
VolChart.AddSeries(Series('Buying Volume', SeriesType.Bar, Color.Green))
to this:
VolChart.AddSeries(Series('Buying Volume', SeriesType.Bar, '$', Color.Green))

You can also find more examples of custom charting in the docs here:

https://www.quantconnect.com/docs/algorithm-reference/charting
0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Hi  Jared Broad and Stefano Raggi 

Many thanks for your feedback and comments. So looks like it could be user errors in the end. The suggestion from Stefano resolved the "name already exists" error. However, my colors are still not applied to the bar chart. (I am still seeing blue and black bars).

This is how I am setting up now:

VolChart = Chart("Volume", ChartType.Stacked)
VolChart.AddSeries(Series('Buying Volume', SeriesType.Bar, '$', Color.Green))
VolChart.AddSeries(Series('Selling Volume', SeriesType.Bar, '$', Color.Red))
self.AddChart(VolChart)

 Is there anything else I am doing wrong in the declarations? 

Given the feedback, I have tried playing around with argument positions and declaring as keyword arguments but am still not having much luck. 

Really appreciate your kind help!

Side Note/Feedback

Just a little user feedback/note on the usability and documentation. I had read the page Stefano linked prior to posting and looked at the examples a few times. It appeared to me that the unit `$` and `Color.XXX` arguments are optional. I made this assumption because they are not always declared in the examples. So I assumed I could omit the unit '$'.

It from my novice perspective, It seems that sometimes we can place arguments in any position and other times we cannot. For example, in the documentation, I see that sometimes the index argument is in the third position and other times the unit argument is in that position. Each time neither is declared with a keyword argument. This can be quite puzzling especially if we cannot put other arguments like Color in the third position.  

Is that a fair assessment or am I still misunderstanding things? 

0

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed