NJ
Experience: Intermediate
Platform: NinjaTrader, tradestation
Trading: forex
Posts: 10 since Oct 2010
Thanks Given: 2
Thanks Received: 0
rleplae
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