Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Time stop active if profit is < 0?

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

    #16
    Yes, please review this link for an example - http://www.ninjatrader-support.com/H...ncelOrder.html
    BertrandNinjaTrader Customer Service

    Comment


      #17
      Below is an excerpt of my strategy. The only parts not made by the wizard is marked in red text: the variable "barNumberofOrder" and the Time Stop condition. Unfortunately the strategy does not compile and I get an error message saying that "&& is not a valid expression".

      I would appreciate if someone could explain what I need to add or change in order to make this time stop work. I just want that if the three most recent bars have all closed at an unrealized profit below 0, the position should be closed.

      publicclass MeanReverter2 : Strategy
      {
      #region Variables

      privateint myInput0 = 1;
      privateint barNumberOfOrder = 0;
      #endregion
      ///<summary>

      ///</summary>
      protectedoverridevoid Initialize()
      {
      CalculateOnBarClose =
      false;
      }
      protectedoverridevoid OnBarUpdate()


      ...



      // Time Stop Condition
      if (CurrentBar > barNumberOfOrder + 3) &&
      (Position.GetProfitLoss(Close[CurrentBar - 1], PerformanceUnit.Points)) < 0 && (Position.GetProfitLoss(Close[CurrentBar - 2], PerformanceUnit.Points)) < 0 && (Position.GetProfitLoss(Close[CurrentBar - 3], PerformanceUnit.Points)) < 0;
      {
      ExitLong(
      "", "");
      ExitShort(
      "", "");
      }

      Comment


        #18
        Hello,

        This compiles. Does that help?:

        if (
        (CurrentBar > barNumberOfOrder +
        3) &&
        (Position.GetProfitLoss(Close[CurrentBar -
        1], PerformanceUnit.Points) < 0) &&
        (Position.GetProfitLoss(Close[CurrentBar -
        2], PerformanceUnit.Points) < 0) &&
        (Position.GetProfitLoss(Close[CurrentBar -
        3], PerformanceUnit.Points) < 0)
        )
        {
        ExitLong(
        "", "");
        ExitShort(
        "", "");
        }
        DenNinjaTrader Customer Service

        Comment


          #19
          Originally posted by NinjaTrader_Ben View Post
          Hello,

          This compiles. Does that help?:

          Yes that helps, thanks. However, there was still some details missing in my code. The expression "CurrentBar" is not needed for the strategy to work, and it seems that neither is the "barNumberOfOrder" variable. I also realized that if I have negative numbers X in "Close[X]" the strategy tries to look at the PnL in three future bars instead of back to the three previous bars. By changing to positive numbers, I was finally able to create the time stop the way I wanted it!

          If you have any opinions about the strategy or code below, I would love to hear it.
          Here is my code for a time stop that exits a position after three consecutive bars of unrealized profit below 0:


          // Time Stop Condition (Exit after three bars of PnL < 0)
          if (
          (Position.GetProfitLoss(Close[
          1], PerformanceUnit.Points) < 0) &&
          (Position.GetProfitLoss(Close[
          2], PerformanceUnit.Points) < 0) &&
          (Position.GetProfitLoss(Close[
          3], PerformanceUnit.Points) < 0)
          )
          {
          ExitLong(
          "", ""); ExitShort("", "");
          }
          Last edited by Fierze; 12-21-2009, 03:43 AM.

          Comment


            #20
            Hello,

            I just got you to compile. What you have looks ok to me, but test it to be sure. I'll have Bert pick this convo back up now.
            DenNinjaTrader Customer Service

            Comment


              #21
              Originally posted by NinjaTrader_Ben View Post
              Hello,
              I just got you to compile. What you have looks ok to me, but test it to be sure. I'll have Bert pick this convo back up now.

              I did test it yesterday and it works like it is supposed to. This kind of time stop will probably be a part of many other systems that I program in the future!

              I still think it is strange that there are not more discussions about time stops on this forum, considering how useful they can be. In that sense, this thread adds a lot of value.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by DJ888, 04-16-2024, 06:09 PM
              4 responses
              12 views
              0 likes
              Last Post DJ888
              by DJ888
               
              Started by terofs, Today, 04:18 PM
              0 responses
              11 views
              0 likes
              Last Post terofs
              by terofs
               
              Started by nandhumca, Today, 03:41 PM
              0 responses
              7 views
              0 likes
              Last Post nandhumca  
              Started by The_Sec, Today, 03:37 PM
              0 responses
              3 views
              0 likes
              Last Post The_Sec
              by The_Sec
               
              Started by GwFutures1988, Today, 02:48 PM
              1 response
              9 views
              0 likes
              Last Post NinjaTrader_Clayton  
              Working...
              X