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

Move stops and targets after position is opened by strategy

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

    Move stops and targets after position is opened by strategy

    Hi,

    I have created a strategy for order entry using strategy builder. The entries work fine. However, I would like to be able to move the stops and profit target manually after the strategy has opened position. I would like get some guidance on this topic.

    Attached is a sample MA crossover strategy. Appreciate it I can get some help to make the stops and target movable after entry

    #2
    Strategy Builder code below:

    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class EMAX : Strategy
    {
    private EMA EMA1;
    private EMA EMA2;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "EMAX";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    FastMA = 34;
    SlowMA = 55;
    TakeProfit = 20;
    Stop = 20;
    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    EMA1 = EMA(Close, Convert.ToInt32(FastMA));
    EMA2 = EMA(Close, Convert.ToInt32(SlowMA));
    SetProfitTarget(@"Long", CalculationMode.Ticks, TakeProfit);
    SetProfitTarget(@"Short", CalculationMode.Price, TakeProfit);
    SetStopLoss(@"Long", CalculationMode.Ticks, Stop, false);
    SetStopLoss(@"Short", CalculationMode.Ticks, Stop, false);
    }
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 1)
    return;

    // Set 1
    if (CrossAbove(EMA1, EMA2, 1))
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), @"Long");
    }

    // Set 2
    if (CrossBelow(EMA1, EMA2, 1))
    {
    EnterShort(Convert.ToInt32(DefaultQuantity), @"Short");
    }

    }

    region Properties
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="FastMA", Order=1, GroupName="Parameters")]
    public int FastMA
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="SlowMA", Order=2, GroupName="Parameters")]
    public int SlowMA
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="TakeProfit", Order=3, GroupName="Parameters")]
    public int TakeProfit
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="Stop", Order=4, GroupName="Parameters")]
    public int Stop
    { get; set; }
    #endregion

    }
    }

    Comment


      #3
      Hello aprilfool,

      Thank you for your post.

      Using the Stops and Targets page of the Strategy Builder will use the Set() methods for submitting your stop loss and target orders at a static price; they may not be moved with this method in the Strategy Builder. That being said, you can certainly use the strategy builder to create an Exit order (such as ExitLongStopMarket) and you can move the order by setting the price of the order as a variable and changing that variable when certain conditions are met.

      My colleague has created examples of moving a stop loss to breakeven as well as setting a trailing stop via the Strategy Builder, and these examples should work to get an idea of how to set the Exit order with a variable for the price, then changing the variable to move the price of the order. Please see the examples in the following post:


      Please let us know if we may be of further assistance.
      Emily C.NinjaTrader Customer Service

      Comment


        #4
        Thank you. I will check this out.

        Comment


          #5
          I wonder in what universe the NT developers live that you would produce a script where the stop and target are immovable??
          I cannot think of a single situation where I would not want the ability to move my stop/target order. NOT ONE.
          They spent time and effort to create something really quite useless rather than produce what TradeSaber has suggested ie something actually
          useable in the real world.
          It would also prevent a lot of people having to ask questions in these forums because the help files would actually help lol.

          I am currently struggling with their SampleOnOrderUpdate Strategy - please bear in mind this code was written by NT to 'help'.
          It works and then it stops working - the strategy name goes yellow ( what does that imply?).
          And all I have done is turn it on and off and on again.
          Doesn't fill you with confidence.


          Comment


            #6
            Hello Mindset,

            Thank you for your feedback.

            To clarify, when using the Stops and Targets screen of the Strategy Builder, this will generate stops and targets using the Set() methods within OnStateChange(). The price and offset are static values when calling the Set() methods from OnStateChange(). Due to the limitations of the Strategy Builder, this is the only place where the Set() methods may be used. If you call the Set() methods from OnBarUpdate() instead, you may dynamically change the price while the position is open. Calling these methods in OnBarUpdate() is only an option in an unlocked script coded manually in the NinjaScript Editor. The information about the values being static vs. dynamic is explained in the "Tips" section at the bottom of the following help guide pages:When the strategy name on the Strategies tab of the Control Center is orange/yellow, that means that the Strategy positions and the Account position are not in sync. When a strategy is enabled, it essentially runs a backtest on historical data to determine if it would be in an open or flat position at the point in time when it is enabled. Depending on the Start Behavior setting selected when enabling the strategy, it may be waiting for the strategy position to become flat before it will sync with the live account and begin to place orders. For more information, please see the resources below:Please let us know if we may be of further assistance.
            Emily C.NinjaTrader Customer Service

            Comment


              #7
              I'm trying to create a strategy with three contracts, each with different stop and target parameters. However, I'm having trouble including and calling these parameters properly in the NinjaScript code. Could you please help me with this? I appreciate any guidance you can provide. Thank you!

              Comment


                #8
                Originally posted by leotraderc View Post
                I'm trying to create a strategy with three contracts, each with different stop and target parameters. However, I'm having trouble including and calling these parameters properly in the NinjaScript code. Could you please help me with this? I appreciate any guidance you can provide. Thank you!
                Hello leotraderc,

                Thanks for your note and welcome to the NinjaTrader forum community!

                I see you have already posted your question to a new thread here:
                I'm trying to create a strategy with three contracts, each with different stop and target parameters. However, I'm having trouble including and calling these parameters properly in the NinjaScript code. Could you please help me with this? I appreciate any guidance you can provide. Thank you!


                We will continue to assist you there with this topic and any related questions you might have. Thank you for using NinjaTrader!
                Emily C.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by andrewtrades, Today, 04:57 PM
                1 response
                7 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Started by chbruno, Today, 04:10 PM
                0 responses
                5 views
                0 likes
                Last Post chbruno
                by chbruno
                 
                Started by josh18955, 03-25-2023, 11:16 AM
                6 responses
                436 views
                0 likes
                Last Post Delerium  
                Started by FAQtrader, Today, 03:35 PM
                0 responses
                7 views
                0 likes
                Last Post FAQtrader  
                Started by rocketman7, Today, 09:41 AM
                5 responses
                19 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Working...
                X