changing bar colours - main chart

Joined
Jan 29, 2020
Posts
8
Likes
2
Hello,

I am trying to code a piece where the price bars (OHLC) on a chart get a color depending on a criteria, such as a moving average. I am used to the EasyLanguage in TradeStation, but not advanced in JAVA.

This is what I have at the moment. When the Close is above a SMA, then the bars should be green, if its below the SMA, they should be red. Thinking it does what I want it to do, but after compiling nothing appears on my chart. Anyone to point out my mistake?

DataSeries series = ctx.getDataSeries();
Double simple_mov_avg = series.sma(index, period, input);
float C ;
C = series.getClose(index);
if (C > simple_mov_avg) {
series.setBarColor(index, X11Colors.GREEN);
} else if (H < simple_mov_avg) {
series.setBarColor(index, X11Colors.RED);
}
 

Spin

Well-known member
Joined
May 22, 2019
Posts
479
Likes
191
Hi there @strawberry_white and welcome to this wonderful Forum ! :)

I once tried to do the same thing, but ended up using different programming logic, because I kept running into trouble.

I now use the following to get the result you are aiming for:
First you need to define BarClose (in your 'Calculate'-function), and the different colors (in your 'initialize'-function).
Then, define and store the MA-value:
Double ma = series.ma(MAmethod, index, MAperiod, input);

And finally something along these lines:

Java:
if (BarClose > ma)
                                
                                {
                                    series.setPriceBarColor(index, getSettings().getColor(MA_UP_COLOR));
                                   
                                }
                  else if (BarClose < ma)
                                {
                                series.setPriceBarColor(index, getSettings().getColor(MA_DOWN_COLOR));
                                   
                                }
                  else series.setPriceBarColor(index, getSettings().getColor(MA_NEUTRAL_COLOR));
 
Joined
Jan 29, 2020
Posts
8
Likes
2
hi Spin,

thanks for a swift reply, I was struggling to find the right "setPriceBarColor" function, but the above helped me. I followed your guide to define the colours in the initialize part. I changed it a bit so bars are red when the high is below the sma, green when the low is above the sma, and gray when otherwise. Attached is the outcome versus a 10-day sma. Again your help is much appreciated!
 

Attachments

  • BarColor.gif
    BarColor.gif
    35.8 KB · Views: 21

Spin

Well-known member
Joined
May 22, 2019
Posts
479
Likes
191
Glad I could help. Looks good !

Nice job (y)
 

shzhning

New member
Joined
Aug 19, 2020
Posts
2
Likes
0
Hello, new user here. I am intimidated by Java SDK, though I have some experience writing my own scripts in CQG and Thinkorswim. I have an ADX paint bar idea from CQG that I wanted to port to MW.

Here's the script

def ADX = ADX(5);
def DIPlus = DIPlus(5);
def DIMinus = DIMinus(5);

def ADXUp = ADX > ADX[1] and ADX > 20 and DIPlus > DIMinus;
def ADXDn = ADX > ADX[1] and ADX > 20 and DIPlus < DIMinus;

AssignPriceColor(if ADXUp then Color.Blue
else if ADXDn then Color.Red
else Color.CURRENT);


Basically, ADXup is defined as ADX is rising and greater than 20 and DI+ is greater than DI-;
and ADXDn is defined as ADX is rising and greater than 20 and DI+ is less than DI-.

Anyone can help?

Thank you
 

MotiveWave_Joe

Moderator
Staff member
Joined
Mar 26, 2019
Posts
223
Likes
67
Hi,

Take a look at the source code for our LBR Paint bar study. It is an example of changing the bar color based when specific conditions are met.

Our source files can be found here:

 

shzhning

New member
Joined
Aug 19, 2020
Posts
2
Likes
0
ok thanks for the reply. I followed SDK documentation and have installed Eclipse IDE, and downloaded MotiveWave Studies Zip file. I don't see LBR Paint Bar contained in the zip file. Can you please let me know where is it?

Thank you
 
Last edited:

SA1

Member
Joined
May 18, 2020
Posts
10
Likes
1
ok thanks for the reply. I followed SDK documentation and have installed Eclipse IDE, and downloaded MotiveWave Studies Zip file. I don't see LBR Paint Bar contained in the zip file. Can you please let me know where is it?

Thank you

Make sure you download the zip file from the Google Drive link on the forum post where the source code is. The file from the SDK page only has the sample projects. LBR Paint Bar is under the folder "com\motivewave\platform\study\custom"
 
Top