Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

EnterLongStop and ExitLongStop on EOD

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

    EnterLongStop and ExitLongStop on EOD

    G'Day Folks,
    I am fairly new to NT and I've been trying to get a simple BackTest to work for several weeks now, unfortunately, without any real success.

    I must be missing something!

    Intermittently, trades are missed.....Reason='This was an exit order but no position exists to exit'

    eg.
    OnBarUpdate 707
    ShortEntryPrice=36.86 Low[0]=36.96 ConfirmationGap=0.1
    The Initial ShortStop value is 37.58 High[0]=37.48

    23/10/2012 4:00:00 AM Entered internal PlaceOrder() method at 23/10/2012 4:00:00 AM: BarsInProgress=0 Action=SellShort OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=36.86 SignalName='GoShort' FromEntrySignal=''

    23/10/2012 4:00:00 AM Entered internal PlaceOrder() method at 23/10/2012 4:00:00 AM: BarsInProgress=0 Action=BuyToCover OrderType=Stop Quantity=0 LimitPrice=0 StopPrice=37.58 SignalName='InitialShortStop' FromEntrySignal='GoShort'

    23/10/2012 4:00:00 AM Ignored PlaceOrder() method: Action=BuyToCover OrderType=Stop Quantity=0 LimitPrice=0 StopPrice=37.58 SignalName='InitialShortStop' FromEntrySignal='GoShort' Reason='This was an exit order but no position exists to exit'

    OnBarUpdate 708....
    24/10/2012 High=36.98 Low=36.54 ...should have entered stop at 36.86

    I don't understand why this validation occurs as the ExitLongStop(BuyToCover) won't even happen until the some stage on the 24th, if at all. And this problem only happens intermittently, but enough to discount to entire test.

    If anyone can spot my problem and let me know, I will be very greatful:-)
    Many Thanks,
    Joytribe

    Overview
    End Of Day Bars - Kinetick(Free EOD)
    Only Make decisions at the the End of the Day.
    Place Long Stop Orders with "If Done" Stops just below.
    Place ShortSell Stop Orders with "If Done" Stops just above.
    I am trying to mimic manually placing trades/stops after EOD.

    My code pretty much looks like this. I have also tried using "SetStopLoss" in place of "ExitLongStop"/"ExitShortStop" but....other errors/problems.

    CalculateOnBarClose = true;
    TraceOrders = true;
    ClearOutputWindow();

    protected override void OnBarUpdate()
    {
    Print("OnBarUpdate " + CurrentBar.ToString() );

    // Long
    if ( <Long condition met> )
    {
    if (Position.MarketPosition != MarketPosition.Long)
    {

    // Setup Long Entry Price
    LongEntryPrice = <something>;

    // Initial Long Stop Price
    LongStop = <something>;

    // Place the Long Stop Order
    EnterLongStop(0, true, DefaultQuantity, LongEntryPrice, "GoLong");

    // Place the Stop for the Long Stop Order (linked to driving order)
    ExitLongStop(LongStop, "InitialLongStop", "GoLong");
    }
    }
    // Short
    if ( <Short condition met> )
    {
    if (Position.MarketPosition != MarketPosition.Short)
    {

    // Setup Short Entry Price
    ShortEntryPrice = <something>;
    //Initial Long Stop Price
    ShortStop = <something>;

    // Place the Short Stop Order
    EnterShortStop(0, true, DefaultQuantity, ShortEntryPrice, "GoShort");

    // Place the Stop for the Short Stop Order (linked to driving order)
    ExitShortStop(ShortStop, "InitialShortStop", "GoShort");
    }
    }

    // Manage the Trailing Stops

    // Manage No Orders
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    Print("No Orders.....Flat");
    }

    // Manage Long Trade
    if (Position.MarketPosition == MarketPosition.Long)
    {
    Print("Manage Open Long Orders");
    LongStop = <something>;
    ExitLongStop(LongStop, "TrailLongStop", "GoLong");
    }
    // Manage Short Trade
    if (Position.MarketPosition == MarketPosition.Short)
    {
    Print("Manage Open Short Orders");
    ShortStop = <something>;
    ExitShortStop(ShortStop, "TrailShortStop", "GoShort");
    }
    }

    #2
    Hello joytribe,

    Thank you for your post and welcome to the NinjaTrader support forum!

    In this example was the position long when the attempted ExitShortStop() was placed?

    You will need to debug your code here to determine if the Exit() method is placed for short when you are long or long when you are short as this will generate the message.

    You could add a Print() statement for the position as well to see if this is truly the case when this occurs:
    Code:
    Print(Position.MarketPosition.ToString());
    Please let me know if you have any questions.
    Last edited by NinjaTrader_PatrickH; 10-16-2013, 10:21 AM.

    Comment


      #3
      Thanks for your reply.

      The Market Position is "Flat".

      I imagined it would be because I am using one Entry and a linked Exit to Stop out.

      Any other suggestions?

      Comment


        #4
        Hello joytribe,

        Thank you for your response.

        Before placing the Exit() orders you will need to ensure you are in a position to exit. For example:
        Code:
        if(Position.MarketPosition == MarketPosition.Short)
        {
        ExitShortStop(ShortStop, "InitialShortStop", "GoShort");
        }
        if(Position.MarketPosition == MarketPosition.Long)
        {
        ExitLongStop(LongStop, "InitialLongStop", "GoLong");
        }
        Please let me know if you have any questions.

        Comment


          #5
          I will give this a try but in my head I am thinking that this would happen wouldn't it?

          Because I am using EOD data and only making decisions at EOD

          Day 1....Long condition met so place EnterLongStop
          Day 2....EnterLongStop executed (changes MarketPosition from Flat to Long)
          Day 2....at end of bar...If Long, then ExitLongStop

          Or is it really subtle and the ExitLongStop use the conditions that surround it as it executes?

          Ta.

          Comment


            #6
            One thing that I forgoet to mention....

            I assumed (perhaps incorrectly) that if you link an ExitLongStop to a EnterLongStop via the "fromEntrySignal" parameter that the ExitLongStop would not look to Exit unless the Enter had actually taken place.

            If this is not the case, what is the purpose of the "fromEntrySignal"?

            Ta.

            Comment


              #7
              Hello joytribe,

              Thank you for your response.

              The fromEntrySignal is used to tie the two orders together but the Exit() cannot be placed if there is no respective position to exit.

              Comment


                #8
                Thanks for your reply.

                Can you please re-read my original post. My responses since have been because you asked me to confirm if I was in a position when trying to use the ExitLongStop. The Market Position was "Flat".

                So, to summarize....the problem is that the EnterLongStop did not execute when the price movement should have triggered the entry. The question is why?

                Then as a side issue, but a concern, why does the ExitLongStop fail (or even try to execute) when it is tied to the EnterLongStop and the EnterLongStop did not happen?

                I guess it comes down to .....Can I reliably use the EnterLongStop / ExitLongStop with EOD data?

                And, is there anything obviously incorrect with the way I have coded this?

                Ta.

                Comment


                  #9
                  Hello joytribe,

                  Thank you for your response.

                  Your original post concerns the following error: 'This was an exit order but no position exists to exit'

                  This occurred as the Exit() methods were submitted when no position existed to exit. Using fromEntrySignal does not mean the Exit() method will wait for that position before submitting, it will submit when you tell it to and if there is no position you will get the message from your original post.

                  To resolve this matter before placing the Exit() orders you will need to ensure you are in a position to exit. For example:
                  Code:
                  if(Position.MarketPosition == MarketPosition.Short)
                  {
                  ExitShortStop(ShortStop, "InitialShortStop", "GoShort");
                  }
                  if(Position.MarketPosition == MarketPosition.Long)
                  {
                  ExitLongStop(LongStop, "InitialLongStop", "GoLong");
                  }
                  Please let me know if you have any questions.

                  Comment


                    #10
                    Sorry Patrick, I must be having trouble in explaining the issue.

                    It sounds to me that you are suggesting this? (your suggestion is in bold).

                    protected override void OnBarUpdate()
                    {
                    Print("OnBarUpdate " + CurrentBar.ToString() );

                    // Long
                    if ( <Long condition met> )
                    {

                    if (Position.MarketPosition != MarketPosition.Long)
                    {
                    // Setup Long Entry Price
                    LongEntryPrice = <something>;

                    // Initial Long Stop Price
                    LongStop = <something>;

                    // Place the Long Stop Order
                    EnterLongStop(0, true, DefaultQuantity, LongEntryPrice, "GoLong");

                    // Patrick's suggested "if"
                    if(Position.MarketPosition == MarketPosition.Long)

                    {
                    // Place the Stop for the Long Stop Order (linked to driving order)
                    ExitLongStop(LongStop, "InitialLongStop", "GoLong");
                    }
                    }
                    }

                    I cannot see this working because the EnterLongStop is wrapped in an if that only happens if NOT already long.

                    If I do not place the Exit at the same time as the Enter, there will not be a stop for the first day. Cannot have that situation.

                    Can anyone else see what I'm asking and help us please?

                    Ta.

                    Comment


                      #11
                      Hello Ta.,

                      Thank you for your response.

                      Place the if(Position.MarketPosition == MarketPosition.Long) outside of the condition to check if not long.

                      Comment


                        #12
                        Ok...I will try that. Still concerned it won't happen until the next day and leave me in without a stop on the first day. Fingers crossed.

                        Ok...back to the main issue if we can please.

                        Why didn't the EnterLongStop happen when the Log shows that the order was placed:

                        Intermittently, trades are missed.....

                        eg. of a short trade that was missed...
                        The ExitLongStop error hightlighted the EnterLongStop not being done....

                        OnBarUpdate 707
                        ShortEntryPrice=36.86 Low[0]=36.96 ConfirmationGap=0.1
                        The Initial ShortStop value is 37.58 High[0]=37.48

                        23/10/2012 4:00:00 AM Entered internal PlaceOrder() method at 23/10/2012 4:00:00 AM: BarsInProgress=0 Action=SellShort OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=36.86 SignalName='GoShort' FromEntrySignal=''

                        23/10/2012 4:00:00 AM Entered internal PlaceOrder() method at 23/10/2012 4:00:00 AM: BarsInProgress=0 Action=BuyToCover OrderType=Stop Quantity=0 LimitPrice=0 StopPrice=37.58 SignalName='InitialShortStop' FromEntrySignal='GoShort'

                        23/10/2012 4:00:00 AM Ignored PlaceOrder() method: Action=BuyToCover OrderType=Stop Quantity=0 LimitPrice=0 StopPrice=37.58 SignalName='InitialShortStop' FromEntrySignal='GoShort' Reason='This was an exit order but no position exists to exit'

                        OnBarUpdate 708....
                        24/10/2012 High=36.98 Low=36.54 ...should have entered stop at 36.86

                        Ta.

                        Comment


                          #13
                          Originally posted by joytribe View Post
                          Ok...I will try that. Still concerned it won't happen until the next day and leave me in without a stop on the first day. Fingers crossed.

                          Ok...back to the main issue if we can please.

                          Why didn't the EnterLongStop happen when the Log shows that the order was placed:

                          Intermittently, trades are missed.....

                          eg. of a short trade that was missed...
                          The ExitLongStop error hightlighted the EnterLongStop not being done....

                          OnBarUpdate 707
                          ShortEntryPrice=36.86 Low[0]=36.96 ConfirmationGap=0.1
                          The Initial ShortStop value is 37.58 High[0]=37.48

                          23/10/2012 4:00:00 AM Entered internal PlaceOrder() method at 23/10/2012 4:00:00 AM: BarsInProgress=0 Action=SellShort OrderType=Stop Quantity=1 LimitPrice=0 StopPrice=36.86 SignalName='GoShort' FromEntrySignal=''

                          23/10/2012 4:00:00 AM Entered internal PlaceOrder() method at 23/10/2012 4:00:00 AM: BarsInProgress=0 Action=BuyToCover OrderType=Stop Quantity=0 LimitPrice=0 StopPrice=37.58 SignalName='InitialShortStop' FromEntrySignal='GoShort'

                          23/10/2012 4:00:00 AM Ignored PlaceOrder() method: Action=BuyToCover OrderType=Stop Quantity=0 LimitPrice=0 StopPrice=37.58 SignalName='InitialShortStop' FromEntrySignal='GoShort' Reason='This was an exit order but no position exists to exit'

                          OnBarUpdate 708....
                          24/10/2012 High=36.98 Low=36.54 ...should have entered stop at 36.86

                          Ta.
                          You're still hitting a race condition as all these events occur at the same time. Where you're checking if your position you are placing both your entry order and the exit order. The reason given is that that the position does not exist yet for the exit as OnBarUpdate is only updated once on your EOD data.

                          What you may want to try is submitting your Exit orders outside of OnBarUpdate and using another event handler such as OnPosition to then submit those exit orders.

                          Code:
                          		protected override void OnPositionUpdate(IPosition position)
                          		{
                          		
                          			if(Position.MarketPosition == MarketPosition.Short)
                          			{
                          				ExitShortStop(ShortStop, "InitialShortStop", "GoShort");
                          
                          			}			
                          			
                          		}
                          You can also consider using an OnOrderUpdate or OnExecution to do this as well. Please see the following tutorial:

                          The OnOrderUpdate() and OnExecution() methods are reserved for experienced programmers. Instead of using Set() methods to submit stop-loss and profit target orders, you can submit and update them manually through the use of IOrder and IExecution objects in the OnOrderUpdate() and OnExecution() methods. The OnOrderUpdate()
                          MatthewNinjaTrader Product Management

                          Comment


                            #14
                            Thanks Matthew....very helpful
                            Ta.

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by ttrader23, Yesterday, 09:04 AM
                            7 responses
                            30 views
                            0 likes
                            Last Post ttrader23  
                            Started by Salahinho99, 05-05-2024, 04:13 AM
                            4 responses
                            47 views
                            0 likes
                            Last Post Salahinho99  
                            Started by goodknight777, Today, 08:43 AM
                            0 responses
                            1 view
                            0 likes
                            Last Post goodknight777  
                            Started by damethetrader, Today, 08:31 AM
                            1 response
                            4 views
                            0 likes
                            Last Post NinjaTrader_Clayton  
                            Started by reynoldsn, Today, 07:23 AM
                            3 responses
                            7 views
                            1 like
                            Last Post NinjaTrader_Gaby  
                            Working...
                            X