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

action before bar is getting closed

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

    action before bar is getting closed

    Hi anyone,

    I'm trying to implement a calulation which is useful in backtest an replay mode too.
    Using the primary bars as daily and a secondary as 5min, I'm like to calculate the major logic on daily bars and the final desicion on how to react (alerting or something like this), has to be in the last minutes before the sessions ends...

    Here is my implementation for OnBarUpdate:

    protected override void OnBarUpdate()
    {
    if(CurrentBars[0] < PeriodSlow || CurrentBars[1] < PeriodSlow)
    return;


    EMA200[0] = SMA(BarsArray[0], PeriodSlow)[0];

    var percentB2 = PercentB(BarsArray[0][2], Bollinger(Deviation, PeriodBollinger).Lower[2], Bollinger(Deviation, PeriodBollinger).Upper[2]);
    var percentB1 = PercentB(BarsArray[0][1], Bollinger(Deviation, PeriodBollinger).Lower[1], Bollinger(Deviation, PeriodBollinger).Upper[1]);
    var percentB0 = PercentB(BarsArray[0][0], Bollinger(Deviation, PeriodBollinger).Lower[0], Bollinger(Deviation, PeriodBollinger).Upper[0]);

    // Update from the 5min Bars
    if(BarsInProgress == 1)
    {
    if(ToTime(Times[1][0]) >= ToTime(21,50,00))
    {
    // long
    if(Closes[0][0] > EMA200[0] && !_enteredLong)
    {
    if(percentB0 < 0.2 && percentB1 < 0.2 && percentB2 < 0.2)
    {
    EntryBuy[0] = Close[0];
    _enteredLong = true;
    }
    }
    // long agressive
    if(_enteredLong && !_aggressiveLong && percentB0 < 0.2)
    {
    EntryBuy[0] = Close[0];
    _aggressiveLong = true;
    }
    // close all long positions
    if(_enteredLong && percentB0 > 0.8)
    {
    _aggressiveLong = false;
    _enteredLong = false;
    Print("Exit agressive longs");
    }
    }
    }
    The hint is, that a minimum of 200 days is needed to calulate the SMA, so the indicators need to wait until these 200 days AND 200 * 480 5 minute bars are loaded...
    Is there any way to speed up this? Or is something wrong with my implementation?
    The preferred usage of this indicator is for market analysing, not necessarely on charts.

    Hope someone can help me :-)

    Thx for your ideas and have a nice weekend.
    Cheers Markus

    #2
    Hello Markus,

    Thanks for your post.

    Technically you would need more than 200 (trading) days for an accurate 200 period simple moving average. You may want to test charting a 200 period SMA with a various number of days to see the differences, for example 200 days, 250 days, 300 days, etc. At some length, the changes will become less significant.

    One workaround for, "The hint is, that a minimum of 200 days is needed to calculate the SMA, so the indicators need to wait until these 200 days AND 200 * 480 5 minute bars are loaded... Is there any way to speed up this?" is to create a data file that contains a date and the value of the 200 SMA for each date then in your indicator read that data file and pull the 200 period value from that historical date.

    To read or write a data file, please see these working references:

    Paul H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by funk10101, Today, 09:43 PM
    0 responses
    6 views
    0 likes
    Last Post funk10101  
    Started by pkefal, 04-11-2024, 07:39 AM
    11 responses
    37 views
    0 likes
    Last Post jeronymite  
    Started by bill2023, Yesterday, 08:51 AM
    8 responses
    44 views
    0 likes
    Last Post bill2023  
    Started by yertle, Today, 08:38 AM
    6 responses
    26 views
    0 likes
    Last Post ryjoga
    by ryjoga
     
    Started by algospoke, Yesterday, 06:40 PM
    2 responses
    24 views
    0 likes
    Last Post algospoke  
    Working...
    X