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

Entry after ProfitTarget

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

    Entry after ProfitTarget

    Hi,
    Just started on how to implement strategies and have SetStopLoss and SetProfitTarget. The problem I'm running into, is that once the Profit Target is hit, it will also immediately EnterLong again. Thanks in advance.

    Code:
    Protected override void Initialize()
    {
        SetStopLoss(CalculateMode.Ticks, MyStop);
        SetProfitTarget(CalculateMode.Ticks, MyTarget);
    }
    
    protected override void OnBarUpdate()
    {
        if(Position.MarketPosition == MarketPosition.Flat)
            {
                SetStopLoss(CalculateMode.Ticks,MyStop);
            }
        if(Condition 1)
            {
                EnterLong();
            }

    #2
    Hello,

    Thank you for the question.

    It seems your Condition could remain true during the exit which would allow for it to enter again after exiting.

    BarsSinceExit may be of use in your condition: http://ninjatrader.com/support/helpG...=barssinceexit

    This has 3 states, -1, 0 and > 0 which means it could be used with initial entry logic before there is an exit.

    You could check
    Code:
    if(BarsSinceExit() != 0)
    That would mean if there is no exit (-1) or if the exit was at least 1 bar ago it would be able to enter. This may be one solution, could you check if this could fit with your current conditions?

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Originally posted by 2Look4me View Post
      Hi,
      Just started on how to implement strategies and have SetStopLoss and SetProfitTarget. The problem I'm running into, is that once the Profit Target is hit, it will also immediately EnterLong again. Thanks in advance.

      Code:
      Protected override void Initialize()
      {
          SetStopLoss(CalculateMode.Ticks, MyStop);
          SetProfitTarget(CalculateMode.Ticks, MyTarget);
      }
      
      protected override void OnBarUpdate()
      {
          if(Position.MarketPosition == MarketPosition.Flat)
              {
                  SetStopLoss(CalculateMode.Ticks,MyStop);
              }
          if(Condition 1)
              {
                  EnterLong();
              }
      After the target is hit, is "Condition 1" still true?

      Comment


        #4
        Thanks Jesse/Koganam for the help.

        I would like to close that trade once StopLoss or ProfitTarget is hit even if condition 1 remains true. Then re enter once Condition 1 becomes true again.

        Comment


          #5
          Hello,

          Originally posted by 2Look4me View Post

          I would like to close that trade once StopLoss or ProfitTarget is hit even if condition 1 remains true. Then re enter once Condition 1 becomes true again.
          I wanted to clarify, unless I am mis-understanding your statement, the script currently does this.

          Walking through the logic you have provided:

          You are flat so a stop is preset
          Condition becomes true and trade is entered and stoploss is set
          The stop gets filled and the strategy immediately trades again.

          Is this correct?

          I also have noticed you are using both Initialize and OnBarUpdate to set the stop, this can cause conflicts I would suggest removing it from Initialize and just SetStopLoss where you need in OnBarUpdate prior to submitting the order.

          Can you provide further details on what you are seeing as this was only a small sample of syntax?

          Is this happening on the same bar and you want to prevent the entry from entering on the same bar twice? or is this an entry on the next bar or after?

          The information in post 2 would be relevant if you are trying to stop intrabar multiple entries or if you wanted to specify that an entry has to be X number of bars after the exit.



          I look forward to being of further assistance.
          JesseNinjaTrader Customer Service

          Comment


            #6
            Jesse, I made the changes to the code and removed the SetStopLoss from Initailize.

            I need help with the following: I do not want to re enter the trade when StopLoss, ProfitTarget or ExitLong (ExitShort) based on Condition AB (or Condition CD) have been triggered even if Condition AB (or Condtion CD) continues to be true. The strategy only trades again after Condition AB/CD becomes true again after being false.

            So using your assumption:
            Position is flat so a StopLoss is preset.
            Condition becomes true and trade is entered and StopLoss, ProfitTarget or Exit is triggered. This will be the only trade even if the condition remains true. Strategy will trade again the next time the condition goes to true.

            Thanks again.

            Code:
            protected override void Initialize()
             {
                        SetProfitTarget(CalculationMode.Ticks,MyTarget);
                        CalculateOnBarClose    = false;
             }
                    protected override void OnBarUpdate()
             {
            if ( ToTime(Time[0]) >= 151000 && Position.MarketPosition != MarketPosition.Flat) //Exit all positions by 3:10 pm
                          {
                            if ( Position.MarketPosition == MarketPosition.Long )
                            {
                                ExitLong();
                            }
                            else
                            {
                                ExitShort();
                            }
                        } 
            if(ToTime(Time[0]) >= 83000 && ToTime(Time[0]) < 151000) //Only trade between 8:30 am to 3:10 pm
                        {                
                            if (Position.MarketPosition == MarketPosition.Flat)
                            {                
                                SetStopLoss(CalculationMode.Ticks,MyStop);
                                if (BarsSinceExit()!=0 && Condition AB)
                                {                        
                                    EnterLong();
                                }
                            }
                            if (Position.MarketPosition == MarketPosition.Long)
                            {
                                if(Condition 1)
                                {
                                    ExitLong();
                                }
                                else if(Condition 2)
                                {
                                    ExitLong();
                                }
                            }
            if (Position.MarketPosition == MarketPosition.Flat)
                            {                
                                SetStopLoss(CalculationMode.Ticks,MyStop);    
                                if (BarsSinceExit()!=0 && Condition CD)
                                {                        
                                    EnterShort();
                                }
                            }
                            if (Position.MarketPosition == MarketPosition.Short)
                            {
                                if(Condition 3)
                                {
                                    ExitShort();
                                }
                                else if(Condition 4)
                                {
                                    ExitShort();
                                }
                            }
                            
                        }
                    }

            Comment


              #7
              Hello,

              Thank you for the reply.

              Based on your reply it sounds like the condition can remain true, but you only want to enter when it initially becomes true once. After you enter, exit and it is still true, do not enter again unless it is becomes no longer true and then becomes true again.

              Based on your sample, this could prove difficult because you are using CalculateOnBarClose = false depending on what exactly you are trying to do.

              I wanted to check with you, should this be able to allow another trade on the same bar or are you looking to avoid trading on the same bar all together?

              If you want to trade only after this bar, it may be easier to use BarsSinceExit. Otherwise if you would like to still allow another entry on the same bar but only after the condition becomes true again, that could proove more difficult and would require more logic.

              Can you confirm which case you are trying to achieve?

              I look forward to being of further assistance.
              JesseNinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Gerik, Today, 09:40 AM
              1 response
              6 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by RookieTrader, Today, 09:37 AM
              1 response
              10 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by alifarahani, Today, 09:40 AM
              0 responses
              5 views
              0 likes
              Last Post alifarahani  
              Started by KennyK, 05-29-2017, 02:02 AM
              3 responses
              1,284 views
              0 likes
              Last Post NinjaTrader_Clayton  
              Started by AttiM, 02-14-2024, 05:20 PM
              11 responses
              186 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Working...
              X