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

Need help with strategy development!!!

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

    Need help with strategy development!!!

    Hi

    I am newbie in the programming and facing issue with developing my first strategy. I was wondering if someone can help me. In my strategy I want to use two different types of stops (Initial Stop and Trailing Stop) as well as Profit Target. Unfortunately, only Trailing Stop and Profit Target seem to work so far. If the position does not move in my favour, instead of triggering Initial Stop Loss, Trailing Stop is triggered, even though Initial Stop is tighter. Please help me. Here is my code:

    protected override void Initialize()
    {
    Add(ATRTrailingStop(ATR_Multiple, ATR_Period));
    SetStopLoss("", CalculationMode.Ticks, StopLoss, false);
    SetProfitTarget("", CalculationMode.Ticks, ProfitTarget);


    CalculateOnBarClose = true;
    }


    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (CrossAbove(Close, ATRTrailingStop(ATR_Multiple, ATR_Period), 1))
    {

    EnterLong(DefaultQuantity, "");
    }

    // Condition set 2
    if (CrossBelow(Close, ATRTrailingStop(ATR_Multiple, ATR_Period), 1))
    {
    EnterShort(DefaultQuantity, "");
    }

    // Reset the stoploss when all positions are closed
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetStopLoss(CalculationMode.Price, ATRTrailingStop(1.5, 10)[0]);
    }
    // Modify the stoploss when there is an open position

    if (Position.MarketPosition == MarketPosition.Long
    || Position.MarketPosition == MarketPosition.Short);

    {
    SetStopLoss(CalculationMode.Price, ATRTrailingStop(atrTrailing_Multip, atrTrailing_Period)[0]);
    }

    }

    Thank you in advance for any help!

    Konstantin

    #2
    Hello Konstantin,

    Thank you for your post and welcome to the NinjaTrader Support Forum!

    What you are running into is the fact that as your code is currently set the trailing stop is going to update every time the bar updates. This is because the only condition for the trailing stop is whether you are long or short.

    You will probably need to add additional conditions here such as the following to ensure we are going in a profitable direction:

    Code:
    if (Position.MarketPosition == MarketPosition.Long [B][I]&& Close[0] > Position.AvgPrice[/I][/B]
    || Position.MarketPosition == MarketPosition.Short [B][I]&& Close[0] < Position.AvgPrice[/I][/B]);
    
    {
    SetStopLoss(CalculationMode.Price, ATRTrailingStop(atrTrailing_Multip, atrTrailing_Period)[0]);
    }
    For information on AvgPrice please visit the following link: http://www.ninjatrader.com/support/h...7/avgprice.htm

    Please let me know if you have any questions.

    Comment


      #3
      What if the ATRTrailingStop indicator has not moved to the correct side of the trade when you enter? Is there a way to use a static stop until the indicator switches sides? I already have the static stop coded. Just need to know how to wait until indicator catches up. How do you monitor for this? Thanks!

      Comment


        #4
        Hello TOOLMachine462,

        Thank you for your post.

        You would use a condition to compare the value of the ATR and the Close. For example:
        Code:
        if (ATRTrailingStop(atrTrailing_Multip, atrTrailing_Period)[0] > Close[0])
        // Use for short position
        Code:
        if (ATRTrailingStop(atrTrailing_Multip, atrTrailing_Period)[0] < Close[0])
        // Use for long position

        Comment


          #5
          Thank you for your reply! Ok,that makes sense after you point it out. LOL I wasn't very thorough in my explanation. I am trying to get my strategy to use a static stop, then a trailing stop based off of the ATRTrailingStop. BUT I don't want an order placed until the bar closes on the undesirable side of the indicator line. I'm trying to test this with a tight stop but miss the stop runners! My utterly lame attempt is below. I think I need to do exit position when close is X instead of SetStopLoss but how do I wait until the ATRTrailingStop is on the correct side of PA? Thanks for your help and have a great day!

          Code:
          #region Stoploss reset, ATR Stop set, & ATR Trail
          			// Resets the stop loss to the original value when all positions are closed
          			if (Position.MarketPosition == MarketPosition.Flat)
          			{
          				//Set initial stop - ATR Stop in ticks
          				int StopLoss = Convert.ToInt32(Math.Round( ATR(ATRStopPeriod)[0] * ATRStopMulti / TickSize));
          				SetStopLoss("SWING LONG",CalculationMode.Ticks, StopLoss, false);
          				SetStopLoss("SWING SHORT",CalculationMode.Ticks, StopLoss, false);
          				
          			}
          			
          			// If a long position is open, use ATRTrailingStop after it moves below price
          			else if (Position.MarketPosition == MarketPosition.Long)
          			{
          				// Once the price is greater than ATRTrailingStop...
          				if (Close[0] > ATRTrailingStop)
          					{
          					SetStopLoss("SWING LONG", CalculationMode.Price, Close [0] < ATRTrailingStop, false);
          					}
          			}
          				
          			// If a short position is open, use ATRTrailingStop after it moves above price
          			else if (Position.MarketPosition == MarketPosition.Short)
          			{
          				// Once the price is greater than ATRTrailingStop...
          				if (Close[0] < ATRTrailingStop)
          					{
          					SetStopLoss("SWING SHORT", CalculationMode.Price, Close [0] > ATRTrailingStop, false);
          					}
          			}
          			#endregion

          Comment


            #6
            Hello TOOLMachine462,

            Thank you for your response.

            After the initial Stop Loss how far from the Close should the trailing stop be? Or is the trailing stop to trail at a value of an indicator?
            Last edited by NinjaTrader_PatrickH; 05-16-2016, 02:32 PM.

            Comment


              #7
              Lets skip the virtual aspect for now. I'll just loosen the stop up. The below code is still not working correctly. Please review and let me know if I have errors. Thanks!!

              Code:
              protected override void OnBarUpdate()
                      {
              			#region Stoploss reset, ATR Stop set, & ATR Trail
              			// Resets the stop loss to the original value when all positions are closed
              			if (Position.MarketPosition == MarketPosition.Flat)
              			{
              				//Set initial stop: ATR Stop in ticks
              				int StopLoss = Convert.ToInt32(Math.Round( ATR(ATRStopPeriod)[0] * ATRStopMulti / TickSize));
              				SetStopLoss("SWING LONG",CalculationMode.Ticks, StopLoss, false);
              				SetStopLoss("SWING SHORT",CalculationMode.Ticks, StopLoss, false);
              				
              			}
              			
              			// If a long position is open, use ATRTrailingStop after it moves below price
              			else if (Position.MarketPosition == MarketPosition.Long)
              			{
              				// Once the price is above ATRTrailingStop...
              				if (Close[0] > ATRTrailingStop(ATRTSmulti, ATRTSperiod)[0])
              					{
              					SetStopLoss("SWING LONG", CalculationMode.Price,  ATRTrailingStop(ATRTSmulti, ATRTSperiod)[0], false);
              					}
              			}
              				
              			// If a short position is open, use ATRTrailingStop after it moves above price
              			else if (Position.MarketPosition == MarketPosition.Short)
              			{
              				// Once the price is below ATRTrailingStop...
              				if (Close[0] < ATRTrailingStop(ATRTSmulti, ATRTSperiod)[0])
              					{
              					SetStopLoss("SWING SHORT", CalculationMode.Price, ATRTrailingStop(ATRTSmulti, ATRTSperiod)[0], false);
              					}
              			}
              			#endregion

              Comment


                #8
                Hello TOOLMachine462,

                Thank you for your response.

                What is not working in the code?

                Can you attach the full strategy you are testing this in?

                Comment


                  #9
                  Originally posted by NinjaTrader_PatrickH View Post
                  Hello TOOLMachine462,

                  Thank you for your response.

                  After the initial Stop Loss how far from the Close should the trailing stop be? Or is the trailing stop to trail at a value of an indicator?
                  I don't really want a trailing stop I guess. What I want is for an exit procedure to trigger once the ATRTrailingStop moves below my long position. Then the exit rules are: exit trade when the bar closes below the ATRTrailingStop value. I don't want it to close position if price hits the stop value, only when it closes below. Is this correct:

                  Code:
                  // If a long position is open, use ATRTrailingStop after it moves below price
                  else if (Position.MarketPosition == MarketPosition.Long)
                  {
                            // Once the price is greater than ATRTrailingStop...
                             if (Close[0] > ATRTrailingStop)
                             {
                                  // Use ATRTrailingStop
                                 if Close[0] < ATRTrailingStop 
                                 {
                                 ExitLong("Long Stop", "SWING LONG")
                                 }
                             }
                  {

                  Comment


                    #10
                    Hello TOOLMachine462,

                    Thanks for your post.

                    The code block here:
                    Code:
                    if (Close[0] > ATRTrailingStop)
                               {
                                    // Use ATRTrailingStop
                                   if Close[0] < ATRTrailingStop 
                                   {
                                   ExitLong("Long Stop", "SWING LONG")
                                   }
                               }
                    Would not work because these statements, if (Close[0] > ATRTrailingStop) and if (Close[0] < ATRTrailingStop ) cannot be true at the same time. The very first time the Close < ATRTrailingStop the first If statement will be false and it will never get to the second if statement.

                    What you said in your comment was: Once the price is greater than ATRTrailingStop...
                    So the word "once" is a clue that we need to use a bool variable that would be true/false that we can then use as a means to then test the close is lower than the ATRtrailingStop.

                    Here is an example. Please note that I've named the bool testForExit and it is initially set to false.

                    Code:
                    [COLOR="Blue"]if (Close[0] > ATRTrailingStop)
                    {
                    testForExit = true;  [COLOR="DarkGreen"]// set the bool to true now[/COLOR]
                    }[/COLOR]
                    [COLOR="Purple"]if (testForExit)
                    {[/COLOR]
                    [COLOR="Magenta"]    if (Close[0] < ATRTrailingStop)
                        {
                           ExitLong("Long Stop", "SWING LONG");
                        }[/COLOR]
                    [COLOR="purple"]}[/COLOR]
                    If the close is greater than the ATRTrailingStop then set the bool testForExit to true. Next we check if the bool testForExit is true, if so then we can now check to see if the Close price is less than the ATRTrailingStop, if that is true then exit the long position.

                    Once you are in a flat position, you will need to reset the bool testForExit back to false.
                    Paul H.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by fx.practic, 10-15-2013, 12:53 AM
                    5 responses
                    5,403 views
                    0 likes
                    Last Post Bidder
                    by Bidder
                     
                    Started by Shai Samuel, 07-02-2022, 02:46 PM
                    4 responses
                    94 views
                    0 likes
                    Last Post Bidder
                    by Bidder
                     
                    Started by DJ888, Yesterday, 10:57 PM
                    0 responses
                    6 views
                    0 likes
                    Last Post DJ888
                    by DJ888
                     
                    Started by MacDad, 02-25-2024, 11:48 PM
                    7 responses
                    158 views
                    0 likes
                    Last Post loganjarosz123  
                    Started by Belfortbucks, Yesterday, 09:29 PM
                    0 responses
                    8 views
                    0 likes
                    Last Post Belfortbucks  
                    Working...
                    X