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

    #46
    Thanks Ryan I believe you've solved the myth again! I now recall the sample strategy may have the "Historical" line in it. Gosh, I spent nights trying to make them work!

    A big round of applause flying over!

    Good news upon closing: All 4 strategies performed as expected most of the time EXCEPT they stopped trading at 3:59:30 (as scripted) and resumed trading again at 3:59:44 per enclosed screenshot!

    What has happened Ryan?? Please enlighten me again! Thank you.
    Attached Files

    Comment


      #47
      Hi Belecona,

      This is expected. The ExitOnClose event will flatten open positions, but the strategy remains running. If conditions evaluate to true then it will continue to submit orders.
      Ryan M.NinjaTrader Customer Service

      Comment


        #48
        I learn something new again. Thank you.

        Since the time code in the strategy does not stop trading (only flatten the position Exit on close), what's the effective way to stop the strategy running at 4:00 pm? Manually stop the strategy around 3:45 pm or so, then close any open position manually??

        Reply next Mon is fine no rush. Thanks Ryan. Enjoy a Great weekend.

        Comment


          #49
          You could add another condition to your entries that checks against time.

          The reference sample below helps with this:
          Using a time filter to limit trading hours.
          Ryan M.NinjaTrader Customer Service

          Comment


            #50
            Thank You .. Thank You .. more goodies to enjoy over the weekend.

            I will be a good girl and no more posts until next week .. smile ..

            Night-night!

            Comment


              #51
              .. knock .. knock .. How's Ryan? It's me!

              Good afternoon Ryan

              I thought I'd be okay today. Well, what can I say?!

              1. Multiple Trades within the same time span happen again even I have the following in the strategy:

              protectedoverridevoid Initialize()
              {

              EntriesPerDirection = 1
              ;
              EntryHandling = EntryHandling.AllEntries;
              CalculateOnBarClose =
              true;
              }

              Oh, I don't have the following, is it why?

              protectedoverridevoid OnBarUpdate()
              {
              // Run realtime
              if (Historical)
              return;

              2. Strategy got stopped by the system with the enclosed error messages. There was a long entry @10545 and the system was trying to submit a sell stop order @10553. It's strange because the trade did not go up to 8 ticks + it's asking for Breakeven when it's 8 ticks in favor. Here it comes my script (which I used on a few strategies and only one strategy got stopped):

              // Resets the stop loss to the original value when all positions are closed
              if (Position.MarketPosition == MarketPosition.Flat)
              {
              SetStopLoss(CalculationMode.Ticks, stoplossticks);
              }

              // If a long position is open, allow for stop loss modification to breakeven
              elseif (Position.MarketPosition == MarketPosition.Long)
              {
              // Once the price is greater than entry price +8 ticks, set stop loss to breakeven
              if (Close[0] > Position.AvgPrice + 8 * TickSize)
              {
              SetStopLoss(CalculationMode.Price, Position.AvgPrice);
              }
              }

              // If a short position is open, allow for stop loss modification to breakeven
              elseif (Position.MarketPosition == MarketPosition.Short)
              {
              // Once the price is smaller than entry price -8 ticks, set stop loss to breakeven
              if (Close[0] < Position.AvgPrice - 8 * TickSize)
              {
              SetStopLoss(CalculationMode.Price, Position.AvgPrice);
              }
              }

              Thank you!
              Attached Files

              Comment


                #52
                Hi Belecona,

                1. The code you have does not limit multiple trades within a timespan. It limits the number of simultaneous entries that are accepted in any one direction. If your position is closed and conditions continue to evaluate true, then the strategy will enter again. You'll have to define and then code for how frequently you want to accept trades.

                2. Have to work with TraceOrders output to see what may have happened there. With TraceOrders enabled in your Initialize() method, you will have more details written to the output window. To view the output window click on Tools > Output window.

                Ryan M.NinjaTrader Customer Service

                Comment


                  #53
                  Thanks Ryan. I am more concerned to resolve Item 1, ie only have 1 trade at one time. Appreciate your providing me with some tips on how to code the script properly. Thank you.

                  I am quite certain that Item 2 is a script-coding related issue since the error messages (same as the one stated in the Output Window) specified no sell stop limit order above the market which makes sense. I will try to find another sample strategy that may have the correct code.

                  Comment


                    #54
                    You must define "one trade at one time" even further. Is this only supposed to trade once per day? One approach to that is below:

                    Check for a bool condition.
                    Within your actions set the bool condition as false.
                    Reset this bool condition each day.

                    Pseudo-Code below:

                    Code:
                     
                    if (FirstBarOfSession)
                       notTradedToday = true;
                     
                     
                    if (allYourConditions && notTradedToday)
                      {
                       placeAnOrder;
                       notTradedToday = false;
                      }
                    The reference sample below should help with this:
                    Resetting values at the beginning of new trading sessions.
                    Ryan M.NinjaTrader Customer Service

                    Comment


                      #55
                      Thanks Ryan. I want one trade (ie 1 open position) at one time. No new trade until the open position is closed within the specified time span, say between 9:30 am and 4:00 pm.

                      I will check the suggested link and hopefully will find the answer there.

                      Night-night.

                      Comment


                        #56
                        If you only want one trade at a time, then this is controlled with EntriesPerDirection.

                        When using EntriesPerDirection, it only works for simultaneous open trades. If your trade is closed and conditions are true, then this property has no effect.

                        My previous reply was for trading once per day.
                        Ryan M.NinjaTrader Customer Service

                        Comment


                          #57
                          Thank You .. Thank You Ryan.

                          I specified in the strategy 1 entry per direction (as follows) and it did not stop multiple trades being triggered .. sigh .. Not sure what I did wrong. Please give me some tips tomorrow .. no rush.

                          protectedoverridevoid Initialize()
                          {
                          EntriesPerDirection = 1;
                          EntryHandling = EntryHandling.AllEntries;

                          Night-night!

                          Comment


                            #58
                            The setting is correct if you aren't expecting further entries in the same direction. When set to 1 it will ignore further signals to enter in the same direction.

                            One thing to keep in mind is that you need to reapply instances of your strategy whenever you make changes to the code. You may expect it to act according to the latest version of the code, but unless you remove and apply again it will be using the older version.

                            If the above doesn't explain can you provide a scenario where we can see this is not behaving this way?
                            Ryan M.NinjaTrader Customer Service

                            Comment


                              #59
                              Hi Ryan

                              Believe your day has been going well. Ditto here as I have 4 strategies running on 2 computers. Am revising a strategy with a trail condition. Please help me:

                              1. I learned from a Forum that CalculateOnBarClose MUST be "false" to have a trail functioning properly. Is it true for both backtest and live environments? I have CalculateOnBarClose at true all the time, what's the impact of the change?

                              2. I am still a little confused on the Trail code. Not sure if I have the "if" and "else if" of the following script properly. FYI: F5 does compile it successfully.

                              3. For this code, am I telling the system the following:

                              i. Move StopLoss to BE when the market moves 8 ticks from my Long entry

                              ii. When Closing Price > Previous Price + 8 ticks (TrailProfitTrigger = 8), move my StopLoss (now BE after i. above) to newPrice (newPrice = Previous Price + 2 ticks (trailStepTicks = 2))

                              iii. Item ii will be in place, ie StopLoss/newPrice will be advanced 2 ticks when the trade continues moving to my favor until it gets stopped out


                              // If a long position is open, allow for stop loss modification to breakeven
                              elseif (Position.MarketPosition == MarketPosition.Long)
                              {
                              // Once the price is greater than entry price +8 ticks, set stop loss to breakeven
                              if (Close[0] > Position.AvgPrice + 8 * TickSize)
                              {
                              SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                              }
                              }

                              // Once at breakeven wait till trailProfitTrigger is reached before advancing stoploss by trailStepTicks size
                              elseif (previousPrice != 0// SL is at Breakeven
                              && Close[0] > previousPrice + trailProfitTrigger * TickSize)
                              {
                              newPrice = previousPrice + trailStepTicks * TickSize;
                              SetStopLoss(CalculationMode.Price, newPrice
                              );
                              previousPrice = newPrice;
                              }

                              // If a short position is open, allow for stop loss modification to breakeven
                              elseif (Position.MarketPosition == MarketPosition.Short)
                              {
                              // Once the price is smaller than entry price -8 ticks, set stop loss to breakeven
                              if (Close[0] < Position.AvgPrice - 8 * TickSize)
                              {
                              SetStopLoss(CalculationMode.Price, Position.AvgPrice);
                              }
                              }


                              // Once at breakeven wait till trailProfitTrigger is reached before advancing stoploss by trailStepTicks size
                              elseif (previousPrice != 0// SL is at Breakeven
                              && Close[0] < previousPrice + trailProfitTrigger * TickSize)
                              {
                              newPrice = previousPrice - trailStepTicks * TickSize;
                              SetStopLoss(CalculationMode.Price, newPrice
                              );
                              previousPrice = newPrice;
                              }

                              Appreciate your guidance Ryan! Thank you.

                              Comment


                                #60
                                Hi Ryan

                                I just backtested the strategy w the trail condition (both CalculateOnBar Close at true and false). No difference (incl the trade log) compare to the strategy without the trail condition. Does it mean that my trail code does not work OR it will only work in a live environment?

                                Appreciate your help and tips on how to add a trail condition. It's critical because I notice hundreds of $ were returned to the market without it. Thank you.

                                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