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 RubenCazorla, Today, 09:07 AM
    0 responses
    1 view
    0 likes
    Last Post RubenCazorla  
    Started by BarzTrading, Today, 07:25 AM
    2 responses
    28 views
    1 like
    Last Post BarzTrading  
    Started by devatechnologies, 04-14-2024, 02:58 PM
    3 responses
    20 views
    0 likes
    Last Post NinjaTrader_BrandonH  
    Started by tkaboris, Today, 08:01 AM
    0 responses
    6 views
    0 likes
    Last Post tkaboris  
    Started by EB Worx, 04-04-2023, 02:34 AM
    7 responses
    165 views
    0 likes
    Last Post VFI26
    by VFI26
     
    Working...
    X