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 GussJ, 03-04-2020, 03:11 PM
    11 responses
    3,227 views
    0 likes
    Last Post xiinteractive  
    Started by andrewtrades, Today, 04:57 PM
    1 response
    13 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by chbruno, Today, 04:10 PM
    0 responses
    7 views
    0 likes
    Last Post chbruno
    by chbruno
     
    Started by josh18955, 03-25-2023, 11:16 AM
    6 responses
    440 views
    0 likes
    Last Post Delerium  
    Started by FAQtrader, Today, 03:35 PM
    0 responses
    12 views
    0 likes
    Last Post FAQtrader  
    Working...
    X