Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Cannot flat the account position after restart Strategy

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

    Cannot flat the account position after restart Strategy

    I am testing if my code can flat my bought stock after restarting Strategy. Here are the steps, but ExitLong() couldn't successfully work:
    1) run the following ode, wait long enough to let EnterLongLimit() bought 100 shares of stock IBM.
    2) stop Strategy, and restart Strategy
    3) ExitLong() doesn't work.

    Can you help to point out what I may have done wrong? Thanks.


    need_flat = first_time = true; // initial value

    protected override void OnBarUpdate()
    {
    if (need_flat)
    {
    Order myExit = ExitLong();
    // being called once, 1st time to run Strategy doesn't do anything. I expect it do the job during the 2nd time to run
    Print (" After () = = " + myExit); // it prints nothing for myExit after re-start Strategy.
    need_flat = false;
    }

    if (first_time)
    {
    myEntry = EnterLongLimit(0, true, 100, GetCurrentBid() - 0.02, "MyBUY");
    // this is called only once, wait until it is executed, so there are 100 bought IBM stocks in account.
    first_time = false;
    need_flat = false;
    }
    }
    Last edited by localappleseed; 09-21-2020, 08:14 PM.

    #2
    Hello localappleseed,

    The strategy would have had to entered the original order as it did in the first test to resume the same position, once it did that it could work in realtime to exit that position using the exit order you have. What you have shown would not work because the exit is called immediately and won't match any open position.

    This will also relate to your strategies selected start behavior. https://ninjatrader.com/support/help...hlightsub=sync

    You could likely use Wait until flat, synchronize account if the intent was to start flat, that will compare the account position and if not flat will submit orders to flatten the account. That would remove the need for your exit condition and that logic, you would instead just have normal exit logic if you plan to exit a position at some point.

    I look forward to being of further assistance.
    Last edited by NinjaTrader_Jesse; 09-22-2020, 06:46 AM.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi, Jesse,

      Thanks for your help on this. Further questions as follows:
      You mentioned "What you have shown would not work because the exit is called immediately and won't match any open position".
      Do you mean my call ExitLongLimit() was called too soon before a match was found?
      Is there a way to find the match using my own code, or confirm a match is found?

      Currently default "Wait Until Flat" is used, but not working as I expected. Per definition "If the Strategy Position is not flat, the strategy will place all trades in a virtual sense until the Strategy Position reaches or crosses a flat state", I am not sure how Strategy is doing this in Virtual, is there a way to Print something to confirm what it is doing?


      Here is a bit of my strategy background:

      I use Strategy to buy 100 shares of IBM stocks, as soon as stocks are bought, the Strategy will use ExitLongLimit() to send an exit order which can survive some time like 1-100 minutes. I try to flat my account Using my strategy if Strategy is re-started during ExitLongLimit() is pending.

      Thanks.






      Comment


        #4
        Hello

        Do you mean my call ExitLongLimit() was called too soon before a match was found?
        The exit method cannot close the previous strategy instances position because it does not know about it, it would have had to have re entered during the historical processing and entered into that position again virtually so it could then exit the position. The exit would have to have happened in realtime for it to affect the account position.
        Is there a way to find the match using my own code, or confirm a match is found?
        You would have to have the strategy re generate the position, it won't just find the existing position when you use the virutalized position of the strategy. If you intend to work with the account in this way or to enable, disable, re enable the strategy you would have to either use a Start behavior which supports that and program the strategy to work in that way or you can research using the account directly by using PositionAccount in realtime.

        Currently default "Wait Until Flat" is used, but not working as I expected. Per definition "If the Strategy Position is not flat, the strategy will place all trades in a virtual sense until the Strategy Position reaches or crosses a flat state", I am not sure how Strategy is doing this in Virtual, is there a way to Print something to confirm what it is doing?
        You can print what the strategy is doing by using a Print statement. The historical processing could be printed out.
        https://ninjatrader.com/support/help...lightsub=print
        https://ninjatrader.com/support/help...lightsub=print

        I use Strategy to buy 100 shares of IBM stocks, as soon as stocks are bought, the Strategy will use ExitLongLimit() to send an exit order which can survive some time like 1-100 minutes. I try to flat my account Using my strategy if Strategy is re-started during ExitLongLimit() is pending.
        If you have the exit limit order open while the strategy is running and want to flatten it, you could cancel the limit order and then submit an exit order to close it immediately. For situations where you restart the strategy and wanted to resume the limit order, you would have to use a different start behavior. Immediately submit, synchronize account would work but your historical logic would need to resubmit the exit limit at the same prices and everything so it can know to resume it. GetRealtimeOrder could be used to transition the order and resume it in your logic.
        https://ninjatrader.com/support/help...tRealtimeOrder

        I look forward to being of further assistance.


        JesseNinjaTrader Customer Service

        Comment


          #5
          Jesse's comments: If you have the exit limit order open while the strategy is running and want to flatten it, you could cancel the limit order and then submit an exit order to close it immediately.

          After my Strategy is re-started, previous pending ExitLongLimit() is no longer resumed by itself. So I don't have pending ExitLongLimit(). You are saying I could submit a ExitLong() immediately? That's what I did in the sample code.

          A couple of more questions:
          1) Can I use EnterShortLimit() to sell my existing 100 share of IBM in account after Strategy starts? This may flat my account.
          2) Is there a way to submit a virtual order and also virtually executed? This can also help me to sync my account.

          Thanks












          Comment


            #6
            Hello localappleseed,

            After my Strategy is re-started, previous pending ExitLongLimit() is no longer resumed by itself. So I don't have pending ExitLongLimit(). You are saying I could submit a ExitLong() immediately? That's what I did in the sample code.
            No, I had explained 3 different use cases in that paragraph. You would need to keep the context and read the whole paragraph. The second case I described or "For situations where you restart the strategy and wanted to resume the limit order...." would be if you had the existing order and needed to resume it.

            After my Strategy is re-started, previous pending ExitLongLimit() is no longer resumed by itself. So I don't have pending ExitLongLimit(). You are saying I could submit a ExitLong() immediately? That's what I did in the sample code.
            No, the ExitLong would be if you didn't restart the strategy or the first case in the paragraph. If you restart the strategy you will have to use the start behavior and the items I noted in the last reply.

            A couple of more questions:
            1) Can I use EnterShortLimit() to sell my existing 100 share of IBM in account after Strategy starts? This may flat my account.
            Yes however if you want the strategy to be in sync with the account it would need to use a start behavior that observes that position. For the strategy to become flat once it sells it would have to first know about the 100 shares. The start behavior can be used for that assuming the strategy re submits the entry which lead to that position when you restart it.

            2) Is there a way to submit a virtual order and also virtually executed? This can also help me to sync my account.
            Yes in historical all orders are virtual. When you restart the strategy the time where you had previously submitted the entry is now historical, it will process that and if it can enter in the same place it will virtually.

            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service

            Comment


              #7
              Thank you very much for your detailed answers.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by mafon, Today, 09:32 AM
              1 response
              10 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by blar58, Today, 09:28 AM
              0 responses
              5 views
              0 likes
              Last Post blar58
              by blar58
               
              Started by sidlercom80, Today, 08:15 AM
              1 response
              10 views
              0 likes
              Last Post NinjaTrader_Kate  
              Started by KarterGoff, Today, 08:52 AM
              1 response
              6 views
              0 likes
              Last Post NinjaTrader_ChrisL  
              Started by AndreaBhs, Today, 08:30 AM
              4 responses
              21 views
              0 likes
              Last Post NinjaTrader_PaulH  
              Working...
              X