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

Multi-Time Frame Strategy Script- Using Private Labels

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

    Multi-Time Frame Strategy Script- Using Private Labels

    I have modified a Ninja Wizard Strategy for a Multi-Time Frame Strategy Script (300 Tick & 50 Ticks) + Using Private Labels (copied below). Though the script complies correctly, it will not execute any trades.

    I would appreciate confirmation if i can i use private labels in a Multi-Time Frame Strategy Scrip and if so how do i do so correctly

    Thank you
    ------------------------------------------

    publicclass MultiTestA : Strategy
    {
    #region Variables
    // Wizard generated variables
    privateint myInput0 = 1; // Default setting for MyInput0
    // User defined variables (add any user defined variables below)
    #endregion


    #region Variables
    // Defining Private LABELS for FOR PRIMARY DATA (ie 300 Ticks)

    private EMA E01; // ema(25)
    private EMA E02; // ema(50)
    private SMA S01; // sma(25)
    private SMA S02; // sma (50)

    #endregion


    ///</summary>
    protectedoverridevoid Initialize()
    {


    // ADD:- Trigger time-frame
    Add(PeriodType.Tick, 50); // BarsArray[1] where Index==1

    // Defining Private labels for Primary Chart/Data Indicators
    E01 = (EMA(25));
    E02 = (EMA(50));
    S01 = (SMA(25));
    S02 = (SMA(50));

    //Adding Trigger related Stochastic Indicators for BarArray[1]
    Add(Stochastics(BarsArray[1],7,14,5));

    //Setting GLOBAL profit/stop loss/& CalcOnBarClose
    SetProfitTarget("", CalculationMode.Ticks, 6);
    SetStopLoss("", CalculationMode.Ticks, 4, true);

    CalculateOnBarClose = true;
    }

    ///<summary>
    /// Called on each bar update event (incoming tick)
    ///</summary>
    protectedoverridevoid OnBarUpdate()
    {
    // Condition set 1 - SHORT

    // when Primary Data >> E02 <below S01 && S01 <below S02
    // + Falling S01
    // >> Then Short = 50 Ticks Stochastic(7,14.5).K >above 80

    if (E01 [0] < S01 [0]
    && S01 [0] < S02 [0]
    && Falling(S01) == true

    // trigger =
    && Stochastics(BarsArray[1],7,14,5).K [0] > 80);

    {
    EnterShort(DefaultQuantity, "");
    }

    #2
    Hello patkum,

    Thanks for your post.

    As a general reference when you add a strategy to a chart and do not see expected executions observe if the strategy has become disabled, this will be indicator by the (D) in front of the strategy name on the chart (at the top left in the chart). Also a good reference is to check the "Log" tab of the control center for any run time errors created when the strategy runs.

    In this case the strategy compiles without errors however when applied to a chart will immediately show (D) and will generate errors. The error messages will show that BarsArray property cannot be accessed with the Initialize() method. So to correct this, comment out the line Add(Stochastics(BarsArray[1],7,14,5));
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hi Paul

      Thanks for your prompt reply. So my better understanding,

      1) can i assume that Private labels (eg private EMA E01 is compatible and hence can be used in coding of Multi Time Frame Strategies?

      2) Can Private labels be used for ALL THE DIFFERENT time frame data (BarArrays) in a Multi Time Frame Strategies? - if so, then would there be any special notation for Primary Data (eg 300T) vs all other (eg secondary = 50T) Data?

      3) If i remove the line Add(Stochastics(BarsArray[1],7,14,5)); then how would the Strategy still be able to use this lower time frame indicator as a condition to effect trade executions

      >> FYI, upon removing this line, the Strategy will execute trades >> HOWEVER, i get multiple trades on (almost) every bar vs the expected SHORT trades only when the primary data (300T) conditions are met + where the 50T stochastic is above 80. >> HENCE, there must be a major error in which i have introduced and am using the lower 50T bar array data within the Strategy

      Your further clarification & guidance is appreciated.

      Comment


        #4
        Hello patkum,

        Thanks for your reply.

        1) Yes

        2) Yes, however you would need to initialize them in the OnStartUp() method , ex:

        Code:
        protected override void OnStartUp()
        {
        E01 = EMA(BarsArray[1], 50);  // init to second bars object.
        }
        3) You use the Stochastics directly in the code precisely as you did in the OnBarUpdate() method, or create as a private label in the same manner as the EMA.

        4) Correct, the issue is that each added barseries will call OnBarUpdate() and will shift references to that dataseries. So the 50 tick series was calling OnBarUpdate() 6 times before the 300 tick series.

        As a quick test for you to see the difference, add this as the first line in OnBarUpdate()
        if (BarsInProgress != 0) return; // only process on 300 tick bars

        Please review the helpguides section on MultiTimeFrame as there is critical need to know info there for coding, here is a link: http://ninjatrader.com/support/helpG...nstruments.htm
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Hi Paul

          Thanks. One further clarification please.

          I understand that introducing if (BarsInProgress != 0) return; to the script will only process on 300 T bar data - which is what i seek as my primary condition & data. However, in this case, how would the script factor and honor the additional 50T trigger condition - so that a SHORT trade only executes if and WHEN BOTH the Primary Data Condition +plus the Secondary lower Data Condition is met?

          Best regards

          Comment


            #6
            Hello patkum,

            Thanks for your reply.

            In your code you have correctly addressed this by using && Stochastics(BarsArray[1],7,14,5).K [0] > 80);

            Your code will execute on the close of each 300 tick bar and will compare these conditions:

            if (E01 [0] < S01 [0]
            && S01 [0] < S02 [0]
            && Falling(S01) == true
            && Stochastics(BarsArray[1],7,14,5).K [0] > 80);
            // check the stochastics K on the 50 tick data.

            Again I would encourage you to read completely the section on Multi Time Frame as all of your questions are answered there: http://ninjatrader.com/support/helpG...nstruments.htm
            Paul H.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by GussJ, 03-04-2020, 03:11 PM
            11 responses
            3,228 views
            0 likes
            Last Post xiinteractive  
            Started by andrewtrades, Today, 04:57 PM
            1 response
            13 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by chbruno, Today, 04:10 PM
            0 responses
            7 views
            0 likes
            Last Post chbruno
            by chbruno
             
            Started by josh18955, 03-25-2023, 11:16 AM
            6 responses
            440 views
            0 likes
            Last Post Delerium  
            Started by FAQtrader, Today, 03:35 PM
            0 responses
            12 views
            0 likes
            Last Post FAQtrader  
            Working...
            X