Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Task failed successfully : Need help with bad MACD code!

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

    Task failed successfully : Need help with bad MACD code!

    So I've developed a somewhat successful automated strategy that simply takes a position at open, then manages/adds to that position with a complicated ATM strategy. I recently decided to add logic that would allow my strategy to predict a short bias for the day by looking at a higher timeframe MACD. It appeared to be a significant improvement, backtested and forward tested well. Then I realized that I had my MACD programmed wrong. I fixed it, and it's significantly worse now. So now I want to figure out what exactly my bad code was doing, because I want to know if I made a random number generator or something.

    What I wanted to do : Bot runs on a 1 minute chart and runs on bar close. I loaded a data series for 60 minute bars for my higher timeframe. The idea was to check the 60 minute timeframe MACD before making decisions for that day. When I use the print function, it appears that whatever timeframe my MACD is running on is neither the 1 minute or the 60 minute timeframes.

    I think it's pretty obvious that the problem was me defining MACD1 using what I now know to be the default timeframe, then calling it as MACD1[1] thinking that was how to reference the 60 minute dataset. I just can't find any documentation that tells me what exactly I did by referencing MACD1.Diff[1] in this context so I can actually verify my "seems too good to be true" backtest!

    Relevant code :

    private MACD MACD1;

    AddDataSeries(Data.BarsPeriodType.Minute, 60);

    MACD1 = MACD(Close, 6, 24, 7);

    if ((Position.MarketPosition == MarketPosition.Flat)
    && (Times[0][0].TimeOfDay == new TimeSpan(17, 2, 0))
    && (MACD1.Diff[1] > 0))​
    {
    BuyState = 0;
    }​

    #2
    Hello YY81056,

    Welcome to the NinjaTrader forums!

    When using multiple series, specify the input series for the indicator.

    private MACD MACD1;

    In State.DataLoaded of OnStateChange():
    MACD1 = MACD(Closes[1], 6, 24, 7);

    In OnBarUpdate() specify the BarsInProgress you want the logic evaluated on:
    if (BarsInProgress == 0 && CurrentBars[0] > 1 && CurrentBars[1] > 1)
    {
    Print(string.Format("{0} | BarsInProgress: {1}, MACD1.Diff[0]: {2}", Time[0], BarsInProgress, MACD1.Diff[0]));
    }
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    113 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    60 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    40 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    43 views
    0 likes
    Last Post TheRealMorford  
    Started by Mindset, 02-28-2026, 06:16 AM
    0 responses
    81 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Working...
    X