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 elderan, Today, 08:03 PM
    0 responses
    0 views
    0 likes
    Last Post elderan
    by elderan
     
    Started by algospoke, Today, 06:40 PM
    0 responses
    10 views
    0 likes
    Last Post algospoke  
    Started by maybeimnotrader, Today, 05:46 PM
    0 responses
    8 views
    0 likes
    Last Post maybeimnotrader  
    Started by quantismo, Today, 05:13 PM
    0 responses
    7 views
    0 likes
    Last Post quantismo  
    Started by AttiM, 02-14-2024, 05:20 PM
    8 responses
    169 views
    0 likes
    Last Post jeronymite  
    Working...
    X