SDK question about stochastics - how to get previous bar value

a_edwall

Active member
Joined
May 22, 2019
Posts
28
Likes
7
I am taking the code from StochasticFull.java and using it in order to generate something I need. What I want to do is determine when the %K value for this current bar is greater than the %K of the previous bar. But I cannot seem to find the answer to this problem.

Here is the code:

var series = ctx.getDataSeries();
int kPeriod = getKPeriod();
Double K = series.stochasticK(index, kPeriod);
series.setDouble(index, Values.K, K);

int maPeriod = getKPPeriod();
int signalPeriod = getDPeriod();

if (index < kPeriod + maPeriod) return;

var method = getSettings().getMAMethod(Inputs.METHOD);
Double pk = series.ma(method, index, maPeriod, Values.K);
series.setDouble(index, Values.P_K, pk);

if (index < kPeriod + maPeriod + signalPeriod) return;

Double signal = series.ma(method, index, signalPeriod, Values.P_K);
series.setDouble(index, Values.P_D, signal);

if (!series.isBarComplete(index) || signal == null) return;

pk is apparently the array that holds the %K values. What I want to do is generate a marker when %K(this_bar) > %K(previous_bar) And %K < 25 And %D < 50.

I have this: if (pk > ????? && pk < 25 && signal < 50), but everything I have tried has failed.

Can anyone help me, please? Thank you.
 
The number pk is actually a moving average, and is being calculated as follows:
1630394046585.png

(I get that info from the SDK API overview: Overview)

The code then 'stores' this value onto the DataSeries with this line:
Java:
series.setDouble(index, Values.P_K, pk);

A value 'P_K' now exists at that particular index (which is actually the number of the bar it corresponds to), so you can get the value of the previous bar like so:

Java:
double PrevVal = series.getDouble(index-1, Values.P_K);



And then check that value against the previous one:

Java:
if (pk > PrevVal)

{

// do all sorts of stuff

}

else {

//do other stuff

}




I know all too well that getting snippets like this one to work the way you want can be challenging, so I kindly remind you of the power of debug-statements:

Java:
debug ("Value pk = " + pk + " Value PrevVal = " + PrevVal);

Drop that line right above the beginning of your if-statement to visually see what's going on. Debug is outputted to the 'Study Log', in MW's 'view'-menu:

1630394906216.png

Good luck !! :)
 
Thank you for all your good information. The debug notes will be VERY helpful as I have not yet learned how to "single-step" through code to check it.

Also, thanks for your all your other info. In the wee hours after I wrote, I somehow stumbled upon this, which seems to work.

Code:
    var method = getSettings().getMAMethod(Inputs.METHOD);

    Double pk1 = series.ma(method, index - 1, maPeriod, Values.K);
    
    series.setDouble(index - 1, Values.P_K, pk1);                                 //this was key

    Double pk = series.ma(method, index, maPeriod, Values.K);        //this was key
    
    series.setDouble(index, Values.P_K, pk);

    if (index < kPeriod + maPeriod + signalPeriod) return;

    Double signal = series.ma(method,  index, signalPeriod, Values.P_K);

    series.setDouble(index, Values.P_D, signal);

    if (!series.isBarComplete(index) || signal == null) return;

    //the marker is one bar to the right of where it should be - how do I fix that?
    var c3 = new Coordinate(series.getStartTime(index), signal);        //THE FIX - check for signal/marker events
    
    if (pk > pk1 && pk < 26 && signal < 50)
    {
      var marker = getSettings().getMarker(Inputs.UP_MARKER);
      String msg = "";
      if (marker.isEnabled()) addFigure(new Marker(c3, Enums.Position.BOTTOM, marker, msg));
    }

When I have recovered from my ordeal, I will give your code a shot and see if it does the same thing.

Thank you for your help.
 
Dear Spin,

I made a copy of my file and renamed it. Then I inserted your code and the two scripts display the same results. I don't know why it took me so long to get something that worked. Thank you for your help. Now I have a good example to draw upon when needed later. Thank you.

And the debug command worked like a charm - thanks again for the great tip.
 
Top