NexusFi: Find Your Edge


Home Menu

 





OnOrderUpdate event in Indicator?


Discussion in NinjaTrader

Updated
    1. trending_up 1,333 views
    2. thumb_up 1 thanks given
    3. group 2 followers
    1. forum 7 posts
    2. attach_file 0 attachments




 
 

OnOrderUpdate event in Indicator?

 
 vantojo 
Vilcabamba, Ecuador
 
Experience: Intermediate
Platform: Ninja
Trading: NQ, UB
Posts: 204 since Jul 2012

Ok, you gurus....

Is there anyway to have the function of the OnOrderUpdate event in an Indicator?

:-)

Started this thread

Can you help answer these questions
from other members on NexusFi?
Deepmoney LLM
Elite Quantitative GenAI/LLM
Futures True Range Report
The Elite Circle
Are there any eval firms that allow you to sink to your …
Traders Hideout
ZombieSqueeze
Platforms and Indicators
Exit Strategy
NinjaTrader
 
 
 
rleplae's Avatar
 rleplae 
Gits (Hooglede) Belgium
Legendary Market Wizard
 
Experience: Master
Platform: NinjaTrader, Proprietary,
Broker: Ninjabrokerage/IQfeed + Synthetic datafeed
Trading: 6A, 6B, 6C, 6E, 6J, 6S, ES, NQ, YM, AEX, CL, NG, ZB, ZN, ZC, ZS, GC
Posts: 3,003 since Sep 2013
Thanks Given: 2,442
Thanks Received: 5,863


Yes this is possible,
but you can not override the OnOrderUpdate

The way to do it is to the event itself, like something like below :

 
Code
 
protected override void Initialize()
{
}
		
protected override void OnStartUp()
{
       // you have to reinitialze if connection gets lost
	NinjaTrader.Cbi.Globals.Connections.ConnectionStatus += new ConnectionStatusEventHandler(OnConnection);
						
	account = NinjaTrader.Cbi.Globals.Accounts.FindByName(accountName);
			
	//return if cannot find the account
	if (account == null)
	{
	 this.DrawTextFixed("msg", "Could not retrieve the account", TextPosition.BottomRight);
	 return;
	}
	
        //does not works in market replay connection or simulated data feed
	if (account.Connection.Name == "Market Replay Connection" || account.Connection.Name == "Simulated Data Feed")
	{
	DrawTextFixed("msg", "Email Trades indicator does not works with " + account.Connection.Name, TextPosition.BottomRight);
	account = null;
	return;
	}
}
		
        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            
        }
		
		
	private void OnExecution(object sender, ExecutionUpdateEventArgs e)
	{
	if (e.Execution.Order != null)
	{
		if (e.Execution.Order.OrderState == OrderState.Filled)
		{
		this.SendMail(fromEmailAddress, toEmailAddress, "NinjaTrader Trade Confirmation", "Trade filled for: " + e.Execution.ToString());
		return;
	       }
				
		if (emailPartFilled && e.Execution.Order.OrderState == OrderState.PartFilled)
		{
		this.SendMail(fromEmailAddress, toEmailAddress, "NinjaTrader Trade Confirmation", "Trade filled for: " + e.Execution.ToString());
		}
	}
}
		
private void OnConnection(object sender, ConnectionStatusEventArgs e)
	{
		if (e.Status == ConnectionStatus.Connected)
		{
			Account a =  e.Connection.Accounts.FindByName(accountName);
			if (a != null)
			{
				account = NinjaTrader.Cbi.Globals.Accounts.FindByName(accountName);
					
				if (account != null)
				{
					if (account.Connection.Name == "Market Replay Connection" || account.Connection.Name == "Simulated Data Feed")
					return;
					
				        account.Execution += new ExecutionUpdateEventHandler(OnExecution);
					DrawTextFixed("msg", "Sending execution email for account: " + account.Name, TextPosition.BottomRight);
					Log("Subscribing to Email Trades indicator for account " + account.Name, LogLevel.Information);		
					return;
				}
			}
		}
		else if (e.Status == ConnectionStatus.ConnectionLost || e.Status == ConnectionStatus.Disconnected)
		{
			if (account != null && e.Connection.Accounts.Contains(account))
			{
				account.Execution -= new ExecutionUpdateEventHandler(OnExecution);
				account = null;
				Log("Unsubscribing to Email Trades indicator on " + e.Status.ToString(), LogLevel.Information);
			}
		}
	}
		
		
		
protected override void OnTermination()
{
		NinjaTrader.Cbi.Globals.Connections.ConnectionStatus -= new ConnectionStatusEventHandler(OnConnection);
			
		if (account != null)
		{
			if (!(account.Connection.Name == "Market Replay Connection" || account.Connection.Name == "Simulated Data Feed"))
			{
				account.Execution -= new ExecutionUpdateEventHandler(OnExecution);
					
				Log("Unsubscribing to Email Trades indicator for account " + account.Name, LogLevel.Information);
			}
		}
		account = null;
		
}

Follow me on Twitter Visit my NexusFi Trade Journal
 
 vantojo 
Vilcabamba, Ecuador
 
Experience: Intermediate
Platform: Ninja
Trading: NQ, UB
Posts: 204 since Jul 2012

Thanks, it looks like OnExecution can be used in an indicator. (but not overridden...?)

That will give me the first part...when an order is filled/partially filled.

Any easy way to know when the position has been closed?

IOrder.OrderState.Unknown?

--------------------------

also, would really like to test this in SIM or Replay.... no way to do that?

Started this thread
 
 
rleplae's Avatar
 rleplae 
Gits (Hooglede) Belgium
Legendary Market Wizard
 
Experience: Master
Platform: NinjaTrader, Proprietary,
Broker: Ninjabrokerage/IQfeed + Synthetic datafeed
Trading: 6A, 6B, 6C, 6E, 6J, 6S, ES, NQ, YM, AEX, CL, NG, ZB, ZN, ZC, ZS, GC
Posts: 3,003 since Sep 2013
Thanks Given: 2,442
Thanks Received: 5,863


vantojo View Post
Thanks, it looks like OnExecution can be used in an indicator. (but not overridden...?)

The example shows the mechanism to subscribe to an 'event' opposed to a method, that is only available
in a strategy. The result is the same, you just need to do a bit more handling.

You should be able to do replay and sim, just play with it and test it out


vantojo View Post

Any easy way to know when the position has been closed?

You should be able to walk through the structures and read that information.

 
Code
for (int i=0; i < NinjaTrader.Cbi.Globals.Accounts.Count; i++ ) {
    for (int j=0; j < NinjaTrader.Cbi.Globals.Accounts[i].Positions.Count; j++ ) {
        Print(" "
            +NinjaTrader.Cbi.Globals.Accounts[i].Name +" "
            +NinjaTrader.Cbi.Globals.Accounts[i].Positions[j].Instrument.FullName +" "
            +NinjaTrader.Cbi.Globals.Accounts[i].Positions[j].MarketPosition +" "
            +NinjaTrader.Cbi.Globals.Accounts[i].Positions[j].Quantity +" "
            +NinjaTrader.Cbi.Globals.Accounts[i].Positions[j].AvgPrice
        );
    }
}

Follow me on Twitter Visit my NexusFi Trade Journal
 
 vantojo 
Vilcabamba, Ecuador
 
Experience: Intermediate
Platform: Ninja
Trading: NQ, UB
Posts: 204 since Jul 2012

Hi,

If this is possible....

account.Execution += new ExecutionUpdateEventHandler(OnExecution);

would this also be possible?

account.OnOrderUpdate += new ExecutionUpdateEventHandler(OnOrderUpdate);

????

(C# is not my first language... )

Started this thread
 
 
rleplae's Avatar
 rleplae 
Gits (Hooglede) Belgium
Legendary Market Wizard
 
Experience: Master
Platform: NinjaTrader, Proprietary,
Broker: Ninjabrokerage/IQfeed + Synthetic datafeed
Trading: 6A, 6B, 6C, 6E, 6J, 6S, ES, NQ, YM, AEX, CL, NG, ZB, ZN, ZC, ZS, GC
Posts: 3,003 since Sep 2013
Thanks Given: 2,442
Thanks Received: 5,863

Give it a shot and try it...

Follow me on Twitter Visit my NexusFi Trade Journal
Thanked by:
 
 vantojo 
Vilcabamba, Ecuador
 
Experience: Intermediate
Platform: Ninja
Trading: NQ, UB
Posts: 204 since Jul 2012

many thanks!

Started this thread

 



Last Updated on July 10, 2016


© 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