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

Set Contract Amount - TP not reflecting the amount

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

    Set Contract Amount - TP not reflecting the amount

    As you can see in the picture Contract amount I set my contract amount to 2 contracts. Now that I have entered a trade, I see that I have a position with 2 contracts. Since I set a protective SL and TP, the next image shows the SL at 2 contracts and the TP at 1. Set 2 - NT8.jpg.
    When I switch the contracts to 1, the SL is now at 1 and the TP is 1. Set 1 - NT8.jpg

    Let me show you the code where the Protective settings are

    I feel like this code it making the TP stay at 1 and not 2 contracts.
    Code:
    // StopLoss & ProfitTarget
            protected override void OnExecutionUpdate( Cbi.Execution e, string executionId, double price, int quantity,
                                                       Cbi.MarketPosition marketPosition, string orderId, DateTime time )
            {            
                if( UseProtectiveStops )
                {
                    if( e.IsEntryStrategy )
                    {
                        if( e.Order.OrderAction == OrderAction.Buy )
                        {
                            // Set stop loss
                            double stopOrderPrice = e.Order.AverageFillPrice - StopLoss * TickSize;
                            ExitLongStopMarket( 0, true, 3, stopOrderPrice, "Stop loss", e.Order.FromEntrySignal );
    
                            // Set profit target 1
                            double profitTarget = e.Order.AverageFillPrice + ProfitTarget * TickSize;;
                            ExitLongLimit( 0, true, 1, profitTarget, "Profit target", e.Order.FromEntrySignal );                                            
                        }
                        if( e.Order.OrderAction == OrderAction.SellShort )
                        {
                            // Set stop loss
                            double stopOrderPrice = e.Order.AverageFillPrice + StopLoss * TickSize;
                            ExitShortStopMarket( 0, true, 3, stopOrderPrice, "Stop loss", e.Order.FromEntrySignal );
    
                            // Set profit target 1
                            double profitTarget = e.Order.AverageFillPrice - ProfitTarget * TickSize;;
                            ExitShortLimit( 0, true, 1, profitTarget, "Profit target", e.Order.FromEntrySignal );
                        }
                    }
                }​
    Attached Files

    #2
    Hello zayone,

    Thanks for your post.

    In the code you shared you are calling ExitLongStopMarket() and ExitShortStopMarket() with a specified quantity of 3. You are also calling ExitLongLimit() and ExitShortLimit() with a specified quantity of 1.

    If you want to submit a stop loss and profit target for the quantity of orders that were filled, you should use execution.Order.Filled for the quantity when calling your Exit methods in OnExecutionUpdate(). The reference sample called SampleOnOrderUpdate linked below demonstrates this.

    SampleOnOrderUpdate: https://ninjatrader.com/support/help...and_onexec.htm

    See the help guide documentation below for more information.

    OnExecutionUpdate(): https://ninjatrader.com/support/help...tionupdate.htm
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Thank you, this updated code seemed to work

      Code:
      if( UseProtectiveStops )
                  {
                      if( e.IsEntryStrategy )
                      {
                          int filledQuantity = e.Order.Filled;
      
                          if( e.Order.OrderAction == OrderAction.Buy )
                          {
                              // Set stop loss
                              double stopOrderPrice = e.Order.AverageFillPrice - StopLoss * TickSize;
                              ExitLongStopMarket( 0, true, filledQuantity, stopOrderPrice, "Stop loss", e.Order.FromEntrySignal );
      
                              // Set profit target 1
                              double profitTarget = e.Order.AverageFillPrice + ProfitTarget * TickSize;;
                              ExitLongLimit( 0, true, filledQuantity, profitTarget, "Profit target", e.Order.FromEntrySignal );                                            
                          }
                          if( e.Order.OrderAction == OrderAction.SellShort )
                          {
                              // Set stop loss
                              double stopOrderPrice = e.Order.AverageFillPrice + StopLoss * TickSize;
                              ExitShortStopMarket( 0, true, filledQuantity, stopOrderPrice, "Stop loss", e.Order.FromEntrySignal );
      
                              // Set profit target 1
                              double profitTarget = e.Order.AverageFillPrice - ProfitTarget * TickSize;;
                              ExitShortLimit( 0, true, filledQuantity, profitTarget, "Profit target", e.Order.FromEntrySignal );
                          }
                      }
                  }​

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by knowmad, 05-07-2024, 03:52 AM
      6 responses
      59 views
      0 likes
      Last Post knowmad
      by knowmad
       
      Started by xepher101, 05-10-2024, 12:19 PM
      9 responses
      114 views
      0 likes
      Last Post jeronymite  
      Started by tkaboris, Today, 07:53 PM
      0 responses
      1 view
      0 likes
      Last Post tkaboris  
      Started by JGriff5646, Yesterday, 05:47 PM
      2 responses
      23 views
      0 likes
      Last Post JGriff5646  
      Started by lezlebric, Today, 06:32 PM
      0 responses
      13 views
      0 likes
      Last Post lezlebric  
      Working...
      X