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 bc24fl, 08-30-2019, 01:58 PM
      4 responses
      259 views
      0 likes
      Last Post PaulMohn  
      Started by sugalt, Today, 04:02 AM
      0 responses
      6 views
      0 likes
      Last Post sugalt
      by sugalt
       
      Started by tradingnasdaqprueba, 04-09-2024, 09:52 AM
      6 responses
      29 views
      0 likes
      Last Post tradingnasdaqprueba  
      Started by PaulMohn, Today, 02:06 AM
      1 response
      7 views
      0 likes
      Last Post PaulMohn  
      Started by Mindset, Today, 01:27 AM
      0 responses
      6 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Working...
      X