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

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.
    Gaby V.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Jimmyk, 01-26-2018, 05:19 AM
    6 responses
    835 views
    0 likes
    Last Post emuns
    by emuns
     
    Started by jxs_xrj, 01-12-2020, 09:49 AM
    6 responses
    3,291 views
    1 like
    Last Post jgualdronc  
    Started by Touch-Ups, Today, 10:36 AM
    0 responses
    10 views
    0 likes
    Last Post Touch-Ups  
    Started by geddyisodin, 04-25-2024, 05:20 AM
    11 responses
    62 views
    0 likes
    Last Post halgo_boulder  
    Started by Option Whisperer, Today, 09:55 AM
    0 responses
    9 views
    0 likes
    Last Post Option Whisperer  
    Working...
    X