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

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.
    Emily C.NinjaTrader Customer Service

    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.
        Emily C.NinjaTrader Customer Service

        Comment


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

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by fx.practic, 10-15-2013, 12:53 AM
          5 responses
          5,404 views
          0 likes
          Last Post Bidder
          by Bidder
           
          Started by Shai Samuel, 07-02-2022, 02:46 PM
          4 responses
          95 views
          0 likes
          Last Post Bidder
          by Bidder
           
          Started by DJ888, Yesterday, 10:57 PM
          0 responses
          8 views
          0 likes
          Last Post DJ888
          by DJ888
           
          Started by MacDad, 02-25-2024, 11:48 PM
          7 responses
          159 views
          0 likes
          Last Post loganjarosz123  
          Started by Belfortbucks, Yesterday, 09:29 PM
          0 responses
          8 views
          0 likes
          Last Post Belfortbucks  
          Working...
          X