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

How to stop multiple trades in the same direction

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

    How to stop multiple trades in the same direction

    As the title states, Im looking to take a long, short, long, short, and so. The way my strategy works best is not allowing 2 longs in a row or 2 shorts in a row. Looking on information to best achieve this.

    Thanks.

    #2
    Hello, thanks for writing in. There is the built in stopping mechanics of EntriesPerDirection = 1; by default. You can also check the current market position to determine an entry signal.



    Kind regards,
    -ChrisL
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the reply. I have 1 entry per direction and unique entries on. I just double checked. And it is still happeneing. Just for clarification, I want my strategy check to see if last trade was a long, If it was, it cannot take a long until a short is taken. vice versa for shorts. also, i have ifFirstTickofBar set to true.

      Comment


        #4
        Hello, thanks for the follow up. If your entries have unique signal names then UniqueEntries will allow more than one entry in a given direction. EntryHandling.AllEntries should be used to keep the entry handling global and limit the entires to only one. You can use C# variables at the class level to save data from OnPositionUpdate and track your position:



        Kind regards,
        -ChrisL
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Thanks,

          Do you happen to havve any strategy examples with this implemented in it?

          Comment


            #6
            I want my strategy check to see if last trade was a long, If it was, it cannot take a long until a short is taken. vice versa for shorts
            One way is to set a variable to the last "side" (short or long) when the exit order is executed:

            Code:
                  
            protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
                    {
                    if (marketPosition == MarketPosition.Long &&&&; execution.IsExitStrategy)
                                 lastOrderPosition="long";
                    if (marketPosition == MarketPosition.Short &&&&; execution.IsExitStrategy)
                                 lastOrderPosition="short";
                    }​
            Then look at the "lastOrderPosition" when you do your next entry...

            Code:
            if ( lastorderPosition != "Long"  &&&& ??)
                                 //enter long
            
            if ( lastorderPosition != "Short"  &&&& ??)
                                 //enter long​
            Note: the "lastOrderPosition" should be initialized to "flat"....
            >> this is just a partial example of a way to do it...

            Comment


              #7
              Thanks. I was able to implement this in my code. Just had to figure out where to initilize lastOrderPosition. BUT the issue still persist.

              A bit of background. I have an indicator that plots an arrow once an condition is met. This indicator calculates onPriceChange. I coded it so i can use it in a strategy. Now that I have a strat, which aslo caculates onPriceChange, it is taking on the initial trade and then immediately entering another trade. I assume its taking on another trade becuase the condition is still true within that same bar. Im thinking of ways to only allow 1 trade per bar. I've tried the following.

              1. Creating a bool call tradeTaken -> true. Setting isFirstTickOfBar to true. and So on, discibed to me in this thread. https://forum.ninjatrader.com/forum/...times-in-a-row My stategy still enters a trade when it meets profit target or stop loss.

              2. The method provided above.

              3. created 2 bools. lastLong and lastShort. Defaulted to true. If long is taken, set lastLong to true and set lastShort to false. and vice versa for shorts. Each type of trade requires the opposite type of entry to be true in oder to enter a trade. This did not stop back to back trades.


              Any input on the best way to only take that initial trade and disregard that the condition is still true would be helpful. Thanks

              Im going to do this and test it out tonight. https://paul-ninjatrader.tinytake.co...Nl8xNDc5OTg3Mg
              Last edited by amigatlin; 03-23-2023, 07:43 PM.

              Comment


                #8
                First, I would install the Microsoft Visual Studio 2019 Community (free): https://visualstudio.microsoft.com/vs/older-downloads/
                Then, from the NT strategy builder, "generate" your strat.
                Now, from the "NEW" menu, select "Ninja script editor"
                This will open the script window.
                Press the last button on the right, a weird VS symbol.
                This will open VS and connect to NT.
                Now open your strat from the list on the right side.

                You now can easily edit your strat code (which is C#) and change it to suit your needs.

                Try it, it does have a learning curve, but there are NT docs on the basics and various commands.....and you can see the existing strats, indicators, etc...

                https://ninjatrader.com/support/helpGuides/nt8/NT%20HelpGuide%20English.html

                .
                Last edited by waltFX; 03-23-2023, 08:47 PM.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by StockTrader88, 03-06-2021, 08:58 AM
                45 responses
                3,991 views
                3 likes
                Last Post johntraderuser2  
                Started by TAJTrades, Today, 09:46 AM
                0 responses
                7 views
                0 likes
                Last Post TAJTrades  
                Started by rhyminkevin, Yesterday, 04:58 PM
                5 responses
                62 views
                0 likes
                Last Post dp8282
                by dp8282
                 
                Started by realblubb, Today, 09:28 AM
                0 responses
                8 views
                0 likes
                Last Post realblubb  
                Started by AaronKoRn, Yesterday, 09:49 PM
                1 response
                19 views
                0 likes
                Last Post Rikazkhan007  
                Working...
                X