Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 bortz, 11-06-2023, 08:04 AM
    47 responses
    1,607 views
    0 likes
    Last Post aligator  
    Started by jaybedreamin, Today, 05:56 PM
    0 responses
    9 views
    0 likes
    Last Post jaybedreamin  
    Started by DJ888, 04-16-2024, 06:09 PM
    6 responses
    19 views
    0 likes
    Last Post DJ888
    by DJ888
     
    Started by Jon17, Today, 04:33 PM
    0 responses
    6 views
    0 likes
    Last Post Jon17
    by Jon17
     
    Started by Javierw.ok, Today, 04:12 PM
    0 responses
    16 views
    0 likes
    Last Post Javierw.ok  
    Working...
    X