Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Reverse using EnterShortLimit()

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

    Reverse using EnterShortLimit()

    I am trying to reverse an open long position using ExitLong(signalName) to close the existing open position and then EnterShortLimit(shares, entryPrice, NewSignalName) to open a new position in the opposite direction.

    My limit order to reverse the position is being canceled. Why? Supposing I am long 100 shares, I want to send 1 sell order for 100 shares as a market order, and 1 sell order for 100 shares as a limit order. As long as both orders execute, I should result in being short 100 shares, no matter what sequence these 2 orders execute in. Is there a way to do this in NT?

    Below is my code, and TraceOrders output:

    //### Exit exising positions
    if ( Position.MarketPosition == MarketPosition.Long ) {
    Print("(MACD_TRADER) Closing Open Position: " +tradeId);
    ExitLong(tradeId); //### Exit any currently open opposite position
    }

    //### Entry Order
    tradeId = "Short ("+CurrentBar +") " +entryPrice;
    Print("(MACD_TRADER) Short Signal: " +hookType +" " +tradeId);
    EnterShortLimit(shares, entryPrice, tradeId); //### Limit order

    //### Stop/Trail Order
    if ( tickTrail > 0 ) SetTrailStop(tradeId, CalculationMode.Ticks, tickTrail, false); //### Trail Stop
    else if ( barStop >= 0 ) SetStopLoss (tradeId, CalculationMode.Price, High[HighestBar(High, barStop)] +(2*TickSize), false); //### Bar Stop
    else SetStopLoss (tradeId, CalculationMode.Ticks, tickStop, false); //### Regular Stop
    SetProfitTarget(tradeId,CalculationMode.Ticks, tickTarget);


    Here are the entries in my output window using TraceOrders=true:

    (MACD_TRADER) Long Signal: Up.Trend Buy (9173) 115.07
    3/10/2010 3:09:25 PM Entered internal PlaceOrder() method at 3/10/2010 3:09:25 PM: Action=Buy OrderType=Limit Quantity=500 LimitPrice=115.07 StopPrice=0 SignalName='Buy (9173) 115.07' FromEntrySignal=''
    3/10/2010 3:09:25 PM Entered internal SetStopTarget() method: Type=TrailStop FromEntrySignal='Buy (9173) 115.07' Mode=Ticks Value=15 Currency=0 Simulated=False
    3/10/2010 3:09:25 PM Entered internal SetStopTarget() method: Type=Target FromEntrySignal='Buy (9173) 115.07' Mode=Ticks Value=1000 Currency=0 Simulated=False
    3/10/2010 3:19:36 PM Amended trail stop order: Order='NT-02952/Sim101' Name='Trail stop' State=Working Instrument='SPY' Action=Sell Limit price=0 Stop price=115.01 Quantity=500 Strategy='MACDTrader' Type=Stop Tif=Gtc Oco='NT-01650' Filled=0 Fill price=0 Token='79a2bdc32d054cb3842ccae00458b58a' Gtd='12/1/2099 12:00:00 AM'

    (MACD_TRADER) Closing Open Position: Buy (9173) 115.07
    3/10/2010 3:27:15 PM Entered internal PlaceOrder() method at 3/10/2010 3:27:15 PM: Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal='Buy (9173) 115.07'

    (MACD_TRADER) Short Signal: Down.Trend Short (9176) 115.03
    3/10/2010 3:27:15 PM Entered internal PlaceOrder() method at 3/10/2010 3:27:15 PM: Action=SellShort OrderType=Limit Quantity=500 LimitPrice=115.03 StopPrice=0 SignalName='Short (9176) 115.03' FromEntrySignal=''
    3/10/2010 3:27:15 PM Entered internal SetStopTarget() method: Type=TrailStop FromEntrySignal='Short (9176) 115.03' Mode=Ticks Value=15 Currency=0 Simulated=False
    3/10/2010 3:27:15 PM Entered internal SetStopTarget() method: Type=Target FromEntrySignal='Short (9176) 115.03' Mode=Ticks Value=1000 Currency=0 Simulated=False
    3/10/2010 3:27:15 PM Cancelled pending exit order, since associated position is closed: Order='NT-02953/Sim101' Name='Profit target' State=Working Instrument='SPY' Action=Sell Limit price=125.07 Stop price=0 Quantity=500 Strategy='MACDTrader' Type=Limit Tif=Gtc Oco='NT-01650' Filled=0 Fill price=0 Token='7f0d7046ab884f7cad8c661fcb194582' Gtd='12/1/2099 12:00:00 AM'
    3/10/2010 3:27:15 PM Cancelled pending exit order, since associated position is closed: Order='NT-02952/Sim101' Name='Trail stop' State=Working Instrument='SPY' Action=Sell Limit price=0 Stop price=115.01 Quantity=500 Strategy='MACDTrader' Type=Stop Tif=Gtc Oco='NT-01650' Filled=0 Fill price=0 Token='79a2bdc32d054cb3842ccae00458b58a' Gtd='12/1/2099 12:00:00 AM'

    #2
    monpere,

    Please post TraceOrder logs in relation to the EnterShortLimit() order. I do not see anything there for that particular order, only things for the stops and targets which would be expected to cancel as the long was closed.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Josh View Post
      monpere,

      Please post TraceOrder logs in relation to the EnterShortLimit() order. I do not see anything there for that particular order, only things for the stops and targets which would be expected to cancel as the long was closed.
      I believe the lines following the entry: "(MACD_TRADER) Short Signal: Down.Trend Short (9176) 115.03" are trace info for the short limit order.

      Comment


        #4
        monpere,

        That was your own output. There is a line from TraceOrders showing you placing a sell short, but nothing else. No indication of any cancelling.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Josh View Post
          monpere,

          That was your own output. There is a line from TraceOrders showing you placing a sell short, but nothing else. No indication of any cancelling.
          I am looking at the following 2 lines. What do they mean? My chart does not show an entry for the short with the SignalName='Short (9176) 115.03' in the output. Did that short order get lost so the associated trail and target orders get cancelled?

          3/10/2010 3:27:15 PM Cancelled pending exit order, since associated position is closed: Order='NT-02953/Sim101' Name='Profit target' State=Working Instrument='SPY' Action=Sell Limit price=125.07 Stop price=0 Quantity=500 Strategy='MACDTrader' Type=Limit Tif=Gtc Oco='NT-01650' Filled=0 Fill price=0 Token='7f0d7046ab884f7cad8c661fcb194582' Gtd='12/1/2099 12:00:00 AM'

          3/10/2010 3:27:15 PM Cancelled pending exit order, since associated position is closed: Order='NT-02952/Sim101' Name='Trail stop' State=Working Instrument='SPY' Action=Sell Limit price=0 Stop price=115.01 Quantity=500 Strategy='MACDTrader' Type=Stop Tif=Gtc Oco='NT-01650' Filled=0 Fill price=0 Token='79a2bdc32d054cb3842ccae00458b58a' Gtd='12/1/2099 12:00:00 AM'
          Last edited by monpere; 03-15-2010, 04:53 PM.

          Comment


            #6
            monpere,

            Those lines are not in relation to your entry order. Those are cancels for your stops and targets which is not what we are looking for. We are looking for the output in relation to the entry order only.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Josh View Post
              monpere,

              Those lines are not in relation to your entry order. Those are cancels for your stops and targets which is not what we are looking for. We are looking for the output in relation to the entry order only.
              I'm not sure what I should be looking for. These are the last lines in my output window. It seems to me that the following line is for the short entry order, but I don;t see that order on the chart.

              (MACD_TRADER) Short Signal: Down.Trend Short (9176) 115.03
              3/10/2010 3:27:15 PM Entered internal PlaceOrder() method at 3/10/2010 3:27:15 PM: Action=SellShort OrderType=Limit Quantity=500 LimitPrice=115.03 StopPrice=0 SignalName='Short (9176) 115.03' FromEntrySignal=''

              Does this mean the order was considered executed? If so, why did the associated sotp/targets get canceled. If that is not the entry, tell me what I should be looking for in the output.

              Comment


                #8
                Originally posted by monpere View Post
                3/10/2010 3:27:15 PM Cancelled pending exit order, since associated position is closed: Order='NT-02953/Sim101' Name='Profit target' State=Working Instrument='SPY' Action=Sell Limit price=125.07 Stop price=0 Quantity=500 Strategy='MACDTrader' Type=Limit Tif=Gtc Oco='NT-01650' Filled=0 Fill price=0 Token='7f0d7046ab884f7cad8c661fcb194582' Gtd='12/1/2099 12:00:00 AM'
                This is the cancellation of the target order set at 3:09:25 PM,
                i.e. 3/10/2010 3:09:25 PM Entered internal SetStopTarget() method: Type=Target FromEntrySignal='Buy (9173) 115.07' Mode=Ticks Value=1000 Currency=0 Simulated=False

                Originally posted by monpere View Post
                3/10/2010 3:27:15 PM Cancelled pending exit order, since associated position is closed: Order='NT-02952/Sim101' Name='Trail stop' State=Working Instrument='SPY' Action=Sell Limit price=0 Stop price=115.01 Quantity=500 Strategy='MACDTrader' Type=Stop Tif=Gtc Oco='NT-01650' Filled=0 Fill price=0 Token='79a2bdc32d054cb3842ccae00458b58a' Gtd='12/1/2099 12:00:00 AM'
                This is the cancellation of the stop order entered at 3:09:25 PM, which was then amended at 3:19:36 PM,

                i.e. first
                3/10/2010 3:09:25 PM Entered internal SetStopTarget() method: Type=TrailStop FromEntrySignal='Buy (9173) 115.07' Mode=Ticks Value=15 Currency=0 Simulated=False

                then 3/10/2010 3:19:36 PM Amended trail stop order: Order='NT-02952/Sim101' Name='Trail stop' State=Working Instrument='SPY' Action=Sell Limit price=0 Stop price=115.01 Quantity=500 Strategy='MACDTrader' Type=Stop Tif=Gtc Oco='NT-01650' Filled=0 Fill price=0 Token='79a2bdc32d054cb3842ccae00458b58a' Gtd='12/1/2099 12:00:00 AM'

                Instead of the above, which as Josh says relate to your exit order, you are looking for similar trace reports, but for your new entry order; for example, for the target order associated with your new entry order you will see "Action=Buy Limit price=105.03" (i.e. 1000 ticks below your entry), and something similar for your stop order trace.

                If you can't see these lines, it's probably because your EnterShortLimit order is being ignored. Put a print statement after this order to see if your code gets this far ...

                Subject to Josh agreeing that the above and the following are correct, if the order is getting ignored, perhaps it is because NT thinks the strategy is trying to enter both a long and a short position at the same time (which I know is not supported). You may need to wait to you have fill confirmation of the exit before you can enter your short order. Again assuming Josh thinks this approach is correct, you may need to use code from the SampeOnOrderUpdate snippet ...

                Hope this helps, i.e that it brings further clarity, rather than muddies the water some more ...

                Comment


                  #9
                  AnotherTrader,

                  3/10/2010 3:27:15 PM Entered internal PlaceOrder() method at 3/10/2010 3:27:15 PM: Action=SellShort OrderType=Limit Quantity=500 LimitPrice=115.03 StopPrice=0 SignalName='Short (9176) 115.03' FromEntrySignal=''

                  That is the line referencing the short being placed. What happens afterwards is unclear as there are no printouts to follow posted yet.

                  monpere,

                  As AnotherTrader noted, should there be no TraceOrders output in addition to what you have already provided you will then need to further track the order through printouts from OnOrderUpdate(). Instead of posting snippets you can just copy paste a larger segment of TraceOrders output around that time and we can sift through it to find what is relevant. It would also be useful if you could just post the log/trace files.
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    Ok, I was trying not to deluge the post with huge logs. I no longer have the data for this particular instance. But I created another instance of the issue.
                    In this instance I have a entered a long, and I am using SetStopLoss() to trail the long position behind certain bars as I get more into profit. The adjusted stop prices are identified on the chart with the red lines, and show up in my output window as "SetStopTarget() method: Type=Stop" entries.

                    I have attached a chart, output window, and NT log at that time. To get the output, I reset the simulator, and reloaded the chart with the strategy. As you can see on the chart and the output log, the position should have stopped out at 1.5191, but there is no stop out there. Why is that? Are my SetStopLoss() calls being ignored? How do I perform a custom trail stop using SetStopLoss()?

                    Output Window:

                    (MACD_TRADER) Long Signal: Up.Trend Buy (22) 1.5182
                    3/16/2010 11:49:08 AM Entered internal PlaceOrder() method at 3/16/2010 11:49:08 AM: Action=Buy OrderType=Limit Quantity=1 LimitPrice=1.5182 StopPrice=0 SignalName='Buy (22) 1.5182' FromEntrySignal=''
                    3/16/2010 11:49:08 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='Buy (22) 1.5182' Mode=Price Value=1.5175 Currency=0 Simulated=False
                    3/16/2010 11:49:08 AM Entered internal SetStopTarget() method: Type=Target FromEntrySignal='Buy (22) 1.5182' Mode=Ticks Value=1000 Currency=0 Simulated=False
                    Trailing Bar 25 to 1.5176
                    3/16/2010 11:53:53 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=1.5176 Currency=0 Simulated=False
                    3/16/2010 11:53:53 AM Ignored internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=1.5176 Currency=0 Simulated=False' Reason='Another SetStopTarget() method call has already submitted a stop/target order'

                    Trailing Bar 26 to 1.5177
                    3/16/2010 11:54:44 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=1.5177 Currency=0 Simulated=False

                    Trailing Bar 28 to 1.5182
                    3/16/2010 11:55:02 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=1.5182 Currency=0 Simulated=False

                    Trailing Bar 29 to 1.5187
                    3/16/2010 11:55:46 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=1.5187 Currency=0 Simulated=False

                    Trailing Bar 30 to 1.5191
                    3/16/2010 11:57:05 AM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=1.5191 Currency=0 Simulated=False

                    NT Log:

                    Date,Category,Message,
                    3/16/2010 12:23:38 PM,Order,Order='7c4b9e402f3b4c70a5065f6f55d4e747/Sim101' Name='Profit target' New State=Working Instrument='6B 06-10' Action=Sell Limit price=1.6182 Stop price=0 Quantity=1 Type=Limit Filled=0 Fill price=0 Error=NoError Native error='',
                    3/16/2010 12:23:38 PM,Order,Order='7c4b9e402f3b4c70a5065f6f55d4e747/Sim101' Name='Profit target' New State=Accepted Instrument='6B 06-10' Action=Sell Limit price=1.6182 Stop price=0 Quantity=1 Type=Limit Filled=0 Fill price=0 Error=NoError Native error='',
                    3/16/2010 12:23:38 PM,Order,Order='9ee9157d455e4265a6b785e2a7f6ba19/Sim101' Name='Stop loss' New State=Accepted Instrument='6B 06-10' Action=Sell Limit price=0 Stop price=1.5175 Quantity=1 Type=Stop Filled=0 Fill price=0 Error=NoError Native error='',
                    3/16/2010 12:23:37 PM,Order,Order='7c4b9e402f3b4c70a5065f6f55d4e747/Sim101' Name='Profit target' New State=PendingSubmit Instrument='6B 06-10' Action=Sell Limit price=1.6182 Stop price=0 Quantity=1 Type=Limit Filled=0 Fill price=0 Error=NoError Native error='',
                    3/16/2010 12:23:37 PM,Order,Order='9ee9157d455e4265a6b785e2a7f6ba19/Sim101' Name='Stop loss' New State=PendingSubmit Instrument='6B 06-10' Action=Sell Limit price=0 Stop price=1.5175 Quantity=1 Type=Stop Filled=0 Fill price=0 Error=NoError Native error='',
                    3/16/2010 12:23:37 PM,Strategy,Starting NinjaScript strategy 'MACDTrader/57ec95ce02944846848d326e4c07f47c' : On starting a real-time strategy - StrategySync=SubmitLive EntryHandling=AllEntries EntriesPerDirection=1 StopTargetHandling=PerEntryExecution ErrorHandling=StopStrategyCancelOrdersClosePositio ns ExitOnClose=False Set order quantity by=Strategy,
                    3/16/2010 12:22:40 PM,Default,Simulation account 'Sim101' reset,
                    Attached Files
                    Last edited by monpere; 03-16-2010, 10:53 AM.

                    Comment


                      #11
                      Please reattach the chart as it seems to be missing. Also, it may be more useful if you can just post the full logs so I can look through it as the snippet you provided seems to not contain all information as it currently stands. Log files are located at Documents\NinjaTrader 6.5\logs. Thank you.
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #12
                        Originally posted by NinjaTrader_Josh View Post
                        Please reattach the chart as it seems to be missing. Also, it may be more useful if you can just post the full logs so I can look through it as the snippet you provided seems to not contain all information as it currently stands. Log files are located at Documents\NinjaTrader 6.5\logs. Thank you.
                        I attached the log and the chart image.
                        Attached Files
                        Last edited by monpere; 03-16-2010, 11:04 AM.

                        Comment


                          #13
                          monpere,

                          Your timestamps posted do not correlate to one another. One is 11:50 and the other is 12:20. It may be easier if you just upload the complete log and trace files and tell me the starting timestamp I should look at. Thank you.
                          Josh P.NinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                          0 responses
                          648 views
                          0 likes
                          Last Post Geovanny Suaza  
                          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                          0 responses
                          369 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by Mindset, 02-09-2026, 11:44 AM
                          0 responses
                          108 views
                          0 likes
                          Last Post Mindset
                          by Mindset
                           
                          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                          0 responses
                          573 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by RFrosty, 01-28-2026, 06:49 PM
                          0 responses
                          575 views
                          1 like
                          Last Post RFrosty
                          by RFrosty
                           
                          Working...
                          X