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

Indicator triggering alert when EMAs cross

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

    Indicator triggering alert when EMAs cross

    Hello everyone,

    I have used openAI (chatbotGPT) to generate a code for an indictaor that when active on a chart should trigger an alarm when EMA 20 a EMA 50 cross each other. But it does not work (seems that this AI is not that inteligent after all ;-).
    I am no coder I just have some very basic understandings about coding.
    Maybe someone can have a look and find the problem? Thanks a lot!

    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

    //This namespace holds Indicators in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class EMA20EMA50Cross : Indicator
    {
    private EMA ema20;
    private EMA ema50;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = "An indicator that alerts when the 20-period EMA and 50-period EMA cross above or below each other";
    Name = "EMA20EMA50Cross";
    ema20 = EMA(20);
    ema50 = EMA(50);
    }
    }

    protected override void OnBarUpdate()
    {





    if (CrossAbove(ema20, ema50, 1) || CrossBelow(ema20, ema50, 1))

    {
    Print("EMA Cross Alert triggered at bar: " + CurrentBar);
    Alert("EMA Cross Alert", Priority.High, "EMA 20 and EMA 50 crossed", "Alert1.wav", 20, Brushes.Red, Brushes.White);
    }
    }
    }
    }

    region NinjaScript generated code. Neither change nor remove.

    namespace NinjaTrader.NinjaScript.Indicators
    {
    public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
    {
    private EMA20EMA50Cross[] cacheEMA20EMA50Cross;
    public EMA20EMA50Cross EMA20EMA50Cross()
    {
    return EMA20EMA50Cross(Input);
    }

    public EMA20EMA50Cross EMA20EMA50Cross(ISeries<double> input)
    {
    if (cacheEMA20EMA50Cross != null)
    for (int idx = 0; idx < cacheEMA20EMA50Cross.Length; idx++)
    if (cacheEMA20EMA50Cross[idx] != null && cacheEMA20EMA50Cross[idx].EqualsInput(input))
    return cacheEMA20EMA50Cross[idx];
    return CacheIndicator<EMA20EMA50Cross>(new EMA20EMA50Cross(), input, ref cacheEMA20EMA50Cross);
    }
    }
    }

    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
    public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
    {
    public Indicators.EMA20EMA50Cross EMA20EMA50Cross()
    {
    return indicator.EMA20EMA50Cross(Input);
    }

    public Indicators.EMA20EMA50Cross EMA20EMA50Cross(ISeries<double> input )
    {
    return indicator.EMA20EMA50Cross(input);
    }
    }
    }

    namespace NinjaTrader.NinjaScript.Strategies
    {
    public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
    {
    public Indicators.EMA20EMA50Cross EMA20EMA50Cross()
    {
    return indicator.EMA20EMA50Cross(Input);
    }

    public Indicators.EMA20EMA50Cross EMA20EMA50Cross(ISeries<double> input )
    {
    return indicator.EMA20EMA50Cross(input);
    }
    }
    }

    #endregion
    Last edited by cyberpete76; 01-09-2023, 02:01 AM.

    #2
    Hello cyberpete76,

    You would need to use the Prints that the code has to see if its working or not. You can use the NinjaScript output window to see if these conditions ever become true in realtime. Alerts only work in realtime so if you were testing historically then this code should do nothing besides print when the conditions are true.

    JesseNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello cyberpete76,

      You would need to use the Prints that the code has to see if its working or not. You can use the NinjaScript output window to see if these conditions ever become true in realtime. Alerts only work in realtime so if you were testing historically then this code should do nothing besides print when the conditions are true.

      https://ninjatrader.com/support/help...nt8/output.htm
      Yes, I see numerous Alerts triggered in the NinjaScript Output window for the chart where the indicator is used, but still no alert in any window appeared. Of course I was testing this on realtime data.
      Maybe there is a problem with the panel in which the indicator is placed?
      Or any other setting in the indicator´s setting? I am attaching two screenshots.
      Attached Files
      Last edited by cyberpete76; 01-09-2023, 02:06 PM.

      Comment


        #4
        Hello cyberpete76,

        To further clarify, are you seeing alerts in the alert log window? That is New -> Alert log

        If you have the audio path wrong for the sound then it will just produce an alert to the log
        JesseNinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_Jesse View Post
          Hello cyberpete76,

          To further clarify, are you seeing alerts in the alert log window? That is New -> Alert log

          If you have the audio path wrong for the sound then it will just produce an alert to the log
          Yes, I have alerts in the alerts log. Seems like I get my colors from the code (red background and white text) right, but still no pop-up window or anything like that. In the code I have the standard Alert1.wav, which is in the default sounds folder: C:\Program Files\NinjaTrader 8\sounds

          Edit: I added the path to the soundfile to the script: NinjaTrader.Core.Globals.InstallDir+...s\Aler t1.wav" according to the Alert script definition.
          In the Ninjascript output I see the date and time for another crossing, also the alert log shows the actual alert, AND the sound was played!
          But still no pop-up
          Attached Files
          Last edited by cyberpete76; 01-10-2023, 07:05 AM.

          Comment


            #6
            Hello cyberpete76,

            There are no popups with alerts, they are just sound and also go to the alert log.

            If you plan to use that chatbot tool in the future you will need to go through whatever it makes line by line to compare against the help guide. That will help to avoid problems as that may generate invalid code since it was developed by a third party.

            JesseNinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_Jesse View Post
              Hello cyberpete76,

              There are no popups with alerts, they are just sound and also go to the alert log.
              I don´t understand? When I set up an alert in my chart manually I can choose in the actions section from: "Play a sound", "Share a message", "Show a pop up a dialoh" etc. So I can´t trigger a pop up dialog and a sound simultanously via the script in the indicator?

              Comment


                #8
                Hello cyberpete76,

                The alerts window has other actions that it can do besides just the alert. If you want to open a message box from NinjaScript you need to use the method for that. There are two ways to do that, you can either log a message and use LogLevel.Alert or use the built in message box class.

                https://ninjatrader.com/support/helpGuides/nt8/log.htm

                https://ninjatrader.com/support/help...sub=messagebox

                If you wanted to play a sound at the same time you can use PlaySound() or also the Alert() method which additionally logs it to the alerts window.

                https://ninjatrader.com/support/help.../playsound.htm
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Thanks Jesse for the help, will work on this further.

                  Comment


                    #10
                    Hi I have something similar where I want my chart alerts to use the 'exit all positions' indicator. I had AI produce this code after quite a few attempts and it cannot fix the last error. it says '}" line 128 col 2 is expected, but as soon as i add that in it creates more errors.I have tried adding/subtracting '}' without any success. Would someone be able to find the actual error? My ultimate goal is to have my chart alerts exit all positions when a certain criteria hits, such as number of positions, above a certain profit level etc. exit ALL positions 2 .txt
                    Last edited by willwin; 04-09-2023, 08:07 AM.

                    Comment


                      #11
                      Hello willwin,

                      From our experience at this time, ChatGpt is not quite adequate to generate valid compilable NinjaScripts that function as the user has intentioned. We often find that the generated code will call non-existent properties and methods, use improper classes or inheritance, and may have incorrect logic. We highly encourage that you create a new NinjaScript yourself using the NinjaScript Editor, and use the code generated by ChatGpt as more as suggestions and guide when coding the script yourself, than using the actual code generated.

                      While It would not be within our support model to correct these scripts at user request, we would be happy to provide insight for any direct specific inquiries you may have if you would like to create this script yourself. Our support is able to assist with finding resources in our help guide as well as simple examples, and we are happy to assist with guiding you through the debugging process to assist you with understanding unexpected behavior.

                      You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like a list of affiliate consultants who would be happy to create this script or any others at your request or provide one on one educational services.​
                      JesseNinjaTrader Customer Service

                      Comment


                        #12
                        Ok thank you for the reply. I have searched around in the support forum for a code that will exit all positions using the chart.alerts indicator. Would you know of anything that would do that?

                        Comment


                          #13
                          Hello willwin,

                          I am not aware of a specific item, you can try looking on the public user app share to see if any users have uploaded something which accomplishes that task. https://ninjatraderecosystem.com/user-app-share/

                          You would otherwise need to create an item yourself or have something developed for you.

                          In general there is not a way to interact with chart alerts from NinjaScript, the code in the file you attached is not valid so fixing the curly braces would not really help to make that work. You would need to form a condition inside NinjaScript for whatever criteria you wanted. The addon framework can be used to submit orders directly to an account to exit positions.

                          If you have follow up questions on something you are making please make a new thread for those specific questions as we are getting off topic in this thread.
                          JesseNinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by kempotrader, Today, 08:56 AM
                          0 responses
                          4 views
                          0 likes
                          Last Post kempotrader  
                          Started by kempotrader, Today, 08:54 AM
                          0 responses
                          3 views
                          0 likes
                          Last Post kempotrader  
                          Started by mmenigma, Today, 08:54 AM
                          0 responses
                          1 view
                          0 likes
                          Last Post mmenigma  
                          Started by halgo_boulder, Today, 08:44 AM
                          0 responses
                          1 view
                          0 likes
                          Last Post halgo_boulder  
                          Started by drewski1980, Today, 08:24 AM
                          0 responses
                          3 views
                          0 likes
                          Last Post drewski1980  
                          Working...
                          X