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

Coding Multiple Profit Targets

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

    Coding Multiple Profit Targets

    Hello,

    I'm not a programmer, but I'm willing to learn.

    I want to backtest my ATM strategy, but I know that to do so I need to use code since the Strategy Wizard is unable to run an ATM.

    My ATM is pretty simple: there are three forex lots of the same size, each lot has a stop loss and a trailing stop after hitting a certain profit point, and each lot has a different profit target. I use ticks for metrics.

    I've looked for an example of how to code the profit targets, but I can't find one. How would I incorporate the stop loss and trailing stop into the code?

    I'm at a loss. Any help would be much appreciated!

    #2
    arichpiano, correct ATM's could not be backtested - the only option here would be using NT's Market Replay feature to get an idea how a certain setting could have performend on historical, recorded data.



    Could a full ATM exit strategy in NinjaScript will not be an easy task for a non programmer, but you to start you would need to scale in to be able to scale out later :



    You can then assign different profit targets to each uniquely named entry part.

    For the trailing stop loss part, please see this example here showing starting with a stop loss value and then trailing at a later point in time as the trade progresses :

    BertrandNinjaTrader Customer Service

    Comment


      #3
      So, to clarify, are you saying that even if I were to code an ATM into ninjascript, the only way it would backtest properly would be to use market replay?

      Comment


        #4
        Ok, so I got the code to a point where it will backtest three lots with three different profit targets. Each lot also has a stop loss associated with it. When I tried to set a trailing stop to each lot, it didn't work. Is there a way to code both a setstoploss and a settrailstop on one lot? Or does a trail stop only work in a live data feed situation?

        If one can code both a setstoploss and setttrailstop on a single lot, is there a way to set the setstoploss to work until a certain breakeven point and then activate the settrailstop?

        I apologize for my ignorance. Thanks for your help!

        Comment


          #5
          Originally posted by arichpiano View Post
          So, to clarify, are you saying that even if I were to code an ATM into ninjascript, the only way it would backtest properly would be to use market replay?
          Correct, if you call ATM strategy templates for exit management - you could not back-test this setup using historical data.
          BertrandNinjaTrader Customer Service

          Comment


            #6
            Originally posted by arichpiano View Post
            Ok, so I got the code to a point where it will backtest three lots with three different profit targets. Each lot also has a stop loss associated with it. When I tried to set a trailing stop to each lot, it didn't work. Is there a way to code both a setstoploss and a settrailstop on one lot? Or does a trail stop only work in a live data feed situation?

            If one can code both a setstoploss and setttrailstop on a single lot, is there a way to set the setstoploss to work until a certain breakeven point and then activate the settrailstop?

            I apologize for my ignorance. Thanks for your help!
            You could unfortunately not attach a SetStopLoss and a SetTrailStop to the same position, it's either one or the other.

            One way to resolve this, is to code with the SetStopLoss only and provide the trailing aspect of the stop in your own code as shown here with a stop loss that's raised to breakeven as a position hits a certain profit trigger - http://www.ninjatrader.com/support/f...ead.php?t=3222
            BertrandNinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Bertrand View Post
              One way to resolve this, is to code with the SetStopLoss only and provide the trailing aspect of the stop in your own code as shown here with a stop loss that's raised to breakeven as a position hits a certain profit trigger - http://www.ninjatrader.com/support/f...ead.php?t=3222

              Is there a way to do that where the SetStopLoss would mimic a SetTrailStop? For example, could I put a SetStopLoss at 40 ticks, move it to breakeven after hitting a profit target of 40 ticks, and then set it to trail at 50 ticks after reaching a profit target of 60 ticks?

              Alternatively, is there a way to SetStopLoss at 40 ticks, move it to breakeven after hitting the 40-tick profit target, and then completely switch to a SetTrailStop mid-trade at a 60-tick profit target?

              I've looked for answers in the forum, but nothing really seems to deal with this...

              Comment


                #8
                Yes, that would be doable with SetStopLoss - but only if you custom coded the trail aspect as shown in the example, the first step would be the breakeven one, then your adjustment for each new tick in profit after hitting the 60 ticks.
                BertrandNinjaTrader Customer Service

                Comment


                  #9
                  why isnt this working

                  Code:
                          private void GoLong()
                          {
                              SetStopLoss("target1", CalculationMode.Price, Close[0] - (Stop*TickSize), false);
                              
                              SetProfitTarget("target1", CalculationMode.Price, Close[0] + ((Target1+Target2+Target3)*TickSize));
                          
                              EnterLong("target1");
                              
                          }
                  
                          private void ManageOrders()
                          {
                  
                              if (Position.MarketPosition == MarketPosition.Long)
                              {
                                  if (High[0] > Position.AvgPrice + ((Target1+Target2+Target3)*TickSize))
                                      SetStopLoss("target1", CalculationMode.Price, Position.AvgPrice + ((Target1+Target2)*TickSize), false);
                                                  else if (High[0] > Position.AvgPrice + ((Target1+Target2)*TickSize))
                                      SetStopLoss("target1", CalculationMode.Price, Position.AvgPrice + (Target1*TickSize), false);
                                                  else if (High[0] > Position.AvgPrice + (Target1*TickSize))
                                      SetStopLoss("target1", CalculationMode.Price, Position.AvgPrice, false);
                                  
                              }
                  here is what i'm trying to do:

                  at entry, set a stop loss X ticks away
                  when my profit target1 is hit, I want to move my stoploss to position.avgprice
                  when my profit target2 is hit, i want to move my stoploss to position.avgprice+target1
                  when my profit target3 is hit, i want to move my stoploss to position.avgprice+target1+target2

                  Comment


                    #10
                    Originally posted by calhawk01 View Post
                    Code:
                            private void GoLong()
                            {
                                SetStopLoss("target1", CalculationMode.Price, Close[0] - (Stop*TickSize), false);
                     
                                SetProfitTarget("target1", CalculationMode.Price, Close[0] + ((Target1+Target2+Target3)*TickSize));
                     
                                EnterLong("target1");
                     
                            }
                     
                            private void ManageOrders()
                            {
                     
                                if (Position.MarketPosition == MarketPosition.Long)
                                {
                                    if (High[0] > Position.AvgPrice + ((Target1+Target2+Target3)*TickSize))
                                        SetStopLoss("target1", CalculationMode.Price, Position.AvgPrice + ((Target1+Target2)*TickSize), false);
                                                    else if (High[0] > Position.AvgPrice + ((Target1+Target2)*TickSize))
                                        SetStopLoss("target1", CalculationMode.Price, Position.AvgPrice + (Target1*TickSize), false);
                                                    else if (High[0] > Position.AvgPrice + (Target1*TickSize))
                                        SetStopLoss("target1", CalculationMode.Price, Position.AvgPrice, false);
                     
                                }
                    here is what i'm trying to do:

                    at entry, set a stop loss X ticks away
                    when my profit target1 is hit, I want to move my stoploss to position.avgprice
                    when my profit target2 is hit, i want to move my stoploss to position.avgprice+target1
                    when my profit target3 is hit, i want to move my stoploss to position.avgprice+target1+target2
                    How do you mean: "It is not working"? What are you expecting to happen, and what actually does happen?

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by wzgy0920, 04-20-2024, 06:09 PM
                    2 responses
                    26 views
                    0 likes
                    Last Post wzgy0920  
                    Started by wzgy0920, 02-22-2024, 01:11 AM
                    5 responses
                    32 views
                    0 likes
                    Last Post wzgy0920  
                    Started by wzgy0920, Yesterday, 09:53 PM
                    2 responses
                    49 views
                    0 likes
                    Last Post wzgy0920  
                    Started by Kensonprib, 04-28-2021, 10:11 AM
                    5 responses
                    191 views
                    0 likes
                    Last Post Hasadafa  
                    Started by GussJ, 03-04-2020, 03:11 PM
                    11 responses
                    3,230 views
                    0 likes
                    Last Post xiinteractive  
                    Working...
                    X