Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Complete Newbie, help converting old inicator into ninjatrader compatible format

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Complete Newbie, help converting old inicator into ninjatrader compatible format

    Hi guys, I was wondering if someone can help me convert on of my favourite indicators into a ninja-trader compatible format.

    The indicator itself is not propriety. I sent an email to my broker and asked if I can use the code and he said it was fine since dukascopy does not have any "premium" or paid indicators.
    The code below is from my forex platform with dukascopy.

    ---------------------------------------------------------------------

    /*
    * Dukascopy® Bank SA.
    * DUKASCOPY
    */
    package com.dukascopy.indicators;

    import com.dukascopy.api.indicators.DoubleRangeDescriptio n;
    import com.dukascopy.api.indicators.IIndicator;
    import com.dukascopy.api.indicators.IIndicatorContext;
    import com.dukascopy.api.indicators.IndicatorInfo;
    import com.dukascopy.api.indicators.IndicatorResult;
    import com.dukascopy.api.indicators.InputParameterInfo;
    import com.dukascopy.api.indicators.IntegerRangeDescripti on;
    import com.dukascopy.api.indicators.OptInputParameterInfo ;
    import com.dukascopy.api.indicators.OutputParameterInfo;

    import java.awt.Color;

    /**
    * @author Sergey Vishnyakov
    */
    public class Trendridersma implements IIndicator {
    private IIndicator sma;
    private int timePeriod = 14;
    private double deviation = 0.1;

    private IndicatorInfo indicatorInfo;
    private InputParameterInfo[] inputParameterInfos;
    private OutputParameterInfo[] outputParameterInfos;
    private OptInputParameterInfo[] optInputParameterInfos;
    private double[][][] inputs = new double[1][][];
    private double[][] outputs = new double[2][];

    public void onStart(IIndicatorContext context) {
    sma = context.getIndicatorsProvider().getIndicator("sma" );

    indicatorInfo =
    new IndicatorInfo("Trendridersma", "Trend Rider sma", "Overlap Studies", true, false, true, 1, 2, 2);
    indicatorInfo.setRecalculateAll(true);
    inputParameterInfos = new InputParameterInfo[]{new InputParameterInfo("Price", InputParameterInfo.Type.PRICE)};
    optInputParameterInfos =
    new OptInputParameterInfo[]{new OptInputParameterInfo("Time Period", OptInputParameterInfo.Type.OTHER, new IntegerRangeDescription(timePeriod, 1, 20000, 1)), new OptInputParameterInfo("Deviation", OptInputParameterInfo.Type.OTHER, new DoubleRangeDescription(deviation, 0.01, 100, 0.01, 2))};
    outputParameterInfos =
    new OutputParameterInfo[]{new OutputParameterInfo("Low Band", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE) {
    {
    setColor(Color.BLUE);
    setGapAtNaN(true);
    }
    }, new OutputParameterInfo("High Band", OutputParameterInfo.Type.DOUBLE, OutputParameterInfo.DrawingStyle.LINE) {
    {
    setColor(Color.ORANGE);
    setGapAtNaN(true);
    }
    },};
    }

    public IndicatorResult calculate(int startIndex, int endIndex) {
    //calculating startIndex taking into account lookback value
    if (startIndex - getLookback() < 0) {
    startIndex -= startIndex - getLookback();
    }
    if (startIndex > endIndex) {
    return new IndicatorResult(0, 0);
    }

    sma.setInputParameter(0, inputs[0][1]);
    sma.setOptInputParameter(0, timePeriod);
    double[] dsma = new double[endIndex - startIndex + 1 + sma.getLookback()];
    sma.setOutputParameter(0, dsma);

    IndicatorResult dSmaResult = sma.calculate(startIndex - 1, endIndex);

    double[] valuesHigh = new double[dSmaResult.getNumberOfElements()];
    double[] valuesLow = new double[dSmaResult.getNumberOfElements()];
    double[] trend = new double[dSmaResult.getNumberOfElements()];
    int i, k;
    for (i = 1, k = dSmaResult.getNumberOfElements(); i < k; i++) {
    //Inputs: 0 open, 1 close, 2 high, 3 low, 4 volume
    //outputs[0][i - 1] = Double.NaN;
    //outputs[1][i - 1] = Double.NaN;
    valuesLow[i -1] = (1 - deviation / 100) * dsma[i -1];
    valuesHigh[i -1] = (1 + deviation / 100) * dsma[i -1];
    if (i > 0) {
    trend[i] = trend[i-1];
    }

    if (inputs[0][1][i - 1 + getLookback()] > valuesHigh[i - 1]) {
    trend[i] = 1;
    }
    if (inputs[0][1][i - 1+ getLookback()] < valuesLow[i - 1]) {
    trend[i] = -1;
    }
    if (trend[i] > 0) {
    outputs[1][i - 1] = Double.NaN;
    if (i > 1 && valuesLow[i -1] < valuesLow[i - 2]) {
    valuesLow[i - 1] = valuesLow[i - 2];
    }
    outputs[0][i - 1] = valuesLow[i -1];
    } else {
    outputs[0][i - 1] = Double.NaN;
    if (i > 1 && valuesHigh[i -1] > valuesHigh[i - 2]) {
    valuesHigh[i -1] = valuesHigh[i - 2];
    }
    outputs[1][i - 1] = valuesHigh[i-1];
    }
    }
    return new IndicatorResult(startIndex, i - 1);
    }

    public IndicatorInfo getIndicatorInfo() {
    return indicatorInfo;
    }

    public InputParameterInfo getInputParameterInfo(int index) {
    if (index <= inputParameterInfos.length) {
    return inputParameterInfos[index];
    }
    return null;
    }

    public int getLookback() {
    return (sma.getLookback() + 1);
    }

    public int getLookforward() {
    return 0;
    }

    public OutputParameterInfo getOutputParameterInfo(int index) {
    if (index <= outputParameterInfos.length) {
    return outputParameterInfos[index];
    }
    return null;
    }

    public void setInputParameter(int index, Object array) {
    inputs[index] = (double[][]) array;
    }

    public void setOutputParameter(int index, Object array) {
    switch (index) {
    case 0:
    outputs[index] = (double[]) array;
    break;
    case 1:
    outputs[index] = (double[]) array;
    break;
    default:
    throw new ArrayIndexOutOfBoundsException(index);
    }
    }

    public OptInputParameterInfo getOptInputParameterInfo(int index) {
    if (index <= optInputParameterInfos.length) {
    return optInputParameterInfos[index];
    }
    return null;
    }

    public void setOptInputParameter(int index, Object value) {
    switch (index) {
    case 0:
    timePeriod = (Integer) value;
    sma.setOptInputParameter(0, timePeriod);
    break;
    case 1:
    deviation = (Double) value;
    break;
    default:
    throw new ArrayIndexOutOfBoundsException(index);
    }
    }
    }

    #2
    fisma028, welcome to our forums - do you have perhaps a screenshot of your study at work? I'm pretty sure it might already exist in our sharing potentially, so you'd have a good starting base here.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Hello, thanks for the response.

      I've posted a screenshot example below.

      In the image, there are 3 separate indicators showing.

      The white line in the center is a simple moving average (20 period)

      The black line is a moving average envelope (20 period, 0.5 deviations). This indicator is available on ninjatrader.

      But the indicator im interested in (with code provided above) is the thick blue and red lines.

      They are also simple moving average (20 period, 0.5 deviations).

      I'm not sure exactly what rule is being used (hopefully the code can enlighten us), but it seems to be something to do with candles opening or closing outside the envelopes, and some other filter.

      Please see image.

      (PS, im looking for this indicator specifically based only on the simple moving average or exponential moving average. Ive searcehd the ninjatrader indicator bank and all the indicators similar to this are all based on the ATR , and not moving averages.)
      Thanks
      Attached Files

      Comment


        #4
        fisma028, thanks - one of our NinjaScript trainees will look into your request.
        BertrandNinjaTrader Customer Service

        Comment


          #5
          Thanks a lot!,

          Will I get a pm or should I keep checking back in this thread once in a while?

          Thanks

          Comment


            #6
            We will post it here in the thread once available, you should get an email notification for the thread updates then. Thanks
            BertrandNinjaTrader Customer Service

            Comment


              #7
              The indicator is a SMA with a channel. The channel width is calculated as a fixed percentage of the prior value of the SMA. In an uptrend the lower band of the channel is converted to a trailing stop. The trend change from uptrend to downtrend is triggered, when the lower band is touched, In a downtrend the upper band of the channel is converted to a trailing stop, The trend change from downtrend to uptrend is triggered when the upper band is touched.

              The same logic could be applied to Bollinger Bands or Keltner Channels.

              Comment


                #8
                Hello,

                Thank you for your patience. I've worked up an indicator that accomplishes what you are looking for (see attached). Just as in your screenshot, this indicator draws a moving average, then plots upper and lower bands based on an envelope percentage. In addition, the indicator includes upper and lower lines that follow the upper and lower bands (only moving in the right direction, never backwards), that will only display if the price last breached the upper or lower band.

                In addition, this indicator will allow you to specify a moving-average type in the Indicators window. You can select between SMA, EMA, HMA, TMA, TEMA, or WMA, and the envelopes and tracking lines will work the same way. You can adjust the envelope percentage and moving-average period in the Indicators window, as well.

                Below is a screenshot of this indicator applied to a chart:



                Please let me know if I can assist further.
                Attached Files
                Dave I.NinjaTrader Product Management

                Comment


                  #9
                  Davel - I thought I'd download this and see how you approached this.

                  Great coding! I learnt quite a bit from the way you did this.

                  Cheers.

                  Comment


                    #10
                    Wow Thanks a lot my friend!!

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by yertle, Yesterday, 08:38 AM
                    7 responses
                    28 views
                    0 likes
                    Last Post yertle
                    by yertle
                     
                    Started by bmartz, 03-12-2024, 06:12 AM
                    2 responses
                    20 views
                    0 likes
                    Last Post bmartz
                    by bmartz
                     
                    Started by funk10101, Today, 12:02 AM
                    0 responses
                    4 views
                    0 likes
                    Last Post funk10101  
                    Started by gravdigaz6, Yesterday, 11:40 PM
                    1 response
                    8 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by MarianApalaghiei, Yesterday, 10:49 PM
                    3 responses
                    10 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Working...
                    X