I'm new to ninjatrader and new to ninja script
I am learning to automate a small strategy, which depends on pending orders
The idea is that I can close the platform, and the next day or later on the same day, turn it on and have control over those orders that I put before.
That is why I am checking the orders of the account and if they have the same "name", it is understood that they are of the same strategy and I want to work them.
The problem arises when I want to close one of those open orders (of the strategy with the same name) and that have not yet been executed. (an example: a sell short limit in "working" state)
I repeat, I am testing, and for this I have created an order with a particular name (which is useful for tracking in this example)
I have tried 2 functions, without success.
The first is as follows:
protected override void OnStateChange() { if (State == State.SetDefaults) { myAccount = Account.All.FirstOrDefault(a => a.Name == "Sim101"); } } protected override void OnBarUpdate () { if (State == State.Historical) return; if (this.CurrentBar> this.BarsRequiredToTrade) { if (done == false) { if (myAccount != null) { // EnterShortLimit (0, true, 1,3000, "1522557SP"); foreach (Order oo in myAccount.Orders) { int idx = oo.Name.IndexOf ("1522557SP"); if (idx >= 0 && oo.OrderState.ToString () == "Working") { myOrder = oo; [B] myAccount.Cancel (myOrder);[/B] Print ("the order had to be canceled"); } } } done = true; } } }
in this case,(above) it doesn't even allow me to compile:
The second case I try to use is this:
private Account myAccount; private bool done = false; private Order myOrder = null; protected override void OnBarUpdate () { if (State == State.Historical) return; if (this.CurrentBar> this.BarsRequiredToTrade) { if (done == false) { if (myAccount != null) { // EnterShortLimit (0, true, 1,3000, "1522557SP"); foreach (Order oo in myAccount.Orders) { int idx = oo.Name.IndexOf ("1522557SP"); if (idx >= 0 && oo.OrderState.ToString () == "Working") { myOrder = oo; [B] CancelOrder (myOrder);[/B] Print ("the order had to be canceled"); } } } done = true; } } } // //////////////// protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment) { Print("hhhhh"); } protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time) { Print("Kjkjkjk"); }
Here, I have no problem compiling, but when I put the strategy to work on the symbol, it goes through the line where I should close or cancel the order, but it does not.
It is evident in the print that it does.
But no cancel
also, do not call events
OnOrderUpdate or OnExecutionUpdate
How can I delete or cancel that order???
I take the opportunity to ask if there is a combination of keys that allows a self-indented code
Thanks for your help!!!
Note: edit the topic title, since I have already updated the question
Comment