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

sum iff coding question

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

    sum iff coding question

    I have a code snippet from Tradingview. There is a code block starting with sum(iff()). How can a translate this into a Ninja c# code?

    xValue = (close + 2 * close[1] + 2 * close[2] + close[3] ) / 6

    CU23 = sum(iff(xValue > xValue[1], xValue - xValue[1], 0), Length)
    CD23 = sum(iff(xValue < xValue[1], xValue[1] - xValue, 0), Length)

    Would the following be similar in c#?

    private Series<double> CU23 = 0.00;
    private Series<double> CD23 = 0.00;
    private Series<double> xValue = 0.00;
    int rsiPeriod = 10;

    xValue[0] = (Close[0] + 2 * Close[1] + 2 * Close[2] + Close[3]) / 6;

    for (int i = 0; i <rsiPeriod; i++)
    {
    if (xValue[0] > xValue[1])
    CU23[0] += xValue[0] - xValue[1];

    if (xValue[0] < xValue[1])
    CD23[0] += xValue[1] - xValue[0];
    }

    Thanks!
    Last edited by Sweet&Sour; 07-27-2018, 05:48 AM.

    #2
    Hello Sweet&Sour,

    Thank you for the post.

    It looks like the definition of the Series<double> that you have made needs corrected, you can find examples here: https://ninjatrader.com/support/help...us/seriest.htm

    Code:
    private Series<double> CU23;
    
    protected override void OnStateChange() 
    {
        if (State == State.DataLoaded)
        {
           CU23= new Series<double>(this, MaximumBarsLookBack.Infinite);
        }
    }
    Each of the series needs to be defined like the above.


    The following calculation looks as if you have correctly converted it:
    Code:
    xValue[0] = (Close[0] + 2 * Close[1] + 2 * Close[2] + Close[3]) / 6;
    Regarding the iff, it looks like this takes a condition and returns one of two supplied inputs. In what you provided, it appears this should be returning a double:

    Code:
    CU23 = sum(iff(xValue > xValue[1], [B]xValue - xValue[1][/B], [B]0[/B]), Length)
    I am a little confused on what happens next in this syntax, does the iff return a series or double here? It is noted in the tradingview docs sum takes a series: https://www.tradingview.com/study-sc...rence/#fun_sum

    Code:
    CU23 = [B]sum([/B]iff(xValue > xValue[1], xValue - xValue[1], 0[B])[/B], Length)
    Can you detail, is the result of the iff a series, or a double value that is being provided to sum?

    The for loop you have created is also likely not going to match the original syntax. You would likely need to do something similar to

    Code:
    //define and create two new series as temporary storage for the calculation you are doing
    //this sample is not complete, please see the help guide for a complete sample of creating a series. 
    private Series<double> tempStorage1; // used for CD23
    private Series<double> tempStorage2;
    
    
    xValue[0] = (Close[0] + 2 * Close[1] + 2 * Close[2] + Close[3]) / 6;
    
    if (xValue[0] > xValue[1])
    tempStorage1[0] = xValue[0] - xValue[1];
    
    if (xValue[0] < xValue[1])
    tempStorage1[0] = xValue[1] - xValue[0];
    
    CD23[0] = Sum(tempStorage1, rsiPeriod)[0];

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi Jesse,

      Thanks for your effort.
      Just some background information. The code is based on "The RSI Smoothed by John F. Ehlers" from TASC Oct 2002. Unfortunately, there isn't any Ninja code available at this time however having a look at the EASYLANGUAGE code written in the article itself may help for translation.

      Code:
      {****************************************************
      Smoothed Relative Strength Index (SRSI)
      Copyright (c) 2001 MESA Software
      *****************************************************}
      Inputs: Len(10);
      Vars: count(0),
      
      Smooth23(0),
      CU23(0),
      CD23(0),
      SRSI(0);
      
      Smooth23 = (Close + 2*Close[1] + 2*Close[2] + Close[3])/6;
      
      CU23 = 0;
      CD23 = 0;
      
      For count = 0 to Len - 1 begin
        If Smooth23[count] > Smooth23[count + 1] then 
          CU23 = CU23 + Smooth23[count] - Smooth23[count + 1];
      
        If Smooth23[count] < Smooth23[count + 1] then 
          CD23 = CD23 + Smooth23[count + 1] - Smooth23[count];
      end;
      
      If CU23 + CD23 <> 0 then 
        SRSI = CU23/(CU23 + CD23);
      
      Plot1(SRSI, “SRSI”);
      For me it looks like all 3 variables are based on a Series<double> data type.
      Based on Ehlers EASYLANGUAGE code i made the following Ninja translation:

      Code:
      private Series<double> xValue; 
      private Series<double> cu23; 
      private Series<double> cd23; 
      
      if (State == State.SetDefaults)
      {
        Period = 10;
        Peak = 0.9;
        Valley = 0.1;
      
        AddPlot(new Stroke(Brushes.DarkOrange, 2), PlotStyle.Line, "SmoothRsi");
        AddLine(Brushes.DeepSkyBlue, 0.1, "Lower");
        AddLine(Brushes.DeepSkyBlue, 0.9, "Upper");
      
      }
      else if (State == State.DataLoaded)
      {
          xValue = new Series<double>(this, MaximumBarsLookBack.Infinite);
          cu23 = new Series<double>(this, MaximumBarsLookBack.Infinite);
          cd23 = new Series<double>(this, MaximumBarsLookBack.Infinite);
      }
      
      protected override void OnBarUpdate()
      {
        if (CurrentBar <= Period)
          return;
        
        // Here we start translate Ehlers sample code into Ninja code
        //Smooth23 = (Close + 2 * Close[1] + 2 * Close[2] + Close[3]) / 6;
        xValue[0] = (Close[0] + 2 * Close[1] + 2 * Close[2] + Close[3]) / 6;
      
        //For count = 0 to Len - 1 begin
        for (int count = 0; count <= Period - 1; count++)
        {
            //If Smooth23[count] > Smooth23[count + 1] then
            //    CU23 = CU23 + Smooth23[count] - Smooth23[count + 1];
            if (xValue[count] > xValue[count + 1])
                cu23[0] = xValue[count] - xValue[count + 1];
      
            //If Smooth23[count] < Smooth23[count + 1] then
            //  CD23 = CD23 + Smooth23[count + 1] - Smooth23[count];
            if (xValue[count] < xValue[count + 1])
                cd23[0] = xValue[count + 1] - xValue[count];
        }
      
        //If CU23 + CD23 <> 0 then
        // SRSI = CU23 / (CU23 + CD23);
        if (cu23[0] + cd23[0] != 0)
            SmoothRsi[0] = cu23[0] / (cu23[0] + cd23[0]);
        else
            SmoothRsi[0] = 0;
      }
      Thanks for your help.

      Comment


        #4
        Hello Sweet&Sour,

        Thank you for the reply.

        The new sample it looks like you have made very similar syntax to the tasc code and everything seems to be formed per the help guide. What was the result of your testing with this, was it similar or different from the tasc result in easy language?

        The task article results can vary from platform to platform due to how scripts are processed and data available. Are you currently able to test the tasc article in the other platform to compare it? If so in some cases, you may need to utilize Prints in both platforms if you have the capability to ensure the logic is working similarly and the same values are used for comparisons.

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by futtrader, 04-21-2024, 01:50 AM
        4 responses
        41 views
        0 likes
        Last Post futtrader  
        Started by Option Whisperer, Today, 09:55 AM
        1 response
        11 views
        0 likes
        Last Post bltdavid  
        Started by port119, Today, 02:43 PM
        0 responses
        7 views
        0 likes
        Last Post port119
        by port119
         
        Started by Philippe56140, Today, 02:35 PM
        0 responses
        7 views
        0 likes
        Last Post Philippe56140  
        Started by 00nevest, Today, 02:27 PM
        0 responses
        7 views
        0 likes
        Last Post 00nevest  
        Working...
        X