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

Strategy problem...

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

    Strategy problem...

    Hi,

    Here is a simplified explanation of a problem I have:

    if (Indicator) > (Indicator) [1 bar ago]
    Go long

    I have profit targets and stop loss targets as well.

    My problem is, once I meet my profit target or stop loss and the above conditions are still true it will re enter another long position. I don't want this. I'm wondering if it's possible to not re enter a position until the next signal is a short signal? So it alternates long/short signals and will never go long then long or short then short.

    #2
    Sure thing. You will need to run a variable along with your condition.

    Do something like:
    Code:
    // In the Initialize section
    private bool wasLong = false;
    
    // In the OnBarUpdate() method
    if (indicator[0] > indicator[1] && wasLong == false)
    {
         EnterLong();
         wasLong = true;
    }
    
    if (indicator[0] < indicator[1] && wasLong == true)
    {
         EnterShort();
         wasLong = false;
    }
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Maybe I need to have a better understanding of coding. I'm sure this is as simple as it gets but I tried what you have there and it gave me a bunch of errors.

      Comment


        #4
        Hi jason83,

        Please post what you have coded exactly and the related error messages. What I posted was more of a framework for how to approach the issue; not actual code.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Hi Josh,

          First of all, thank you for your help, especially on a holiday. Do you ever sleep haha. I'm not a programmer but picked up an intro C# book, so hopefully I will be able to understand the basics soon. Right now I start the code in the Wizard then unlock it. I just copied what you had into the code below, but obviously I needed to do more than that.

          Regards,
          Jason

          Code:
          // This namespace holds all strategies and is required. Do not change it.
          namespace NinjaTrader.Strategy
          {
              /// <summary>
              /// 
              /// </summary>
              [Description("")]
              public class Trend : Strategy
              {
                  #region Variables
                  // Wizard generated variables
                  private int movingAv = 24; // Default setting for MovingAv
                  // 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>
                  protected override void Initialize()
                  {
                      SetProfitTarget("", CalculationMode.Ticks, 16);
                      SetStopLoss("", CalculationMode.Ticks, 8, false);
          
                      CalculateOnBarClose = false;
                  }
          
                  /// <summary>
                  /// Called on each bar update event (incoming tick)
                  /// </summary>
                  protected override void OnBarUpdate()
                  {
                      // Condition set 1
                      if (EMA(MovingAv)[0] >= EMA(MovingAv)[1])
                      {
                          EnterLong(DefaultQuantity, "");
                      }
          
                      // Condition set 2
                      if (EMA(MovingAv)[0] <= EMA(MovingAv)[1])
                      {
                          EnterShort(DefaultQuantity, "");
                      }
                  }
          
                  #region Properties
                  [Description("")]
                  [Category("Parameters")]
                  public int MovingAv
                  {
                      get { return movingAv; }
                      set { movingAv = Math.Max(1, value); }
                  }
                  #endregion
              }
          }

          Comment


            #6
            Hi jason83,

            You are missing the critical step with the bool. You need "private bool wasLong = false" in the Initialize section (next to where you have movingAv declared). Then you need to add the additional conditional check on the bool and change the bool value depending on if you just went short/long as outlined in my previous post.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Josh,

              Thanks for your help. I wasn't expanding the variables section and inserting the private bool there. I was inserting it near the stop loss and profit targets.

              Thank you.

              Comment


                #8
                Oh sorry for the confusion. Not the Initialize() but the Variables section.
                Josh P.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by PhillT, Today, 02:16 PM
                2 responses
                6 views
                0 likes
                Last Post PhillT
                by PhillT
                 
                Started by Kaledus, Today, 01:29 PM
                3 responses
                10 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Started by frankthearm, Yesterday, 09:08 AM
                14 responses
                47 views
                0 likes
                Last Post NinjaTrader_Clayton  
                Started by gentlebenthebear, Today, 01:30 AM
                2 responses
                14 views
                0 likes
                Last Post gentlebenthebear  
                Started by PaulMohn, Today, 12:36 PM
                2 responses
                17 views
                0 likes
                Last Post PaulMohn  
                Working...
                X