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

Accessing value from custom indicator from strategy

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

    Accessing value from custom indicator from strategy

    I have a custom indicator that returns a value. How do I access that value from my strategy?

    #2
    imported post

    What do you mean by "returns a value"? The OnBarUpdate method of an indicator has no return value.

    What you probably want to do is calculating a value which you want to make accessible in a strategy. You then need to define a plot series in the indicator wizard, change the OnBarUpdate method of the generated code. Your plot series then is accessible by a strategy.

    I suggest strating of wizard generated code and not try to code free-hand.

    Comment


      #3
      imported post

      Bad choice of words -> "returns a value" it doesn't actually RETURN a value.

      I guess what I need to do is create a UserDefinedMethod(). Correct?

      Here's the code from my indicator that I would like accessible.


      protected
      overridevoid OnBarUpdate()

      {

      if (CurrentBar == 0){

      Value.Set(
      0);

      }
      else{

      double d = 0;

      d =Math.Abs((CCI(Close, T)[
      0] - CCI(Close, M)[0]));

      Mplot.Set(d);

      }



      }

      Comment


        #4
        imported post

        Please be as specific as possible when you report an problem: What is the problem you get? Do you get an error message? Where?

        Speculating: If you have generated "Mplot" by the indicator wizard, then you can access the value by <YourIndicatorsName>.Mplot[0].

        Comment


          #5
          imported post

          Ok, it is a custom indicator created with the wizard. It works fine. It's called MyDiffCCI(). Plots correctly in the DataBox(which is the whole reason I needed to create it as a seperate indicator). Now, when I try to access it via MyDiffCCI.Mplot[0]; I get this error:

          'NinjaTrader.Strategy.Strategy.MyDiffCCI(int, int)' is a 'method', which is not valid in the given context

          This is all I'm doing:

          Print ("Mplot "+MyDiffCCI.Mplot[0]);

          Comment


            #6
            imported post

            Your indicator has 2 integer parameters which you need to apply:
            Print ("Mplot "+MyDiffCCI(<param1>, <param2>).Mplot[0]);

            You need to set actual values for these parameters.

            Comment


              #7
              imported post

              Ok, I got it. Thanks.

              Comment


                #8
                imported post

                1. Ok, since I'm not really plotting anything but using the value in my strategy calculations, I see "Dummy" in the DataBox instead of "Mplot", which I would like to see. How do I correct that?

                2. Also, in the indicator itself, can you set the "AutoScale", and which panel to display in via NinjaScript? Or is it only accessible via the indicator properties window?

                ie:

                AutoScale = false;

                DisplayInDataBox = true;

                Panel = 1;

                etc.

                Comment


                  #9
                  imported post

                  1) As suggested below I recommend you go through the wizard which allows you to set the explicit plot name you want to see. Changing the code manually could be done but is prone to errors.

                  2) Set Overlay property to determine if the indicator should be displayed on the chart panel or on a seperate panel. The docs hold a good explanation for this property.

                  Comment


                    #10
                    Need Help - Stop Loss and problem

                    Hallo.
                    I'm new Ninja user. I alredy developed my simple strategy and strategy works well until I tryed to modify stop loss price to breakeven. I used the sample shown here: http://www.ninjatrader-support.com/v...ead.php?t=3222

                    Now strategy when moves stop loss to breakeven and then reverse postition do not reset stop loss to original position and place stop order with the price from former turn. This price is over the market price what is case of error and strategy termination.
                    Can anybody help me to explan where the problem is???

                    Strategy has condition:
                    if (Position.MarketPosition == MarketPosition.Flat)
                    {
                    SetStopLoss(CalculationMode.Ticks, stoplossticks);
                    }
                    but looks it does not work????

                    Rgds
                    Czarek

                    My strategy below:
                    //
                    // Copyright (C) 2007, NinjaTrader LLC <www.ninjatrader.com>.
                    // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
                    //
                    #region Using declarations
                    using System;
                    using System.ComponentModel;
                    using System.Diagnostics;
                    using System.Drawing;
                    using System.Drawing.Drawing2D;
                    using System.Xml.Serialization;
                    using NinjaTrader.Cbi;
                    using NinjaTrader.Data;
                    using NinjaTrader.Indicator;
                    using NinjaTrader.Strategy;
                    #endregion
                    // This namespace holds all strategies and is required. Do not change it.
                    namespace NinjaTrader.Strategy
                    {
                    /// <summary>
                    /// Sample strategy using StopLoss and ProfitTarget orders.
                    /// </summary>
                    [Description("Sample strategy using StopLoss and ProfitTarget orders.")]
                    public class SamplePriceModification : Strategy
                    {
                    #region Variables
                    private int stoplossticks = 20;
                    private int profittargetticks = 100;
                    #endregion
                    /// <summary>
                    /// This method is used to configure the strategy and is called once before any strategy method is called.
                    /// </summary>
                    protected override void Initialize()
                    {
                    /* There are several ways you can use SetStopLoss and SetProfitTarget. You can have them set to a currency value
                    or some sort of calculation mode. Calculation modes available are by percent, price, and ticks. SetStopLoss and
                    SetProfitTarget will submit real working orders unless you decide to simulate the orders. */
                    SetStopLoss(CalculationMode.Ticks, stoplossticks);
                    SetProfitTarget(CalculationMode.Ticks, profittargetticks);

                    CalculateOnBarClose = true;
                    }
                    /// <summary>
                    /// Called on each bar update event (incoming tick)
                    /// </summary>
                    protected override void OnBarUpdate()
                    {
                    // Resets the stop loss to the original value when all positions are closed
                    if (Position.MarketPosition == MarketPosition.Flat)
                    {
                    SetStopLoss(CalculationMode.Ticks, stoplossticks);
                    }

                    // If a long position is open, allow for stop loss modification to breakeven
                    else if (Position.MarketPosition == MarketPosition.Long)
                    {
                    // Once the price is greater than entry price+50 ticks, set stop loss to breakeven
                    if (Close[0] > Position.AvgPrice + 50 * TickSize)
                    {
                    SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                    }
                    }

                    // Condition set 1
                    if (Close[1] < Close[0]
                    && Close[2] < Close[0]
                    && Close[3] < Close[0]
                    && Close[4] < Close[0]
                    && Close[5] < Close[0]
                    && Close[6] < Close[0]
                    && Close[7] < Close[0]
                    && Close[8] < Close[0]
                    && Close[9] < Close[0]
                    && Close[10] < Close[0]
                    && Open[0] < Close[0]
                    && Open[1] < Close[0]
                    && Open[2] < Close[0]
                    && Open[3] < Close[0]
                    && Open[4] < Close[0]
                    && Open[5] < Close[0]
                    && Open[6] < Close[0]
                    && Open[7] < Close[0]
                    && Open[8] < Close[0]
                    && Open[9] < Close[0]
                    && Open[10] < Close[0])
                    {
                    EnterLong(1, "");
                    }
                    // Condition set 2
                    if (Close[1] > Close[0]
                    && Close[2] > Close[0]
                    && Close[3] > Close[0]
                    && Close[4] > Close[0]
                    && Close[5] > Close[0]
                    && Close[6] > Close[0]
                    && Close[7] > Close[0]
                    && Close[8] > Close[0]
                    && Close[9] > Close[0]
                    && Close[10] > Close[0]
                    && Open[0] > Close[0]
                    && Open[1] > Close[0]
                    && Open[2] > Close[0]
                    && Open[3] > Close[0]
                    && Open[4] > Close[0]
                    && Open[5] > Close[0]
                    && Open[6] > Close[0]
                    && Open[7] > Close[0]
                    && Open[8] > Close[0]
                    && Open[9] > Close[0]
                    && Open[10] > Close[0])


                    {
                    EnterShort(1, "");

                    }
                    }
                    #region Properties
                    /// <summary>
                    /// </summary>
                    [Description("Numbers of ticks away from entry price for the Stop Loss order")]
                    [Category("Parameters")]
                    public int StopLossTicks
                    {
                    get { return stoplossticks; }
                    set { stoplossticks = Math.Max(0, value); }
                    }

                    /// <summary>
                    /// </summary>
                    [Description("Number of ticks away from entry price for the Profit Target order")]
                    [Category("Parameters")]
                    public int ProfitTargetTicks
                    {
                    get { return profittargetticks; }
                    set { profittargetticks = Math.Max(0, value); }
                    }


                    #endregion
                    }
                    }

                    Comment


                      #11
                      First you would need to address issues outlined in this tip: http://www.ninjatrader-support.com/v...ead.php?t=3170
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #12
                        Link doesn't go anywhere.

                        I was trying to review the link referred to by Czarek in post #10, but it doesn't seem to go anywhere. Does anybody know if the link has changed?
                        Thank you,
                        Jeff

                        Comment


                          #13
                          Go to NinjaTrader Support Forum > NinjaScript Educational Resources Reference Samples


                          Last topic:Strategy: Modifying the price of stop loss and profit target orders
                          Czarek

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by nandhumca, Today, 03:41 PM
                          0 responses
                          1 view
                          0 likes
                          Last Post nandhumca  
                          Started by The_Sec, Today, 03:37 PM
                          0 responses
                          3 views
                          0 likes
                          Last Post The_Sec
                          by The_Sec
                           
                          Started by GwFutures1988, Today, 02:48 PM
                          1 response
                          5 views
                          0 likes
                          Last Post NinjaTrader_Clayton  
                          Started by ScottWalsh, 04-16-2024, 04:29 PM
                          6 responses
                          32 views
                          0 likes
                          Last Post ScottWalsh  
                          Started by frankthearm, Today, 09:08 AM
                          10 responses
                          36 views
                          0 likes
                          Last Post frankthearm  
                          Working...
                          X