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

Using a time filter

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

    #16
    Hello newuser,

    I would say that if you do not know how many entries your strategy is going to have, then, yes, the best approach would be to use a counter to to append to your signal name.

    I would like to note that a trade is considered as an entry and exit. Performance.LongTrades.Count will not increment until a long trade (entry and exit) has occurred. This probably would not be the value you want to use.

    You could use CurrentBar. CurrentBar returns the bar index of the bar the script is currently processing. The first bar on your chart (the left most bar) will be 0, the next would be 1, so on and so forth.
    Zachary G.NinjaTrader Customer Service

    Comment


      #17
      Thanks, that's a good point about Performance.Longtrades.Count that I hadn't thought about.

      CurrentBar might cause me problems if it based off the position of the chart?

      What about if I used BarsSinceEntry() instead? If I've understood the help guide correctly I can use it to specify a condition for both how many bars a given trade has been open and also whether I want the most recent trade (entriesAgo = 0), the second trade (entriesAgo =1) or any other number in the sequence?

      Comment


        #18
        Hello newuser,

        I'm not quite sure by what you mean by "CurrentBar might cause me problems if it based off the position of the chart?"

        All CurrentBar is going to return is the index value of the bar your strategy is currently running on. This should not cause any problems unless you're having multiple entries occur on the same bar (as CurrentBar wouldn't change between the two entries).

        If you're using the BarsSinceEntry() overload that asks for an entriesAgo parameter, you'll use 0 for the last entry, 1 for the previous, so on and so forth.
        Zachary G.NinjaTrader Customer Service

        Comment


          #19
          Sorry what I meant was that if CurrentBar is based off the first (left most) bar on the chart being 0 then I wasn't sure how I would go about coding that to append to a signal name.

          So given that I always want to set the initial stop for a buy order at the low of the setup minus 2 ticks I tried adding a line: if BarsSinceEntry equal to 0 (my logic being that it should only set the stop loss at that point when the trade first triggers).

          I notice in the backtest that all trades seem to end in 1 bar so something didn't quite work, but I did also notice that the time filter was now working properly. Can you take a look at the order management code below please and let me know what I have done wrong.

          {
          if (BarsSinceEntry()==0);
          SetStopLoss(CalculationMode.Price, (Low[0]) - (2 * TickSize));
          EnterLongLimit(DefaultQuantity, High[0] + 2 * TickSize, "");
          SetProfitTarget(CalculationMode.Price, ((High[0]+2*TickSize)-(Low[0]-2*TickSize))*2);

          }

          Comment


            #20
            Am I correct in thinking that even with a value of 0 BarsSinceEntry will not apply the stop loss until after the first candle of the open trade has actually closed?

            If that is the case then that won't work for me, as there's always a chance that the market could trigger my position and then move so far against my position that by the time the stop is placed the market has already moved past it.

            Comment


              #21
              Hello newuser,

              I see that you have placed a semicolon ( ; ) after if (BarsSinceEntry() == 0). The logic below the if won't be affected by if the condition is true or not; it will run regardless.

              You'll want to do this:

              Code:
              if (BarsSinceEntry() == 0)
              {
                   // logic
              }
              What steps have you done to debug your code?

              I would suggest checking to see if the values you are using for your stop loss and profit target are where you are expecting to be placed at. You can do this by using prints.

              Testing on my end, your profit targets are being placed below the entry. Ensure that the values you're trying to calculate are actually what you're expecting.

              The stop loss and profit target are not going to be submitted until the entry fills. It has nothing to do with if BarsSinceEntry() == 0 or not.
              Zachary G.NinjaTrader Customer Service

              Comment


                #22
                I realized after reading your reply that I had made an error in the SetProfitTarget logic, thank you. What I had previously was just calculating the tick value for twice the initial risk, but I forgot to add the bit of code for "entry price plus."

                I amended the code to reflect your previous post and I only had one trade returned in the backtest. I surmised that including the EnterLongLimit() code after the if statement was blocking any further entries from being placed, so I put the entry code above it (as follows) and it looks like it has worked =)

                Can you see anything wrong with that or does it make sense?

                {
                EnterLongLimit(DefaultQuantity, High[0] + 2 * TickSize,"");
                if (BarsSinceEntry()==0)
                {
                //stop loss and profit target logic
                }
                }

                Comment


                  #23
                  Hello newuser,

                  The logic you have provided will submit a long limit order. The stop loss and profit target values will be modified as long as BarsSinceEntry() == 0 and they'll be submitted to the market whenever your order is filled.

                  As a sidenote, I would like to note that a limit order submitted above the current market price will be fill immediately as it would be a better price than you have specified.
                  Zachary G.NinjaTrader Customer Service

                  Comment


                    #24
                    I've been looking through the results of the backtest and I've noticed two issues (for reference I have run the test on the AUDUSD forex pair, using a 60 minute chart).

                    The first issue is that orders are being triggered even though the market isn't reaching the price level as set by the EnterLongLimit(). I noticed for example that my code generates a signal at 10am on 2 Sep 2016 which triggers. When I look at the executions tab in the strategy analyser window it says the entry price is 0.0000, but on the trades tab it is listed as 0.7559. I have confirmed that it is a valid entry and the entry price should be 0.7559, but the market never reaches that price point so the trade should never trigger. I'm at a loss to explain why that has occurred.

                    The second issue (which might be related to the first but I'm not sure) is that trades are also being ignored due to the signal limit being exceeded. I know this because I set traceorders=true; in my code and I looked through the output (sample pasted below). I haven't specified any sort of limit on the maximum number of entries so presumably there is a default value that I am butting up against?

                    18/08/2016 6:00:00 PM Ignored PlaceOrder() method at 18/08/2016 6:00:00 PM: Action=Buy OrderType=Limit Quantity=1 LimitPrice=0.0000 StopPrice=0 SignalName='Buy' FromEntrySignal='' Reason='Exceeded entry signals limit based on EntryHandling and EntriesPerDirection properties'

                    Comment


                      #25
                      Hello newuser,

                      Thank you for your response.

                      For the second item, set the EntriesPerDirection and EntryHandling as needed to allow for the number of signals you wish to see. You can find details on EntryHandling at the following link: http://ninjatrader.com/support/helpG...ryhandling.htm

                      For the first item, I would like to recreate this on my end.
                      Can you send me the strategy and a screenshot of the settings used in the Strategy Analyzer?

                      Please send the strategy and screenshot in an email to platformsupport[at]ninjatrader[dot]com with 'ATTN: Patrick H - 1564110' in the subject line.

                      You can attach your strategy to your email by going to File > Utilities > Export NinjaScript > Export selected source files > select your strategy > select the right arrow > Export. The file will be located under (My) Documents\NinjaTrader 7\bin\Custom\ExportNinjaScript.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by jhjhawkins, Today, 12:25 PM
                      0 responses
                      5 views
                      0 likes
                      Last Post jhjhawkins  
                      Started by rexsole, Today, 11:48 AM
                      1 response
                      13 views
                      0 likes
                      Last Post NinjaTrader_PatrickG  
                      Started by jeronymite, Yesterday, 05:05 PM
                      3 responses
                      54 views
                      0 likes
                      Last Post NinjaTrader_ChristopherJ  
                      Started by Don22Trader1, Yesterday, 05:54 PM
                      3 responses
                      49 views
                      0 likes
                      Last Post NinjaTrader_Jesse  
                      Started by Jebulay, Today, 08:36 AM
                      1 response
                      43 views
                      0 likes
                      Last Post NinjaTrader_PatrickG  
                      Working...
                      X