Announcement

Collapse
No announcement yet.

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:

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    81 views
    1 like
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    42 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    64 views
    2 likes
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    66 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    54 views
    0 likes
    Last Post CarlTrading  
    Working...
    X