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

Exit Trade if unprofitable after N days

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

    #16
    Hello Sweet&Sour,

    Thank you for your response.

    You have already detailed that information and that would not provide the needed behavior. Please refer to my notes below as you will need to implement more logic to correctly exit the trade as needed.

    First, your exit condition needs to be the following and only the following.
    Code:
    if (BarsSinceEntryExecution() >= 4 && Position.MarketPosition != MarketPosition.Flat && Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) <= 0)
    You then need to actually exit the position. Cancelling an order does not exit a position. Please see the full condition and action together here.
    Code:
                    if (BarsSinceEntryExecution() >= 4 && Position.MarketPosition != MarketPosition.Flat && Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) <= 0)
                    {
                        BackBrush = Brushes.LightSalmon;                
    					SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.Market, Position.Quantity, 0, 0, "", "Close After Loss");
                    }
    Now that your OnBarUpdate is properly set up let's tackle OnOrderUpdate(). Each of your Order objects you create needs to have logic to properly set them to null. For example:
    Code:
            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.Name == "LongMarket" && entryOrder == null)
                    entryOrder = order;
                if (entryOrder != null && entryOrder.OrderState == OrderState.Cancelled)
                    entryOrder = null;
    			
    			if (order.Name == "Profit Taker" && profitTakerOrder == null)
                    profitTakerOrder = order;
    			if (profitTakerOrder != null && profitTakerOrder.OrderState == OrderState.Cancelled)
                    profitTakerOrder = null;
    			if (order.Name == "Stop Loss" && stopLossOrder == null)
                    stopLossOrder = order;
    			if (stopLossOrder != null && stopLossOrder.OrderState == OrderState.Cancelled)
                    stopLossOrder = null;
            }
    Next we take on the OnExecutionUpdate(). Here you only need to implement the following code in BOLD.
    Code:
            protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
            {
                if (execution.Order.OrderState != OrderState.Filled)
                    return;
    			
    [B]			if (execution.Order.Name == "Close After Loss")
    			{
    				CancelOrder(profitTakerOrder);
    				CancelOrder(stopLossOrder);
    			}[/B]
    Now your exit will work as needed. Please see my attach screenshot proving this resolves the matter.

    I highly recommend reviewing the documentation on Order objects, OnOrderUpdate(),
    OnExecutionUpdate(), and Unmanaged Order Approach.

    Please let me know if you have any questions.
    Attached Files

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by GussJ, 03-04-2020, 03:11 PM
    16 responses
    3,279 views
    0 likes
    Last Post Leafcutter  
    Started by WHICKED, Today, 12:45 PM
    2 responses
    19 views
    0 likes
    Last Post WHICKED
    by WHICKED
     
    Started by Tim-c, Today, 02:10 PM
    1 response
    9 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by Taddypole, Today, 02:47 PM
    0 responses
    5 views
    0 likes
    Last Post Taddypole  
    Started by chbruno, 04-24-2024, 04:10 PM
    4 responses
    52 views
    0 likes
    Last Post chbruno
    by chbruno
     
    Working...
    X