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

The stop order is not following the moving average

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

    The stop order is not following the moving average

    Hi,

    I'm using your SampleOnOrderUpdate find on the forum

    you're example is based on if the market hit 4 ticks above the average price raise the stop to breakeven... but me, i want to move the stop to the moving average when the moving average is surpassing the average price, i wrote the script below.

    It is working and raise my stop to the moving average but the problem is that my stop is frozen and is not following the moving average on each new bar...

    if (entryOrder == null)
    {


    if


    Here my bying condition
    {

    /* The entryOrder object will take on a unique ID from our EnterLong()
    that we can use later for order identification purposes in the OnOrderUpdate() method. */
    entryOrder = EnterLongStop(DefaultQuantity, High[0]+TickSize, "Buy");

    }
    }

    if (Position.MarketPosition == MarketPosition.Long && SMA(SMA55)[0] > Position.AvgPrice + 1*TickSize)
    {
    StopMA=SMA(SMA55)[
    0];
    // Checks to see if our Stop Order has been submitted already
    if (stopOrder != null && stopOrder.StopPrice < Position.AvgPrice)
    {
    // Modifies stop-loss to MA - 2 ticks
    stopOrder = ExitLongStop(0, true, stopOrder.Quantity, StopMA, "StopLong", "buy");

    #2
    Thomas, can you please clarify this statement - "It is working and raise my stop to the moving average but the problem is that my stop is frozen and is not following the moving average on each new bar"?

    Does it move a few time and then stop? Does it never move at all? Your sentence seems to indicate both.

    As for the moving of the stop, if you wanted it to be two ticks below the moving average, you would need to use this code:
    Code:
    [FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]stopOrder = ExitLongStop([/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2], [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2], stopOrder.Quantity, StopMA - 2 * TickSize, [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]"StopLong"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2], [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000][FONT=Courier New][SIZE=2][COLOR=#800000]"buy"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]);[/SIZE][/FONT][/SIZE][/FONT]
    Are you sure that statement is getting executed? You can add print statements to see the progress of your script. To view these print statements, please go to Tools -> Output Window and leave it open when you run your script.

    Code:
    if (Position.MarketPosition == MarketPosition.Long && SMA(SMA55)[0] > Position.AvgPrice + 1*TickSize)
    {
        Print("in long position and moving average is greater than position");
        StopMA=SMA(SMA55)[0];
        
        // Checks to see if our Stop Order has been submitted already
        if (stopOrder != null && stopOrder.StopPrice < Position.AvgPrice)
        {
            Print("order not null and stop price < avgPrice, moving stop");
            // Modifies stop-loss to MA - 2 ticks
            stopOrder = ExitLongStop(0, true, stopOrder.Quantity, StopMA, "StopLong", "buy");
        }
    }
    AustinNinjaTrader Customer Service

    Comment


      #3
      Hi Austin

      i mean than my stop move up 2 ticks below the moving average when the moving average surpass the breakeven but thereafter... the stop stay on the same level, but after each bar the moving average continue to move up but not the stop...

      i mean the stop is not updated, is not following the moving average after each new bar...

      i would like a recall of the exitlongstop after each bar because my moving average is "moving"

      can you help ?

      Thx
      Thomas

      Comment


        #4
        Originally posted by Thomas79 View Post
        Hi Austin

        i mean than my stop move up 2 ticks below the moving average when the moving average surpass the breakeven but thereafter... the stop stay on the same level, but after each bar the moving average continue to move up but not the stop...

        i mean the stop is not updated, is not following the moving average after each new bar...

        i would like a recall of the exitlongstop after each bar because my moving average is "moving"

        can you help ?

        Thx
        Thomas
        Thomas,

        I am happy to assist you on behalf of Austin.

        You will most likely want to use an unmanaged approach. If you use SetStopLoss() and SetTrailStop() in the same strategy, SetStopLoss() will always take precedence.

        You can update SetStopLoss() according to your own logic and it will be somewhat of a trailing stop.

        Here is more information on the unmanaged approach : http://www.ninjatrader.com/support/h...d_approach.htm

        Please let me know if I may assist further.
        Last edited by NinjaTrader_AdamP; 11-07-2011, 02:37 PM.
        Adam P.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by Thomas79 View Post

          // Checks to see if our Stop Order has been submitted already
          if (stopOrder != null && stopOrder.StopPrice < Position.AvgPrice)
          {
          // Modifies stop-loss to MA - 2 ticks
          stopOrder = ExitLongStop(0, true, stopOrder.Quantity, StopMA, "StopLong", "buy");
          Emphasis mine. As written, once the StopPrice goes above the AvgPrice, as that is the gating condition, there will be no more updates of the StopLoss. The motion of the moving average becomes irrelevant. You need to write your condition so that it does what you want.

          Comment


            #6
            Thx Koganam

            it's working now !

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by bortz, 11-06-2023, 08:04 AM
            47 responses
            1,608 views
            0 likes
            Last Post aligator  
            Started by jaybedreamin, Today, 05:56 PM
            0 responses
            9 views
            0 likes
            Last Post jaybedreamin  
            Started by DJ888, 04-16-2024, 06:09 PM
            6 responses
            19 views
            0 likes
            Last Post DJ888
            by DJ888
             
            Started by Jon17, Today, 04:33 PM
            0 responses
            6 views
            0 likes
            Last Post Jon17
            by Jon17
             
            Started by Javierw.ok, Today, 04:12 PM
            0 responses
            16 views
            0 likes
            Last Post Javierw.ok  
            Working...
            X