Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using inputs to back test different pre-defined filters

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

    Using inputs to back test different pre-defined filters

    Not sure exactly how to word this questions. I want to have different pre-defined entry filters stated as variables (f1, f2, f3, f4, etc) that I can cycle through using and Input to determine which filter to use. Input: 1 = f1, 2 = f2, 3 = f3, 4 =f4

    Here is an idea of the code.

    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class BuilderTemplateCode : Strategy
    {


    private ADX ADX1;
    private ADX ADX2;
    private ADX ADX3;
    private ADX ADX4;
    private RSI RSI1;
    private RSI RSI2;


    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "BuilderTemplateCode";
    Calculate = Calculate.OnEachTick;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 300;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 1;
    IncludeCommission = true;
    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;

    Input1 = 0;
    Input2 = 0;

    }

    else if (State == State.Configure)
    {

    }

    else if (State == State.DataLoaded)
    {
    SetStopLoss("", CalculationMode.Ticks, 10, true);
    SetProfitTarget("", CalculationMode.Ticks, 30);
    ADX1 = ADX(Close, 5);
    ADX2 = ADX(Close, 10);
    ADX3 = ADX(Close, 15);
    ADX4 = ADX(Close, 20);
    RSI1 = RSI(Close, 15, 3);
    RSI2 = RSI(Close, 12, 3);

    }
    }

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

    if (CurrentBars[0] < 5)
    return;

    var f1 = (ADX1[0] > 30);
    var f2 = (ADX2[0] > 45);
    var f3 = (RSI1[0] > 80);
    var f4 = (RSI1[0] < 20);


    if (
    // - EntryConditions
    && (Close[0] > Close[1])
    && (Close[1] > Close[2])
    // - Filters
    && (f[Input1])
    )
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), "");
    }

    if (
    // - EntryConditions
    && (Close[0] < Close[1])
    && (Close[1] < Close[2])
    // - Filters
    && (f[Input2])
    )

    {
    EnterShort(Convert.ToInt32(DefaultQuantity), "");
    }

    }

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

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




    #2
    Hello rodriguesdanny,

    Thank you for your post.

    Please clarify; are the inputs supposed to determine which indicators are being used in the logic/conditions?
    Code:
    var f1 = (ADX1[0] > 30);
    var f2 = (ADX2[0] > 45);
    var f3 = (RSI1[0] > 80);
    var f4 = (RSI1[0] < 20);​
    Please describe the concept you are going for; for example, if Input1 and Input2 are set to specific values, what do you expect the outcome to be in your logic?

    I look forward to your clarification.

    Comment


      #3
      Correct if the Input = 1 then use var f1 as a condition for the entry, if Input = 2 use f2, etc. then use that as a condition to determine whether a trade should be taken or not.

      Comment


        #4
        Hello rodriguesdanny,

        Thank you for your reply.

        You could set up the desired conditions as bools, such as conditionToMeet1 and conditionToMeet2. Then, you could configure the bools based on the input provided. For example:
        Code:
        protected override void OnBarUpdate()
        {
        if (BarsInProgress != 0)
        return;
        
        if (CurrentBars[0] < 5)
        return;
        
        if (Input1 = 1)
        conditionToMeet1 = ADX1[0] > 30;
        else if (Input1 = 2)
        conditionToMeet1 = ADX2[0] > 45;
        else if (Input1 = 3)
        conditionToMeet1 = RSI1[0] > 80;
        else if (Input1 = 3)
        conditionToMeet1 = RSI1[0] < 20;
        else
        return;
        
        if (Input2 = 1)
        conditionToMeet2 = ADX1[0] > 30;
        else if (Input2 = 2)
        conditionToMeet2 = ADX2[0] > 45;
        else if (Input2 = 3)
        conditionToMeet2 = RSI1[0] > 80;
        else if (Input2 = 3)
        conditionToMeet2 = RSI1[0] < 20;
        else
        return;​
        
        if (
        // - EntryConditions
        && (Close[0] > Close[1])
        && (Close[1] > Close[2])
        // - Filters
        && (conditionToMeet1)
        )
        {
        EnterLong(Convert.ToInt32(DefaultQuantity), "");
        }
        if (
        // - EntryConditions
        && (Close[0] < Close[1])
        && (Close[1] < Close[2])
        // - Filters
        && (conditionToMeet2)
        )
        
        {
        EnterShort(Convert.ToInt32(DefaultQuantity), "");
        }
        }​

        Otherwise, if you're looking for other ways to customize the selections of the conditions or input values, I suggest checking out this reference sample with different ways to customize property grid behavior to see if one of these use cases might suit your needs:


        ​​​​​​​Please let us know if we may be of further assistance.

        Comment


          #5
          Thank you, I will try this tonight and follow up.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by NullPointStrategies, Yesterday, 05:17 AM
          0 responses
          53 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          130 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          70 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          44 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          49 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X