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

reference value from indicator in strategy

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

    reference value from indicator in strategy

    HI i am able to print values from indicaotr but I need to print what trend, is it bullish or bearish. I need to find either Trend is red or Trend is Green

    Print("Trend"+halftrendInd.Trend[0]);
    Print("LowerChannel"+halftrendInd.LowerChannel[0]);
    Print("UpperChannel"+halftrendInd.UpperChannel[0]);

    indicator plots Trend as eiher red or green.
    Can

    ​​

    #2
    Hello,

    If you want to determine the color of the trend (red or green) directly from the HalfTrend indicator, which is used to signify whether the trend is bullish or bearish, and this color logic is embedded within the indicator's plotting logic, you'll need to refer to the implementation of the indicator itself to understand how these colors are assigned.

    Here's a step-by-step approach to handle this scenario, assuming you do not have the ability to modify the indicator's code directly:

    1. Understanding the Indicator's Color Logic:
    Indicators like HalfTrend typically assign plot colors within their internal logic based on certain conditions—often related to the direction of the trend or crossing of certain levels. You'll need to know these conditions to replicate or interpret them in your strategy.

    Example Logic for Plot Coloring:
    If the trend color changes based on certain directional changes, it might look something like this inside the indicator:
    Code:
    if (currentValue > previousValue)
        PlotBrushes[0][0] = Brushes.Green;  // Bullish
    else
        PlotBrushes[0][0] = Brushes.Red;    // Bearish
    ​
    2. Replicating Indicator Conditions in Your Strategy:
    Since NinjaTrader does not expose plot colors directly to strategies, you'll need to replicate the condition logic that the indicator uses to set the color. This means writing similar conditions in your strategy script based on the indicator's trend values:
    Code:
    protected override void OnBarUpdate()
    {
        double trendValue = halftrendInd.Trend[0];
        double prevTrendValue = halftrendInd.Trend[1];
    
        // Check trend direction based on assumed indicator color logic
        if (trendValue > prevTrendValue)
        {
            Print("Trend is Green (Bullish)");
        }
        else if (trendValue < prevTrendValue)
        {
            Print("Trend is Red (Bearish)");
        }
        else
        {
            Print("Trend is Unchanged");
        }
    
        // Print other indicator values
        Print("LowerChannel: " + halftrendInd.LowerChannel[0]);
        Print("UpperChannel: " + halftrendInd.UpperChannel[0]);
    }
    ​
    Please let us know if you have any other questions.
    Ryan S.NinjaTrader Customer Service

    Comment


      #3
      Hi if i simply put this line into the strategy it compains trendvalue doesnt exists and I need to impor the whole indicator logic into the strategy.
      trendValue > prevTrendValue

      I was looking if there is a way to reference from indicator that is already there in the strategy.

      Comment


        #4
        Thank you I think i got it now

        Comment


          #5
          HI again.
          how can i code it so that my condition for entry not when switch happes trendValue > prevTrendValue and downTrend = true;


          if (downTrend && High[0] > halftrendInd.UpperChannel[0])

          in other words i need a bool for downTrend at all times when indicator is showing downtrend

          Comment


            #6
            Hello,

            To implement your condition where downTrend is true whenever the indicator is showing a downtrend, you'll need to define how the downtrend is determined by your indicator, which seems to involve the halftrendInd.UpperChannel in your provided condition.

            Assuming halftrendInd is your indicator that has properties like UpperChannel and possibly LowerChannel, which might indicate an uptrend or downtrend, let’s set up a boolean flag downTrend that updates based on the trend direction indicated by your halftrendInd. You can then use this boolean for your entry condition logic.

            Here’s how you might code this logic in NinjaTrader:
            1. Define a Boolean Variable: First, declare a boolean variable downTrend at the class level.
            2. Determine Downtrend Logic: You’ll need to specify exactly how a downtrend is determined by your indicator. Assuming a simple logic where the current price being below a certain channel indicates a downtrend.
            3. Update the Boolean Based on Indicator Values: In OnBarUpdate(), update downTrend based on your indicator's output.
            4. Use the Boolean for Entry Logic: Use the downTrend boolean in your condition to check for trade entries.

            Here's a sample implementation assuming halftrendInd provides the UpperChannel and that you have a way of determining when the market is in a downtrend:

            Code:
            private bool downTrend = false;
            
            protected override void OnBarUpdate()
            {
                // Assuming halftrendInd is initialized and is an instance of an indicator
                // Update downtrend status based on some condition, for example:
                // If the close is below the upper channel, it might be considered a downtrend
                downTrend = Close[0] < halftrendInd.UpperChannel[0];
            
                // Entry condition
                if (downTrend && High[0] > halftrendInd.UpperChannel[0])
                {
                    // Place entry logic here
                    EnterShort("EnterShortDowntrend"); // Example entry for a short position
                }
            }
            ​
            Additional Considerations:
            • Indicator Initialization: Ensure halftrendInd is properly initialized. If it’s a custom indicator, you need to instantiate it correctly in your strategy, typically in the State changes to State.Configure or State.SetDefaults.
            • Accuracy of Downtrend Detection: The logic to determine a downtrend should be accurate and reflective of your trading strategy's requirements. Adjust the condition to determine downTrend as necessary based on the actual indicator’s outputs or other market conditions.
            • Execution and Order Management: Consider the implications of how orders are managed and ensure you have adequate protections like stop losses or checks for existing positions before entering new trades.

            Make sure to test this logic in a simulated environment to validate that it behaves as expected with your specific indicator and market conditions.
            Ryan S.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Skifree, Today, 11:47 AM
            1 response
            7 views
            0 likes
            Last Post Skifree
            by Skifree
             
            Started by ETFVoyageur, Today, 10:34 AM
            3 responses
            11 views
            0 likes
            Last Post ETFVoyageur  
            Started by warpinator, 05-16-2024, 10:44 AM
            6 responses
            46 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by StefanA, Today, 11:24 AM
            2 responses
            7 views
            0 likes
            Last Post StefanA
            by StefanA
             
            Started by Austiner87, Today, 11:19 AM
            1 response
            5 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Working...
            X