Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy Help

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

    Strategy Help

    Good evening all,

    I copied this strategy as seen below from a programmer on youtube...partly I liked the strategy basics, and partly to help me to learn the Strategy Builder/NinjaScript/C#. I did it twice and followed it to the T. It compiles no problem, but when I try to implement it, it doesn't take any trades?

    It is a simple strategy that will watch for price to move away from a EMA for at least 4 bars and a distance of at least10 ticks...then retrace to the EMA and bounce above/below it. Could someone with a more experienced eye than me see what it is missing? Thanks in advance.

    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion

    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class GDEMABounce : Strategy
    {
    private int Counter_Long;
    private int Counter_Short;

    private EMA EMA1;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "GDEMABounce";
    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;
    Target = 15;
    Stop = 15;
    Bars_Space = 4;
    Distance_Min = 10;
    MAPeriod = 17;
    Counter_Long = 0;
    Counter_Short = 0;
    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    EMA1 = EMA(Close, Convert.ToInt32(MAPeriod));
    SetProfitTarget("", CalculationMode.Ticks, Target);
    SetStopLoss("", CalculationMode.Ticks, Stop, false);
    }
    }

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

    if (CurrentBars[0] < 1)
    return;

    // Set 1
    if ((High[0] + (Distance_Min)) <= EMA1[0])
    {
    Counter_Long = 0;
    Counter_Short += 1;
    }

    // Set 2
    if (Low[0] >= (EMA1[0] + (Distance_Min)) )
    {
    Counter_Long += 1;
    Counter_Short = 0;
    }

    // Set 3
    if ((Counter_Long >= Bars_Space)
    && (Low[0] < EMA1[0])
    && (Close[0] > EMA1[0]))
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), "");
    }

    // Set 4
    if ((Counter_Short >= Bars_Space)
    && (High[0] > EMA1[0])
    && (Close[0] < EMA1[0]))
    {
    EnterShort(Convert.ToInt32(DefaultQuantity), "");
    }

    }

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

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

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

    [NinjaScriptProperty]
    [Display(Name="Distance_Min", Order=4, GroupName="Parameters")]
    public double Distance_Min
    { get; set; }

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

    }
    }

    #2
    Hello 90bideven,

    If the strategy is getting data and the strategy is enabled or a backtest is run with no errors in the Log tab of the Control Center, then would likely indicate the logic conditions did not evaluate as true or orders are being ignored or cancelled.

    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.

    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.


    As a tip for the future, to export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient do the following:
    1. Click Tools -> Export -> NinjaScript...
    2. Click the 'add' link -> check the box(es) for the script(s) and reference(s) you want to include
    3. Click the 'Export' button
    4. Enter a unique name for the file in the value for 'File name:'
    5. Choose a save location -> click Save
    6. Click OK to clear the export location message
    By default your exported file will be in the following location:
    • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>
    Below is a link to the help guide on Exporting NinjaScripts.
    http://ninjatrader.com/support/helpG...-us/export.htm
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      OK cool, thanks. That gives me something to dig into.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Today, 05:17 AM
      0 responses
      38 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      124 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      64 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      41 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      46 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X