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

Screenshots with indicator conditions

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

    Screenshots with indicator conditions

    Hi all !

    Please, help me.
    How I can save screenshots when conditions from my indicator is going?
    For example: indicator with crossing two SMA.I would like take screenshot automatically when its crossing.
    I see posts with automated screenshot with button, but I don't understand how add it with indicator conditions.
    Thanks for help.

    #2
    Hello vitaly_p,

    You can find a sample of taking a screenshot here:


    https://ninjatrader.com/support/foru...=34#post758176
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hello Jesse
      Thank yo for answer.
      I wrote, that I read themes with information about screenshots with button, but I don't understand how it make automatically with some conditions.

      I have code:

      #region Using declarations
      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.ComponentModel.DataAnnotations;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Windows;
      using System.Windows.Input;
      using System.Windows.Media;
      using System.Xml.Serialization;
      using NinjaTrader.Cbi;
      using NinjaTrader.Gui;
      using NinjaTrader.Gui.Chart;
      using NinjaTrader.Gui.SuperDom;
      using NinjaTrader.Gui.Tools;
      using NinjaTrader.Data;
      using NinjaTrader.NinjaScript;
      using NinjaTrader.Core.FloatingPoint;
      using NinjaTrader.NinjaScript.DrawingTools;
      #endregion

      using System.IO;
      using System.Windows.Media.Imaging;

      //This namespace holds Indicators in this folder and is required. Do not change it.
      namespace NinjaTrader.NinjaScript.Indicators
      {
      public class A1 : Indicator
      {
      private System.Windows.Controls.Grid buttonGrid;
      private NinjaTrader.Gui.Chart.Chart chart;
      private System.Windows.Media.Imaging.BitmapFrame outputFrame;
      private System.Windows.Controls.Button shotButton;
      private bool takeShot = true;
      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Введите описание новой пользовательской Индикатор здесь.";
      Name = "A1";
      Calculate = Calculate.OnBarClose;
      IsOverlay = false;
      DisplayInDataBox = true;
      DrawOnPricePanel = true;
      DrawHorizontalGridLines = true;
      DrawVerticalGridLines = true;
      PaintPriceMarkers = true;
      ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
      //Disable this property if your indicator requires custom values that cumulate with each new market data event.
      //See Help Guide for additional information.
      IsSuspendedWhileInactive = true;

      }
      else if (State == State.Configure)
      {
      chart = Window.GetWindow(ChartControl) as Chart;
      }
      }

      protected override void OnBarUpdate()
      {
      //Добавьте логику пользовательского indicator здесь.

      if (CurrentBar <20) return;

      if (CrossAbove(EMA(10), EMA(20), 1))
      {

      RenderTargetBitmap screenCapture = chart.GetScreenshot(ShareScreenshotType.Chart);
      outputFrame = BitmapFrame.Create(screenCapture);

      //Dispatcher.InvokeAsync

      if (screenCapture != null)
      {
      try
      {
      PngBitmapEncoder png = new PngBitmapEncoder();
      png.Frames.Add(outputFrame);
      var ddd = DateTime.Now;
      string ddd1 = ddd.ToString();
      using (Stream stream = File.Create(string.Format(@"{0}\{1}", Core.Globals.UserDataDir, "MyScreenshot" + ddd1 + ".png"))) png.Save(stream);
      Print("Screenshot saved to " + Core.Globals.UserDataDir);
      }
      catch (IOException)
      {
      Print("ScreenshotTakerExample: Could not take screenshot");
      }
      }
      };
      }
      }
      }

      and when Indicator started - I see "The calling thread cannot access this object because another thread owns this object."

      Comment


        #4
        Hello vitaly_p,

        I see that you commented out the dispatcher, that is needed. Please refer back to the sample for a working example of taking a screenshot. if you wanted to do that based on a condition you would need to surround the code with a condition like you have.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hello Jesse
          Thank you for help.
          I read about Async but I'm not shore that understand it fully I surround condition and don't see errors in Output Window but code don't take screenshots.



          if ( IsFirstTickOfBar && DateTime.Now.Minute == 30)
          {
          Dispatcher.InvokeAsync((Action)(() =>
          {
          RenderTargetBitmap screenCapture = chart.GetScreenshot(ShareScreenshotType.Chart);
          outputFrame = BitmapFrame.Create(screenCapture);
          if (screenCapture != null)
          {
          try
          {
          PngBitmapEncoder png = new PngBitmapEncoder();
          png.Frames.Add(outputFrame);
          var ddd = DateTime.Now;
          string ddd1 = ddd.ToString();
          using (Stream stream = File.Create(string.Format(@"{0}\{1}", Core.Globals.UserDataDir, "MyScreenshot-1-" + ddd1 + ".png"))) png.Save(stream);
          Print("Screenshot saved to " + Core.Globals.UserDataDir);
          }
          catch (IOException)
          {
          Print("ScreenshotTakerExample: Could not take screenshot");
          }
          }
          }));
          };

          With this conditions I would like to make screenshot every 30 min past every hour. I think that I put condition in wrong place...

          Comment


            #6
            Hello vitaly_p,

            I would suggest testing the sample before trying to make your own modifications to make sure you see the sample saving the images. The code you have used has a print so if the file is not saved and the print is not visible that means the code did not get executed.

            One problem in your condition is that you are using the PC clock time and not the times your script is using, you can use Time[0] to get the current time in NinjaScript.

            JesseNinjaTrader Customer Service

            Comment


              #7
              Thank you Jesse

              I will try again

              Comment


                #8
                Hello Jesse

                My code is working now. Thank you for help.

                I have another two questions:
                1. In the real-time I adding current time ( in format Time[0]) to the name of the file with screenshot, but when I start Market replay - the time is wrong (adding current time, not time from the chart). Which format of the time work correctly with Market replay?

                2. Is possible take screenshot from history? For example, I open the chart with last 5 days, add my indicator and it looks back and find all situations and take screenshots with conditions which it have.
                Because when I add State == State.Historical "true" Indicator take just one screenshot with current situation.

                Comment


                  #9
                  Helllo vitaly_p,

                  How is the time wrong when using playback? The Time[0] would represent the current bars time, the same as it would in realtime. When you use playback you should see past dates being used for the TIme[0] or whatever timeframe you had played through

                  Your script would not be able to take a historical screenshot, it would only be able to take screenshots while the chart is in view and rendered which basically would mean in realtime. You can take screenshots in playback while playing forward in time.

                  JesseNinjaTrader Customer Service

                  Comment


                    #10
                    Hello Jesse

                    I add screenshot with part of the code and you can see that in the code I have Time[0].ToString("dd/MM_HH mm ss") (part of the names of screenshots)
                    On the chart I run market replay for date 1st of July and we have 2 screenshot for my conditions BUT I have just ONE screenshot with the date 26.06.22, 18:01 (it's time from the first loading candle on this window ).
                    On Real-Time it's working OK.
                    Attached Files

                    Comment


                      #11
                      Hello vitaly_p,

                      From the image you cant really tell what's happening with the print you added. You would have to output the actual filename or time with the print to get a better idea of what's happening. You only know the condition happened but none of the specifics about the variables you are using when that happens are being output. If the condition happened within the same bar then you would get multiple prints but a non changing filename, it would overwrite the file each time. If the condition happened for a historical bar then you would get no screenshots for those bars.

                      Also just to clarify because you mentioned realtime is working, playback is considered realtime when you are playing forward in time so that should work similar to realtime on a data feed. Playback does also do a historical backtest when you apply the script, the screenshot will not work for that portion so any conditions becoming true as it processes historical will be ignored. Once it enters realtime and the chart is actually rendered the screenshot can work.


                      JesseNinjaTrader Customer Service

                      Comment


                        #12
                        Hello Jesse

                        Previous time I was wrong. The code not working correct with Time[0] in the ALL connections(real-time, Market replay and Simulated).
                        Time[0] is different Before "Async" method and into the "Async".
                        Screenshots files generate well but names of the files with part of the name Time[0] is the same and everytime resave with the same name. All time Time[0] into the "Asyns" is like the first candle of the chart (from left side) NOT REALTIME.

                        And next point what I don't understand: why Print commands after
                        using (Stream stream = File.Create(string.Format(@"{0}\{1}", Core.Globals.UserDataDir, "MyScreenshot-2-"+ (Time[0].ToString("dd/MM_HH mm ss"))+".png"))) png.Save(stream); not working ? Screenshots are generate but Print not work...
                        When I put comment for las part of this line /*png.Save(stream)*/
                        indicator generate clean file(without screenshot) but commands Print are work!!
                        Attached Files

                        Comment


                          #13
                          Hello vitaly_p,

                          If you want to use script variables you need to make a local variable before calling the dispatcher. My previous comment was in referenced to when you had used DateTIme.Now.in the actual code you posted, the images it looks like you are using Time[0] which wont work there.

                          DateTime myTime = Time[0];
                          Dispatcher.....
                          then use myTime inside the dispatcher.

                          The prints you are using are the same concept, that is not printing within the context of your script because its in the dispatcher. You could use the last print which is just text, you otherwise need to make variables for items that are script related.
                          JesseNinjaTrader Customer Service

                          Comment


                            #14
                            Jesse, Thank you!! It's work

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by MatthewLesko, Today, 08:42 AM
                            1 response
                            20 views
                            0 likes
                            Last Post NinjaTrader_ChelseaB  
                            Started by dallasstarsfan, Today, 08:22 AM
                            1 response
                            26 views
                            0 likes
                            Last Post dallasstarsfan  
                            Started by 309338, Yesterday, 12:06 PM
                            0 responses
                            29 views
                            0 likes
                            Last Post 309338
                            by 309338
                             
                            Started by GamarIsmayilova, Yesterday, 01:14 PM
                            3 responses
                            25 views
                            0 likes
                            Last Post NinjaTrader_Kate  
                            Started by OChem, Yesterday, 06:47 PM
                            2 responses
                            38 views
                            0 likes
                            Last Post aligator  
                            Working...
                            X