Go Back   NinjaTrader Support Forum > NinjaTrader 8 > Strategy Development

Strategy Development Support for the development of custom automated trading strategies using NinjaScript.

NinjaTrader
Reply
 
Thread Tools Display Modes
Old 03-08-2018, 03:33 PM   #1
ballboy11
Senior Member
 
Join Date: Jan 2016
Posts: 374
Thanks: 0
Thanked 10 times in 10 posts
Default getting proper OrderID

I have a strategy that will place orders, but there is a scenario that I have that if a user has an order in play and then starts the strategy it will take the users orderID as the first transaction. When flat then the strategy will take over automatically placing orders

How do I get the orderID that the user placed so I can close that order in the strategy if needed?
ballboy11 is offline  
Reply With Quote
Old 03-08-2018, 03:40 PM   #2
NinjaTrader_PatrickH
NinjaTrader Customer Service
 
NinjaTrader_PatrickH's Avatar
 
Join Date: Jul 2011
Location: Denver, CO
Posts: 11,646
Thanks: 783
Thanked 2,012 times in 1,938 posts
Default

Hello ballboy11,

Thank you for your post.

You can use the Account class to pull the Orders collection. Please visit the following link for more information: https://ninjatrader.com/support/help...ount_class.htm

Please let me know if you have any questions.
NinjaTrader_PatrickH is offline  
Reply With Quote
Old 03-08-2018, 05:52 PM   #3
ballboy11
Senior Member
 
Join Date: Jan 2016
Posts: 374
Thanks: 0
Thanked 10 times in 10 posts
Default

I am close I have my auto entries working perfectly but I don't know how to close my manual entry this is what I have. This scenario is if the user placed an order THEN turned on the strategy.


if(bManualOrder==true)
{
Print("Manual order is true");
ExitLong();
ExitShort();
}

i know in my logic i can get to my manual order but I don't know how to close I am manually using an atmstrategy called AtmStrategyTemplate.

Lastly, it is a very simple strategy it will only have 1 contract. I just added exitlong and exit short because i wasn't sure how to close.
Last edited by ballboy11; 03-08-2018 at 06:54 PM.
ballboy11 is offline  
Reply With Quote
Old 03-09-2018, 07:38 AM   #4
ballboy11
Senior Member
 
Join Date: Jan 2016
Posts: 374
Thanks: 0
Thanked 10 times in 10 posts
Default how to find an active order in a strategy that the strategy did not place

what I want is have a strategy that the user can place the order then start the strategy.

My scenario would be this.

Stopped out
Profit target
Strategy closes order.


after that scenario is finished the strategy will place orders.

condition is managed. i can get the strategy to place, cancel and close orders but I can not find the manual order to close.
ballboy11 is offline  
Reply With Quote
Old 03-09-2018, 07:45 AM   #5
NinjaTrader_PatrickH
NinjaTrader Customer Service
 
NinjaTrader_PatrickH's Avatar
 
Join Date: Jul 2011
Location: Denver, CO
Posts: 11,646
Thanks: 783
Thanked 2,012 times in 1,938 posts
Default

Hello ballboy11,

Thank you for your response.

If you utilize the logic to pull the account orders for the instrument the strategy is running on you would then need to use the Account functions for CreateOrder() and Submit(). The managed approach functions for strategies (such as ExitLong() and ExitShort()) will not work to close the account position as they focus on the Strategy position. Please visit the following link for information on account versus strategy position: https://ninjatrader.com/support/help..._account_p.htm

CreateOrder(): https://ninjatrader.com/support/help...reateorder.htm
Submit(): https://ninjatrader.com/support/help...-us/submit.htm

Please let me know if you have any questions.
NinjaTrader_PatrickH is offline  
Reply With Quote
Old 03-09-2018, 07:52 AM   #6
ballboy11
Senior Member
 
Join Date: Jan 2016
Posts: 374
Thanks: 0
Thanked 10 times in 10 posts
Default

if the order was created BEFORE the strategy started will create order work?
ballboy11 is offline  
Reply With Quote
Old 03-09-2018, 10:27 AM   #7
NinjaTrader_PatrickH
NinjaTrader Customer Service
 
NinjaTrader_PatrickH's Avatar
 
Join Date: Jul 2011
Location: Denver, CO
Posts: 11,646
Thanks: 783
Thanked 2,012 times in 1,938 posts
Default

Quote:
Originally Posted by ballboy11 View Post
if the order was created BEFORE the strategy started will create order work?
Hello ballboy11,

Thank you for your response.

The Account Orders collection needs to be used to capture the order if you wish to manage it. If you need to manage a position then you would use Create and Submit.

Please let me know if you have any questions.
NinjaTrader_PatrickH is offline  
Reply With Quote
Old 03-09-2018, 12:08 PM   #8
ballboy11
Senior Member
 
Join Date: Jan 2016
Posts: 374
Thanks: 0
Thanked 10 times in 10 posts
Default

i am not sure if i am in the the right area for closing an order


Print("I am in the account loop");

DateTime tCheckTime = new DateTime(2018, 01, 01);
foreach (Order order in Position.Account.Orders)
{
// Print(String.Format("Order placed: {0} - {1}", order.Name, order.OrderAction));

if (order.OrderState == OrderState.Filled)
{
if (tCheckTime < order.Time)
{
lManualOrderID = order.Id;
tCheckTime = order.Time;

}

Print(order.Name);
}
if(order.OrderState == OrderState.Working)
{
lManualTargetID = order.Id;
}

if(order.OrderState == OrderState.Accepted)
{
lManualStop1ID = order.Id;
}


// }
Order stopOrder;
stopOrder = Account.CreateOrder(myInstrument, OrderAction.Sell, OrderType.StopMarket, OrderEntry.Manual, TimeInForce.Day, 1, 0, 1400, "", "stopOrder", Core.Globals.MaxDate, null);

Account.Submit(new[] { stopOrder });


myAccount.Submit(new[] { stopOrder });
ballboy11 is offline  
Reply With Quote
Old 03-13-2018, 09:21 AM   #9
NinjaTrader_PatrickH
NinjaTrader Customer Service
 
NinjaTrader_PatrickH's Avatar
 
Join Date: Jul 2011
Location: Denver, CO
Posts: 11,646
Thanks: 783
Thanked 2,012 times in 1,938 posts
Default

Hello ballboy11,

Thank you for your patience.

To cancel an order on the account for the instrument that the strategy is running on you can use the following:
Code:
		private Account acc;
		
		protected override void OnStateChange()
		{
			if (State == State.SetDefaults)
			{
				Description									= @"Enter the description for your new custom Indicator here.";
				Name										= "TestGrabAccountOrder";
			}
			else if (State == State.Configure)
			{
				lock (Account.All)
					acc = Account.All.FirstOrDefault(a => a.Name == "Sim101");
			}
			else if (State == State.Realtime)
			{
				if (acc.Orders.Count != 0)
				{
					acc.CancelAllOrders(Instrument);
				}
			}
		}
My example cancels the orders when the script begins processing Realtime data, however you can change the place that you call the CancelAllOrders in your script.

Please let me know if you have any questions.
NinjaTrader_PatrickH is offline  
Reply With Quote
Old 03-13-2018, 10:15 AM   #10
ballboy11
Senior Member
 
Join Date: Jan 2016
Posts: 374
Thanks: 0
Thanked 10 times in 10 posts
Default

thanks i will give it a try, the statement

acc = Account.All.FirstOrDefault(a => a.Name == "Sim101");

the search criteria is Sim101, how would I get the other account?
or do I just do

acc = Account.All.FirstOrDefault(a => a.Name != "Sim101");
ballboy11 is offline  
Reply With Quote
Old 03-13-2018, 12:38 PM   #11
NinjaTrader_PatrickH
NinjaTrader Customer Service
 
NinjaTrader_PatrickH's Avatar
 
Join Date: Jul 2011
Location: Denver, CO
Posts: 11,646
Thanks: 783
Thanked 2,012 times in 1,938 posts
Default

Hello ballboy11,

Thank you for your response.

Within the quotations you would put the name of the account. So "Sim101" becomes "YourLiveAccountName".

Please let me know if you have any questions.
NinjaTrader_PatrickH is offline  
Reply With Quote
Old 03-13-2018, 04:29 PM   #12
ballboy11
Senior Member
 
Join Date: Jan 2016
Posts: 374
Thanks: 0
Thanked 10 times in 10 posts
Default

Thanks for the cancel all orders it worked great but the last thing to do is close the active order this is what is showing. The targets and the stops close out but the active order is still running.
Attached Images
File Type: png Capture.PNG (6.7 KB, 4 views)
ballboy11 is offline  
Reply With Quote
Old 03-13-2018, 06:28 PM   #13
ballboy11
Senior Member
 
Join Date: Jan 2016
Posts: 374
Thanks: 0
Thanked 10 times in 10 posts
Default

I figured how to close my position. In a strategy if a manual strategy is entered and then the strategy starts MarketPosition will display flat eventhough there is a position activated.

I can close the targets and stops AND I can close the postion. by CreateOrder AND Submit.

There is a scenario that I do have an issue with. If a target is hit OR a stop is hit AND at the time I close the order with CreateOrder it will of course make one. What I need to do is NOT Create the order. If MarketPosition is flat in a strategy because it can not find the Manual order what trigger do I use in order to see if the target was hit. i tried these triggers.

protected override void OnAccountItemUpdate(Cbi.Account account, Cbi.AccountItem accountItem, double value)
{
Print(" in OnAccountItemUpdate");
}

protected override void OnConnectionStatusUpdate(ConnectionStatusEventArgs connectionStatusUpdate)
{

}

protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity,
Cbi.MarketPosition marketPosition, string orderId, DateTime time)
{
Print("in onExecutionUpdate");
}



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("In Order update");
}

protected override void OnPositionUpdate(Cbi.Position position, double averagePrice,
int quantity, Cbi.MarketPosition marketPosition)
{
Print("in On Position Update");
}

All will not show if the target or stop hit except OnAccountItemUpdate. This one activates every tick when there is a positon active.
ballboy11 is offline  
Reply With Quote
Old 03-14-2018, 10:57 AM   #14
NinjaTrader_PatrickH
NinjaTrader Customer Service
 
NinjaTrader_PatrickH's Avatar
 
Join Date: Jul 2011
Location: Denver, CO
Posts: 11,646
Thanks: 783
Thanked 2,012 times in 1,938 posts
Default

Hello ballboy11,

Thank you for your response.

It looks like you are mixing the Strategy functions with the Account functions. You actually want to use the OrderUpdate of the Account class to see when orders on the account fill. Please visit the following link for more information and an example on how to use the function: https://ninjatrader.com/support/help...rderupdate.htm

Please let me know if you have any questions.
NinjaTrader_PatrickH is offline  
Reply With Quote
Old 03-15-2018, 07:16 AM   #15
ballboy11
Senior Member
 
Join Date: Jan 2016
Posts: 374
Thanks: 0
Thanked 10 times in 10 posts
Default

I am going through the orders create order etc. and this is where I am having the issue.

I can not find my manually entered order if I place the Manual order BEFORE the strategy has started

All I want to do is close my Manual order at a specific time if my order has not hit the target OR hit the stoploss.

This is the logic in the area

{


//ISSUES: CAN NOT FIND MANUAL ORDER POSITION SIZE IF IT LONG OR SHORT
// IT ALWAYS STATES MARKET POSITION IS FLAT IF MANUAL ORDER IS ENTERED
// IF MANUAL POSITION IS CLOSED BY TARGET OR STOP CREATE ORDER WILL
// CREATE A SELL.
// NEED TO FIND POSITION SIZE POSITION DIRECTION CLOSE MANUAL POSITON
//SCENARIO OF AUTOTRADE HAS NO ISSUES


int tHour = Trade1_EndTime.Hour;
int tMinute = Trade1_EndTime.Minute;

Print("In Closing section");
// CLOSE ALL ORDERS IF MANUAL OR STRATEGY DRIVEN
if (tHour == tMarketHour && tMinute == tMarketMinute)
{
bTrade1Complete = true;
// IF A MANUAL ORDER DO SCENARIO ELSE DO CLOSE STRATEGY SCENARIO
if (bManualOrder == true)
{
if (acc.Orders.Count != 0)
{
//THIS SECTION WORKES FOR CANCELLING ALL TARGETS AND STOPS
acc.CancelAllOrders(Instrument);

//MANUAL ORDER MARKET POSTION IS ALWAYS FLAT EVEN IF THERE IS AN ACTIVE POSITION
// if (Position.MarketPosition == MarketPosition.Long)
// {
Print("marketPosition is " + Position.MarketPosition );

Order stopOrder;
stopOrder = acc.CreateOrder(Instrument,
OrderAction.Sell,
OrderType.Market,
OrderEntry.Automated,
TimeInForce.Day,
1, 0, 0, "", "",
Core.Globals.MaxDate, null);

acc.Submit(new[]{stopOrder});
// }
// else if (Position.MarketPosition == MarketPosition.Short)

}
}
else
{
//THIS WORKS PERFECTLY IF THERE IS NO MANUAL ORDER ENTERED BEFORE THE STRATEGY HAS STARTED
if (GetAtmStrategyMarketPosition(atmStrategyId) != MarketPosition.Flat)
{
Print("in close time");
AtmStrategyClose(atmStrategyId);
}
}
atmStrategyId = String.Empty;
orderId = String.Empty;
}
}




}
ballboy11 is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
OrderID and StrategyID DavidHP Strategy Development 3 02-22-2015 11:12 PM
IB orderID benoirat Platform Technical Support 1 07-25-2013 02:37 AM
getatm - orderId tonynt Platform Technical Support 1 12-08-2011 05:41 PM
OrderId vs. Token terenyi Strategy Development 6 02-02-2011 01:36 PM
OrderID not used? furedde Platform Technical Support 5 01-31-2007 06:53 AM


All times are GMT -6. The time now is 04:38 PM.

Futures, foreign currency and options trading contains substantial risk and is not for every investor. An investor could potentially lose all or more than the initial investment. Risk capital is money that can be lost without jeopardizing ones financial security or lifestyle. Only risk capital should be used for trading and only those with sufficient risk capital should consider trading. Past performance is not necessarily indicative of future results. View Full Risk Disclosure.

CFTC Rules 4.41 - Hypothetical or Simulated performance results have certain limitations, unlike an actual performance record, simulated results do not represent actual trading. Also, since the trades have not been executed, the results may have under-or-over compensated for the impact, if any, of certain market factors, such as lack of liquidity. Simulated trading programs in general are also subject to the fact that they are designed with the benefit of hindsight. No representation is being made that any account will or is likely to achieve profit or losses similar to those shown.

This website is hosted and operated by NinjaTrader, LLC (“NT”), a software development company which owns and supports all proprietary technology relating to and including the NinjaTrader trading platform. NT is an affiliated company to NinjaTrader Brokerage (“NTB”), which is a NFA registered introducing broker (NFA #0339976) providing brokerage services to traders of futures and foreign exchange products. This website is intended for educational and informational purposes only and should not be viewed as a solicitation or recommendation of any product, service or trading strategy. No offer or solicitation to buy or sell securities, securities derivative or futures products of any kind, or any type of trading or investment advice, recommendation or strategy, is made, given, or in any manner endorsed by any NT affiliate and the information made available on this Web site is not an offer or solicitation of any kind. Specific questions related to a brokerage account should be sent to your broker directly. The content and opinions expressed on this website are those of the authors and do not necessarily reflect the official policy or position of NT or any of its affiliates. 

Vendors along with their websites, products and services, collectively referred to as (“Vendor Content”), are independent persons or companies that are in no manner affiliated with NT or any if its affiliates. NT or any of its affiliates are not responsible for, do not approve, recommend or endorse any Vendor Content referenced on this website and it’s your sole responsibility to evaluate Vendor Content. Please be aware that any performance information provided by a vendor should be considered hypothetical and must contain the disclosures required by NFA Rule 2-29(c). If you are interested in learning more about, or investigating the quality of, any such Vendor Content you must contact the vendor, provider or seller of such Vendor Content. No person employed by, or associated with, NT or any of its affiliates is authorized to provide any information about any such Vendor Content.