NexusFi: Find Your Edge


Home Menu

 





Getting data out of NinjaTrader.SDF SQL Server Compact


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one rleplae with 5 posts (2 thanks)
    2. looks_two saurabh with 3 posts (0 thanks)
    3. looks_3 vantojo with 2 posts (0 thanks)
    4. looks_4 Quick Summary with 1 posts (0 thanks)
    1. trending_up 8,895 views
    2. thumb_up 2 thanks given
    3. group 4 followers
    1. forum 10 posts
    2. attach_file 0 attachments




 
Search this Thread

Getting data out of NinjaTrader.SDF SQL Server Compact

  #11 (permalink)
 saurabh 
NJ
 
Experience: Intermediate
Platform: NinjaTrader, tradestation
Trading: forex
Posts: 10 since Oct 2010
Thanks Given: 2
Thanks Received: 0


rleplae View Post
Hope this helps you getting started :

 
Code
   // update order status 
        // search for order based on NTorderId
        // - check limit
        // - check quantity
        // - (status is not updated) because we do this through the ATI interface
        public void synchroniseLimitQuantityPositions(int dbDebug, List<PositionRec> PositionTable)
        {
            int int_i;

            int quantity;
            decimal limitprice;
            decimal stopprice;
            decimal avgfillprice;
            int orderstate;

            try
            {
                // open NT DB
                string sdfFILE = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\Documents\\NinjaTrader 7\\db\\NinjaTrader.sdf; " + "File Mode=Read Only; SSCE:Temp File Directory=C:\\myTempDir\\;";
                // open the ninjatrader.sdf database 
                SqlCeConnection con = new SqlCeConnection("Data Source = " + sdfFILE);
                {
                    con.Open();

                    // order table holds the open orders (entry orders)
                    //
                    for (int_i = PositionTable.Count - 1; int_i >= 0; int_i--)
                    {
                        if (!PositionTable[int_i].isSimulation && PositionTable[int_i].NTtargetId != "")
                        {
                            // prepare SQL statement
                            string mySQLcommand = "SELECT quantity, limitprice, avgfillprice,  orderstate FROM nt_order WHERE name='Target1' AND orderid='" + PositionTable[int_i].NTtargetId + "'";

                            // Read in all values in the table.
                            using (SqlCeCommand com = new SqlCeCommand(mySQLcommand, con))
                            {
                                SqlCeDataReader reader = com.ExecuteReader();
                                while (reader.Read())
                                {
                                    quantity = reader.GetInt32(0);
                                    limitprice = (decimal)reader.GetDouble(1);
                                    avgfillprice = (decimal)reader.GetDouble(2);
                                    orderstate = reader.GetInt32(3);

                                    // order quantity changed -> update quantity
                                    if (quantity != PositionTable[int_i].Quantity)
                                    {
                                        if (dbDebug > 0 && PositionTable[int_i].Quantity !=0)
                                            Log(PositionTable[int_i].NTcode + " Qty was manualy updated in NT for order " + PositionTable[int_i].OrderSeqNr + " from : " + PositionTable[int_i].Quantity + " to : " + quantity);
                                        PositionTable[int_i].Quantity = quantity;
                                        PositionTable[int_i].Manual = true;
                                    }
                                    // order limit changed - > update order 
                                    if (limitprice != PositionTable[int_i].Target && orderstate == 9)
                                    {
                                        if (dbDebug > 0 && PositionTable[int_i].Target != 0)
                                            Log(PositionTable[int_i].NTcode + " Target limit was manualy updated in NT for order " + PositionTable[int_i].OrderSeqNr + " from : " + PositionTable[int_i].Target + " to : " + limitprice);
                                        PositionTable[int_i].Target = limitprice;
                                        PositionTable[int_i].Manual = true;
                                    }
                                    // order was executed, price is avgfillprice
                                    if (orderstate == 2)
                                    {
                                        if (dbDebug > 0)
                                            Log(PositionTable[int_i].NTcode + " Target got hit in NT for order " + PositionTable[int_i].OrderSeqNr + " at : " + avgfillprice);
                                        PositionTable[int_i].AvgTorS = "T";
                                        PositionTable[int_i].Avgfillprice = avgfillprice;
                                    }
                                }
                                reader.Close();
                            }
                        }
                        if (!PositionTable[int_i].isSimulation && PositionTable[int_i].NTstopId != "")
                        {
                            // prepare SQL statement
                            string mySQLcommand = "SELECT quantity, stopprice, avgfillprice, orderstate FROM nt_order WHERE name='Stop1' AND orderid='" + PositionTable[int_i].NTstopId + "'";

                            // Read in all values in the table.
                            using (SqlCeCommand com = new SqlCeCommand(mySQLcommand, con))
                            {
                                SqlCeDataReader reader = com.ExecuteReader();
                                while (reader.Read())
                                {
                                    quantity = reader.GetInt32(0);
                                    stopprice = (decimal)reader.GetDouble(1);
                                    avgfillprice = (decimal)reader.GetDouble(2);
                                    orderstate = reader.GetInt32(3);

                                    // order quantity changed -> update quantity
                                    if (quantity != PositionTable[int_i].Quantity)
                                    {
                                        if (dbDebug > 0  && PositionTable[int_i].Quantity != 0)
                                            Log(PositionTable[int_i].NTcode + " Qty was manualy updated in NT for order " + PositionTable[int_i].OrderSeqNr + " from : " + PositionTable[int_i].Quantity + " to : " + quantity);
                                        PositionTable[int_i].Quantity = quantity;
                                        PositionTable[int_i].Manual = true;
                                    }
                                    // order limit changed - > update order 
                                    // 
                                    if (stopprice != PositionTable[int_i].Stop && orderstate ==0)
                                    {
                                        if (dbDebug > 0 && PositionTable[int_i].Stop != 0)
                                            Log(PositionTable[int_i].NTcode + " Stop limit was manualy updated in NT for order " + PositionTable[int_i].OrderSeqNr + " from : " + PositionTable[int_i].Stop + " to : " + stopprice);
                                        PositionTable[int_i].Stop = stopprice;
                                        PositionTable[int_i].Manual = true;
                                    }
                                    if (orderstate == 2)
                                    {
                                        if (dbDebug > 0)
                                            Log(PositionTable[int_i].NTcode + " Stop got hit in NT for order " + PositionTable[int_i].OrderSeqNr + " at : " + avgfillprice);
                                        PositionTable[int_i].AvgTorS = "S";
                                        PositionTable[int_i].Avgfillprice = avgfillprice;
                                    }

                                }
                                reader.Close();
                            }
                        }
                    }
                    con.Close();
                }
            }
            catch
            {
                Log("Exception in : synchroniseLimitQuantityPositions ");
            }
        }


Thank you so much. It is very valuable for me and will help me unblock on my idea.
Again, that for taking out time and responding to my query

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
What broker to use for trading palladium futures
Commodities
REcommedations for programming help
Sierra Chart
Better Renko Gaps
The Elite Circle
Cheap historycal L1 data for stocks
Stocks and ETFs
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
 




Last Updated on September 4, 2017


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts