• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to trail limit entry and stop?

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

    How to trail limit entry and stop?

    Hi,

    I'm working on a strategy that has a limit entry order and stoploss trailing indicator values. Historical trades seem to plot as they should, yet in realtime, the pending orders are cancelled every other bar and are not replaced with new orders though the conditions still remain true. How do I either modify the limit price to the new indicator value, or cancel and replace the order so that I always have an active order when the conditions are true?

    protected override void OnBarUpdate()
    {

    if (CurrentBars[0] < 51)
    return;

    // Set 1
    if (EMA1[0] > EMA2[0])
    {
    EnterLongLimit(10000, EMA(Close, 50)[0] + (TickSize * 10), "AverageBuy" + CurrentBar);
    SetStopLoss(CalculationMode.Price, EMA(Close, 50)[0] - (TickSize * 100) );
    }

    #2
    Hello Ticnician,

    Thanks for your post and welcome to the forums!

    Orders submitted with the Managed Approach (the NinjaScript approach taken with the Strategy Builder) will only stay alive until the next bar is processed. To keep the order alive the Enter method will need to be called on each bar it should be active. If you are unlocking the code, you can use the IsLiveUntilCancelled overloads to keep orders alive until you cancel them or their Time in Force is reached.

    IsLiveUntilCancelled - https://ninjatrader.com/support/help...ancelledOrders

    Help Guide syntax - EnterLongLimit(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double limitPrice, string signalName)

    Information on EnterLongLimit parameters can be found here - https://ninjatrader.com/support/help...rlonglimit.htm

    If you would like to explicitly cancel orders in your logic, then you may use CancelOrder. Please note that you will need to track these orders as Order objects in order to use CancelOrder. Please see the example below and associated documentation for more details.

    SampleCancelOrder - https://ninjatrader.com/support/help...thod_to_ca.htm

    Also to note, SetProfitTarget/SetStopLoss should be set to an initial level when the strategy is in a flat position to ensure it is submitted to an appropriate level when your next Entry order fills. Please see the example and documentation below for further details.

    SamplePriceModification - https://ninjatrader.com/support/help...of_stop_lo.htm

    Please let me know if I can be of further assistance.
    Last edited by NinjaTrader_Jim; 12-04-2019, 06:23 AM.
    JimNinjaTrader Customer Service

    Comment


      #3
      Hi Jim,

      could you explain how to set a stop loss to the current level of an indicator, say the SMA.
      Is that at all possible?

      Thanks!

      Comment


        #4
        Hello wittmjo,

        Please see the SamplePriceModification example strategy for a demonstration on how to move the Stop Loss to a specific price level. You can use an indicator value as the price level to move it to the current level of an indicator.


        Code:
                    else if (Position.MarketPosition == MarketPosition.Long)
                    {
                        // Once the price is greater than entry price+50 ticks, set stop loss to SMA level
                        if (Close[0] > Position.AveragePrice + 50 * TickSize)
                        {
                            SetStopLoss(CalculationMode.Price, SMA1[0]);
                        }
                    }

        If you would like to see syntax for adding an indicator to your script, you can use the Strategy Builder to create a condition that adds an indicator, and then you can click View Code to see the generated code.

        Please let us know if you have any questions.
        JimNinjaTrader Customer Service

        Comment


          #5
          Hey Jim, thanks for the reply!

          I am sorry, but I meant using the Strategy Builder.
          You wrote:

          Originally posted by NinjaTrader_Jim View Post
          Orders submitted with the Managed Approach (the NinjaScript approach taken with the Strategy Builder) will only stay alive until the next bar is processed. To keep the order alive the Enter method will need to be called on each bar iot should be active. If you are unlocking the code, you can use the IsLiveUntilCancelled overloads to keep orders alive until you cancel them or their Time in Force is reached..
          I think I can handle unlocking the code and using the isliveuntilcancelled function. But I would like to not unlock the code, just to be more efficient in my analyses.

          Thanks!

          Comment


            #6
            Hello wittmjo,

            With the Strategy Builder, this would involve using your own Exit methods in Conditions and Actions to control where your Stop Loss stop market order gets placed. (SetStopLoss would not be used.) The order method must be called with each iteration of the logic if you would like to keep it alive. I have attached an example strategy that demonstrates how you can use Exit methods in Conditions and Actions for your stop loss and how that stop market order can be moved.

            Let us know if we can be of further assistance.
            Attached Files
            JimNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Derjan, Today, 03:09 AM
            0 responses
            2 views
            0 likes
            Last Post Derjan
            by Derjan
             
            Started by exutmlay, Today, 02:38 AM
            0 responses
            1 view
            0 likes
            Last Post exutmlay  
            Started by CDXTrader, Today, 12:29 AM
            0 responses
            3 views
            0 likes
            Last Post CDXTrader  
            Started by TopGun13, Yesterday, 10:35 PM
            0 responses
            4 views
            0 likes
            Last Post TopGun13  
            Started by Audiofail, Yesterday, 05:40 AM
            6 responses
            19 views
            0 likes
            Last Post willwin
            by willwin
             
            Working...
            X