• 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

Cancel Position and Open New Position in same bar

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

  • Capablanca
    started a topic Cancel Position and Open New Position in same bar

    Cancel Position and Open New Position in same bar

    I'm stuck in this simple problem, and can't find a way out.
    I'm using a multi-time series strategy and I'm doing trades in both instruments simultaneously.

    I need the strategy to close any open position when I enter a reverse trade. (ej. if I am going short, I would like to close it and then enter a long position)
    Everything has to be done in the same bar and in the same run of the BarUpdate() and if I have the entry condition for a long but I'm already in a long position, nothing is done.

    The problem is that the Short position (in the example code below) is not close with the ExitShort() command. What can be wrong?

    Mi code is the following:
    PHP Code:
    if(Entry Condition)
          if(
    myEntryOrder2 != null)
          {
                  if(
    myEntryOrder2.OrderAction == OrderAction.SellShort)
                  {
                         
    ExitShort(1,c2,"Inst2","Inst2"); 
                         
    myEntryOrder2=EnterLong(1,c2,"Inst2");
                   }
           }
           else
                   
    myEntryOrder2=EnterLong(1,c2,"Inst2");


  • NinjaTrader_Jesse
    replied

    Hello Capablanca,

    But for some reason the position is not closed and instead of having 1 contract per position I get 2 contracts and the whole strategy becomes a mess.
    I was using the ExitLong and ExitShort because from what I read in the link:
    Using Exit and Enter in the same OnBarUpdate creates this effect. When you exit the position, enter a position and close a position at once this is creating an incorrect quantity.

    I look forward to being of further assistance.

    Leave a comment:


  • NinjaTrader_Jesse
    replied
    Hello bltdavid,

    I presumed OP was wanting the position reversal to be completed entirely within the same OnBarUpdate call.

    That is, I presumed the OP wanted the exiting of the current position and the entry into the new position to
    be completed before the OnBarUpdate returned, such that if MarketPosition was Long at the beginning of
    OnBarUpdate, he could reverse the position and MarketPosition would be Short at end of the SAME call
    to OnBarUpdate. I said to require this is not possible.
    I had not reviewed your reply for inaccuracies but only provided information in contrast to the original question. I am also not making any assumptions of what the op's indented goal may be other that what is stated in the question. I was unable to reach the same conclusions you have made based on the given syntax, however you are correct it is not possible for the position to update in that span of time. I also touched on this with my comment

    Using two actions in a single OnBarUpdate will not work as that does not allow the position to update before the second order method has been called.

    Jesse, what do you mean by "one action in one OnBarUpdate call"? Are you saying MarketPosition can be Long
    on entry to OnBarUpdate and by a single call to EnterShort() the OP can make MarketPosition change to Short
    before the same OnBarUpdate returns?
    No, by one action I mean what I had written I will expand on this:

    Using two actions in a single OnBarUpdate will not work as that does not allow the position to update before the second order method has been called.
    You cant call ExitShort then EnterLong (two actions) in the same OnBarUpdate as this does not allow the position to update before the second action (EnterLong) was called. By the end of OnBarUpdate, the position has not changed even after calling an order entry method, you need to wait for the position to update. On the next OnBarUpdate, if the position has changed we will see that reflected with the MarketPosition property.

    Contrast your answer with Op's stated desire "Everything has to be done in the same bar and in the same run
    of the BarUpdate()".
    I am not sure what you are asking me to contrast here, my reply was in contrast to this question. Correctly using the Entry methods instead of both an Exit and an Entry for a reversal allows a reversal to happen in one call to OnBarUpdate. If you are Short and call EnterLong, this reverses the position and in a single call to EnterLong which equates to a single call to OnBarUpdate or all at once.

    If either of our understanding missed the mark on the OP's question, I am sure that the OP will correct us and provide more details about why. Rather than making any assumptions it would be best to provide the core information for what is being done in the sample, why what is being done wont work and the suggestion on what could work in contrast to the given details. If more is needed beyond that I am sure we will hear back from OP. If you feel you still have more questions on this topic, I would ask that you make a new thread for your specific questions so that OP has the chance to use the thread they have created for their questions.



    Please let me know if I may be of additional assistance.



    Leave a comment:


  • bltdavid
    replied
    Are you using unique signal names for your orders?

    I think by "unique" NinjaTrader assumes all order names to be unique
    within the context of a single execution run of the strategy. The best
    way to achieve this is to append a counter to a basename, and increment
    the counter for every order submitted.

    [I myself just use "S"+CurrentBar for short entries and "L"+CurrentBar
    for long entries -- and every order name is guaranteed to be unique.]

    Leave a comment:


  • Capablanca
    replied
    Thank you David and Jesse,

    I have these lines in Initialize()
    CalculateOnBarClose = true;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    ExitOnClose = true;

    I agree with what you say:
    "I don't think your call to ExitShort() is strictly necessary, and should probably not be
    there. Why? Because the next line is EnterLong(), and it knows to automatically exit the
    current Short position (if there is one) before entering the Long order."

    But for some reason the position is not closed and instead of having 1 contract per position I get 2 contracts and the whole strategy becomes a mess.
    I was using the ExitLong and ExitShort because from what I read in the link:

    https://ninjatrader.com/support/help...d_approach.htm
    Methods that generate orders to exit a position will be ignored if:
    A position is open and an order submitted by an enter method (EnterLongLimit() for example) is active and the order is used to open a position in the opposite direction
    A position is open and an order submitted by a set method (SetStopLoss() for example) is active
    Note: These rules do not apply to market orders like ExitLong() or ExitShort().


    And yes I want exactly "the position reversal to be completed entirely within the same OnBarUpdate call."

    Leave a comment:


  • bltdavid
    replied
    Originally posted by NinjaTrader_Jesse View Post
    This will close the long position and enter short as one action in one OnBarUpdate call.
    I presumed OP was wanting the position reversal to be completed entirely within the same OnBarUpdate call.

    That is, I presumed the OP wanted the exiting of the current position and the entry into the new position to
    be completed before the OnBarUpdate returned, such that if MarketPosition was Long at the beginning of
    OnBarUpdate, he could reverse the position and MarketPosition would be Short at end of the SAME call
    to OnBarUpdate. I said to require this is not possible.

    Jesse, what do you mean by "one action in one OnBarUpdate call"? Are you saying MarketPosition can be Long
    on entry to OnBarUpdate and by a single call to EnterShort() the OP can make MarketPosition change to Short
    before the same OnBarUpdate returns?

    Contrast your answer with Op's stated desire "Everything has to be done in the same bar and in the same run
    of the BarUpdate()".

    Thanks.

    Leave a comment:


  • NinjaTrader_Jesse
    replied
    Hello Capablanca,

    To reverse a position you can simply call the opposing entry order to do what you are asking.

    For example, if you are long and want to reverse to short, you would call EnterShort(). This will close the long position and enter short as one action in one OnBarUpdate call. Using two actions in a single OnBarUpdate will not work as that does not allow the position to update before the second order method has been called.

    I have the entry condition for a long but I'm already in a long position, nothing is done.
    You can use the Position.MarketPosition to know you are already long to prevent anything else from happening. Otherwise if you mean that you wanted another long position to happen, you would need to increase the EntriesPerDirection to allow more than one entry in a single direction.

    https://ninjatrader.com/support/help...MarketPosition
    https://ninjatrader.com/support/help...sub=entriesper

    I look forward to being of further assistance.

    Leave a comment:


  • bltdavid
    replied
    Originally posted by Capablanca View Post
    I'm stuck in this simple problem, and can't find a way out.
    I'm using a multi-time series strategy and I'm doing trades in both instruments simultaneously.

    I need the strategy to close any open position when I enter a reverse trade. (ej. if I am going short, I would like to close it and then enter a long position)
    Everything has to be done in the same bar and in the same run of the BarUpdate() and if I have the entry condition for a long but I'm already in a long position, nothing is done.
    Same run of the OnBarUpdate?

    Strictly speaking, that is not possible.

    Since you are using managed mode, you can just enter an order in the opposite direction
    and the managed mode approach will automatically take care of reversing the position for
    you, as necessary.

    However, I'm pretty sure this reversal will not happen immediately enough to be considered
    "in the same run of the OnBarUpdate" -- like I said, to require this is simply not possible.

    You may have a misunderstanding in how to use the managed approach properly.

    I mean, I don't think your call to ExitShort() is strictly necessary, and should probably not be
    there. Why? Because the next line is EnterLong(), and it knows to automatically exit the
    current Short position (if there is one) before entering the Long order.

    What is your EntriesPerDirection set to?
    What is your EntryHandling set to?

    Usually these should be "1" and "AllEntries".

    Last thing, this page,
    https://ninjatrader.com/support/help...d_approach.htm

    has a section titled "Internal Order Handling Rules that Reduce Unwanted Positions",
    which states several reasons why a call to an Entry method would be ignored.

    One of them says this,

    "A position is open and an order submitted by an exit method (ExitLongLimit() for example)
    is active and the order is used to open a position in the opposite direction."

    Does the code you've written take into account these rules?
    Attached Files

    Leave a comment:

Latest Posts

Collapse

Topics Statistics Last Post
Started by bchip, Today, 09:25 AM
1 response
9 views
0 likes
Last Post NinjaTrader_ChelseaB  
Started by Tomass, Today, 09:25 AM
2 responses
12 views
0 likes
Last Post Tomass
by Tomass
 
Started by hundert, Today, 08:37 AM
3 responses
18 views
0 likes
Last Post NinjaTrader_Melissa  
Started by ronaldgreene828, Today, 08:16 AM
1 response
7 views
0 likes
Last Post NinjaTrader_PatrickG  
Started by hir04068, Today, 01:36 AM
1 response
18 views
0 likes
Last Post NinjaTrader_PaulH  
Working...
X