NexusFi: Find Your Edge


Home Menu

 





NinjaScript - Intercepting order submission


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one askerix with 4 posts (8 thanks)
    2. looks_two Adfra with 4 posts (2 thanks)
    3. looks_3 Jasonnator with 3 posts (4 thanks)
    4. looks_4 planetkill with 2 posts (6 thanks)
      Best Posters
    1. looks_one planetkill with 3 thanks per post
    2. looks_two askerix with 2 thanks per post
    3. looks_3 gomi with 2 thanks per post
    4. looks_4 Jasonnator with 1.3 thanks per post
    1. trending_up 4,808 views
    2. thumb_up 22 thanks given
    3. group 9 followers
    1. forum 15 posts
    2. attach_file 1 attachments




 
Search this Thread

NinjaScript - Intercepting order submission

  #11 (permalink)
 
Jasonnator's Avatar
 Jasonnator 
Denver, Colorado United States
 
Experience: Intermediate
Platform: NT8 + Custom
Broker: NT Brokerage, Kinetick, IQFeed, Interactive Brokers
Trading: ES
Posts: 159 since Dec 2014
Thanks Given: 40
Thanks Received: 166


Adfra View Post
Hi there,
Looking for some pointers or a code snippet that would allow me to intercept (and cancel!) a users order submission. So to be precise: I'd like to:

a) intercepts the button clicks on the chart trader (buy market, sell market etc.)
b) evaluate certain conditions and
c) based on the result cancel the submission of the order under certain circumstances

Frank

My first thought would be to simply do your logic in the OnOrderUpdate. That would be, in my opinion, the best bulletproof way to catch any inner monkey order and cancel it immediately. You could then either log a message in the control center or even pop up a dialog to let you know you made an erroneous move.

Gotchas: This would require you to use a strategy and would work only on the chart you applied it to but I think it'd be foolproof.

You'd be able to check logic against any indicator value without intervention. It would definitely take some work to get it right but you'd only have to do it once then you'd have your desired protection.

Reply With Quote
Thanked by:

Can you help answer these questions
from other members on NexusFi?
Cheap historycal L1 data for stocks
Stocks and ETFs
ZombieSqueeze
Platforms and Indicators
Better Renko Gaps
The Elite Circle
REcommedations for programming help
Sierra Chart
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
 
  #12 (permalink)
 Adfra 
Melbourne
 
Experience: Intermediate
Platform: NinjaTrader
Broker: MB Trading, Kinetick
Trading: Forex, AUD/USD
Posts: 19 since Aug 2011
Thanks Given: 11
Thanks Received: 9


Jasonnator View Post
My first thought would be to simply do your logic in the OnOrderUpdate. That would be, in my opinion, the best bulletproof way to catch any inner monkey order and cancel it immediately.

Yes I was looking at OnOrderUpdate for that reason, but still couldn't work out how to then cancel the incoming order. Do you have a code snippet that shows this?
I was thinking (sudo code):

OnOrderUpdate(order){
if(order.orderstatus == OrderStatus.Initialised){
if(!isAcceptableTradingContext())
order.orderstatus = OrderStatus.Cancelled;
}
}

...but not sure that's how this will work.

Started this thread Reply With Quote
  #13 (permalink)
 Adfra 
Melbourne
 
Experience: Intermediate
Platform: NinjaTrader
Broker: MB Trading, Kinetick
Trading: Forex, AUD/USD
Posts: 19 since Aug 2011
Thanks Given: 11
Thanks Received: 9



userque View Post
If this is the case, can a new function be to close the newly opened position (or cancel the newly placed order)?

Why would such a person click the button that might block what they want to do;

I trade from Australia, so trading the S&P500 is 11:30pm to 2:00am here, which as has the expected bad side effects you can imagine. It's really about putting a safety net in place to counteract being tired etc. and making mistakes thereof. So I view this as a safety feature rather than a restriction and am confident just implementing this as a strategy would do the job.


askerix View Post
...finding a solution to hide the original charttrader entry-buttons...

Thanks Asterik, this is something that I would be keen todo, just to avoid the habitual mis-click on the standard buttons. Do you have a way on how this could be achieved. Doesn't need to be pretty, could be hiding the buttons or putting another GUI element in front of the buttons or similar.

Started this thread Reply With Quote
Thanked by:
  #14 (permalink)
 
Jasonnator's Avatar
 Jasonnator 
Denver, Colorado United States
 
Experience: Intermediate
Platform: NT8 + Custom
Broker: NT Brokerage, Kinetick, IQFeed, Interactive Brokers
Trading: ES
Posts: 159 since Dec 2014
Thanks Given: 40
Thanks Received: 166

That is almost exactly what I had in mind. Make sure to look at the NT8 documentation about order states because there are some gotchas in there from what I remember. I don't memorize them but always remember to check when I'm doing something based on order states. I never store my order object until I'm in that override versus when you do your buy or sell. It's possible the buy or sell method return order object won't be as populated as the order parameter you get from the override method.

Any resting order where you go from historical to realtime, you need to convert that order to a realtime order in the OnStateChanged state transition. You'll get an error in the control center (or maybe a pop-up). It has to do with how NT8 tracks orders internally.

You will probably need a local bool flag like isCancelInProgress that you first check for and then set to true so you don't end up in a loop as well as you let legitimate orders through. Also, I would store the order object from that override and check in OnExecution against any executed order just in case. That way you could immediately close it and only be out the spread plus maybe a tick.


Adfra View Post
Yes I was looking at OnOrderUpdate for that reason, but still couldn't work out how to then cancel the incoming order. Do you have a code snippet that shows this?
I was thinking (sudo code):

OnOrderUpdate(order){
if(order.orderstatus == OrderStatus.Initialised){
if(!isAcceptableTradingContext())
order.orderstatus = OrderStatus.Cancelled;
}
}

...but not sure that's how this will work.


Reply With Quote
Thanked by:
  #15 (permalink)
 askerix 
Zurich Switzerland
 
Experience: None
Platform: NT
Trading: Ukulele
Frequency: Never
Duration: Never
Posts: 60 since Mar 2011
Thanks Given: 554
Thanks Received: 62


Adfra View Post
Thanks Asterik, this is something that I would be keen todo, just to avoid the habitual mis-click on the standard buttons. Do you have a way on how this could be achieved. Doesn't need to be pretty, could be hiding the buttons or putting another GUI element in front of the buttons or similar.


 
Code
private System.Windows.Controls.Button	buttonBuyMarket;
private Gui.Chart.Chart				chartWindow;
private System.Windows.Controls.Grid			chartTraderGrid, chartTraderButtonsGrid;

protected void HideOrgButton()
{
chartWindow = Window.GetWindow(ChartControl.Parent) as Gui.Chart.Chart; // if not added to a chart, do nothing if (chartWindow == null) return; // this is the entire chart trader area grid chartTraderControl = chartWindow.FindFirst("ChartWindowChartTraderControl") as Gui.Chart.ChartTrader; chartTraderGrid = chartTraderControl.Content as System.Windows.Controls.Grid; // this grid contains the existing chart trader buttons chartTraderButtonsGrid = chartTraderGrid.Children[0] as System.Windows.Controls.Grid; buttonBuyMarket = chartTraderButtonsGrid.FindFirst("ChartTraderControlQuickBuyMarketButton") as System.Windows.Controls.Button; // option 1 //buttonBuyMarket.IsEnabled = false; // option 2 buttonBuyMarket.Visibility = Visibility.Hidden;
}
Option 1) disables the button
Option 2) hides the button

keep in mind to remove the change in the termination state of your indicator
 
Code
buttonBuyMarket.IsEnabled = true;
buttonBuyMarket.Visibility = Visibility.Visible;
this is the example for the upper left "Buy Mkt" button, the AutomationIDs of the other 5 buttons are:
  • ChartTraderControlQuickSellMarketButton
  • ChartTraderControlQuickBuyAskButton
  • ChartTraderControlQuickSellAskButton
  • ChartTraderControlQuickBuyBidButton
  • ChartTraderControlQuickSellBidButton

cheers,
askerix

Reply With Quote
  #16 (permalink)
 
Jasonnator's Avatar
 Jasonnator 
Denver, Colorado United States
 
Experience: Intermediate
Platform: NT8 + Custom
Broker: NT Brokerage, Kinetick, IQFeed, Interactive Brokers
Trading: ES
Posts: 159 since Dec 2014
Thanks Given: 40
Thanks Received: 166

This is a much simpler (aka better) solution!


askerix View Post
 
Code
private System.Windows.Controls.Button	buttonBuyMarket;
private Gui.Chart.Chart				chartWindow;
private System.Windows.Controls.Grid			chartTraderGrid, chartTraderButtonsGrid;

protected void HideOrgButton()
{
chartWindow = Window.GetWindow(ChartControl.Parent) as Gui.Chart.Chart; // if not added to a chart, do nothing if (chartWindow == null) return; // this is the entire chart trader area grid chartTraderControl = chartWindow.FindFirst("ChartWindowChartTraderControl") as Gui.Chart.ChartTrader; chartTraderGrid = chartTraderControl.Content as System.Windows.Controls.Grid; // this grid contains the existing chart trader buttons chartTraderButtonsGrid = chartTraderGrid.Children[0] as System.Windows.Controls.Grid; buttonBuyMarket = chartTraderButtonsGrid.FindFirst("ChartTraderControlQuickBuyMarketButton") as System.Windows.Controls.Button; // option 1 //buttonBuyMarket.IsEnabled = false; // option 2 buttonBuyMarket.Visibility = Visibility.Hidden;
}
Option 1) disables the button
Option 2) hides the button

keep in mind to remove the change in the termination state of your indicator
 
Code
buttonBuyMarket.IsEnabled = true;
buttonBuyMarket.Visibility = Visibility.Visible;
this is the example for the upper left "Buy Mkt" button, the AutomationIDs of the other 5 buttons are:
  • ChartTraderControlQuickSellMarketButton
  • ChartTraderControlQuickBuyAskButton
  • ChartTraderControlQuickSellAskButton
  • ChartTraderControlQuickBuyBidButton
  • ChartTraderControlQuickSellBidButton

cheers,
askerix


Reply With Quote
Thanked by:




Last Updated on September 6, 2020


© 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