Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

MACD code

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

    MACD code

    Would someone who wants a proven MACD indicator to improve their track record take a look at this code and please correct the errors and let me know. A friend gave this code from TS but when I paste the script into NT there are 2 errors. The errors are indicated below and in BOLD in the script provided ...

    a namespace declaration is expected on line 3

    a using clause must proceed all the other namespace elements except extern alias declarations on line 5-6 below...


    //[LegacyColorValue = true];
    // by PepperPhd
    { General idea is to provide clarity of the location of the MACD dot (above/below BB band) and whether the MACD is rising/falling.
    Also, provides insight intothe location of a MACD dot in a Higher Time Frame (by using MACD2Factor).

    Summary...........
    MACD Dot color:
    - GREEN - rising and ABOVE upper band
    - DarkCyan - Rising
    - DarkMagenta - Falling
    - RED - Falling and BELOW lower band

    BB Line Color ... matches the MACD dot lcoation on a synthetic higher time frame (e.g., a 110 tick chart with
    a MACD2Factor of 4, will simulate the MACD dot location on a 440 tick chart).
    - GREEN - ABOVE upper band
    - Yellow (was DarkCyan) - between upper band and center line
    - Yellow (was DarkMagenta) - between lower band and center line
    - Black - BELOW lower band

    Zero Line:
    - MACD dot location toggles between above/below
    - Thickening will occur if MACD dot location on Higher Time Frame is also on same side of zero line

    BB Center Line:
    - MACD dot location toggles between above/below centerline
    - Thickening will occur if Higher Time Frame MACD Dot separation is accelerating in direction of MACD trend

    BB Fill Color:
    -- BB will have a fill line when MACD dot is above/below band
    }


    Inputs: Price( Close), FastMA(12), SlowMA(26), Length( 10), BBlength(10), NumDevs( 1), NumDevsDn( -1),
    MACD2factor(2), // creates synthetic higher time frame (e.g. x4)
    HistoPlot(true),
    ZeroXadj(1), ZeroXColor(White), // Zero Line Cross Display
    BBabove(green), BBmidup(darkcyan), BBmidlo(darkmagenta), BBbelow(black), // BB colors
    DecAdj(100), // adjust value of MACD price
    Displace( 0) ;
    Vars: MACDprice(0), Avg( 0 ), SDev( 0 ), LowerBand( 0 ), UpperBand( 0 ),
    MACDprice2(0), Avg2( 0 ), SDev2( 0 ), LowerBand2( 0 ), UpperBand2( 0 ) ;

    // Regular time frame
    MACDprice = MACD(Price, FastMA, SlowMA)*DecAdj;
    Avg = Xaverage( MACDprice, Length ) ;
    SDev = StdDev( MACDprice, BBlength) ;
    UpperBand = Avg + SDev * NumDevs ;
    LowerBand = Avg - SDev * NumDevs ;
    {

    // Next higher time frame
    MACDprice2 = MACD(Price, FastMA*MACD2factor, SlowMA*MACD2factor);
    Avg2 = Xaverage( MACDprice2, Length*MACD2factor ) ;
    SDev2 = StdDev( MACDprice2, BBlength*MACD2factor) ;
    UpperBand2 = Avg2 + SDev2 * NumDevs ;
    LowerBand2 = Avg2 - SDev2 * NumDevs ;
    }

    // Basic Plots
    if Displace >= 0 or CurrentBar > AbsValue( Displace ) then
    begin
    Plot1 [Displace]( MACDprice, "MACDdot" ) ;
    Plot2 [Displace]( 0, "Zcross" ) ;
    Plot3 [Displace]( MACDprice, "MACDshadow");
    Plot23[Displace]( UpperBand, "UpperBand" ) ;
    Plot24[Displace]( LowerBand, "LowerBand" ) ;
    Plot5 [Displace]( MACDprice, "MACDline") ;
    Plot6 [Displace]( Avg, "MACDavg");
    End;

    // MACD Trend Color Plots
    If MACDprice > MACDprice[1] then // MACD dot going up, but below Upper Band
    SetPlotColor(1,BBmidup)
    else if MACDprice < MACDprice[1] then // MACD dot going down, but above Lower Band
    setplotcolor(1,BBmidlo);

    If MACDprice > MACDprice[1] AND MACDprice > UpperBand // MACD dot going up, AND above Upper Band
    then setplotColor(1,BBabove);
    If MACDprice < MACDprice[1] AND MACDprice < LowerBand // MACD dot going down, AND below Lower Band
    then setplotColor(1,BBbelow);


    // BB Color Plots - Changes color of BB based on location of MACD on higher timeframe
    If MACDprice2 > UpperBand2 then begin
    SetPlotColor(23,BBabove);
    SetPlotColor(24,BBabove);
    end;
    if MACDprice2 < UpperBand2 AND MACDprice2 > Avg2 then begin
    setplotcolor(23,BBmidup);
    setplotcolor(24,BBmidup);
    end;

    If MACDprice2 < LowerBand2 then begin
    setplotColor(23,BBbelow);
    setplotColor(24,BBbelow);
    end;
    If MACDprice2 > LowerBand2 AND MACDprice2 < Avg2 then begin
    setplotColor(23,BBmidlo);
    setplotColor(24,BBmidlo);
    end;

    // BB plot acceleration - thickens BB center line if higher timeframe is accelerating in same trend direction
    If MACDprice2>UpperBand2 AND (MACDprice2-Upperband2)[0] > (MACDprice2-Upperband2)[1] then
    SetPlotWidth(6,3);
    If MACDprice2<LowerBand2 AND (MACDprice2-LowerBand2)[0] < (MACDprice2-LowerBand2)[1] then
    SetPlotWidth(6,3);

    // Zero Line Color Plot
    If Plot1 < 0 then
    SetPlotColor(2,darkblue);
    If Plot1 > 0 then
    SetPlotColor(2,darkblue);
    {

    // Zero Line Cross over Plots
    If Plot1[0] crosses under 0 then begin
    Plot2[0](-_tickadj(ZeroXadj),"Zcross",ZeroXColor,default,2);
    Plot2[1](0,"Zcross",ZeroXColor,default,2);
    Alert("Go Short Zero Cross");
    end else begin

    if Plot1 crosses over 0 then begin
    Plot2[0](_tickadj(ZeroXadj),"Zcross",ZeroXColor,default,2) ;
    Plot2[1](0,"Zcross",ZeroXColor,default,2);
    Alert("Go Long Zero Cross");
    End; End;
    }

    // BB Histo Plot - plots color trend when MACD is outside BB
    If HistoPlot = true
    and (MACDprice[1] > UpperBand[1] OR MACDprice[1] < LowerBand[1]) then begin
    Plot31[1](UpperBand[1],"HistoUpper");
    Plot32[1](LowerBand[1],"HistoLower");
    end;
    If MACDprice < Avg then begin
    SetPlotColor(31,BBbelow);
    SetPlotColor(32,BBbelow);
    end;
    If MACDprice > Avg then begin
    SetPlotColor(31,BBabove);
    SetPlotColor(32,BBabove);
    end;


    // Avg Plot - Centerline of BB
    If MACDprice < Avg then
    SetPlotColor(6,BBbelow);
    If MACDprice > Avg then
    SetPlotColor(6,BBabove);


    {

    // ....................... Higher Time Frame Plots ........................
    // Zero Line Color Plot of Higher Time Frame - Thickens Zline if MACD of HTF is on same side
    If MACDprice2 < 0 AND MACDprice < 0 then
    SetPlotWidth(2,4);
    If MACDprice2 > 0 AND MACDprice > 0 then
    SetPlotWidth(2,4);
    }

    #2
    Umm... after you fix the comment ERROR you highlighted in bold, you will have 100+ more errors.


    Originally posted by unispec View Post
    Would someone who wants a proven MACD indicator to improve their track record take a look at this code and please correct the errors and let me know. A friend gave this code from TS but when I paste the script into NT there are 2 errors. The errors are indicated below and in BOLD in the script provided ...

    a namespace declaration is expected on line 3

    a using clause must proceed all the other namespace elements except extern alias declarations on line 5-6 below...


    //[LegacyColorValue = true];
    // by PepperPhd
    { General idea is to provide clarity of the location of the MACD dot (above/below BB band) and whether the MACD is rising/falling.
    Also, provides insight intothe location of a MACD dot in a Higher Time Frame (by using MACD2Factor).

    Summary...........
    MACD Dot color:
    - GREEN - rising and ABOVE upper band
    - DarkCyan - Rising
    - DarkMagenta - Falling
    - RED - Falling and BELOW lower band

    BB Line Color ... matches the MACD dot lcoation on a synthetic higher time frame (e.g., a 110 tick chart with
    a MACD2Factor of 4, will simulate the MACD dot location on a 440 tick chart).
    - GREEN - ABOVE upper band
    - Yellow (was DarkCyan) - between upper band and center line
    - Yellow (was DarkMagenta) - between lower band and center line
    - Black - BELOW lower band

    Zero Line:
    - MACD dot location toggles between above/below
    - Thickening will occur if MACD dot location on Higher Time Frame is also on same side of zero line

    BB Center Line:
    - MACD dot location toggles between above/below centerline
    - Thickening will occur if Higher Time Frame MACD Dot separation is accelerating in direction of MACD trend

    BB Fill Color:
    -- BB will have a fill line when MACD dot is above/below band
    }


    Inputs: Price( Close), FastMA(12), SlowMA(26), Length( 10), BBlength(10), NumDevs( 1), NumDevsDn( -1),
    MACD2factor(2), // creates synthetic higher time frame (e.g. x4)
    HistoPlot(true),
    ZeroXadj(1), ZeroXColor(White), // Zero Line Cross Display
    BBabove(green), BBmidup(darkcyan), BBmidlo(darkmagenta), BBbelow(black), // BB colors
    DecAdj(100), // adjust value of MACD price
    Displace( 0) ;
    Vars: MACDprice(0), Avg( 0 ), SDev( 0 ), LowerBand( 0 ), UpperBand( 0 ),
    MACDprice2(0), Avg2( 0 ), SDev2( 0 ), LowerBand2( 0 ), UpperBand2( 0 ) ;

    // Regular time frame
    MACDprice = MACD(Price, FastMA, SlowMA)*DecAdj;
    Avg = Xaverage( MACDprice, Length ) ;
    SDev = StdDev( MACDprice, BBlength) ;
    UpperBand = Avg + SDev * NumDevs ;
    LowerBand = Avg - SDev * NumDevs ;
    {

    // Next higher time frame
    MACDprice2 = MACD(Price, FastMA*MACD2factor, SlowMA*MACD2factor);
    Avg2 = Xaverage( MACDprice2, Length*MACD2factor ) ;
    SDev2 = StdDev( MACDprice2, BBlength*MACD2factor) ;
    UpperBand2 = Avg2 + SDev2 * NumDevs ;
    LowerBand2 = Avg2 - SDev2 * NumDevs ;
    }

    // Basic Plots
    if Displace >= 0 or CurrentBar > AbsValue( Displace ) then
    begin
    Plot1 [Displace]( MACDprice, "MACDdot" ) ;
    Plot2 [Displace]( 0, "Zcross" ) ;
    Plot3 [Displace]( MACDprice, "MACDshadow");
    Plot23[Displace]( UpperBand, "UpperBand" ) ;
    Plot24[Displace]( LowerBand, "LowerBand" ) ;
    Plot5 [Displace]( MACDprice, "MACDline") ;
    Plot6 [Displace]( Avg, "MACDavg");
    End;

    // MACD Trend Color Plots
    If MACDprice > MACDprice[1] then // MACD dot going up, but below Upper Band
    SetPlotColor(1,BBmidup)
    else if MACDprice < MACDprice[1] then // MACD dot going down, but above Lower Band
    setplotcolor(1,BBmidlo);

    If MACDprice > MACDprice[1] AND MACDprice > UpperBand // MACD dot going up, AND above Upper Band
    then setplotColor(1,BBabove);
    If MACDprice < MACDprice[1] AND MACDprice < LowerBand // MACD dot going down, AND below Lower Band
    then setplotColor(1,BBbelow);


    // BB Color Plots - Changes color of BB based on location of MACD on higher timeframe
    If MACDprice2 > UpperBand2 then begin
    SetPlotColor(23,BBabove);
    SetPlotColor(24,BBabove);
    end;
    if MACDprice2 < UpperBand2 AND MACDprice2 > Avg2 then begin
    setplotcolor(23,BBmidup);
    setplotcolor(24,BBmidup);
    end;

    If MACDprice2 < LowerBand2 then begin
    setplotColor(23,BBbelow);
    setplotColor(24,BBbelow);
    end;
    If MACDprice2 > LowerBand2 AND MACDprice2 < Avg2 then begin
    setplotColor(23,BBmidlo);
    setplotColor(24,BBmidlo);
    end;

    // BB plot acceleration - thickens BB center line if higher timeframe is accelerating in same trend direction
    If MACDprice2>UpperBand2 AND (MACDprice2-Upperband2)[0] > (MACDprice2-Upperband2)[1] then
    SetPlotWidth(6,3);
    If MACDprice2<LowerBand2 AND (MACDprice2-LowerBand2)[0] < (MACDprice2-LowerBand2)[1] then
    SetPlotWidth(6,3);

    // Zero Line Color Plot
    If Plot1 < 0 then
    SetPlotColor(2,darkblue);
    If Plot1 > 0 then
    SetPlotColor(2,darkblue);
    {

    // Zero Line Cross over Plots
    If Plot1[0] crosses under 0 then begin
    Plot2[0](-_tickadj(ZeroXadj),"Zcross",ZeroXColor,default,2);
    Plot2[1](0,"Zcross",ZeroXColor,default,2);
    Alert("Go Short Zero Cross");
    end else begin

    if Plot1 crosses over 0 then begin
    Plot2[0](_tickadj(ZeroXadj),"Zcross",ZeroXColor,default,2) ;
    Plot2[1](0,"Zcross",ZeroXColor,default,2);
    Alert("Go Long Zero Cross");
    End; End;
    }

    // BB Histo Plot - plots color trend when MACD is outside BB
    If HistoPlot = true
    and (MACDprice[1] > UpperBand[1] OR MACDprice[1] < LowerBand[1]) then begin
    Plot31[1](UpperBand[1],"HistoUpper");
    Plot32[1](LowerBand[1],"HistoLower");
    end;
    If MACDprice < Avg then begin
    SetPlotColor(31,BBbelow);
    SetPlotColor(32,BBbelow);
    end;
    If MACDprice > Avg then begin
    SetPlotColor(31,BBabove);
    SetPlotColor(32,BBabove);
    end;


    // Avg Plot - Centerline of BB
    If MACDprice < Avg then
    SetPlotColor(6,BBbelow);
    If MACDprice > Avg then
    SetPlotColor(6,BBabove);


    {

    // ....................... Higher Time Frame Plots ........................
    // Zero Line Color Plot of Higher Time Frame - Thickens Zline if MACD of HTF is on same side
    If MACDprice2 < 0 AND MACDprice < 0 then
    SetPlotWidth(2,4);
    If MACDprice2 > 0 AND MACDprice > 0 then
    SetPlotWidth(2,4);
    }

    Comment


      #3
      unispec, sledge is unfortunately correct here, since this is TS Easy Lanuage code you could not copy this into the C# based NinjaScript environment and expect it to compile / work without changes. I will leave this thread open here if a community member wants to give it a go at converting, but code conversion work is also done professionally by those certified NinjaScript consultants here if you desire to take that route -

      BertrandNinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by bortz, 11-06-2023, 08:04 AM
      47 responses
      1,603 views
      0 likes
      Last Post aligator  
      Started by jaybedreamin, Today, 05:56 PM
      0 responses
      8 views
      0 likes
      Last Post jaybedreamin  
      Started by DJ888, 04-16-2024, 06:09 PM
      6 responses
      18 views
      0 likes
      Last Post DJ888
      by DJ888
       
      Started by Jon17, Today, 04:33 PM
      0 responses
      4 views
      0 likes
      Last Post Jon17
      by Jon17
       
      Started by Javierw.ok, Today, 04:12 PM
      0 responses
      12 views
      0 likes
      Last Post Javierw.ok  
      Working...
      X