Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Moving average with 1 percentage band

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

    Moving average with 1 percentage band

    Hi!

    I would like to have the following strategy backtested: when the fast Moving Average crosses the slow MA above AND the fast MA is 1% higher than the slow MA gives me a buy signal that I hold for 10 days and the same for the short signal: when the Fast MA crosses the slow MA below then this is a enter short signal WHEN the fast MA is 1 Percent below the slow MA.

    Now I created the code below but I would like to enter only once(!) a long or short position after the signal was created. Right now with the code below I enter all the time short and long positions as long the condition is true.

    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (((SMA(Fast)[0])*1.01) > SMA(Slow)[0])
    EnterLong();

    // Condition set 2
    if (BarsSinceEntry() == 9)
    {
    ExitLong("", "");
    }
    // Condition set 3
    if (((SMA(Fast)[0])*1.01) < SMA(Slow)[0])
    EnterShort();

    // Condition set 4
    if (BarsSinceEntry() == 9)
    {
    ExitShort("", "");
    }

    }

    I would be grateful for any help or comments!

    cheers, Trados

    #2
    Hi Trados,

    Welcome to the NinjaTrader Support Forums.

    To clarify, are you wanting it to place only one long and one short order at a time? Hold that position for 10 days, exit, and then allow for new entries again?

    If so, try adding this condition with both your long/short entry conditions. This will prevent any further entry orders to be placed from when you first open a position.
    Code:
    if (Position.MarketPosition == MarketPosition.Flat)
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Hi Josh!

      Thanx for ur answer! With the strategy I assume that stocks, as soon as the fast MA crosses the slow MA there is some kind of short momentum I would like to capture. Therefore no new positions should be entered as long the fast is above the slow MA or the fast MA is below the slow MA ONLY when they cross again.

      I also tried it with the following code but it didn't work because I got an error message:

      if (CrossAbove(SMA(Fast)*1.01, SMA(Slow), 1))
      EnterLong();
      else if (CrossBelow(SMA(Fast)*1.01, SMA(Slow), 1))
      EnterShort();

      I could be wrong but I think for what I m trying to do the position.marketposition is not appropriate.

      Thx in advance!

      Comment


        #4
        Hi Trados,

        I'm not sure I understand. You want to enter long when the fast MA crosses above the short MA. Enter short when the fast MA crosses below the short MA. Is that correct?

        In that case you will need to change your code. You need to first build yourself a DataSeries that contains the value of SMA(Fast)[0] * 1.01 and then you will be able to use the CrossAbove()/CrossBelow() method.

        You can read up on how to make a DataSeries in the Help Guide. Search for "DataSeries Class".

        Here is some untested code.
        In Variables region of your code:
        Code:
        private DataSeries smaFast;
        In Initialize() method:
        Code:
        smaFast = new DataSeries(this);
        In OnBarUpdate():
        Code:
        smaFast.Set(SMA(Fast)[0] * 1.01);
        if (CrossAbove(smaFast, SMA(Slow), 1))
             EnterLong();
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Hi Josh!

          Thank u a lot for giving me an idea!!

          I would like to post the code for those who will have similar problems/ challenges:

          for the Initialize():
          smaSlow_top = new DataSeries(this);
          smaSlow_bottom = new DataSeries(this);

          and for OnBarUpdate():
          // Condition set 1
          smaSlow_top.Set(SMA(Slow)[0] * 1.01);
          if (CrossAbove(SMA(Fast), smaSlow_top, 1))
          {
          EnterLong(DefaultQuantity, "");
          }
          // Condition set 2
          if (BarsSinceEntry() == 9)
          {
          ExitLong("", "");
          }
          // Condition set 3
          smaSlow_bottom.Set(SMA(Slow)[0] * 0.99);
          if (CrossBelow(SMA(Fast), smaSlow_bottom, 1))
          {
          EnterShort(DefaultQuantity, "");
          }
          // Condition set 4
          if (BarsSinceEntry() == 9)
          {
          ExitShort("", "");

          Comment


            #6
            Thank you for sharing Trados. I am sure someone will benefit.
            Josh P.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            572 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            331 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            101 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            549 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            550 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X