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

Recreate Strategy in Strategy Builder

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

    Recreate Strategy in Strategy Builder

    I have a simple strategy code for entries that uses stochastic rsi, an EMA, DI, and ADX to the long and short sides. While trying to accomplish certain exit conditions within ninjascript editor, the code has become much more complex than necessary due to trial and error. I have the code for the entry conditions, but the exit conditions are proving difficult, as I am not an expert in C#. I simply want to have a trailing stop in ticks, a profit target in ticks (both optimizable in strategy analyzer), as well as multiple entries per direction. Is it possible for me to create these exit conditions in strategy builder and add my entry code after?

    Thanks

    #2
    Hi thereyoflite,
    You can always combine coding assistance from the StrategyBuilder with the freedom / functionalities of NinjaScript.
    Watch-Out: If you "unlock" a strategy created in the StrategyBuilder, you cannot go back and make further edits in the StrategyBuilder anymore.
    However, you can always:
    1) Click "View Code" in StrategyBuilder (highly recommended if you just wanted to check how a certain piece of code should look like in NinjaScript without unlocking the strategy),
    2) Create a copy of your strategy created in Strategy Builder, give it a distinct name then unlock that copy, then further modify that copy using NInjaScript.
    And you can always copy/paste sections from other strategies (be it "viewed", "unlocked" or "native" Ninjascript) into your own NinjaScript code.

    If you want to leverage the StrategyBuilder for your strategy, you will have to 1) be either long or short and 2) use distinct names for your entries (e.g. EMA, ADX, or EMA1 to EMA5 if you wanted to allow up to 5 EMA entries in parallel and use individual stops/targets, etc.). Trailing Stops and Take Profit can have different tick sizes for each entry name (e.g. EMA 10, 20, ADX 20, 30).

    I recommend to create a very basic strategy in Strategy Builder, add "Input parameters" [which will allow optimization] for your Take Profit(s) and Stop Loss(es), throw in some dummy entry rules, give your entries distinct names, then add a trailing stop loss and a take profit for each of them referring to i) the entry names and ii) the respective "input parameter".

    Then click "view code". The code will not be very complex, but required bits and pieces will be spread across separate sections, which perhaps is not too intuitive at the beginning.

    By the way: Make sure to set the "Default Properties" - "Entry handling" to "Unique entries" in the code already or when running the strategy, or increase "Entries per direction" as needed.

    NT-Roland

    Comment


      #3
      Hello thereyoflite,

      Thanks for your post.

      NT-Roland has shared some useful advise. I would also like to point out our NinjaScript Editor 401 tutorial and our Strategy Builder resources to help with using the Strategy Builder and using the Strategy Builder to generate code that can be copied into an unlocked strategy in the NinjaScript Editor.

      Strategy Builder 301 (publicly available resource) — https://www.youtube.com/watch?v=_KQF2Sv27oE

      Conditions examples —https://ninjatrader.com/support/help...on_builder.htm

      Actions examples — https://ninjatrader.com/support/help...us/actions.htm

      NinjaScript Editor 401 - https://www.youtube.com/watch?v=O5-C8GyV9OI

      The resources above are publicly available.

      If you have any additional questions, please don't hesitate to ask.
      JimNinjaTrader Customer Service

      Comment


        #4
        Thank you both for your quick responses and useful information. A couple things:

        I may not have been clear, the EMA, ADX, DI, and Stoch RSI all agree to create 1 entry signal, they are not generating their own entries each. Does this affect how I would get multiple entries per direction? For example, in a strong bullish trend, I may get 3-5 long signals without the first position getting its exit signal. I want the strategy to enter those signals despite already being in a position.

        In order to use strategy builder to create that "template" of having my trailing stoploss and my profit target in ticks, the whole strategy has to be long or short? Meaning I would have to run two strategies (the long strategy and the short strategy) at the same time to take advantage of both directions simultaneously? ( As described in the first paragraph, there are only two entry signals, one long and one short).

        Thanks!

        Comment


          #5
          Hello thereyoflite,

          If you are using one entry at a a time, it would not matter if EntryHandling is set to UniqueEntries or AllEntries. Each entry will be the same and we would then just be limited by EntriesPerDirection.

          If you want to consider long/short trades, you would want to set up a second entry which would define when short entries should take place as you would likely want separate entry criteria for long/short positions. Please note that if you call EnterLong when short or EnterShort when long, the strategy will reverse positions. We cannot be long/short at the same time, so I would suggest using a position check to ensure that your entry logic only takes place when the strategy is flat. (Please see Conditions examples linked in post #3)

          If you want to be long/short at the same time, you would need 2 accounts, and run a long strategy on account A and a short strategy on account B. You may wish to contact your broker to set up linked accounts for your login so you can use 2 accounts with your connection.

          Let us know if there is anything else we can do to help.
          JimNinjaTrader Customer Service

          Comment


            #6
            Thank you

            I am trying to build the entry conditions within strategy builder as well, which I don't think will be as difficult as I thought now that I have begun to learn it. I see how to do crossovers such as one EMA cross another EMA, but I am confused as to how to do a crossover entry with the Stochastics Indicator because I need K and D to cross to signal a long or short entry.

            Attached is an example of what I am referring to on a chart image. How would I signal this K D crossover within Strategy Builder? Or would I need to do it in ninjascript afterward?

            Comment


              #7
              Hello thereyoflite,

              Please be sure to see the Conditions examples as well as the Strategy Builder 301 tutorial.

              Under Conditions examples, you will see examples for how to create crossovers and how to reference plots from multi-plot indicators like Stochastics.

              A direct link is included here: https://ninjatrader.com/support/help...plotIndicators


              JimNinjaTrader Customer Service

              Comment


                #8
                Thank you

                I have created the entries and exits in strategy builder, and the stop loss and profit target are working correctly.
                During backtesting and optimization, there are a few days that are bad. How can I set the strategy to stop for the day when max drawdown on the day has reached a certain amount? Can I do this within strategy builder?

                Comment


                  #9
                  Hello thereyoflite,

                  It would not be possible to access drawdown through the Strategy Builder and this must be implemented in unlocked code. You could consider making a copy of the strategy so you can continue using the Strategy Builder, and so you can copy and paste portions of the Strategy Builder generated code into the unlocked strategy.

                  To get drawdown within the strategy code, please see below.



                  We look forward to assisting.
                  JimNinjaTrader Customer Service

                  Comment


                    #10
                    Thank you,

                    How would I Implement that code to close positions and stop strategy when a certain value is reached? (For the remainder of the trading day)

                    Comment


                      #11
                      Hello thereyoflite,

                      You could call ExitLong/ExitShort to close positions, and CancelOrder can be called to cancel pending orders. (Note if you use the Strategy Builder, the order methods will not use IsLiveUntilCancelled so non-market orders will be automatically cancelled if they are not filled on the next bar.)

                      To prevent the strategy from processing further, a bool can be used so for example:
                      1. The first condition checks if first bar of session is true, and then sets the bool "AllowTrades" to true
                      2. Another condition checks if you should set AllowTrades to false.
                      3. The next condition checks if AllowTrades is false and then calls ExitLong/ExitShort.
                      4. All subsequent trading conditions should then check if AllowTrades is true in order for those conditions to become true.
                      Or instead of #4, you could check if AllowTrades is false and call return; to prevent further code execution.

                      Bools can be created in the Strategy Builder so you may wish to model that logic there if you are not as familiar working with unlocked code.

                      If you would like to learn more about tracking order objects and using CancelOrder, please see below.

                      https://ninjatrader.com/support/help...thod_to_ca.htm

                      Let us know if there is anything else we can do to help.
                      JimNinjaTrader Customer Service

                      Comment


                        #12
                        Thank you so much!

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by helpwanted, Today, 03:06 AM
                        1 response
                        7 views
                        0 likes
                        Last Post sarafuenonly123  
                        Started by Brevo, Today, 01:45 AM
                        0 responses
                        7 views
                        0 likes
                        Last Post Brevo
                        by Brevo
                         
                        Started by aussugardefender, Today, 01:07 AM
                        0 responses
                        5 views
                        0 likes
                        Last Post aussugardefender  
                        Started by pvincent, 06-23-2022, 12:53 PM
                        14 responses
                        242 views
                        0 likes
                        Last Post Nyman
                        by Nyman
                         
                        Started by TraderG23, 12-08-2023, 07:56 AM
                        9 responses
                        384 views
                        1 like
                        Last Post Gavini
                        by Gavini
                         
                        Working...
                        X