NexusFi: Find Your Edge


Home Menu

 





A little trouble with limit orders...


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one BigDog with 4 posts (0 thanks)
    2. looks_two Big Mike with 2 posts (0 thanks)
    3. looks_3 DarthTrader with 2 posts (0 thanks)
    4. looks_4 zeller4 with 1 posts (0 thanks)
    1. trending_up 4,839 views
    2. thumb_up 0 thanks given
    3. group 2 followers
    1. forum 8 posts
    2. attach_file 1 attachments




 
Search this Thread

A little trouble with limit orders...

  #1 (permalink)
BigDog
Chicago
 
Posts: 60 since Jun 2009
Thanks Given: 0
Thanks Received: 17

I'm having some trouble getting my Limit Orders to Cancel on this Ninja Script... would really appreciate it if some kind soul could take a look and help me out...

also attached the file

Thanks!

 
Code
        protected override void OnBarUpdate()
        {
            // Submit an entry limit order if we currently don't have an entry order open
            if (entryOrder == null && Close[0] > Open[0])
            {
                entryOrder = EnterLongLimit(0, true, 1, Median[0] + -2 * TickSize, "MyEntry");
            }
  
            if (Position.MarketPosition == MarketPosition.Long && Close[0] >= Position.AvgPrice + (7 * (TickSize / 2)))
            {
                // Checks to see if our Stop Order has been submitted already
                if (stopOrder != null && stopOrder.StopPrice < Position.AvgPrice)
                {
                    // Modifies stop-loss to breakeven
                    stopOrder = ExitLongStop(0, true, stopOrder.Quantity, Position.AvgPrice, "MyStop", "MyEntry");
                }
            }
        }

        /// <summary>
        /// Called on each incoming order event
        /// </summary>
        protected override void OnOrderUpdate(IOrder order)
        {
            // Handle entry orders here. The entryOrder object allows us to identify that the order that is calling the OnOrderUpdate() method is the entry order.
            if (entryOrder != null && entryOrder.Token == order.Token)
            {    

                // Reset the entryOrder object to null if order was cancelled without any fill
                if (order.OrderState == OrderState.Working
                    && order.LimitPrice < GetCurrentBid() - 4 * TickSize)
                {
                    CancelOrder(entryOrder);
                    entryOrder = null;
                }
            }
        }
        
        /// <summary>
        /// Called on each incoming execution
        /// </summary>
        protected override void OnExecution(IExecution execution)
        {
            if (entryOrder != null && entryOrder.Token == execution.Order.Token)
            {
                if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                {
                    // Stop-Loss order 4 ticks below our entry price
                    stopOrder     = ExitLongStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - 4 * TickSize, "MyStop", "MyEntry");
                    
                    // Target order 8 ticks above our entry price
                    targetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + 8 * TickSize, "MyTarget", "MyEntry");
                    
                    // Resets the entryOrder object to null after the order has been filled or partially filled
                    if (execution.Order.OrderState != OrderState.PartFilled)
                    {
                        entryOrder     = null;
                    }
                }
            }
            
            // Reset our stop order and target orders' IOrder objects after our position is closed.
            if ((stopOrder != null && stopOrder.Token == execution.Order.Token) || (targetOrder != null && targetOrder.Token == execution.Order.Token))
            {
                if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
                {
                    stopOrder = null;
                    targetOrder = null;
                }
            }
        }

        /// <summary>
        /// Called on each incoming position event
        /// </summary>
        protected override void OnPositionUpdate(IPosition position)
        {
            // Print our current position to the lower right hand corner of the chart
            DrawTextFixed("MyTag", position.ToString(), TextPosition.BottomRight);
        }

Attached Files
Elite Membership required to download: LimitOrder.zip
Reply With Quote

Can you help answer these questions
from other members on NexusFi?
How to apply profiles
Traders Hideout
What broker to use for trading palladium futures
Commodities
Trade idea based off three indicators.
Traders Hideout
MC PL editor upgrade
MultiCharts
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
 
  #2 (permalink)
BigDog
Chicago
 
Posts: 60 since Jun 2009
Thanks Given: 0
Thanks Received: 17

bump to top

Reply With Quote
  #3 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,463 since Jun 2009
Thanks Given: 33,236
Thanks Received: 101,658


BigDog,

I don't have any experience with IOrder actually, I just never needed it I guess. Perhaps some other people will, and they can examine the code more closely.

Mike

Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #4 (permalink)
 DarthTrader 
Cologne
 
Experience: Intermediate
Platform: NinjaTrader
Broker: Mirus/Zen-Fire, Velocity/TT
Trading: Futures, Stocks
Posts: 10 since Jun 2009
Thanks Given: 29
Thanks Received: 3

Hm ... maybe there is a timing problem. LimitOrders are only active for one bar. If you want to cancel the order manually it could be already cancelled by NT?

What is the detailed error-message? What says the log-tab?

Reply With Quote
  #5 (permalink)
BigDog
Chicago
 
Posts: 60 since Jun 2009
Thanks Given: 0
Thanks Received: 17

from my discussions with Josh, apparently i should be able to use ticks as a criteria to cancel limit orders too... the issue i'm noticing is when i get to the part which contains the cancellation logic... its not executing... yet for what its worth it seems sound...

Reply With Quote
  #6 (permalink)
 DarthTrader 
Cologne
 
Experience: Intermediate
Platform: NinjaTrader
Broker: Mirus/Zen-Fire, Velocity/TT
Trading: Futures, Stocks
Posts: 10 since Jun 2009
Thanks Given: 29
Thanks Received: 3

No exceptions, no errors?

I believe you have to put some Print(..) statements in your code.
What are the values of the order-variables, what is the place in your code
which is executed and which is not ...

Reply With Quote
  #7 (permalink)
BigDog
Chicago
 
Posts: 60 since Jun 2009
Thanks Given: 0
Thanks Received: 17

Darth,
absolutly none... not a single error at all... just isn't reading the order.LimitPrice variable... which is odd because shouldn't be an issue being in the OnOrderUpdate() section...

Reply With Quote
  #8 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,463 since Jun 2009
Thanks Given: 33,236
Thanks Received: 101,658

It might be useful to post a video illustrating exactly what is occurring. You might try the jing software package (free) if you need one for video capturing.

Mike

Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
  #9 (permalink)
 zeller4 
Orlando Florida
 
Experience: Intermediate
Platform: NT8
Trading: CL, NQ, ES, RTY
Posts: 477 since Jun 2009
Thanks Given: 1,416
Thanks Received: 404

BigDog,
I've tried to get this strat to place orders but was having no luck. I've commented out some of the lines you've been using and am able to get orders to go.

Now for your question. If you cancel the orders, is it due to a no-fill and then you just want to re-position the limit to the new Median[0] +/- 2ticks? or is it time based?

kz



Reply With Quote




Last Updated on June 21, 2009


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts