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

Reverse Position / Close position Stop loss conflicts

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

  • liquid150
    replied
    Thanks Chris,

    I'll see about substituting ExitLongStopMarket and the equal command for shorts for my SetStopLoss commands. I already have functional code moving the stop loss when I want, but thanks.

    Leave a comment:


  • NinjaTrader_ChrisL
    replied
    Hello liquid150,

    Thanks for the reply.

    Orders made with SetStopLoss should not be modified directly. If you want to change the stop loss value, simply call SetStopLoss again before you make the next order. See the sample linked below for a demonstration of that.



    You can do something like the following to change your stop easily, this is a modification of the SampleMACrossover strategy:

    Code:
    protected override void OnBarUpdate()
            {
                if (CurrentBar < BarsRequiredToTrade)
                    return;
    
                if (CrossAbove(smaFast, smaSlow, 1))
                {
                    //Do stop price calculation logic above this line
                    SetStopLoss(CalculationMode.Ticks, 10);
                    EnterLong();
                }
                else if (CrossBelow(smaFast, smaSlow, 1))
                {
                    //Do stop price calculation logic above this line
                    SetStopLoss(CalculationMode.Ticks, 5);
                    EnterShort();
                }
            }
    If you need direct control of the stop-loss order, use ExitLongStopMarket() or ExitLongStopLimit() (for long orders). With these methods, you can assign order object as the "Using CancelOrder" example demonstrates. Please also see this example that shows how to properly set up protective orders. It shows how to set orders to a variable, and how to cancel those orders properly:



    Please let me know if I can assist further.

    Leave a comment:


  • liquid150
    replied
    Commentary on progress, or lack thereof. Realized I will have to use a list of orders and probably multiple stop order objects to accomplish this due to the nature of my strategy.

    I can't seem to get Stop loss orders passed to variable order objects, no matter what I do. Is there a conflict using SetStopLoss and this methodology at the same time? I can't even pass the stop loss orders to a list using this code in OnOrderUpdate with appropriate list variable callout and list creation during configuration:

    Code:
    if (order.OrderState == OrderState.Initialized && order.Name == "Stop loss"
                    )
                {
                    stopLossOrders.Add(order);
                    Print("Stop Loss Orders are " + stopLossOrders);
                }
    The print never appears in Output. I am using Simulated Stops, so I have to stick with Initialized orders...but changing them from Simulated to not has no effect, anyway.

    I am calling "Stop loss" based on the documentation for SetStopLoss. I would like to figure out how to get past this first assignment step, because everything depends on it.

    Leave a comment:


  • liquid150
    replied
    Well it does not seem the simple solution worked, either, in backtest. I will see about using the file setup.

    Leave a comment:


  • liquid150
    replied
    Originally posted by NinjaTrader_ChrisL View Post
    Hello liquid150,

    Thank you for the post.

    You should make sure the stop loss for the previous order is canceled before you submit a reversal. Please see the reference sample linked below. It is an exemplary approach to using CancelOrder in your script.



    Also, note that SetStopLoss and SetProfitTarget will handle the OCO logic for you and can be called dynamically in OnBarUpdate.

    Please let me know if I can assist further.
    I am already using SetStopLoss in OnBarUpdate code, thinking its standard operating code would work for flipped positions, but it appears the order of operations which occurs in this specific scenario does not play well with this command under these very specific, admittedly uncommon, conditions.

    It is good to know I need to ensure the stop loss for previous orders are canceled before submission of reversal. I will see what I can do elsewhere in the code to try to accomplish this cancellation earlier. Based on your post, it should be sufficient to create a second set of conditions for each potential reversal (long and short) order. I will try adding position condition calls to one of each set, and try performing the order cancellation the line before the new order placement. I will let you know my results.

    If this more simple way of solving the problem doesn't work, I will try implementing the schema shown in the linked download.

    Leave a comment:


  • NinjaTrader_ChrisL
    replied
    Hello liquid150,

    Thank you for the post.

    You should make sure the stop loss for the previous order is canceled before you submit a reversal. Please see the reference sample linked below. It is an exemplary approach to using CancelOrder in your script.



    Also, note that SetStopLoss and SetProfitTarget will handle the OCO logic for you and can be called dynamically in OnBarUpdate.

    Please let me know if I can assist further.

    Leave a comment:


  • Reverse Position / Close position Stop loss conflicts

    I have an issue where, under conditions where a position reversal occurs and the Close Position order is used to close an open position, extant stop loss orders remain active and immediately trigger upon order placement of the new, reversed, position's order.

    I have looked at the CancelOrder() and SetStopLoss sections of the manual. I also have looked through the Order, OnOrderUpdate, and OnExecutionUpdate parts of the manual. I am confused as to how to proceed, because the code does not SEEM to be working on historical data.

    The relevant code I have so far is below. I am unsure how to proceed from here. For reference what happens is I will be short, and a bar pattern will occur triggering a long position. The short position will close with an execution called Close position, submit the long, fill, and then the old short position stop loss will activate. I am trying to cancel it to reset upon submittal...there has to be something I am doing wrong.

    Code:
    private Order stoplossorder
    
    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment)
    {
    if ((order.FromEntrySignal == "Stop loss")
    && order.OrderState == OrderState.Initialized)
    {
    stoplossorder = order;
    }
    }
    
    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
    if (execution.Name == "Stop loss" || execution.Name == "Close position")
    {
    CancelOrder(stoplossorder);
    }

Latest Posts

Collapse

Topics Statistics Last Post
Started by MarianApalaghiei, Today, 10:49 PM
3 responses
9 views
0 likes
Last Post NinjaTrader_Manfred  
Started by XXtrader, Today, 11:30 PM
0 responses
3 views
0 likes
Last Post XXtrader  
Started by love2code2trade, Yesterday, 01:45 PM
4 responses
28 views
0 likes
Last Post love2code2trade  
Started by funk10101, Today, 09:43 PM
0 responses
9 views
0 likes
Last Post funk10101  
Started by pkefal, 04-11-2024, 07:39 AM
11 responses
37 views
0 likes
Last Post jeronymite  
Working...
X