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

Show/Hide Chart Indicator based on Order/Trade

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

    Show/Hide Chart Indicator based on Order/Trade

    Is it possible to script a strategy that when I place an order, it will Show a specific chart indicator and when the trade is closed, Hide that indicator?

    #2
    Hello sastrades,

    Yes you can do that however it requires the straetg to be manually coded.

    The most simple way to do this would be to first make your strategy using the strategy builder so variables for the indicators use are made and then unlock the code. One you have the variables for the indicators you can toggle them using the IsVisible property. For example if you made a strategy that uses an SMA you would get code like this:


    Code:
    private SMA SMA1;
    
    
    else if (State == State.DataLoaded)
    {
        SMA1 = SMA(Close, 14);
    }

    If you wanted to toggle its visibility you would do:

    Code:
    ​SMA1.IsVisible = false; 
    or
    ​SMA1.IsVisible = true;
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thank you. I'm in the Strategy Builder, however, I don't follow your input.

      1) How do I conditional upon order filled, to then display the Indicator?

      2) Where would I add the visibility toggle?

      Click image for larger version

Name:	nt-strat-conditions.jpg
Views:	42
Size:	72.1 KB
ID:	1296691

      Comment


        #4
        Hello sastrades,

        This needs to be manually coded, my comment about the builder is just so that you can make conditions initially that involve the indicators you wanted to hide or show. Once you unlock the code you could use the IsVisible property for the indicators that were generated.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Jesse View Post
          Hello sastrades,

          This needs to be manually coded
          Ah... Would you please reference source for me to learn how to manually code a Strategy. Thanks.

          Comment


            #6
            I'm getting somewhere now. I have unlocked the strategy. Now, where in this code do I place the code you provided?

            Click image for larger version

Name:	strat-unlocked.jpg
Views:	39
Size:	96.2 KB
ID:	1296698

            Comment


              #7
              Hello sastrades,

              The best way to learn how to manually code strategies is to use the builder to make conditions and then click view code. Toggling the visibility of indicators would require unlocking the code so that can be one of the last steps that you do. I would suggest to start by creating the conditions you wanted using the indicators that you wanted in the strategy builder and then confirm the strategy works as you wanted. Once you are satisfied with the strategy results you can add the toggling of the indicators using the IsVisible property.

              JesseNinjaTrader Customer Service

              Comment


                #8
                I'm still not understanding this... In the builder, there is no condition for trade or OrderState.Filled, Below is a screenshot of what I created...

                1) If OrderState.Filled is the proper condition to check against, how would I change the existing code for this?

                2) Where does the IsVisible code go?

                Click image for larger version

Name:	strat-unlocked.jpg
Views:	37
Size:	131.8 KB
ID:	1296899

                Comment


                  #9
                  Hello sastrades,

                  In the builder you cannot see when orders fill, that would require manual coding so you can observe the execution events. In the builder you can instead check if the position is not flat to know you are in a position.

                  https://ninjatrader.com/support/helpGuides/nt8/index.html?strategybuilder_condition_builder.htm#H owToCreateMarketPositionComparisons

                  The IsVisible is a property of an indicator, that would come later after you have fully built the strategy and unlocked the code. At that point you would set the indicators IsVisible property to true or false depending on the condition that you want to toggle that. That would be similar to the sample provided in post 2.

                  If you are just getting started with the builder I would suggest using the strategy builder weibar as a first step, that goes over how to use each of the features and also make conditions that use indicators. The code that you have now is checking if the EMA is equal to the EMA however that would unlikely to be true due to how double numbers are stored in memory.




                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    Thank you... Getting closer.

                    How do I swap your code with the below 'Draw.Line' code? I could not find an example of adding EMA code.

                    Code:
                                 // Set 1
                                if (Position.MarketPosition != MarketPosition.Flat)
                                {
                                    Draw.Line(this, @"TEST Line_1", false, 0, 0, 0, 0, Brushes.CornflowerBlue, DashStyleHelper.Solid, 2);
                                }​

                    Comment


                      #11
                      Hello sastrades,

                      If you mean to toggle the indicators visibility you would use the code from post 2 in place of the Draw.Line code. You would remove the single line of code that starts with Draw.Line and replace it with the indicator variable you used. If you used the SMA the default variable for that is ​SMA1 so you would use ​SMA1.IsVisible = false; to show or hide the indicator. True will make it visible and fase will hide it. If you used a different indicator you need to look at the code and see what the variable name is for that indicator and use that instead of SMA1.


                      JesseNinjaTrader Customer Service

                      Comment


                        #12
                        Making some progress...

                        1) I'm still not understanding the 'Variables' aspect. How do I change the EMA1 code to be effective?

                        2) After changing out the code line at the bottom, I receive an error (screenshot attached)

                        Code:
                        {
                            public class Show5EMA : Strategy
                            {
                               private int EMA1;
                        
                        
                                protected override void OnStateChange()
                                {
                                    if (State == State.SetDefaults)
                                    {
                                        Description                                    = @"Show 5EMA upon trade open, chide upon close.";
                                        Name                                        = "Show5EMA";
                                        Calculate                                    = Calculate.OnPriceChange;
                                        EntriesPerDirection                            = 1;
                                        EntryHandling                                = EntryHandling.UniqueEntries;
                                        IsExitOnSessionCloseStrategy                = true;
                                        ExitOnSessionCloseSeconds                    = 30;
                                        IsFillLimitOnTouch                            = false;
                                        MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                                        OrderFillResolution                            = OrderFillResolution.Standard;
                                        Slippage                                    = 0;
                                        StartBehavior                                = StartBehavior.WaitUntilFlat;
                                        TimeInForce                                    = TimeInForce.Gtc;
                                        TraceOrders                                    = false;
                                        RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                                        StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                                        BarsRequiredToTrade                            = 1;
                                        // Disable this property for performance gains in Strategy Analyzer optimizations
                                        // See the Help Guide for additional information
                                        IsInstantiatedOnEachOptimizationIteration    = true;
                                        EMA1                    = 5;
                                    }
                                    else if (State == State.Configure)
                                    {
                                    }
                                }
                        
                                protected override void OnBarUpdate()
                                {
                                    if (BarsInProgress != 0)
                                        return;
                        
                                     // Set 1
                                    if (MarketPosition.Flat != MarketPosition.Flat)
                                    {
                                        EMA1.IsVisible = true;
                                    }
                                    
                                }
                            }
                        }​
                        Click image for larger version  Name:	ema_error.jpg Views:	0 Size:	19.2 KB ID:	1298595

                        Comment


                          #13
                          Hello sastrades,

                          In what you provided you defined a user variable which is not correct. You would need to start over and make a condition that uses the indicator you want to use. If you wanted to use the EMA you need to make a condition with the ema and make sure plot on chart is checked in the condition. After doing that a variable for the EMA indicator will be generated and it will be called EMA1.

                          JesseNinjaTrader Customer Service

                          Comment


                            #14
                            I'm going around-and-around on this. I've watched that prev vid and others and none show me how the code should look like.

                            If you would please, don't tell me again what I need to do, please SHOW me what it should look like, using the below existing code...

                            Code:
                            {
                                public class Show5EMA : Strategy
                                {
                                    protected override void OnStateChange()
                                    {
                                        if (State == State.SetDefaults)
                                        {
                                            Description                                    = @"Show 5 EMA when placing an order, hide when trade is closed.";
                                            Name                                        = "Show5EMA";
                                            Calculate                                    = Calculate.OnEachTick;
                                            EntriesPerDirection                            = 1;
                                            EntryHandling                                = EntryHandling.AllEntries;
                                            IsExitOnSessionCloseStrategy                = true;
                                            ExitOnSessionCloseSeconds                    = 30;
                                            IsFillLimitOnTouch                            = false;
                                            MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                                            OrderFillResolution                            = OrderFillResolution.Standard;
                                            Slippage                                    = 0;
                                            StartBehavior                                = StartBehavior.ImmediatelySubmit;
                                            TimeInForce                                    = TimeInForce.Gtc;
                                            TraceOrders                                    = false;
                                            RealtimeErrorHandling                        = RealtimeErrorHandling.StopCancelClose;
                                            StopTargetHandling                            = StopTargetHandling.PerEntryExecution;
                                            BarsRequiredToTrade                            = 1;
                                            // Disable this property for performance gains in Strategy Analyzer optimizations
                                            // See the Help Guide for additional information
                                            IsInstantiatedOnEachOptimizationIteration    = true;
                                        }
                                        else if (State == State.Configure)
                                        {
                                        }
                                    }
                            
                                    protected override void OnBarUpdate()
                                    {
                                        if (BarsInProgress != 0)
                                            return;
                            
                                         // Set 1
                                        if (MarketPosition.Flat != MarketPosition.Flat)
                                        {
                                            EMA1.IsVisible = true;
                                        }
                                        
                                    }
                                }
                            }​

                            Comment


                              #15
                              Hello sastrades,

                              If you watched builder videos those are going to show you concepts you would use while using the builder so they will only be so helpful. What you asked for in this post is not possible using the builder but you can eventually get to that point by manually. Eventually you need to unlock the code to do that.


                              The variable names for the indicator in the resulting code will be variable, I can only provide so much in regard to how the future code may look as an example, you will need to take some credit here and make your own code that is reflective of the code you actually end up with. For your original question of "Is it possible to script a strategy that when I place an order, it will Show a specific chart indicator", yes that is absolutely possible by using the code we have discussed in this thread. In manual coding that is possible.

                              JesseNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by burtoninlondon, Today, 12:38 AM
                              0 responses
                              9 views
                              0 likes
                              Last Post burtoninlondon  
                              Started by AaronKoRn, Yesterday, 09:49 PM
                              0 responses
                              14 views
                              0 likes
                              Last Post AaronKoRn  
                              Started by carnitron, Yesterday, 08:42 PM
                              0 responses
                              11 views
                              0 likes
                              Last Post carnitron  
                              Started by strategist007, Yesterday, 07:51 PM
                              0 responses
                              13 views
                              0 likes
                              Last Post strategist007  
                              Started by StockTrader88, 03-06-2021, 08:58 AM
                              44 responses
                              3,983 views
                              3 likes
                              Last Post jhudas88  
                              Working...
                              X