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

Dyno SR level from EMA

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

    Dyno SR level from EMA

    Hello,
    I'm very new to coding and a bit of a copy and paster. But I wanted to ask if anyone could help with this.
    I'm wanting to have a horizontal line plot globally at the EMA of the 5 min chart and change on bar close or each tick.
    This is what I hacked to together. The line plots but doesn't move.
    Thanks for any help.
    James

    EMA1_Color = Brushes.DodgerBlue;

    Show_EMA1 = true;
    EMA1_TimeFrame = 5;
    EMA1_Period = 20;

    }
    else if (State == State.Configure)
    {

    AddDataSeries(Data.BarsPeriodType.Minute, (EMA1_TimeFrame)); //add our secondary data series for calculating the EMA

    }

    else if (State == State.DataLoaded)
    {

    EMA1 = EMA(BarsArray[1], (EMA1_Period)); //set EMA here so we make sure it's calculated on the secondary data series



    }

    }

    protected override void OnBarUpdate()


    {

    if (CurrentBars[0] < 1 || CurrentBars[1] < 20 ) // make sure there's at least one bar for the primary series and at least 20 of the secondary series prior to processing
    return;

    if(BarsInProgress == 0) // if OnBarUpdate was called from the primary bar series, then set the current value to the latest EMA1 value


    if
    (Show_EMA1==true)
    Draw.HorizontalLine(this, @"dyno1", false, EMA1[0], EMA1_Color, true);
    {​

    #2
    Hello laoshr,

    Thanks for your notes.

    First, I see you are calling AddDataSeries() in your script dynamically. Note that this is not supported and the arguments supplied to AddDataSeries() must be hardcoded.

    From the AddDataSeries() help guide page linked below: Arguments supplied to AddDataSeries() should be hardcoded and NOT dependent on run-time variables which cannot be reliably obtained during State.Configure (e.g., Instrument, Bars, or user input). Attempting to add a data series dynamically is NOT guaranteed and therefore should be avoided. Trying to load bars dynamically may result in an error similar to: Unable to load bars series. Your NinjaScript may be trying to use an additional data series dynamically in an unsupported manner.

    AddDataSeries(): https://ninjatrader.com/support/help...dataseries.htm

    You would need to make sure to use the proper Draw.HorizontalLine() syntax that allows you to specify the isGlobal argument. The correct syntax for drawing a horizontal line globally using Draw.HorizontalLine() could be seen below. You would set the isGlobal argument to true to have the line drawn globally.

    Draw.HorizontalLine(NinjaScriptBase owner, string tag, double y, bool isGlobal, string templateName)

    See this help guide page for more information about Draw.HorizontalLine(): https://ninjatrader.com/support/help...zontalline.htm

    ​Logic can be separated between Calculate.OnEachTick and Calculate.OnBarClose using IsFirstTickOfBar. Please note that a hosted script will inherit the Calculate mode of the script that hosts it. You can take the following approach to differentiate logic between OnBarClose and OnEachTick processing.

    Please see this reference sample which demonstrates a technique used for those who need to separate their logic to calculate some values on each tick and others only on the close of a bar. You will set your host script to Calculate.OnEachTick and use if(IsFirstTickOfBar) and place all code that needs to calculate once every bar within that condition check. Then place all code that you want to calculate OnEachTick outside of the IsFirstTickOfBar condition check.

    SampleEnterOnceExitEveryTick -https://ninjatrader.com/support/help...either_cal.htm

    Further, if a script is not behaving as expected then debugging prints should be added to the script to understand exactly how your logic is evaluating.

    ​Below is a link to a forum post that demonstrates how to use prints to understand behavior.
    https://ninjatrader.com/support/foru...121#post791121
    Brandon H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by port119, Today, 02:43 PM
    0 responses
    1 view
    0 likes
    Last Post port119
    by port119
     
    Started by Philippe56140, Today, 02:35 PM
    0 responses
    2 views
    0 likes
    Last Post Philippe56140  
    Started by 00nevest, Today, 02:27 PM
    0 responses
    1 view
    0 likes
    Last Post 00nevest  
    Started by Jonafare, 12-06-2012, 03:48 PM
    5 responses
    3,986 views
    0 likes
    Last Post rene69851  
    Started by Fitspressorest, Today, 01:38 PM
    0 responses
    2 views
    0 likes
    Last Post Fitspressorest  
    Working...
    X