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

StopLoss & Reverse Giving Too Many Contracts

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

    StopLoss & Reverse Giving Too Many Contracts

    I'm running into problem occassionally when my stoploss is hit and then my strategy goes opposite direction at the same time. However rather than exiting and reentering the opposite way, my strategy is putting on extra positions on. So I'm left with having too many contracts on with no stoploss or profit target for the unwanted position. Basically I'm getting unwanted positions sometimes.

    Do I need to add if market.position == flat? or how do i stop this from happening?

    Thank you

    #2
    Hello elcowen,

    Thank you for your reply.

    Are you running the strategy On Bar Close, or On Each Tick or On Price Change?

    Are you using Exit orders for your stop loss or are you using the Set methods?

    Could you provide a code example of how you are reversing the position?

    Thanks in advance; I look forward to assisting you further.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hi Kate - below is my code. I took some logic out but this is the entries/exits. I'm also using heiken ashi bars (is there a better way to reference than to list out all the Heiken Ashi dataseries?) Thank you so much for your help!



      else if (State == State.Configure)
      {
      AddHeikenAshi("ES 09-20", BarsPeriodType.Minute, 3, MarketDataType.Last);
      AddHeikenAshi("YM 09-20", BarsPeriodType.Minute, 3, MarketDataType.Last);
      AddHeikenAshi("CL 09-20", BarsPeriodType.Minute, 3, MarketDataType.Last);
      AddHeikenAshi("GC 08-20", BarsPeriodType.Minute, 3, MarketDataType.Last);
      AddHeikenAshi("ZN 09-20", BarsPeriodType.Minute, 15, MarketDataType.Last);
      AddHeikenAshi("NG 09-20", BarsPeriodType.Minute, 3, MarketDataType.Last);
      }
      }

      protected override void OnBarUpdate()

      protected override void OnBarUpdate()
      {
      if (BarsInProgress != 0)
      return;

      if (CurrentBars[0] < 3)
      return;

      // ---------------------------------------------------------------------------------------------- SHORT
      if ((IsFirstTickOfBar
      && Close[1] < Open[1])

      {
      EnterShort(Convert.ToInt32(DefaultQuantity), @"SE1");
      EnterShort(Convert.ToInt32(DefaultQuantity), @"SE2");
      }

      // --------------------------------------------------------------------------------------------- EXIT - SHORT
      if (IsFirstTickOfBar
      && Low[1] >= Low[2])
      {
      ExitShort(Convert.ToInt32(DefaultQuantity), @"ST1", @"SE1");
      }

      ExitShortStopMarket(High[1] + (1 * TickSize));


      // --------------------------------------------------------------------------------------------- LONG
      if ((IsFirstTickOfBar
      && Close[1] > Open[1])

      {
      EnterLong(Convert.ToInt32(DefaultQuantity), @"LE1");
      EnterLong(Convert.ToInt32(DefaultQuantity), @"LE2");
      }

      // --------------------------------------------------------------------------------------------- EXIT - LONG
      if (IsFirstTickOfBar
      && High[1] <= High[2])
      {
      ExitLong(Convert.ToInt32(DefaultQuantity), @"LT1", @"LE1");
      }

      ExitLongStopMarket(Low[1] - (1 * TickSize));

      Comment


        #4
        Hello elcowen,

        Thank you for your reply.

        It looks like what you're running into are cases where both an entry and exit method are triggered on the same bar.

        For example, if we are currently in a 2 long position, and both the Exit Long and your two Enter Shorts are triggered on the same bar, the exit long will submit a short order to close one of your long contracts, but it will not have updated the position yet when the two Enter Shorts are submitted. EnterShort would then submit two orders for each entry, a short order to close the original long position, and then a short order to open the position in the new direction. So what you'd end up with here is 3 short rather than 2 short as you'd expect.

        If you're going to be using Exit and Entry orders, it's important to not have them execute on the same bar for this reason. I would look at adjusting your conditions for entry and exit so they would not be simultaneously true.

        Please let us know if we may be of further assistance to you.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          I'm running into the same issue on bar update
          Code:
           // Long Entry and Short Exit
                       if ((MACDDiff > UpperBand) ))
                      {
                          ExitShort(Convert.ToInt32(Contracts), "", "");
                          EnterLong(Convert.ToInt32(Contracts), "");
                      }
          
                       // Short Entry and Long Exit
                      if ((MACDDiff < UpperBand))
                          )
                      {
                          ExitLong(Convert.ToInt32(Contracts), "", "");
                          EnterShort(Convert.ToInt32(Contracts), "");
                      }
          Sell 2 10976.25 7/21/2020 6:31:18 AM a5e84100d7bd4a888a9e018a5b0f8c19 Entry/Exit 1 S 4da3502f606a4b7eb0f464fa6eaa0ee7 Close position
          Sell 1 10976.50 7/21/2020 6:31:18 AM ab6c5f6ad4894d649e57b448fa98e990 Exit 1 L 5c00a5228a6741ccae7e95293896ab41 Sell
          Buy 1 10986.50 7/21/2020 6:31:10 AM 03173b92c5504e20a8463076d392474e Entry 2 L 2710d97e3c8242ca8e91b95c96cb396d Buy
          Buy 2 10986.50 7/21/2020 6:31:10 AM 5fb1d93ae7e945c4beac5261c7ed409d Entry/Exit 1 L 9a74d0ef9d7a4e91b5a8a2a34ad176d3 Close position
          Buy 1 10986.50 7/21/2020 6:31:10 AM 2dfd7ea92bdf4ab9bd0eb21bbf54b0eb Exit 1 S 2be7132830a8457fa2788187b77aadd5 Buy to cover
          Sell 1 10993.00 7/21/2020 6:30:48 AM e161fe42e6c342db92f603a7563d459a Entry 2 S 3fa49b15cd2e40318085c2974419c19c Sell short
          Sell 2 10992.50 7/21/2020 6:30:48 AM 5b7c73038ba543a4996fa12fbce9a258 Entry/Exit 1 S 988a04803c1043bfae8a3f41695d697f Close position
          Sell 1 10993.00 7/21/2020 6:30:48 AM 80a6d079a62b4329a537672d980cbbd4 Exit 1 L 496465bd27a14771a3c3881973fef948 Sell
          Buy 1 11000.00 7/21/2020 6:30:31 AM 3ae2b8ae0f9148d6a26a343726092ee9 Entry 2 L 13a4bb903bd34a1c88d5c4a5480706e4 Buy
          Buy 1 11000.00 7/21/2020 6:30:31 AM 0846484e31ff4c5fbb79337c59586be5 Entry 1 L 4202041e0a50450e930c8bfc13d143e8 Close position
          Entries is set to 1 per direction and calculate on bar close. Isn't there a fix for this other than changing the logic so both exit and reverse aren't true?

          Comment


            #6
            Hello Kentoes,

            Thank you for your reply.

            In your case, since you're just reversing each time, you'd want to simply call the Enter method for the opposite direction of your current position. This will automatically submit an order to close the current position prior to entering the opposite direction. Try modifying the code like this:

            Code:
             // Long Entry and Short Exit
                         if ((MACDDiff > UpperBand) ))
                        {
                            EnterLong(Convert.ToInt32(Contracts), "");
                        }
            
                         // Short Entry and Long Exit
                        if ((MACDDiff < UpperBand))
                            )
                        {
                            EnterShort(Convert.ToInt32(Contracts), "");
                        }
            Please let us know if we may be of further assistance to you.
            Kate W.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Shansen, 08-30-2019, 10:18 PM
            24 responses
            939 views
            0 likes
            Last Post spwizard  
            Started by Max238, Today, 01:28 AM
            0 responses
            6 views
            0 likes
            Last Post Max238
            by Max238
             
            Started by rocketman7, Today, 01:00 AM
            0 responses
            4 views
            0 likes
            Last Post rocketman7  
            Started by wzgy0920, 04-20-2024, 06:09 PM
            2 responses
            28 views
            0 likes
            Last Post wzgy0920  
            Started by wzgy0920, 02-22-2024, 01:11 AM
            5 responses
            33 views
            0 likes
            Last Post wzgy0920  
            Working...
            X