Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to prevent Script from Running if ATM Strategy is null or empty

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

    How to prevent Script from Running if ATM Strategy is null or empty

    Hi,

    I have hard time to figure out where and which condition I need to put to prevent the script from throwing an error if the ATM strategy on the chart trader is none or custom


    Basically look into the forum to compile a script that will run a sell limit order taking the information of the account , the quantity and the atm strategy name on click ( Code will be below for reference)


    Now the problem is that I am unsure what is the if else condition i need to do to prevent the script from running or even running the script but without ATM if it's null

    I attempt this condition which is basically checking if not null and length of the string is greater than 0 than i can use the atm strategy if not i use a normal order without atm

    But then it throws an error when i do a sell limit with the ATM none or custom

    "Error on triggering custom event for NinjaScript 'ClickLimitOrderIndicatorExpanded' on bar 508: Could not find file 'myfolder\NinjaTrader 8\templates\AtmStrategy\.xml'."



    Code:
    if ( (ChartControl.OwnerChart.ChartTrader.AtmStrategy.Template) != null || (ChartControl.OwnerChart.ChartTrader.AtmStrategy.Template.ToString().Length) > 0 ) {
    
    entryOrder = myAccount.CreateOrder(Instrument, OrderAction.Sell, OrderType.Limit,
    TimeInForce.Day, quantitySelector.Value, priceClicked, 0, string.Empty, "Entry", null);
    
    // Submits our entry order with the ATM strategy named "myAtmStrategyName"
    NinjaTrader.NinjaScript.AtmStrategy.StartAtmStrate gy(ChartControl.OwnerChart.ChartTrader.AtmStrategy .Template.ToString(), entryOrder);
    
    }else {
    
    limitOrder = myAccount.CreateOrder(Instrument, OrderAction.Sell, OrderType.Limit, OrderEntry.Manual, TimeInForce.Day, quantitySelector.Value, priceClicked, 0, "", "limitOrder"+DateTime.Now.ToString(), DateTime.MaxValue, null);
    
    }


    Thanks in advance



    Code that do an entry on ctrl + Click in case

    Code:
    private Account myAccount;
    private ChartScale MyChartScale;
    private Order entryOrder;
    
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Indicator here.";
    Name = "ClickLimitOrderIndicatorExpanded";
    Calculate = Calculate.OnPriceChange;
    IsOverlay = true;
    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;
    AccountName = "Sim101";
    }
    else if (State == State.DataLoaded)
    {
    if (ChartControl != null)
    ChartControl.MouseLeftButtonDown += LeftMouseDown;
    
    if (ChartControl != null)
    ChartPanel.MouseMove += ChartControl_MouseMove;
    
    // Find our account
    lock (Account.All)
    myAccount = Account.All.FirstOrDefault(a => a.Name == AccountName);
    
    }
    else if (State == State.Terminated)
    {
    if (ChartControl != null)
    ChartControl.MouseLeftButtonDown -= LeftMouseDown;
    
    if (ChartControl != null)
    ChartPanel.MouseMove -= ChartControl_MouseMove;
    
    }
    }
    
    protected override void OnBarUpdate()
    {
    
    
    }
    
    
    protected void LeftMouseDown(object sender, MouseButtonEventArgs e)
    {
    
    
    NinjaTrader.Gui.Tools.QuantityUpDown quantitySelector = (Window.GetWindow(ChartControl.Parent).FindFirst(" ChartTraderControlQuantitySelector") as NinjaTrader.Gui.Tools.QuantityUpDown);
    Print("Quantity: "+quantitySelector.Value);
    
    NinjaTrader.Gui.Tools.AccountSelector xAlselector = Window.GetWindow(ChartControl.Parent).FindFirst("C hartTraderControlAccountSelector") as NinjaTrader.Gui.Tools.AccountSelector;
    Print("Which Account Name"+ xAlselector.SelectedAccount.ToString());
    
    lock (Account.All)
    myAccount = Account.All.FirstOrDefault(a => a.Name == xAlselector.SelectedAccount.ToString());
    
    if ( (ChartControl.OwnerChart.ChartTrader.AtmStrategy.T emplate) != null) {
    Print("ATMStrategyName is : "+ ChartControl.OwnerChart.ChartTrader.AtmStrategy.Te mplate);
    }
    
    Print(ChartControl.OwnerChart.ChartTrader.AtmStrat egy.Template.ToString().Length);
    
    
    TriggerCustomEvent(o =>
    {
    
    
    
    int Y = ChartingExtensions.ConvertToVerticalPixels(e.GetPo sition(ChartControl as IInputElement).Y, ChartControl.PresentationSource);
    
    double priceClicked = MyChartScale.GetValueByY(Y);
    
    Order limitOrder = null;
    Order stopOrder = null;
    
    if (priceClicked > Close[0] && Keyboard.IsKeyDown(Key.LeftCtrl))
    {
    
    
    // stopOrder = myAccount.CreateOrder(Instrument, OrderAction.Buy, OrderType.StopMarket, OrderEntry.Manual, TimeInForce.Day, 1, 0, priceClicked, "", "stopOrder"+DateTime.Now.ToString(), DateTime.MaxValue, null);
    
    if ( (ChartControl.OwnerChart.ChartTrader.AtmStrategy.T emplate) != null || (ChartControl.OwnerChart.ChartTrader.AtmStrategy.T emplate.ToString().Length) > 0 ) {
    
    entryOrder = myAccount.CreateOrder(Instrument, OrderAction.Sell, OrderType.Limit,
    TimeInForce.Day, quantitySelector.Value, priceClicked, 0, string.Empty, "Entry", null);
    
    // Submits our entry order with the ATM strategy named "myAtmStrategyName"
    NinjaTrader.NinjaScript.AtmStrategy.StartAtmStrate gy(ChartControl.OwnerChart.ChartTrader.AtmStrategy .Template.ToString(), entryOrder);
    
    }else {
    
    limitOrder = myAccount.CreateOrder(Instrument, OrderAction.Sell, OrderType.Limit, OrderEntry.Manual, TimeInForce.Day, quantitySelector.Value, priceClicked, 0, "", "limitOrder"+DateTime.Now.ToString(), DateTime.MaxValue, null);
    
    }
    
    
    }
    else if (priceClicked < Close[0] && Keyboard.IsKeyDown(Key.LeftCtrl))
    {
    
    
    limitOrder = myAccount.CreateOrder(Instrument, OrderAction.Buy, OrderType.Limit, OrderEntry.Manual, TimeInForce.Day, quantitySelector.Value , priceClicked, 0, "", "limitOrder"+DateTime.Now.ToString(), DateTime.MaxValue, null);
    // stopOrder = myAccount.CreateOrder(Instrument, OrderAction.Sell, OrderType.StopMarket, OrderEntry.Manual, TimeInForce.Day, 1, 0, priceClicked, "", "stopOrder"+DateTime.Now.ToString(), DateTime.MaxValue, null);
    
    }
    
    myAccount.Submit(new[] { limitOrder, stopOrder });
    }, null);
    e.Handled = true;
    }
    
    void ChartControl_MouseMove (object sender, System.Windows.Input.MouseEventArgs e)
    {
    
    }
    
    protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
    {
    base.OnRender(chartControl, chartScale);
    
    MyChartScale = chartScale;
    }
    
    [TypeConverter(typeof(NinjaTrader.NinjaScript.Accou ntNameConverter))]
    public string AccountName { get; set; }
    }
    }

    #2
    Oh actually after several test I think I found a solution but not sure if it makes sense but it works

    I created the variable on the barupdate with a Async

    Code:
    protected override void OnBarUpdate()
    {
    
    
    ChartControl.Dispatcher.InvokeAsync((Action)(() =>
    {
    test = ChartControl.OwnerChart.ChartTrader.AtmStrategy.Te mplate.ToString();
    }));
    
    }
    Then check if the string is length is greater than 0

    Code:
    if ( (test.Length) > 0 ) {
    Print("ATMStrategyName is : "+ test);
    // Print(ChartControl.OwnerChart.ChartTrader.AtmStrat egy.Template.ToString().Length);
    }
    I guess it does the job

    Maybe another question is how to differentiate the "None" ATM from the "Custom" as both return an empty string

    Comment


      #3
      Hello papaoutai,

      To get the name of the selected template you would need to access the AtmStrategySelector and the SelectedAtmStrategy. Here is a quick example. also yes you do need to use dispatchers when accessing UI controls in most use cases.


      Code:
      if(ChartControl != null)
      {
          ChartControl.Dispatcher.InvokeAsync((Action)(() =>
         {
            ChartTrader chartTrader = Window.GetWindow(ChartControl.Parent).FindFirst("ChartWindowChartTraderControl") as ChartTrader;
      
      
            NinjaTrader.Gui.NinjaScript.AtmStrategy.AtmStrategySelector selector = chartTrader.FindFirst("ChartTraderControlATMStrategySelector") as NinjaTrader.Gui.NinjaScript.AtmStrategy.AtmStrategySelector;
      
            string test = selector.SelectedAtmStrategy.DisplayName.ToString( );
            Print(test);
      
         }));
      }
      I look forward to being of further assistance.

      Comment


        #4
        Thank you Jesse,

        Indeed with the atmstrategyselector. I can now get the "custom" but it generate another problem which is if I use the custom Click image for larger version

Name:	custom.PNG
Views:	312
Size:	39.2 KB
ID:	1154762


        and then apply it to call that ATM it will throw an error saying that the xml file doesn't exist

        Documents\NinjaTrader 8\templates\AtmStrategy\Custom.xml'.

        Which I did check and indeed it's not there though my other strategy are. I wonder if there is a way to use the custom one that is not saved as a template.
        It kind of make sense that it doesn't work as I am calling an atm strategy of a template that is not saved as "template" but still I wonder how we could call that custom that can be configured but not saved
        Code:
        NinjaTrader.NinjaScript.AtmStrategy.StartAtmStrategy(test, entryOrder);

        Comment


          #5
          Hello papaoutai,

          Right, Custom won't be a template. You should jut pass the selected strategy into the method instead of trying to work with the names.


          Code:
          NinjaTrader.NinjaScript.AtmStrategy.StartAtmStrategy(selector.SelectedAtmStrategy, entryOrder);
          I look forward to being of further assistance.

          Comment


            #6
            Thanks Jesse you just saved me hours . That is exactly the answer I needed to unblock me

            Have a nice day

            Comment


              #7
              Originally posted by papaoutai View Post
              Thanks Jesse you just saved me hours . That is exactly the answer I needed to unblock me

              Have a nice day
              Hi papaoutai,

              I was in a different thread that points to this thread and which seems to be relevant to my problematic issue. So, could you please share/post at least your full "Code that do an entry on ctrl + Click ...".

              omololu

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              637 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              366 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              107 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              569 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              572 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X