Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy won't trade??

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

    Strategy won't trade??

    OK, I'm losing my mind here.

    I was testing my complicated strategy in Sim Mode, but nothing ever fired off when it was supposed to.

    So I started debugging, commenting out section by section to see what might have been causing the problems.

    I got down to the bare bones, no calculations, nothing at all except the "if this then enter" lines. Still nothing.

    So I created a new strategy in Sim that had absolutely nothing but this in it:

    Code:
    protected override void OnBarUpdate()
            {
    				if (Close[0] < Open[0])
    					{ 
                                               EnterLong(4);			
    					}
    							
    				if (Close[0] > Open[0])
    					{
                                               EnterLong(4);			
    					}
            }
    I know it's logically preposterous, but I'm just trying to get down to a level that works, and build back up from there. But even THIS doesn't work. No entries. Not on a 3 Min chart, not on a tick chart, nowhere. Every bar on every chart running this strategy should go long, and none of them do. No errors, no messages, no "order pending", no nothing. What am I missing here? (and yes, the strategies are set to Enabled = TRUE.)

    Thanks in advance,
    Gary

    #2
    Hello Gary,
    Thanks for posting today.

    If you could test a few things. Close the current workspace and create a new blank workspace. Load a new chart and try running the script.

    Make sure that the strategy is enabled when running as you have said you had before. Lastly from the Control Center>Tools>Output Window...

    Let me know if any messages appear here.

    I look forward to your reply.
    Alex G.NinjaTrader Customer Service

    Comment


      #3
      Interesting, I was doing exactly that as you were replying. I rebooted, created a new workspace, created one single chart and created a new strategy in the Wizard:

      Code:
        protected override void OnBarUpdate()
              {
                  // Condition set 1
                  if (Close[0] > Open[0])
                  {
                      EnterLong(4, "");
                  }
              }
      Still absolutely nothing. Output window is blank.

      Comment


        #4
        OK, now I added some visual cues just to see if any of the logic is actually doing anything:

        Code:
         protected override void OnBarUpdate()
                {
                    // Condition set 1
                    if (Close[0] > Open[0])
                    {
                        DrawArrowDown("ArrowDown" + CurrentBar.ToString(), true, 0, High[0] + (TickSize * 7), Color.Green);
                        EnterLong(4, "");
                    }
                }
        And I'm still not getting any order entries, but at least the arrows are showing up.
        Attached Files
        Last edited by hawks67; 03-19-2015, 10:57 AM.

        Comment


          #5
          Hello Gary,
          Thanks for the reply and test information.

          From the Control Center>Strategies Tab
          When the strategy is enabled is it showing up as yellow?

          A yellow highlighted "Strategy" cell indicates the strategy is waiting until it reaches a flat position to be in sync with the account position before fully starting.

          This would prevent an order being placed if a strategy is being run over historical data. Even if your position is flat the strategy WOULD have placed an order. So the Strategy Position would be 4 long even if your count is Flat.

          Here is a link to help guide describing Strategy Position vs. Account Position.
          http://www.ninjatrader.com/support/h..._account_p.htm

          If you wish your strategy to still place an order before the strategy position is flat you can change a setting from within the Strategies tab in the Options menu.
          • From the Control Center>Strategies Tab>NinjaScript Tab
          • Select the Immediately submit live working historical orders option
          • Left mouse click OK to close the Options menu


          This should allow you to place your orders but be wary that this will affect how your strategy is run as it concerns strategy position versus account position.
          Alex G.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_AlexG View Post

            When the strategy is enabled is it showing up as yellow?
            Yes, indeed it is. In reading up on how that might have occurred, my brain exploded.

            A yellow highlighted "Strategy" cell indicates the strategy is waiting until it reaches a flat position to be in sync with the account position before fully starting.

            This would prevent an order being placed if a strategy is being run over historical data. Even if your position is flat the strategy WOULD have placed an order. So the Strategy Position would be 4 long even if your count is Flat.
            Sounds reasonable. All I really want to know is how to fix this without changing any important settings and risking anything unusual. The info found at the links you sent says something about manually entering an offsetting trade. This would be fine, but I have no idea what the "position" my strategy is currently holding is. I don't know the direction and I don't know the quantity. I was hoping this is just a matter of going into the Control Panel, selecting Market Order and a quantity and hitting either BUY or SELL, but I did that a few times in both directions with a variety of quantities, to no avail. The Strategy is still yellow.

            Any ideas? Anything really basic and simple I can do without changing any significant settings going forward? I just want to "flatten" this strategy position.

            Comment


              #7
              OK, since I was in Sim, I tried some things.

              I changed the setting to Immediately submit live working historical orders, and re-enabled the strategy. As expected, an order was immediately "placed". No problem. I "Flattened Everything", expecting that this would even/balance everything out. Set it back to "Wait until flat", re-enabled the strategy, and it's still yellow. This is SO odd...never saw this before in all my years using NT...

              (Went back and tried again)

              This time I looked at the strategy status when it got re-enabled with the Immediately submit live working historical orders box checked, and it was green. Saw that it was 4L, so I went and manually Sold 4 of the same contract. That should have done it, right? Nope. Disbled and re-enabled the strategy and it's back to yellow again.
              Last edited by hawks67; 03-19-2015, 12:48 PM.

              Comment


                #8
                Hello Gary,

                The steps you first took make sense with what you see.
                • First you turned on "Immediately submit live working historical orders"
                • When you turn on the strategy an historical order is filled. (Strategy position long 4 /Account position FLAT)
                • Your account is not actually long 4 since the EnterLong(4) would have executed before any live data was loaded.


                The strategy is running on all loaded bars (historical data). It would have executed the EnterLong(4); call on the first loaded bar. If 365 day bars loaded, it would EnterLong(4) 1 year ago.

                When you close the strategy nothing happens. You flatten everything, but nothing needs to be flattened as your account position is still flat.

                When you switch back to "Wait until Flat" the strategy position would be 4 long. Yet it does not show this because it is waiting until the strategy would be flat before placing an order to the account position. So the strategy tab shows yellow.

                This example from the help guide explains what is happening well.

                "An extremely common scenario…
                An extremely common scenario is starting a NinjaScript strategy in the middle of a trading session, such as one hour after the session has begun. The NinjaScript strategy is run on each historical bar for the 1st hour of the session (it will actually run on all historical data loaded in a chart) to determine the current position state it would be in if it had been running live since the start of the session. This position state then becomes the Strategy Position for your strategy. Let us assume that during the historical hour your strategy would have entered a LONG 1 position and the position is still open. This would mean the Strategy Position is LONG 1 and since this trade was not actually executed on an account, your Account Position is FLAT."


                When your strategy(4long) was showing 4 long it wasn't actually 4 long on your account(FLAT). So if you manually placed 4 short your account position would be 4 short.

                If you have data loaded on your chart it will always be yellow with the strategy
                Code:
                protected override void OnBarUpdate()
                        {
                				if (Close[0] < Open[0])
                					{ 
                                                           EnterLong(4);			
                					}
                							
                				if (Close[0] > Open[0])
                					{
                                                           EnterLong(4);			
                					}
                        }
                because your code would never close the position allowing it to sync with your account.

                The behavior you are seeing is expected even if it is a little confusing.

                A solution to add to your strategy is to do a check if historical.

                Add this line of code to your OnBarUpdate() method before you call your first if statement.

                Code:
                if (historical)
                {
                return;
                }
                What this will do is to skip over all the historical data before entering a position. Thus you will start your strategy as flat when OnBarsUpdate() calls the current bar.

                I hope this clarifies the behavior you are seeing and if not let me know and I can find another way to explain what you are experiencing.
                Alex G.NinjaTrader Customer Service

                Comment


                  #9
                  Alex, thanks.

                  It's good to know there is at least something I can do, and I will do those things if I absolutely have to, but I would rather exhaust all other options before I go adding lines of code (like the "if (historical)") that I never had to add before I got into this pickle. NT has always worked perfectly well regardless of how I abused it with my code...and I never had to make changes to system settings to get things to work right. If things didn't work right, it was because my code was inferior...and this time is no exception.

                  I fully understand how I got here now, thank you. The temporary code I used to test the issues I was having with my main strategy (which handles trades properly and doesn't leave them hanging) caused more problems than it was addressing. Suffice it to say I won't do that again.

                  So, since that was just a garbage test strategy, if I were to trash it and create a new one with proper exit procedures that prevent "permanent" long positions, will it fix this current situation and avoid its recurrence? Or has the fact that this situation was brought into existence something that is going to last forever - virtual strategy positions that can never be closed because they were once opened without an exit strategy - regardless of any other strategy, new or old, that is executed? If this is merely a case of trashing this strategy - again, it was only created to test a problem I was experiencing - and creating a completely new one that doesn't leave hanging positions, then that's exactly what I want to do.

                  I appreciate the significant time you spent explaining the situation. At this point, I don't even care about the how; I just want this problem gone, and I want to avoid it in the future. We've achieved the second part, for sure. Will deleting the offending code and replacing it with proper position-managing code (like I've always used in the past) completely take care of the first part?

                  Thanks!

                  Comment


                    #10
                    Gary,

                    No, this will not leave a lasting effect. When you start a new strategy it will have its own instance with its own set of positions and not reference old positions from old strategy instances.

                    Yes, a new strategy with properly managed entries and exits will not be effected by this past strategy instance.

                    You may create a new strategy without this instance causing any positional issues.

                    Glad to have helped, please let us know if we may be of further assistance for anything NinjaTrader.
                    Alex G.NinjaTrader Customer Service

                    Comment


                      #11
                      Thanks, Alex.

                      I went ahead and went back to the issue I was having that started this whole thing, and it's still not behaving right. I don't know if this should be a new thread or not, but...technically, it's still about a strategy that won't trade....

                      I've whittled it down to the basics again, this time with appropriate trade management. Again, it's preposterous code, but I wanted to ensure that I'd get examples. Once I get this part down, I'll build it from there with more specific conditions. And, again, every bar here should kick off a trade.

                      Local variables are: quantity, profitTicks and stopTicks.

                      Code:
                      		if (Close[0] < Open[0])
                      				{ 
                      
                      					if ( (Position.MarketPosition == MarketPosition.Flat) && 
                      						(useEntries == true)   ) 
                      					{	EnterLongLimit(quantity, GetCurrentBid() - (TickSize * 4));  
                      						SetProfitTarget(CalculationMode.Ticks, profitTicks); SetStopLoss(CalculationMode.Ticks, stopTicks);  
                      					}		
                      			
                      				}
                      							
                      			if (Close[0] > Open[0])
                      				{
                      
                      					if ( (Position.MarketPosition == MarketPosition.Flat) && 
                      						(useEntries == true)   ) 
                      					{	EnterLongLimit(quantity, GetCurrentBid() - (TickSize * 4));  
                      						SetProfitTarget(CalculationMode.Ticks, profitTicks); SetStopLoss(CalculationMode.Ticks, stopTicks);  
                      					}		
                      			
                      				}
                      I run it in Sim and it seems like every bar I hear "order pending", but each time it's almost immediately followed by "order cancelled". No trades actually take place. No "order filled", EVER. Same behavior regardless of the data series (seconds, minutes, ticks). (The strategy is highlighted green in the Strategy tab.)
                      Last edited by hawks67; 03-20-2015, 09:12 AM.

                      Comment


                        #12
                        This morning I started anew, with a new workspace and a new strategy that contained only this (same as last post):

                        Code:
                                                if (Close[0] < Open[0])
                        				{ 
                        
                        					if ( (Position.MarketPosition == MarketPosition.Flat) && 
                        						(useEntries == true)   ) 
                        					{	EnterLongLimit(quantity, GetCurrentBid() - (TickSize * 4));  
                        						SetProfitTarget(CalculationMode.Ticks, profitTicks); SetStopLoss(CalculationMode.Ticks, stopTicks);  
                        					}		
                        			
                        				}
                        							
                        			if (Close[0] > Open[0])
                        				{
                        
                        					if ( (Position.MarketPosition == MarketPosition.Flat) && 
                        						(useEntries == true)   ) 
                        					{	EnterLongLimit(quantity, GetCurrentBid() - (TickSize * 4));  
                        						SetProfitTarget(CalculationMode.Ticks, profitTicks); SetStopLoss(CalculationMode.Ticks, stopTicks);  
                        					}		
                        			
                        				}
                        And still just "order pending" and then, immediately, "order cancelled". The logs don't tell me anything.....

                        Comment


                          #13
                          Hello Gary,
                          Thanks for the update today.

                          This behavior indicates an issue with the Sim101 account and how it manages the database. If you could please reset the Sim101 account and then retest this strategy.

                          Please follow the instructions below to reset a simulation account.
                          • From the NinjaTrader Control Center window select the menu Tools > Options
                          • Select the "Simulator" tab
                          • Press the "Reset" button


                          Please let me know if this resolves the issue that you are experiencing.
                          Alex G.NinjaTrader Customer Service

                          Comment


                            #14
                            Regrettably, Alex, it did not.

                            I'm running it on a 13 tick chart (again, just to get lots of opportunities for this thing to fire off), and each bar I'm still getting "order pending", followed by "order cancelled".

                            One thing I found interesting is that on another chart that has Chart Trader going on it, the Buy LMT indicator was floating in synchronization with the price movement between the "order pending" and "order cancelled". Why would that be? If the limit order was set to a specific price point, shouldn't it stay put?
                            Attached Files

                            Comment


                              #15
                              Hello Gary,

                              I conferred with another Ninjacscript support representative about what you are seeing. This is an under the hood default that NinjaTrader uses to prevent unwanted extra orders.

                              In the help guide it describes why this would be seen when using the managed approach.
                              http://www.ninjatrader.com/support/h...d_approach.htm

                              A few key points:
                              • Orders are submitted as live working orders when a strategy is running in real-time
                              • Profit targets, stop loss and trail stop orders are submitted immediately as entry orders are filled and are also OCO tied
                              • Order changes and cancellations are queued in the event that the order is in a state where it can't be cancelled or modified
                              • By default, orders submitted via Entry() and Exit() methods automatically cancel at the end of a bar if not re-submitted


                              In this case, since the bars are really short you will see the orders submit and seem immediately cancelled. It is cancelling when that bar closes. If your target or stop loss were hit before that bar closes they would have been filled before being canceled.

                              Here is a link to a sample code showing how to prevent a position from closing, called Keeping orders alive.
                              http://www.ninjatrader.com/support/f...ad.php?t=19169

                              This sample should be able to be adapted to your code and prevent your entry from closing the position.
                              Alex G.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by FrancisMorro, Today, 03:24 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post FrancisMorro  
                              Started by Segwin, 05-07-2018, 02:15 PM
                              10 responses
                              1,770 views
                              0 likes
                              Last Post Leafcutter  
                              Started by Rapine Heihei, 04-23-2024, 07:51 PM
                              2 responses
                              31 views
                              0 likes
                              Last Post Max238
                              by Max238
                               
                              Started by Shansen, 08-30-2019, 10:18 PM
                              24 responses
                              944 views
                              0 likes
                              Last Post spwizard  
                              Started by Max238, Today, 01:28 AM
                              0 responses
                              11 views
                              0 likes
                              Last Post Max238
                              by Max238
                               
                              Working...
                              X