• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy doesn't execute all entries

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

    Strategy doesn't execute all entries

    I've built a strategy (using Strategy Builder) that opens a long position if last candle is an inside bar. Entry point is at last candle's high, TP +20 pips, SL -20 pips.
    I've tested this strategy through Strategy Analizer using EURUSD 60 min chart from 01/01/2016 to 06/30/2016. It recognizes all inside bars except 4 (01/14/2016 19h, 01/20/2016 21h, 02/02/2016 11h and 06/16/2016 17h, all CST)
    Can someone explain me why does the script doesn't recognize these inside bars?
    Attached are charts with ib's, conditions and actions and stops and targets from strategy builder
    Attached Files

    #2
    Hello esborsa,

    Welcome to our Support Forums!

    The condition you have outlined will correctly place the buy stop market order at the high of the current bar. As is the nature of stop orders, the price must be reached before the order gets filled as a market order.

    To guarantee entries when the conditions become true, you will want to submit a simple buy market order using the EnterLong() order method.

    You can also verify your logic by placing a Print() statement that outputs the time of the current bar "Time[0]" within your entry condition. This way you can see an entry in the NinjaScript output window that tells you exactly when the condition became true and the order has been placed.

    These Print() statements can be added through the Strategy Builder, as well. I have attached a picture that demonstrates how to create such an action.

    Debugging tips are shared on our forum here: http://ninjatrader.com/support/forum...ead.php?t=3418

    Please let me know if you have any questions.
    Attached Files
    JimNinjaTrader Customer Service

    Comment


      #3
      Hello Jim, thanks for your reply.
      I'm a bit confused because I'm trying to achieve what you're explaining in your comment.
      The sequence should be as follows:
      1. There's a candle followed by an inside candle
      2. Next candle following inside candle opens a long position through buy stop order.
      In all 4 cases mentioned in my post (01/14/2016 19h, 01/20/2016 21h, 02/02/2016 11h and 06/16/2016 17h, all CST), strategy analyzer detects the inside candle but doesn't open a large position despite its high is larger than inside candle high. The price is reached but the order doesn't get filled.
      I attach an image from 06/16/2016: 17:00h candle is an inside candle. 18:00h candle's high is larger than 17:00h high, so there should be a long position and I don't understand why it isn't.
      Attached Files

      Comment


        #4
        Hello esborsa,

        Thanks for the reply.

        Yes, it does appear that the condition would have become true for the order to be placed. To be certain, you will have to debug your script to verify the condition became true, and you will have to use Trace Orders to verify that your order gets placed and filled.

        For example, if you add a Print() statement to your condition as described in my previous post, you would see a line in the NinjaScript Output window when this order was placed.

        Code:
        if ((High[0] <= High[1])
        	 && (Low[0] >= Low[1]))
        {
        	Print(Time[0]);
        	EnterLongStopMarket(Convert.ToInt32(DefaultQuantity), High[0], @"BTO");
        }
        This would tell you if the if statement became true to place the order. If you do not see plot execution markers, but you see an entry that the if statement became true during that time, you can infer that EnterLongStopMarket(Convert.ToInt32(DefaultQuantit y), High[0], @"BTO"); was reached and the order was submitted.

        If there are neither plot executions or an entry in the Output Window for that time, this would mean that the condition did not become true. I would advise to use the middle mouse button to verify the Open High, Low and Close values for those bars to see if they will actually allow your condition to become true.

        Trace Orders will tell you the status of those orders. If the condition becomes true, we will see an entry in the Output Window, but we would still need to know the status of the order. An order will return states of Submitted, Accepted, Working, and finally Filled. Once filled it will be plotted on a chart. These are observed in the Output window as well.

        Trace Orders can be turned on by setting TraceOrders = true; in State == State.SetDefaults.

        You may also wish to test when using a simple EnterLong() order entry method and observing if you get different results than with EnterLongStopMarket().

        Please review the Strategy Analyzer results after you have added those lines of code for debugging and have run another test using EnterLong() instead of EnterLongStopMarket().

        Output Window - https://ninjatrader.com/support/help...us/?output.htm

        Please let me know if you have any further questions.
        JimNinjaTrader Customer Service

        Comment


          #5
          Hello Jim,

          This is what I've tried:

          1. All this 4 cases not producing a long position are inside bars. This was the first I checked.

          2. When using a simple EnterLong() order there's no error, but that's not the objective of the strategy, as order is opened at open price and not at high price.

          3. I've activated Trace Orders in Strategy Builder and Output window shows this error:

          02/02/2016 11:00:00 Strategy 'DIBS/-1': Entered internal SubmitOrderManaged() method at 02/02/2016 11:00:00: BarsInProgress=0 Action=Buy OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=1,0908'0 SignalName='BTO' FromEntrySignal=''

          02/02/2016 11:00:00 Strategy 'DIBS/-1': Ignored SubmitOrderManaged() method at 02/02/2016 11:00:00: BarsInProgress=0 Action=Buy OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=1,0908'0 SignalName='BTO' FromEntrySignal='' Reason='Invalid order price, please see log tab'

          Nothing appears in log tab. But this has given me the idea that the problem could be from data. This is data from IB.

          4. I've been comparing 60 min chart and 1 min chart and all 4 cases have in common that high and close from inside bar and open from the entry bar has the same value.
          For example, 02/02/2016 11h High = 1.09080, Close = 1.09080 and 02/02/2016 12h Open = 1.09080
          This is the same for all 4 cases not opening position. Neither of other positions has this coincidence.
          I've added an offset to my entry and long positions are opened (I attach an image with 01/14/2016 19h long position after adding an offset of 2 pips). Is this as expected?
          Attached Files
          Last edited by esborsa; 04-07-2017, 10:53 AM.

          Comment


            #6
            Hello esborsa,

            Thanks for the reply.

            You have uncovered some useful information on what is occurring with your strategy.

            2. When using a simple EnterLong() order there's no error, but that's not the objective of the strategy, as order is opened at open price and not at high price.
            This proves that changing the order type is giving you entries when the condition is true. The issue lies in the order entry method, not the entry condition logic.

            02/02/2016 11:00:00 Strategy 'DIBS/-1': Entered internal SubmitOrderManaged() method at 02/02/2016 11:00:00: BarsInProgress=0 Action=Buy OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=1,0908'0 SignalName='BTO' FromEntrySignal=''

            02/02/2016 11:00:00 Strategy 'DIBS/-1': Ignored SubmitOrderManaged() method at 02/02/2016 11:00:00: BarsInProgress=0 Action=Buy OrderType=StopMarket Quantity=1 LimitPrice=0 StopPrice=1,0908'0 SignalName='BTO' FromEntrySignal='' Reason='Invalid order price, please see log tab'
            Trace Orders has shown that the order submission occurred, but the order submission was ignored because of an "Invalid order price." You have an entry price of the current bar's high. You may try further using an offset from High[0] or from Close[0].

            I've added an offset to my entry and long positions are opened (I attach an image with 01/14/2016 19h long position after adding an offset of 2 pips). Is this as expected?
            Yes, you have provided an appropriate solution. Placing a limit order too close to the current market price can cause these issues with "Invalid order prices."

            Please let me know if you have any further questions.
            JimNinjaTrader Customer Service

            Comment


              #7
              Hello Jim,

              Now, I'm trying to add a time filter to my strategy. I'd like to get long only from 0.00 to 5.00h. So I'm adding a time condition to my strategy builder. Problem here is that I can't get entries that triggers at 0.00h, as time filter affects to all my conditions.

              The sequence is as follows: If time is between 0.00 and 5.00h, look at actual high vs previous high, look at actual low vs previous low and, if all conditions are satisfied, buy at high + 2pip. This opens long positions from 1.00 to 5.00h.

              But I also want long positions that are triggered at 0.00. This would imply that inside bars at 23.00h (day -1) should be evaluated. But if I set my time filter from 23.00 to 5.00h, no long position is opened anywhere, even if I create a group with date and time series to filter.

              Is there any way to solve this problem using strategy builder?
              Attached Files

              Comment


                #8
                Hello esborsa,

                Thanks for your post.

                You should be able to make an overnight hours condition by grouping 2 time conditions to set a bool when either condition group is true.

                I have created a video that outlines how you can create the condition groups in the Strategy Builder.

                https://www.screencast.com/t/AQNUdkXJdoL

                Please let me know if this does not resolve your inquiry.
                JimNinjaTrader Customer Service

                Comment


                  #9
                  Hello Jim,

                  Thank you for yor answer and your time. Your video was very useful.

                  I had to add another condition to set initial value of InTimeFrame to false, otherwise strategy analyzer would ignore time filters. It seems that declaring the variable doesn't set its initial value to false. Is this as expected?
                  Attached Files

                  Comment


                    #10
                    Hello esborsa,

                    Thanks for the reply.

                    Your logic is correct. I have not added a condition or any action to reset the variable back to false in my example, so the behavior would be expected.

                    Please let us know if you have any other questions.
                    JimNinjaTrader Customer Service

                    Comment


                      #11
                      Hello Jim,

                      Can strategy builder do some math? I want to set a stop at previous low and profit target to a multiple of my risk (entry - stop). For that I've created a variable InitialRisk and would like to assign InitialRisk = High[0]-Low[0]. Then, set my target to a multiple of InitialRisk or move my initial stop to breakeven.

                      EDIT: I didn't realize that it's possible to nest some values using Arithmetic offset and I've achieved to assign InitialRisk = High[0]-Low[0] + an imput called RiskOffset (spread+offset). I'll use this variable to set stop level i pips. Is there a shortest path to do this with strategy builder?
                      Attached Files
                      Last edited by esborsa; 04-11-2017, 01:45 PM.

                      Comment


                        #12
                        Hello esborsa,

                        Thanks for the additional question.

                        I am glad you have been able to use some math within the Strategy Builder.

                        As for a quickest path to do this, there is not much to offer through the GUI to make this any easier. The only way that you can decrease the number of steps involved in making the strategy is to reconsider the logic to come up with a more efficient approach. Through the design and function of the Strategy Builder, this is not always possible.

                        Please let me know if you have any further questions.
                        JimNinjaTrader Customer Service

                        Comment


                          #13
                          Hello Jim,

                          I'm trying to set my stop to the low of the signal bar and a take profit to 1xrisk. Since I'm opening a long position at High of previous bar, my risk is Hihg[0]-Low[0].
                          To do so I've created a variable called InitialRisk which is calculated when opening conditions are met. I want stop and target to remain the same value until one of them is executed, so I set stop loss in Stops and Targets section as Low[0] and profit target as High[0]+InitialRisk.

                          But when executing the strategy in SA, the next error appears:

                          "Error on calling 'OnStateChange' method: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart."

                          This error appears if I only set stop, only set target or set stop and target, and dissapears if I don't set either stop nor target.

                          Is it possible to set a stop to previous bar's low and a target to a multiple of risk?
                          Attached Files

                          Comment


                            #14
                            Hello esborsa,

                            The error you are receiving is from the added ProfitTarget and StopLoss. The methods for SetProfitTart() and SetStopLoss() can use a variable as input as you have done, but only when that method is called within the OnBarUpdate().

                            The Strategy Builder places these order methods within OnStateChange(). Specifically within State == State.Configure. Since OnStateChange() is called for State.Configure before OnBarUpdate() is called, placing a profit target or stop loss with by a price object will throw an indexing error for that profit target or stop loss.

                            If you want to set a dynamic profit target or stop loss before in which you can use a price object to set the order price, you will have to unlock the code and place the SetProfitTarget() and SetStopLoss() methods within appropriate logic in OnBarUpdate().

                            Please see the documentation below for syntax and usage:

                            SetProfitTarget() - https://ninjatrader.com/support/help...ofittarget.htm

                            SetStopLoss() - https://ninjatrader.com/support/help...etstoploss.htm

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

                            Comment


                              #15
                              Hello Jim,

                              I tried to set stop loss and profit target through Conditions and Actions part of Strategy builder using exit long position and either unlocking the code and moving SetStopLoss() from State.Configure to OnBarUpdate() but stop always went to last bar's low. I wanted it fixed to one especific bar's low, so it didn't worked for me. But playing with the code I realized that defining SetStopLoss() before EnterLongStopMarket() sets stop loss point and doesn't move at each bar's low if conditions aren't met. Code could be as follows:

                              if (entry conditions)
                              {
                              SetStopLoss(CalculationMode.Price, Low[0]);
                              EnterLong();
                              }

                              But it moves stop loss to another bar's low if conditions defined for entry are met.

                              For example, I will enter long if (High[0] <= High[1]) && (Low[0] >= Low[1]) and place a stop loss at Low[0] and won't move it unless previous conditions appear again. This is not a problem if I have no long position but will move my stop loss if I have a long position working. I've solved this adding if ((Position.MarketPosition == MarketPosition.Flat) but this won't work if I hold multiple posiitons from previous entry signals. How should I manage this whith multiple positions?

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by DTSSTS, Today, 02:46 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post DTSSTS
                              by DTSSTS
                               
                              Started by corman, Today, 02:45 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post corman
                              by corman
                               
                              Started by sandman, Today, 02:32 PM
                              1 response
                              6 views
                              0 likes
                              Last Post NinjaTrader_PatrickG  
                              Started by ksarin, Today, 12:39 PM
                              3 responses
                              12 views
                              0 likes
                              Last Post NinjaTrader_PatrickG  
                              Started by ttodua, Today, 12:03 PM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Working...
                              X