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

Strategy builder to enter using indicator color

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

    Strategy builder to enter using indicator color

    Hello,
    I am creating an automated strategy and one of the indicators that I am using is called Short-Term Swings. The indicator file is attached. Basically it changes color to Green if the trens is up, red if the trend is down.

    One of the things that I want incorporate in my automated strategy is to open buy when its green and to reverse the order once its red and so on.

    What I am struggling with is how to come up with a statement that mentions 'go long if the color of the Short-Term Swings is green' and rev the position once the color changed to red.

    Any ideas??
    Greatly appreciate any help you can provide.
    Attached Files

    #2
    Hello neshinx89,

    Thank you for your inquiry.

    That particular indicator uses an internal data series called "Trend" to determine the plot color, however, this internal series is not exposed, and therefore cannot be referenced from the Strategy Builder.

    That being said, I've created a modified version, simply adding a plot and assigning the Trend value to it. This will either be 1 (if the line is green on that bar), or -1 (if the plot is red). You can download and use this modified version in your Strategy Builder strategy - make sure you are choosing the "TrendExposed" plot in the indicator settings when using it and then you can compare it to Misc > Numeric Value of 1 - if it's true, the line is green, if false, it's red. You can then use this to trigger entries.

    Please let us know if we may be of further assistance to you.
    Attached Files
    Last edited by NinjaTrader_Kate; 08-11-2021, 10:03 AM. Reason: updated script as it referenced the incorrect plot when exposing trend
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Click image for larger version

Name:	Trend long.PNG
Views:	747
Size:	38.4 KB
ID:	1167402Click image for larger version

Name:	enter long.PNG
Views:	757
Size:	24.2 KB
ID:	1167403

      Dear Kate,

      Thanks for creating the modified version. Appreciate it. Is the setting above correct if I want to go long when the indicator is green?

      Comment


        #4
        Hello neshnix89,

        Thank you for your reply.

        Yes, that appears to be correct. For the opposite, when it turns red, check that it's a -1.

        Please let us know if we may be of further assistance to you.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Dear Kate,

          I have somehow completed the strategy. However when I load it into the Ninjatrader, I cant get any results. I'm using 1 Min chart on Ninjatrader 8. Attached is the strategy file. Are you able to advise where the problem is?
          Attached Files

          Comment


            #6
            Hello neshnix89,

            Thank you for your reply.

            There's a few issues here, one of which was a minor typo in the script I posted above for the TrendExposed version of the indicator. You'll want to import the new version I posted above on post #2. When it asks if you want to replace the existing script, say yes for the new version to be applied.

            Two other issues - one, you're calling both an Exit method and an Entry method to reverse the position on each bar. The issue with this is that Entry methods like EnterLong or EnterShort will automatically submit an order to exit the prior position in the previous direction. For example, let's say we're in a 1 long position, and get a signal to go short. If we call both Exit Long and Enter Short on the same bar, what happens is that the Exit will fire off a 1 short order to close the existing position. However, there's not enough time between that and when we call Enter Short, it's going to think we're still in that 1 long position, so it too will fire off a 1 short order to close what it thinks is an existing position, then a 1 short order to bring it to a 1 short position. So, we actually end up in a 2 short position because of that. This will continue increasing the position as you go.

            Rather, simply use EnterLong or EnterShort when reversing, as these will automatically close the prior position for you.

            The other problem is in your time conditions. You've got conditions saying if the time is greater than 9:30 pm and less than or equal to midnight, then enter. The problem here is in how times work. Midnight is equivalent to 00:00 - no time in a single day is less than midnight. I'd try setting it to 11:59pm rather than midnight, because how you have it right now, that's never going to be true, that the time of day is both less than midnight and greater than 9:30 pm. If you set it to 11:59, after correcting the other items above, you should see it take trades.

            One final note, you may want to set up a condition to exit any open trades at midnight, so any open position gets closed. There you could use if the current time = midnight, ExitShort and ExitLong (since if you don't have a long or a short position whichever you don't have would simply be ignored).

            Please let us know if we may be of further assistance to you.
            Kate W.NinjaTrader Customer Service

            Comment


              #7
              Dear Kate,

              Thanks a lot. Did what you mentioned and able to get the strategy working. I added some new rules including to take long above 200SMA and short below 200 SMA. By doing this i'm not able to stick with Reversing my orders anymore. In this case I have to exit my trade once the indicator color change. Can you advise how I can achieve that?

              Comment


                #8
                Hello neshnix89,

                Thank you for your reply.

                As long as they're not firing off on the same bar as an entry order, you could certainly use exit orders to do this. For example, you could add a condition to your entries to only enter if the current position is flat, and then for the exit say something like if the line changes from green to red and the Strategy > Current Position = Position Long, then Exit Long, or the reverse to exit a short trade - you'd want new sets for each exit you define.

                Please let us know if we may be of further assistance to you.
                Kate W.NinjaTrader Customer Service

                Comment


                  #9
                  Dear Kate,

                  I tried but doesn't seems to have any changes on my exit. Could you have a look at it?

                  Attached Files

                  Comment


                    #10
                    Hello neshnix89,

                    Two things here.

                    First, you have the Market Position condition mixed up for short and long exits, you're checking if you're in a long position and then trying to send a ExitShort, and checking if you're in a short position and then trying to send an ExitLong order. You'll want to reverse those for the exits to work.

                    Also, you haven't added a condition to check if the current position is a flat position for either the Long or Short entry, which I would advise adding to both, otherwise you could see both an exit and entry on the same bar.

                    Please let us know if we may be of further assistance to you.
                    Kate W.NinjaTrader Customer Service

                    Comment


                      #11
                      Dear Kate,

                      I would like your help on the strategy that I'm working on. I would like to add take profit target in my strategy builder but I realized that I would need to unlock the script from the Strategy Builder and manually code this logic into my strategy. Can you please help me with the code?

                      My stop loss logic is as attached.

                      - I want to set profit target to be 1 to 1 with my stop loss which is the range of my trigger candle.
                      Attached Files

                      Comment


                        #12
                        Hello neshnix89,

                        Thank you for your reply

                        You would need to unlock your strategy anyway if you want the stop loss price to be based on the price of the triggering candle, or use exit orders in the Conditions and Actions screen to mimic Set Stop Loss and Set Profit Target. You can only set dynamic prices for SetStopLoss and SetProfitTarget when manually coding, and what you have there wouldn't work anyway, since you've selected Ticks as the Calculation Mode but have given it a price for the value.

                        Here's a very simple code example that will set the stop and target at a 1:1 ratio:

                        Code:
                         protected override void OnBarUpdate()
                        {
                        if(CurrentBar < 1)
                        return;
                        
                        if(Close[0] > Open[0] && Position.MarketPosition == MarketPosition.Flat)
                        {
                        // make sure you set up the stop and target before the entry - calling them after can cause incorrect prices
                        SetStopLoss(CalculationMode.Price, Low[0]);
                        SetProfitTarget(CalculationMode.Price, (Close[0] + (Close[0] - Low[0])));
                        EnterLong();
                        }
                        
                        if(Close[0] < Open[0] && Position.MarketPosition == MarketPosition.Flat)
                        {
                        SetStopLoss(CalculationMode.Price, Low[0]);
                        SetProfitTarget(CalculationMode.Price, (Close[0] - (High[0] - Close[0])));
                        EnterShort();
                        }
                        
                        
                        }
                        Please let us know if we may be of further assistance to you.
                        Kate W.NinjaTrader Customer Service

                        Comment


                          #13
                          Dear Kate,

                          Appreciate the help. I have incorporate in the code. Getting an error. can you have a look. Thanks.
                          Attached Files

                          Comment


                            #14
                            Dear Kate,

                            Appreciate the help. I have incorporate in the code. Getting an error. can you have a look. Thanks.
                            Attached Files

                            Comment


                              #15
                              Hello neshnix89,

                              Thank you for your reply.

                              If you double click on the error it should take you to the line that's erroring. What gets highlighted?

                              Thanks in advance; I look forward to assisting you further.
                              Kate W.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by andrewtrades, Today, 04:57 PM
                              1 response
                              8 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by chbruno, Today, 04:10 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post chbruno
                              by chbruno
                               
                              Started by josh18955, 03-25-2023, 11:16 AM
                              6 responses
                              436 views
                              0 likes
                              Last Post Delerium  
                              Started by FAQtrader, Today, 03:35 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post FAQtrader  
                              Started by rocketman7, Today, 09:41 AM
                              5 responses
                              19 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Working...
                              X