Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Check unique entry name

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Check unique entry name

    Code:
    			if (Position.MarketPosition == MarketPosition.Flat
    				&& XYZ < 0)
    				
    			{
    				EnterLongLimit(Convert.ToInt32(DefaultQuantity), GetCurrentBid(0), "Buy1");
    				SetProfitTarget("Buy1", CalculationMode.Percent, PT1);
    				CandleOutlineBrush = Brushes.CornflowerBlue;
    			}
    			
    			 if (Position.MarketPosition == MarketPosition.Long)
    				{
    				
    				if (Close[0] > Position.AveragePrice * (1+ Target1))
    				{
    				EnterLongLimit(Convert.ToInt32(DefaultQuantity), GetCurrentBid(0), "Buy2");
    				SetProfitTarget("Buy2", CalculationMode.Percent, PT2);
    				}
    Hi,

    I'm trying to "average up." If close > (buy1.averagePrice + X) {enter long}.

    The above code works, but it seems because buy2 condition is true even when my buy1 position is flat because my profit target for buy1 got him.

    Because of the condition "if market is long" enterlong; it causes it to be true even when buy1 is flat. How can I code so that buy2 condition is:

    if market is long and buy1 position is not flat {enter long}?

    I lookedup iorder but that's only for n7, and the current suggestion for n8 seemed confusing. Would appreciate a sample code! Thanks

    Again, i'm trying to check to see if my intial buy1 position is flat or live so that I can include that condition in my second argument for buy2.

    #2
    Hello calhawk01,

    Thanks for your inquiry.

    You are correct that in NT7, tracking each order with an IOrder object would be the valid approach. In NT8, this has changed to an Order object which is a direct reference to the internal Order object and not just an interface to it.

    The general practice is to null the Order object when it is filled or cancelled. That way you can check if the Order object is null to see if that order is in position. The same can be done to nullify an Order object on a limit order so it gets nullified (to allow reentry) when it's target or stop are met.

    Code:
    private Order myEntry = null;
    protected override void OnBarUpdate()
    {
    	if(myEntry == null)
    		EnterLongLimit(0, true, 1, Low[0] - 10*TickSize, "LimitEntry");
    }
    
    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)
    {
    	if (order.Name == "LimitEntry")
    		myEntry = order;
    
    	// if entry order exists
    	if (myEntry != null && myEntry == order)
    	{
    		Print(order.ToString());
    		if (order.OrderState == OrderState.Cancelled)
    		{
    			// Nullify Entry Order as it was cancelled. This can also be nullified for a fill of this order or the fill of the Target/Stop within OnExecutionUpdate(). Always handle fills in OnExecutionUpdate()
    			myEntry = null;
    		}
    	}
    }
    if market is long and buy1 position is not flat {enter long}?
    Position.MarketPosition tells you the virtual position of the strategy. I am unsure if this is what you would like to check since the strategy's "buy1" order would make the strategy position long when that order is filled.

    Nonetheless, you could create such a check by looking at the strategy's Position.MarketPosition along with the Order object not being null.

    I recommend reviewing the SampleOnOrderUpdate strategy and the associated documentation as it demonstrates how Order objects can be managed appropriately within a NinjaScript strategy.

    SampleOnOrderUpdate - https://ninjatrader.com/support/foru...ead.php?t=7499

    I have also included a link to our publicly available documentation on Strategy Position vs. Account position in case there is any confusion.

    Strategy position vs. Account position - https://ninjatrader.com/support/help..._account_p.htm

    If you have any additional questions, please don't hesitate to write back.
    JimNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by fredrikn, 05-08-2019, 04:01 AM
    57 responses
    4,391 views
    2 likes
    Last Post Momentum  
    Started by Blairski, Yesterday, 09:19 AM
    2 responses
    11 views
    0 likes
    Last Post Blairski  
    Started by tonynt, Today, 09:33 AM
    1 response
    6 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by minotti1, Today, 08:31 AM
    1 response
    13 views
    0 likes
    Last Post NinjaTrader_Jim  
    Started by SmartArtsStudio, 05-15-2021, 10:46 AM
    1 response
    15 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Working...
    X