Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SAR with conditional (stop) orders

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

    SAR with conditional (stop) orders

    Dear friends,
    I'm a NT newbie, so I'm learning to code.
    I want to set up a very simple system (Tomasini&Jaeckle "Luxor": nice book, indeed!).
    For long entries it is as simple as looking at 2 SMA crossover and placing a conditional stop buy order at the high of the candle where the crossover took place.
    The order must remain active till it is filled, or a crossover of the SMA's in the opposite direction takes place.
    Trades are closed just by an order of the opposite direction being filled (pure "stop and reverse").

    This is my code:

    public class LuxorTrial1 : Strategy
    {
    #region Variables
    // Wizard generated variables
    private int slowMAperiod = 30; // Default setting for SlowMAperiod
    private int fastMAperiod = 3; // Default setting for FastMAperiod
    private IOrder myLongOrder = null;
    private IOrder myShortOrder = null;
    // User defined variables (add any user defined variables below)
    #endregion

    /// <summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    /// </summary>

    protected override void Initialize()
    {
    Add(SMA(Close, FastMAperiod));
    Add(SMA(Close, SlowMAperiod));

    CalculateOnBarClose = true;
    EntriesPerDirection = 1;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>



    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (CrossAbove(SMA(Close, FastMAperiod), SMA(Close, SlowMAperiod), 1))
    {
    myLongOrder = EnterLongStop(0, true, 1, High[1], "MA bull crossover");

    if(myShortOrder != null)
    {
    CancelOrder(myShortOrder);
    myShortOrder = null;
    }
    }

    // Condition set 2
    if (CrossBelow(SMA(Close, FastMAperiod), SMA(Close, SlowMAperiod), 1))
    {
    myShortOrder = EnterShortStop(0, true, 1, Low[1], "MA bear crossover");

    if(myLongOrder != null)
    {
    CancelOrder(myLongOrder);
    myLongOrder = null;
    }
    }
    }

    Now, in backtesting I see the following:



    Would any mighty guy be so kind to explaining me what's going wrong and why it doesn't work?

    Thanks in advance!

    fsprea

    #2
    Should be High[0] and Low[0]

    Comment


      #3
      Thanks Baruch.
      Spot on the point!

      Bye

      fsprea

      Comment


        #4
        Still one more small question:
        what happens into NT with EnterLong/ShortStop if the stop is actually very close/equal to the market price of the instrument? I mean: in this small system it can happen (especially if applied to forex) that the high/low of the signal bar corresponds to the open of the next.
        In this case, of course, the order to enter should be changed in a market one.
        Does NT automatically take care of changing the stop order in a market one, or does an error be generated, and no trade entered?
        In this case I should insert some lines of control into the code and submit a different order.

        Sorry, but with a metatrader background shuch things were daily headaches!

        Thanks in advance for teaching!

        Bye

        fs

        Comment


          #5
          fsprea,

          Stop orders would be located on your brokers servers or the exchange and as such, on their end the stop order would become a market order if the price was reached.

          Could you clarify what you mean here, as in what situation are you trying to avoid? I'd be happy to comment further.
          Adam P.NinjaTrader Customer Service

          Comment


            #6
            Thank you very much Adam.

            I want to use that script for backtesting. And so I was wondering what happens if in the historical data stream the following bar opens at (or above) the previous high or below the previous low.

            Will be the EnterLongStop/ EnterShortStop be executed (at market) or cancelled, in backtesting, when these facts happen?

            Thank you in advance

            fs

            Comment


              #7
              fsprea,

              It would behave just like any stop order behaves. I.e. if the price is reached or exceeded, the stop order becomes a market order.

              You can read about other order handling rules here : http://www.ninjatrader.com/support/h...d_approach.htm

              If you would like to avoid this situation occurring, depending on your order entry logic, suggestions would be different here.
              Adam P.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              646 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              367 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              107 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              569 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              573 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X