Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Drawing line in oscillator

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

    Drawing line in oscillator

    Hi,
    I am developing a strategy and want to add two line to MACD indicator when I run strategy .
    I have used AddLine method but it doesn't work.

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "ReverseUniRenkoStratetegy";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 0;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    LookbackOffset = 2;
    MaxProfit = 0;
    MaxLoss = 0;
    TakeProfit = 0;
    StopLoss = 0;
    ProfitPrice = 0;
    LossPrice = 0;
    MACDUpperBoundary = 0;
    MACDLowerBoundary = 0;
    }
    else if (State == State.Configure)
    {
    InitMACD();
    }
    else if (State == State.DataLoaded)
    {
    ClearOutputWindow();
    DayNetProfit = 0;
    LastTradeIndexProcessed = SystemPerformance.AllTrades.Count();
    sessionIterator = new SessionIterator(Bars);
    MyPrint(string.Format("Current Profit", SystemPerformance.AllTrades.TradesPerformance.Curr ency.CumProfit));
    }
    }


    void InitMACD()
    {
    if(MACDUpperBoundary != 0 || MACDLowerBoundary != 0)
    {
    macd = MACD(12, 26, 9);
    macd.Panel = 2;
    macd.Plots[0].Brush = Brushes.Transparent;
    macd.Plots[1].Brush = Brushes.Transparent;
    macd.Plots[2].Width = 4;
    AddChartIndicator(macd);

    AddLine(Brushes.Red, MACDUpperBoundary, "UpperBoundary"); // MACDUpperBoundary=0.3
    AddLine(Brushes.Red, MACDLowerBoundary, "LowerBoundary");// MACDLowerBoundary=-0.3
    }
    }



    #2
    Hello bosajin,

    (edited)
    AddLine() must be called in State.SetDefaults or State.Configure.

    Below is a link to the help guide. Please read the note at the top.
    https://ninjatrader.com/support/help...t8/addline.htm

    You can alternatively use Draw.HorizontalLine() in OnBarUpdate().
    https://ninjatrader.com/support/help...zontalline.htm
    Last edited by NinjaTrader_ChelseaB; 07-28-2020, 12:04 PM.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi,

      It still doesn't work.
      Please take a look at the attached code.
      Attached Files

      Comment


        #4
        Hello bosajin,

        As a tip, to export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient do the following:
        1. Click Tools -> Export -> NinjaScript...
        2. Click the 'add' link -> check the box(es) for the script(s) and reference(s) you want to include
        3. Click the 'Export' button
        4. Enter a unique name for the file in the value for 'File name:'
        5. Choose a save location -> click Save
        6. Click OK to clear the export location message
        By default your exported file will be in the following location:
        • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>
        Below is a link to the help guide on Exporting NinjaScripts.



        Apologies, I should have stated State.SetDefaults or State.Configure as stated in the help guide.



        A small note, AddLine() is primarily used for indicators and is in the Indicators section of the help guide.
        Typically, an indicator would be created to do drawing and adding lines and this would be added to the chart by the strategy with AddChartIndicator().
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hi,

          I attached my code.
          I guess ,I have done everything you have suggested.
          Attached Files

          Comment


            #6
            Hello bosajin,

            You have the calls to AddLine() in the InitMACD() method called from State.DataLoaded.

            Move these lines (62 and 63) to a new else if with State.Configure.

            Code:
            else if (State == State.Configure)
            {
                AddLine(Brushes.Red, -0.3, "UpperBoundary");
                AddLine(Brushes.Red, 0.3, "LowerBoundary");
            }
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              It doesn't work.

              protected override void OnStateChange()
              {
              if (State == State.SetDefaults)
              {
              Description = "Test Drewing";
              Name = "TestDrewing";
              IsInstantiatedOnEachOptimizationIteration = false;


              }
              else if (State == State.Configure)
              {
              AddLine(Brushes.Red, -0.3, "UpperBoundary");
              AddLine(Brushes.Red, 0.3, "LowerBoundary");
              }
              else if (State == State.DataLoaded)
              {
              smaFast = SMA(10);
              smaSlow = SMA(25);
              InitMACD();
              }
              }

              void InitMACD()
              {

              macd = MACD(12, 26, 9);
              macd.Panel = 2;
              macd.Plots[0].Brush = Brushes.Transparent;
              macd.Plots[1].Brush = Brushes.Transparent;
              macd.Plots[2].Width = 4;
              AddChartIndicator(macd);
              }

              Comment


                #8
                Hello bosajin,

                I am showing this is working without issue.

                Below is a link to a video.


                Your screenshot is not showing the chart at -0.3 and 0.3. You need to scroll down to see those lines.

                (I have previously recommended that you add the lines to an indicator and not in a Strategy, which can be added to another panel)
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Hi Chelsea,

                  Thanks for the video.
                  As I mentioned in title, I want to add line to oscillator (MACD indictator), not chart.
                  Last edited by bosajin; 07-28-2020, 01:05 PM.

                  Comment


                    #10

                    Comment


                      #11
                      Hello bosajin,

                      Make a copy of the MACD indicator.
                      Use a unique name for the indicator name and Name property.

                      Add the AddLine() calls to the new copy indicator.

                      Call this indicator from your strategy.
                      Chelsea B.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by NullPointStrategies, Today, 05:17 AM
                      0 responses
                      38 views
                      0 likes
                      Last Post NullPointStrategies  
                      Started by argusthome, 03-08-2026, 10:06 AM
                      0 responses
                      124 views
                      0 likes
                      Last Post argusthome  
                      Started by NabilKhattabi, 03-06-2026, 11:18 AM
                      0 responses
                      64 views
                      0 likes
                      Last Post NabilKhattabi  
                      Started by Deep42, 03-06-2026, 12:28 AM
                      0 responses
                      41 views
                      0 likes
                      Last Post Deep42
                      by Deep42
                       
                      Started by TheRealMorford, 03-05-2026, 06:15 PM
                      0 responses
                      46 views
                      0 likes
                      Last Post TheRealMorford  
                      Working...
                      X