Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Sell Short and Stop loss on the same bar

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

    Sell Short and Stop loss on the same bar

    Hi,

    I Have this strategy:

    #region Variables
    // Wizard generated variables
    privatedouble stop = 0.03; // Default setting for Stop
    privatedouble target = 0.05; // Default setting for Target
    // User defined variables (add any user defined variables below)
    #endregion
    ///<summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    ///</summary>
    protectedoverridevoid Initialize()
    {
    Add (PeriodType.Day, 1);

    Add (GetHighLowByTimeRange(10 , 15, 9, 30));

    SetProfitTarget("", CalculationMode.Percent, Target);

    SetStopLoss("", CalculationMode.Percent, Stop, false);
    CalculateOnBarClose = true;
    }
    ///<summary>
    /// Called on each bar update event (incoming tick)
    ///</summary>
    protectedoverridevoid OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    // Checks if the macd daily fast period is above 0 in the last bar
    if (MACD(BarsArray[1], 12, 26, 9)[1] > 0)
    {
    EnterLongStop(DefaultQuantity, GetHighLowByTimeRange(10, 15, 9, 30).HighestHigh[0] + 1 * TickSize, "");

    }

    // Checks if the macd daily fast period is below 0 in the last bar
    if (MACD(BarsArray[1], 12, 26, 9)[1] < 0)
    {
    EnterShortStop(DefaultQuantity, GetHighLowByTimeRange(10, 15, 9, 30).LowestLow[0] - 1 * TickSize, "");

    }
    }

    And I Want not to make the entry and the stoploss on the same bar, How can I do that?
    Here I let you an image that what is happenning on a chart
    Attached Files

    #2
    Hello,

    I suggest eliminating the SetStopLoss() in the intialization section, since it will be called everytime and add a condition for ExitLongStop() in the body of your program to make this happen.

    This link may help:
    DenNinjaTrader Customer Service

    Comment


      #3
      Something like this?

      if (MACD(BarsArray[1], 12, 26, 9)[1] < 0)
      {
      EnterShortStop(DefaultQuantity, GetHighLowByTimeRange(10, 15, 9, 30).LowestLow[0] - 1 * TickSize, "");
      stopshort = GetHighLowByTimeRange(10, 15, 9, 30).LowestLow[0] + ((GetHighLowByTimeRange(10, 15, 9, 30).LowestLow[0])*stop);
      }
      ExitShortStop(stopshort);

      Where stop=0,03.
      But it doesn't works... Why?
      Last edited by julen; 09-15-2008, 01:21 PM.

      Comment


        #4
        Hello,

        Try something like this:

        if(...your condition for entry...)
        EnterShort(...);
        if(Position.MarketPosition == MarketPosition.Short && BarsSinceEntry() >1)
        ExitShortStop(...);

        ...so that your ExitShortStop is only called when you have a short order open and after 1 bar (in this example).

        You may want to use TraceOrders:


        Or Print(stopshort):


        ...to debug your strategy:
        DenNinjaTrader Customer Service

        Comment


          #5
          Sorry I'm being so tedious but it's my first time programing with ninja...
          I've tried what you propose like this:

          if (MACD(BarsArray[1], 12, 26, 9)[1] < 0)
          {
          EnterShortStop(DefaultQuantity, GetHighLowByTimeRange(10, 15, 9, 30).LowestLow[0] - 1 * TickSize, "");
          }
          if(Position.MarketPosition == MarketPosition.Short && BarsSinceEntry() >1)
          ExitShortStop(Position.AvgPrice*(1+stop));

          And it doesn't works too, in the control center log appears the following message:
          "Error on calling 'OnBarUpdate' method for strategy 'RotRangH3':You must use the overload that has a 'BarsInProgress'parameter when calling the BarsSinceEntryMethod()in the context of a multi-time frame and instrument strategy"

          In graph it only makes one trade and it's still open...

          What I'm doing wrong?

          Sure in my next project I'm not having so much troubles, Sorry.

          Comment


            #6
            Hello?

            Have you already seen my last post?

            Help me please...

            Comment


              #7
              Remove the "&& BarsSinceEntry() >1" part. If you find this condition suitable for your needs you will need to use the multi-instrument syntax version of it. Please reference the Help Guide on the proper syntaxing.
              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
              576 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              334 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              101 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              553 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              551 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X