Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

error indicator and freeze NJ8

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

    error indicator and freeze NJ8

    hi i try to script a indicator , i have created this code
    HTML 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;
    using MySql.Data;
    using MySql.Data.MySqlClient;
    
    #endregion
    
    //This namespace holds Indicators in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Indicators
    {
    public class MysqlInsert : Indicator
    {
    private int nData = 0;
    private double bidPrice;
    private double askPrice;
    private long askVolume;
    private long bidVolume;
    private string LastTot;
    
    //private MySqlConnection myConnection ;
    //private string myConnectionString = "Server=127.0.0.1;User ID=root;Password=kronos123;Database=cfd;Port= 3306;Pooling=false";
    //private MySqlCommand myCommand;
    //private string mySelectQuery;
    
    MySqlConnection myConnection = new MySqlConnection("Server=127.0.0.1;Database=cfd;Dat a Source=6E;User Id=root;Password=kronos123");
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Indicator here.";
    Name = "MysqlInsert";
    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)
    {
    }
    }
    
    protected override void OnBarUpdate()
    {
    //Add your custom indicator logic here.
    }
    
    protected override void OnMarketData(MarketDataEventArgs marketDataUpdate )
    {
    
    nData++;
    string dataCSV = string.Empty;
    
    if (marketDataUpdate.MarketDataType == MarketDataType.Ask)
    {
    //Print("ASK PRICE -- "+e.Price);
    askPrice = marketDataUpdate.Price;
    askVolume = marketDataUpdate.Volume;
    }
    if (marketDataUpdate.MarketDataType == MarketDataType.Bid)
    {
    //Print("BID PRICE -- "+e.Price);
    bidPrice = marketDataUpdate.Price;
    bidVolume = marketDataUpdate.Volume;
    }
    if (marketDataUpdate.MarketDataType == MarketDataType.Last)
    {
    LastTot=string.Format("{0:HH:mm:ss:fffff} | last: {1}, ask: {2} bid: {3} volume: {4} |", marketDataUpdate.Time, marketDataUpdate.Price, marketDataUpdate.Ask, marketDataUpdate.Bid,marketDataUpdate.Volume);
    Print (LastTot);
    }
    try
    {
    //byte[] data = Encoding.Unicode.GetBytes(LastTot); //quasi ok
    //MemoryMappedViewAccessor accessor = file.CreateViewAccessor();
    //accessor.Write(0, (char)data.Length);
    //accessor.WriteArray(0, data, 0, data.Length);
    string myInsertQuery = String.Format("INSERT INTO 6E (TickTime, PriceAskTick, PriceBidTick, RealVolume) VALUES({0}, {1}, {2}, {3})", marketDataUpdate.Time, marketDataUpdate.Ask, marketDataUpdate.Bid, marketDataUpdate.Volume);
    //Print(myInsertQuery);
    MySqlCommand myCommand = new MySqlCommand(myInsertQuery);
    myCommand.Connection = myConnection;
    myConnection.Open();
    myCommand.ExecuteNonQuery();
    myCommand.Connection.Close();
    }
    catch
    {
    Print("Data file busy...");
    }
    
    
    
    
    
    }
    
    
    
    }
    }
    
    #region NinjaScript generated code. Neither change nor remove.
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
    public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
    {
    private MysqlInsert[] cacheMysqlInsert;
    public MysqlInsert MysqlInsert()
    {
    return MysqlInsert(Input);
    }
    
    public MysqlInsert MysqlInsert(ISeries<double> input)
    {
    if (cacheMysqlInsert != null)
    for (int idx = 0; idx < cacheMysqlInsert.Length; idx++)
    if (cacheMysqlInsert[idx] != null && cacheMysqlInsert[idx].EqualsInput(input))
    return cacheMysqlInsert[idx];
    return CacheIndicator<MysqlInsert>(new MysqlInsert(), input, ref cacheMysqlInsert);
    }
    }
    }
    
    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
    public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
    {
    public Indicators.MysqlInsert MysqlInsert()
    {
    return indicator.MysqlInsert(Input);
    }
    
    public Indicators.MysqlInsert MysqlInsert(ISeries<double> input )
    {
    return indicator.MysqlInsert(input);
    }
    }
    }
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
    {
    public Indicators.MysqlInsert MysqlInsert()
    {
    return indicator.MysqlInsert(Input);
    }
    
    public Indicators.MysqlInsert MysqlInsert(ISeries<double> input )
    {
    return indicator.MysqlInsert(input);
    }
    }
    }
    
    #endregion
    but when i run it return me this 2 error









    Attached Files

    #2
    Hello faustf, thanks for writing in.

    The indicator is using an SQL package and the issue is likely coming from that. The error is that you are accessing an object that has not yet been initialized. I assume the SQL code is not working or not initialized properly. Use Visual Studio debugging to attach to the NinjaTrader process and see what line is causing the unhandled exception:
    https://ninjatrader.com/support/help..._debugging.htm

    If the issue is coming from the use of this SQL library, I would recommend first testing out that package within a stand alone C# application and once you find how that works exactly, try implementing it within an indicator. Unfortunately, I will not be able to look further into that package because it goes outside of the scope of support I can provide.

    We have a full example on reading/writing txt files here for an alternative approach:
    https://ninjatrader.com/support/help..._propertie.htm

    Kind regards,
    -ChrisL

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    571 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    331 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    101 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    549 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    550 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X