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

Limit Orders on a Stop and Reverse Strategy

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

    Limit Orders on a Stop and Reverse Strategy

    Hi,

    I'm trying to use limit orders in a 'Stop and Reverse' strategy. I would like to make sure the prior limit order (if unfilled) has been cancelled before placing a new limit order in the opposition direction. In addition, if the order was filled, the position would be exited before taking a position in the opposite direction. Can someone look at the code below and let me know if I'm on the right path to accomplishing this?

    I'm working with these variables/Order objects:

    bool notExecutedLong = true;
    bool notExecutedShort = true;
    IOrder xLong = null;
    IOrder xShort = null;


    I'm setting the bool variables in the OnExecute method:

    protected override void OnExecution(IExecution execution)
    {
    if (xLong != null && xLong == execution.Order)
    notExecutedLong = false;

    if (xShort != null && xShort == execution.Order)
    notExecutedShort = false;
    }


    Here's the code within OnBarUpdate:

    if (highIndex == 0) // Cancel Short, Enter Long
    {
    if (notExecutedShort) // Short order not filled
    {
    CancelOrder(xShort);

    while (xShort.OrderState != OrderState.Cancelled)
    Thread.Sleep(100); // sleep for 1/10th of a second, do not proceed until order has been cancelled
    }
    else if (!notExecutedShort) // Short order was filled
    {
    ExitShort("Short");
    }

    xShort = null;
    xLong = EnterLongLimit(0, true, qty, GetCurrentBid(), "Long");
    notExecutedLong = false;
    }
    else if (lowIndex == 0) // Cancel Long, Enter Short
    {
    if (notExecutedLong) // Long order not filled
    {
    CancelOrder(xLong);

    while (xLong.OrderState != OrderState.Cancelled)
    Thread.Sleep(100); // sleep for 1/10th of a second, do not proceed until order has been cancelled
    }
    else if (!notExecutedLong) // Long order was filled
    {
    ExitLong("Long");
    }

    xLong = null;
    xShort = EnterShortLimit(0, true, qty, GetCurrentAsk(), "Short");
    notExecutedShort = false;
    }



    Thanks,
    gbrad

    #2
    Hello,

    Thank you for the post.

    After looking at what you currently have I can say that the use of while loops and Thread.Sleep is frowned upon as these items lock the thread which prevents the event-driven nature of the platform.

    In general, if you are awaiting an order action to complete, you should use the existing event-driven order methods like OnOrderUpdate or OnExecution to know when order actions are finished.




    OnBarUpdate should instead be used to execute your logic when data is received, that can either be on each tick or at a bar intreval for CalculateOnBarClose = true. This prevents the need to use items like while loops or sleep on the thread. Holding OnBarUpdate would never be suggested as this could allow for your logic to happen at a later time after the actual time the data was recieved.

    We have a sample that shows the use of the two overrides here:

    The OnOrderUpdate() and OnExecution() methods are reserved for experienced programmers. Instead of using Set() methods to submit stop-loss and profit target orders, you can submit and update them manually through the use of IOrder and IExecution objects in the OnOrderUpdate() and OnExecution() methods. The OnOrderUpdate()


    In this case, I would suggest removing the while loops and sleep from OnBarUpdate and migrate your cancellation logic into OnOrderUpdate so you can see when this event happens in realtime. After doing this, you could utilize Prints or just view the output of the strategy to view if it runs per your trading goals.

    After making the changes, you could review how the script works in relation to what you want it to do, if it is doing other actions we could review the modified syntax to see why that is the case.


    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by timmbbo, Today, 08:59 AM
    1 response
    2 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by KennyK, 05-29-2017, 02:02 AM
    2 responses
    1,279 views
    0 likes
    Last Post marcus2300  
    Started by fernandobr, Today, 09:11 AM
    0 responses
    0 views
    0 likes
    Last Post fernandobr  
    Started by itrader46, Today, 09:04 AM
    1 response
    3 views
    0 likes
    Last Post NinjaTrader_Clayton  
    Started by bmartz, 03-12-2024, 06:12 AM
    5 responses
    33 views
    0 likes
    Last Post NinjaTrader_Zachary  
    Working...
    X