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

Referencing Indicator Values....

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

    Referencing Indicator Values....


    Ok, this one has probaly been past a couple of times.

    I want to store an indicator value and then reference this later on.
    I understand from the error that the CrossesOver() method expects a priceplot and not a double value.

    Ok fine, how do you solve this then?

    Within Metastock I can do the below in a hart beat but this obviously is not for beginners..

    Also apperciate links to sites where I can go through some source code that beginners can understand.

    Thanks,
    Robin

    protectedoverridevoid Initialize()
    {
    Add(ATR(AtrValue));
    CalculateOnBarClose = true;
    }
    ///<summary>
    /// Called on each bar update event (incoming tick)
    ///</summary>
    protectedoverridevoid OnBarUpdate()
    {
    // Condition set 1
    double dblPrevClose = Close[1];
    double dblAtrVal = ATR(20)[1];
    double dblTrigger = dblPrevClose + dblAtrVal;
    // if (Close[0] > dblTrigger)

    if (CrossAbove(Close[0], dblTrigger, 1)) --> This goes wrong!!
    {

    DrawArrowUp(
    "My up arrow" + CurrentBar, false,0, 0, Color.Lime);
    }
    }

    #2
    I believe you need to change this:

    if (CrossAbove(Close[0], dblTrigger, 1))

    to this:

    if (CrossAbove(Close, dblTrigger, 1))

    because you cannot specify a bar to make the cross like that.
    eDanny
    NinjaTrader Ecosystem Vendor - Integrity Traders

    Comment


      #3
      Referencing indicator values

      He Danny,

      That was it yes! Interesting how the error message in NT hints to another problem entirely :-)

      The magic of my programming does not seem to trigger any events on the charts to my surprise, can you hint on how to best debug / evaluate the logic?

      Thanks for the help.
      Robin

      Comment


        #4
        Probably because you are trying to reference a previous bar when none exists, when the bar is the first bar. Try adding this at the beginning of OnBarUpdate() section:

        if(CurrentBar < 1) return;
        eDanny
        NinjaTrader Ecosystem Vendor - Integrity Traders

        Comment


          #5
          Referencing indicator values

          Think you are in the right direction again.

          I found the Print() statement to return all the values in the strategy. The output will return all values loaded in the chart except for the last bar.

          I added the statement from the previous mail but that does not invoke anything.

          I am using a daily historical barchart btw for a stock AAPL.

          Think I have still missed something..

          Comment


            #6
            Robin, are there any error in the logs (right-most tab of Control Center)? Are you sure you placed the code provided by eDanny at the very top of OnBarUpdate()? That is a fairly common error and it needs to look exactly like this. Also, try a value of 2 instead of 1 for the CurrentBar check:
            Code:
            protected override void OnBarUpdate()
            {
                if (CurrentBar < 2)
                    return;
            
                // all your other code goes below the current bar check
            }
            AustinNinjaTrader Customer Service

            Comment


              #7
              Pretty sure its OK, below is how it looks now.

              I changed the coding a little to see if the statments would actually return 'True' using the boolean variable.

              Attached is the output window and the stock I am looking at.

              When the stock gaps up, the statement is 'True' which indeed is correct so a signal should be triggered.

              Checked the log, and nothing like an error statement is given anyware.

              I did another simple strategy using the strategy builder, not changing the code.

              The trigger is if the arroon oscillator crosses above 0, print a dot, same for the reverse. Also this does not produce any signals while for a fact I can see these events happen. You can verify this from the stock in the screenshot with strategy attached...

              I am starting to believe it is the setup somehow, only 1 strategy produces signals on this end which is the SampleMaCrossover that can be viewed in the last screenshot.

              I guess I am not able to create something myself in sim mode right? I am using the Yahoo finance data connection to generate the charts if that helps.


              current coding.

              protectedoverridevoid OnBarUpdate()
              {

              if(CurrentBar < 2) return; --> Tried with 2, no change..

              // Condition set 1
              double dblPrevClose = Close[1];
              double dblAtrVal = ATR(20)[1];
              double dblTrigger = dblPrevClose + dblAtrVal;
              bool blnTrigger = (Close[0] > dblTrigger);

              Print(dblPrevClose +
              " " + dblAtrVal + " " + dblTrigger + " " + Close + " " + blnTrigger);

              //if(Close[1]<dblTrigger[1])
              if(Close[0] > dblTrigger);
              //if (CrossAbove(Close, dblTrigger,0))
              {

              DrawArrowUp(
              "My up arrow" + CurrentBar, false,0, 0, Color.Lime);
              }
              }
              Attached Files

              Comment


                #8
                leduc, you can create strategies without even being connected to a data source so that isn't an issue.

                What I did notice; however, is that your drawing objects are drawn at a price of 0. That means the draw object will not appear on your chart unless the instrument you're viewing trades at 0 (highly unlikely).

                For DrawArrow, please try something like this:
                Code:
                [FONT=Courier New][SIZE=2][SIZE=2][FONT=Courier New]DrawArrowUp([/FONT][/SIZE][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]"My up arrow"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] + CurrentBar, [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2],[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2], [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]Low[0] - 3 * TickSize[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2], Color.Lime);[/SIZE][/FONT][/SIZE][/FONT]
                AustinNinjaTrader Customer Service

                Comment


                  #9
                  Makes alot of sence now you tell me :-) I blindly copy pasted the code from another strategy without looking at it closely.

                  This hint + a slight revision to the coding has finaly made NT to draw the arrows correctly


                  Coding is now like below, I am positing it for NT programming newbees like yours truly to reference later.

                  Many thanks for all the help guys, you made it work in the end!!

                  One last question though... Is it always nessecary to explicitly plot the indicators used by the startegy (ATR in this case) when initializing or can this be ignored / done differently?




                  Coding that works :

                  protectedoverridevoid OnBarUpdate()
                  {

                  if(CurrentBar < 1) return;


                  // Condition set 1
                  double dblPrevClose = Close[1];
                  double dblAtrVal = ATR(20)[1];
                  double dblTrigger = dblPrevClose + dblAtrVal;

                  if (CrossAbove(Close, dblTrigger,1))
                  {
                  DrawArrowUp(
                  "My up arrow" + CurrentBar, false,0, Low[0] - 3 * TickSize , Color.Lime);
                  }
                  }

                  Comment


                    #10
                    Great to hear you got it working - correct for strategies you would need to add the indicators to be plotted by it in the Initialize() - plot customization options are included in those tips below as well -

                    BertrandNinjaTrader Customer Service

                    Comment


                      #11
                      Very Good! Thanks for helping out.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by frankthearm, Today, 09:08 AM
                      9 responses
                      32 views
                      0 likes
                      Last Post NinjaTrader_Clayton  
                      Started by NRITV, Today, 01:15 PM
                      2 responses
                      9 views
                      0 likes
                      Last Post NRITV
                      by NRITV
                       
                      Started by maybeimnotrader, Yesterday, 05:46 PM
                      5 responses
                      26 views
                      0 likes
                      Last Post NinjaTrader_ChelseaB  
                      Started by quantismo, Yesterday, 05:13 PM
                      2 responses
                      20 views
                      0 likes
                      Last Post quantismo  
                      Started by adeelshahzad, Today, 03:54 AM
                      5 responses
                      33 views
                      0 likes
                      Last Post NinjaTrader_BrandonH  
                      Working...
                      X