Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Where to start if I want to create a strategy on a trading set-up?

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

    Hi Ryan

    Believe your morning has been going well. Ditto here.

    Appreciate your kind guidance on the following please:

    1. I have Sell and Buy Stop orders for some strategies. Sim-trading them for some weeks and everything behave normal. Plan to run them live soon. Since they are not Market orders, I should not encounter "order reject" when running them live, right??

    2. 2-contract trades with different profit targets/exit - Until I figure out how to add scale in/out code to my strategy, I am thinking of running the same strategy twice at the same time, ie both 1-contract trades yet have different profit target and trail. Will they pose any conflict when I run them live?

    Thanks. Have a great afternoon.

    Comment


      Hello belecona,

      1) Unfortunately can't really say here. There are so many order type/TIF/instrument/broker/exchange combinations that we can't predict if your orders will be accepted.

      2) This is not advisable since there is no cross communication between strategies. You can certainly run separate instances but be sure to understand that each strategy is running completely independent from the other.

      2 contact orders with separate targets and stops should work like this:

      Use two different signal names for your entry positions
      EnterLong(1, '"a");
      EnterLong(1, "b");

      Refer to these signal names in the fromEntrySignal parameter for your targets and stop losses.

      Example for targets:
      SetProfitTarget("a", CalculationMode.Ticks, 10);
      SetProfitTarget("b", CalculationMode.Ticks, 20);

      Set EntriesPerDirection = 1 and EntryHandling = Unique Entries to allow one entry per direction for every uniquely named rule.
      Ryan M.NinjaTrader Customer Service

      Comment


        Thanks Ryan for your swift response (as always). Hmm, I will try running the strategy twice on sim first just in case. Your suggested codes for 2 contracts help! Thank you.

        Comment


          Hi Ryan

          I came across the SamplePriceModification strategy in the reference library and really like it. Added the following (under protected override void OnBarUpdate) and F5 compiled successfully. Unfortunately, there're no revised SL orders modifying SL to -4 ticks even after a 6-tick move:

          Code:
          [FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]// Resets the stop loss to the original value when all positions are closed[/COLOR][/SIZE][/FONT]
          [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] (Position.MarketPosition == MarketPosition.Flat)[/SIZE][/FONT]
          [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
          [SIZE=2][FONT=Courier New] debug ([/FONT][/SIZE][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]"Reset Stops"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]);[/SIZE][/FONT]
          [SIZE=2][FONT=Courier New] SetStopLoss(longEntryID, CalculationMode.Ticks, stoplossticks, [/FONT][/SIZE][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]);[/SIZE][/FONT]
          [SIZE=2][FONT=Courier New] SetStopLoss(shortEntryID, CalculationMode.Ticks, stoplossticks, [/FONT][/SIZE][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]);[/SIZE][/FONT]
          [SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
          [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]// If a short position is open, allow for stop loss modification to -4 ticks[/COLOR][/SIZE][/FONT]
          [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]else [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] (Position.MarketPosition == MarketPosition.Short)[/SIZE][/FONT]
          [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
          [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]// Once the price is greater than entry price+4 ticks, set stop loss to -4 ticks[/COLOR][/SIZE][/FONT]
          [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] (Close[[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]] < Position.AvgPrice - [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]4[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] * TickSize)[/SIZE][/FONT]
          [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
          [SIZE=2][FONT=Courier New] SetStopLoss(CalculationMode.Price, Position.AvgPrice + [/FONT][/SIZE][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]4[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]*TickSize);[/SIZE][/FONT]
          [SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
          [SIZE=2][FONT=Courier New]} [/FONT][/SIZE]
          [/SIZE][/FONT]
          I also want to learn how to add a similar code when a long position is open. Thank you.

          Comment


            Hi Belecona,

            One thing to check is that you're using different method signatures when flat than when you're in a position. One allows you to specify an entry signal and the other does not. You should make these consistent. A link to possible method signatures for SetStopLoss() is here:


            The stop loss movement works as expected here. In addition to the suggestion above, you'll have to use Print() statements to verify values and view TraceOrders output. I suggest testing with the simulated data feed so you can control market direction rather than waiting for test conditions.

            For long positions, everything is reversed:
            Code:
            else if (Position.MarketPosition == MarketPosition.Long)
            {
            // Once the price is greater than entry price+4 ticks, set stop loss to -4 ticks
            if (Close[0] > Position.AvgPrice + 4 * TickSize)
            {
            SetStopLoss(CalculationMode.Price, Position.AvgPrice - 4*TickSize);
            }
            }

            Ryan M.NinjaTrader Customer Service

            Comment


              Hi Ryan

              Thanks for the tips. Your suggested code for a Long position matched mine when I had both in the strategy. When it's not working, I removed the Long just to test out the Short. I have amended the codes as follows (consistent names):

              Code:
              [FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]// Resets the stop loss to the original value when all positions are closed
              [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] (Position.MarketPosition == MarketPosition.Flat)
              {
              debug ([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]"Reset Stops"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]);
              SetStopLoss(CalculationMode.Ticks, stoplossticks);
              }
              [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]// If a short position is open, allow for stop loss modification to -4 ticks
              [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] (Position.MarketPosition == MarketPosition.Short)
              {
              [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]// Once the price is greater than entry price+4 ticks, set stop loss to -4 ticks
              [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] (Close[[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]] < Position.AvgPrice - [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]4[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] * TickSize)
              {
              SetStopLoss(CalculationMode.Price, Position.AvgPrice + [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]4[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]*TickSize);
              }
              }
              [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]// If a long position is open, allow for stop loss modification to -4 ticks
              [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]else[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] (Position.MarketPosition == MarketPosition.Long)
              {
              [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]// Once the price is greater than entry price+4 ticks, set stop loss to -4 ticks
              [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] (Close[[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]] > Position.AvgPrice + [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]4[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] * TickSize)
              {
              SetStopLoss(CalculationMode.Price, Position.AvgPrice - [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]4[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]*TickSize);
              }
              }
              [/SIZE][/FONT][/SIZE][/FONT]


              F5 compile successful. Unfortunately, it does not make any difference when I compare the trades with the strategy that does not have the SL modification codes .. sigh .. I have sim traded the strategy for over 3 months and it only triggers a few trades a month. If the historical trade log does not show the SL modification, guess it will not work when I sim-trade it. Are there other areas I should check? Appreciate your guidance. Thank you.

              Comment


                I don't know if you'll get much different results when backtesting. To evaluate this code properly you need to test real time or with market replay. The simulated data feed works well for this.

                Discrepencies expected real time versus backtest.
                Ryan M.NinjaTrader Customer Service

                Comment


                  Hi Ryan

                  I am not backtesting the strategy. I simply activated the strategy on a ES chart and checked the historical performance. You can tell from the enclosed examples that price moved in favor ($112.50 and $150.00 = 9 and 12 ticks) yet no revised SL orders to -4 ticks as coded (I checked the Orders tab). I will check if I have 25 Aug and 27 Aug data for market replay. Maybe the revised strategy will work. Else I will continue searching for a fix. Thank you.
                  Attached Files

                  Comment


                    Hi Ryan

                    I just market replayed the revised strategy on a ES chart. Price went 29 ticks in favor (predetermined target @30 ticks) and ended up closing at -15 ticks (initial stoploss @15). The Price Modification (adjust SL to -4 ticks after a 4-tick move) is not working unfortunately. I will do more research and hopefully I will fix it in the very near future. Thanks again for your help.

                    Comment


                      This is in response to your post #128
                      ===
                      This is the same process as backtesting. It represents historical fills that would have taken place. This is different than running a strategy live with incoming data.

                      There's no automatic granularity in backtesting so if the original stop loss could be filled on the bar then it will be filled.

                      If you're seeking this type of granularity in a backtest, please see the following reference sample for help:
                      Backtesting NinjaScript Strategies with an intrabar granularity

                      To evaluate if the code is behaving as you expect: The best way is with the simulated data feed.
                      Last edited by NinjaTrader_RyanM1; 08-31-2010, 03:25 PM. Reason: Replies crossed
                      Ryan M.NinjaTrader Customer Service

                      Comment


                        Thanks Ryan. I will sim trade both the original and revised strategies per your kind suggestion. Have a Nice evening.

                        Comment


                          You're welcome, Belecona. Attached is a simple strategy.
                          • Runs in real time only
                          • Enters short
                          • Initial Stop loss at 15 ticks
                          • Whenever you reach 4 ticks in profit, the stop loss is adjusted 4 ticks below entry price. Position can only be closed with stop loss and then it would enter again.
                          Try it on simulated data feed and control market direction down with trend slider to see the stop movement.
                          Attached Files
                          Ryan M.NinjaTrader Customer Service

                          Comment


                            Big Big Thank You to You Ryan for the unexpected nice surprise! I will study it!!

                            Comment


                              Good morning Ryan

                              I spent the past 2 hours going through many posts and sample strategies. When I market replayed the strategy a few times tonight, the following came to my mind:

                              1. Price went down 29 ticks in favor of the short after the entry bar. Yet the Price modification code would only be triggered upon the completion of Bar 2, ie it needs 2 bars going to the right direction. Am I right on the timing, ie completion of Bar 2 and price has to continue falling before revised SL will get triggered?

                              2. If yes, price went back up after completion of Bar 2. That may explain why the Price modification code (revised SL to -4 ticks) was never triggered on Bar 3.

                              If my understanding is right, how can I code my strategy so that the Price modification code will be triggered after the entry bar? I may get it wrong. Well, at least that's my analysis now and will take a break instead of never-ending search. My trillion thanks again Ryan.
                              Attached Files

                              Comment


                                Your description sounds accurate when using CalculateOnBarClose = true. The code isn't processed until a bar is complete so you will not see this movement until the next bar after the condition.

                                For real time processing, you have to set CalculateOnBarClose = false.
                                Ryan M.NinjaTrader Customer Service

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                                0 responses
                                651 views
                                0 likes
                                Last Post Geovanny Suaza  
                                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                                0 responses
                                370 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by Mindset, 02-09-2026, 11:44 AM
                                0 responses
                                109 views
                                0 likes
                                Last Post Mindset
                                by Mindset
                                 
                                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                                0 responses
                                574 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by RFrosty, 01-28-2026, 06:49 PM
                                0 responses
                                577 views
                                1 like
                                Last Post RFrosty
                                by RFrosty
                                 
                                Working...
                                X