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

Issue with Iorders

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

    Issue with Iorders

    Hi, i have the following code and i really don't understand why the orders are not triggered properly. The behavior is really weird as there as some entry signal triggered but the exit are not triggered properly.

    Do you have any idea what is wrong with the Iorders below??


    Code:
    private IOrder stopLongOrder = null;
    private IOrder stopShortOrder = null;
    private IOrder entryLongOrder = null;
     private IOrder entryShortOrder = null;
    public int contracts;
    
     protected override void Initialize()
    {
        CalculateOnBarClose = true;
        ExitOnClose = false;
        RealtimeErrorHandling = RealtimeErrorHandling.TakeNoAction;
    }
    private void Bracket()
    {
        Stop = 50;
        contracts = (int)(10000 * (0.02 * 10000) / Stop);
    }
    private void LongEntry()
    {
        Bracket();
        barNumberOfOrder = CurrentBar;
    
        entryLongOrder = EnterLongStop(contracts, Close[0] + 1 * TickSize, "LongEntry");
    }
    private void ShortEntry()
    {
        Bracket();
        barNumberOfOrder = CurrentBar;
    
        entryShortOrder = EnterShortStop(contracts, Close[0] - 1 * TickSize, "ShortEntry");
    }
    private void ExitPosition()
    {
        int NbBars = CurrentBar - barNumberOfOrder;
        if (NbBars >= 20)                 
        {
             if (stopLongOrder == null && stopShortOrder == null 
    
                 && Position.MarketPosition == MarketPosition.Long)
             {
                stopLongOrder = ExitLongStop(Close[0] - 1 * TickSize, "LongEntry");
            }
             if (stopShortOrder == null && stopLongOrder == null 
    
               && Position.MarketPosition == MarketPosition.Short)
             {
                stopShortOrder = ExitShortStop(Close[0] + 1 * TickSize, "ShortEntry");
            }
        }
    }
    protected override void OnBarUpdate()
    {
        ExitPosition();
    
             if (entryLongOrder == null && entryShortOrder == null 
    
               && Position.MarketPosition == MarketPosition.Flat 
    
                && Close[0] < Close[1])
             {
                LongEntry();
            }
             if (entryShortOrder == null && entryShortOrder == null 
    
                && Position.MarketPosition == MarketPosition.Flat `
                && Close[0] > Close[1])                                
             {
                ShortEntry();           
            }    
    }
    protected override void OnOrderUpdate(IOrder order)
    {
        if (entryLongOrder != null && entryLongOrder == order)
        {
            if (order.OrderState == OrderState.Rejected)
            {
                entryLongOrder = EnterLong(contracts, "LongEntry");
            }
            if (order.OrderState == OrderState.Filled)
            {
                entryLongOrder = null;
            }
        }
        if (entryShortOrder != null && entryShortOrder == order)
        {
            if (order.OrderState == OrderState.Rejected)
            {
                entryShortOrder = EnterShort(contracts, "ShortEntry");
            }
            if (order.OrderState == OrderState.Filled)
            {
                entryShortOrder = null;
            }
        }
        if (stopLongOrder != null && stopLongOrder == order)
        {
            if (order.OrderState == OrderState.Rejected)
            {
                stopLongOrder = ExitLong("LongEntry");
            }
            if (order.OrderState == OrderState.Filled)
            {
                stopLongOrder = null;
            }
        }
        if (stopShortOrder != null && stopShortOrder == order)
        {
            if (order.OrderState == OrderState.Rejected)
            {
                stopShortOrder = ExitShort("ShortEntry");
            }
            if (order.OrderState == OrderState.Filled)
            {
                stopShortOrder = null;
            }
        }
    }

    #2
    Hello cbadr,

    Have you used prints to ensure the condition that places the exit is evaluating as true when you expect it?

    Below is a public link to a post that demonstrates using prints to understand behavior.


    If so, are you printing the order object in OnOrderUpdate?
    Is the order entering an accepted state?
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by DynamicTest, Today, 11:18 PM
    0 responses
    3 views
    0 likes
    Last Post DynamicTest  
    Started by dcriador, Today, 01:43 AM
    3 responses
    19 views
    0 likes
    Last Post dcriador  
    Started by smartromain, Today, 10:50 PM
    0 responses
    5 views
    0 likes
    Last Post smartromain  
    Started by popecapllc, 08-09-2023, 07:42 PM
    10 responses
    1,366 views
    0 likes
    Last Post BartMan
    by BartMan
     
    Started by jbays87, Today, 09:46 PM
    0 responses
    6 views
    0 likes
    Last Post jbays87
    by jbays87
     
    Working...
    X