Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Moving Average Strategy Assistance:

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

    Moving Average Strategy Assistance:

    Hello,

    Can I get some assistance on this strat I'm trying to build? Basic MA crossover strategy: For Long, enter when 9ma crosses above the 20ma but I also want to enter when price crosses back over the 9ma but only while the 9ma is above the 20ma, and vice versa for shorts. And for exits on both long and short, when price closes below and above the 9ma respectively.

    Thank you,




    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class MyMA : Strategy
    {
    private SMA SMA1;
    private SMA SMA2;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "MyMA";
    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;
    Long = 1;
    Short = 1;
    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    SMA1 = SMA(Close, 9);
    SMA2 = SMA(Close, 20);
    }
    }

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

    if (CurrentBars[0] < 1)
    return;

    // Set 1
    if ((CrossAbove(SMA1, SMA2, 1))
    && (Close[0] > SMA1[0]))
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), @"");
    }

    // Set 2
    if ((CrossBelow(SMA1, SMA2, 1))
    && (Close[0] < SMA1[0]))
    {
    EnterShort(Convert.ToInt32(DefaultQuantity), "");
    }

    // Set 3
    if (CrossBelow(Close, SMA1, 1))
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), "", @"");
    }

    // Set 4
    if (CrossAbove(Close, SMA1, 1))
    {
    ExitShort(Convert.ToInt32(DefaultQuantity), "", "");
    }

    }

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

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

    }
    }

    region Wizard settings, neither change nor remove
    /*@

    #2
    Hello Wsummers,

    Thank you for your post.


    Please note that your strategy should not submit an entry and an exit at the same time. This can cause the position to double in the opposite direction or cause an overfill. I recommend using a check in your exit condition that requires the cross to be false.

    The forum post below goes over this in detail:
    https://forum.ninjatrader.com/forum/...16#post1036616


    In order to better understand how the code is working, it will be necessary to use Print to see how the conditions are evaluating and enable TraceOrders to see if orders are being submitted, ignored, rejected, or cancelled.

    Below is a link to a forum post that demonstrates using prints to understand behavior and includes a link to a video recorded using the Strategy Builder to add prints.
    https://ninjatrader.com/support/foru...121#post791121

    Enable TraceOrders, print the time of the bar and all values used in the conditions that submit entry orders. Include labels for all values and comparison operators.

    Let me know if you need any assistance creating a print or enabling TraceOrders.

    Save the output from the output window to a text file and provide this with your reply.

    I'll be happy to assist with analyzing the output.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    62 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    134 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    75 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    45 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    50 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X