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

SetProfitTarget was not considered

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

    SetProfitTarget was not considered

    Hi again, some strange behavior, in this code.....(just the end of many conditions) there is normally set a stop loss and a stop proft. Actually, today, in the live Simulation of the strategy, the stop loss was, but the stop proft was not considered, even in the log file there is nothing mentioned about it.

    PHP Code:
     if (Close[0] <= EMA_Fast[0])
                                {
                                    if (
    EMA_Fast[0] <= EMA_Slow[0] && EMA_Fast[0] <= SMA[0])
                                    {
                                        
    EnterShort(Convert.ToInt32(orderQuantity), @"Enter_SHORT");
                                        
    SetStopLoss(CalculationMode.TicksInit_Stop_short);
                                        
    SetProfitTarget(@"Profit_SHORT",CalculationMode.TicksStop_Profit_Short);
                                    }&
    #8203; 
    attached also the log file
    Attached Files

    #2
    Hello Triple_M,

    Thank you for your post.

    Are your stop loss and/or profit target dynamic? Please note that set methods cannot be unset. This means call Set methods with CalculationMode.Ticks when flat, and before calling a new entry.

    From the Help Guide:

    "Should you call this method to dynamically change the target price in the strategy OnBarUpdate() method, you should always reset the target price / offset value when your strategy is flat otherwise, the last price/offset value set will be used to generate your profit target order on your next open position​"



    Also note that the fromEntrySignal ("Profit_SHORT") specified in your SetProfitTarget() does not match the signal name from EnterShort() ("Enter_SHORT"). In order to properly tie these together, the fromEntrySignal needs to match the signal name of the entry order you want to tie it to.


    If the expected trade(s) are not appearing, this would indicate that the condition to place the order is not evaluating as true and the order is not being submitted, or the order is being ignored for other reasons, or the order is being cancelled.

    To understand why the script is behaving as it is, such as placing orders or not placing orders or drawing objects when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

    In the strategy add prints (outside of any conditions) that print the date time of the bar and all values compared in every condition that places an order.

    The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very important to include a text label for each value and for each comparison operator in the print to understand what is being compared in the condition sets.

    Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

    Further, enable TraceOrders which will let us know if any orders are being ignored and not being submitted when the condition to place the orders is evaluating as true.

    I am happy to assist you with analyzing the output from the output window.

    Run or backtest the script and when the output from the output window appears save this by right-clicking the output window and selecting Save As... -> give the output file a name and save -> then attach the output text file to your reply.

    Below is a link to a forum post that demonstrates using informative prints to understand behavior and includes a link to a video recorded using the Strategy Builder to add prints.


    Please let me know if I may further assist with analyzing the output or if you need any assistance creating a print or enabling TraceOrders.​
    Gaby V.NinjaTrader Customer Service

    Comment


      #3
      Hi Gaby, yes and no, the stop loss is dynamic, the stop profit not. It worked all well, the stop loss and proft, but i implemented a martingale sequenz, .... this may be the reason. But how to search for ......Or is there also a possibility that it could happen in a fast moving market?

      the trade is managed herewith (former and working code)
      PHP Code:
      else // if not flat manage trade
                  
      {
                      if (
      Position.MarketPosition == MarketPosition.Long)
                      {
                          
      // before changing the current trailing stop price, keep a copy
                          
      previousTrail_Stop CurrentTrail_Stop;
                          
                          if (
      IsFirstTickOfBar)
                          {
                              if (
      BarsSinceEntryExecution(0""0) > Bars_Since_Entry)
                              {
                                  if (
      ParabolicSAR1[1] < Low[1])
                                  {
                                      if (
      ParabolicSAR1[1] > (Position.AveragePrice - (Init_Stop_long TickSize)) )
                                      {
                                          if (
      Close[0] > ParabolicSAR1[1])
                                          {
                                              
      CurrentTrail_Stop ParabolicSAR1[0];
                                          }
                                      }
                                  }
                              }
                          }
                          
                          
      // if the CurrentTrail_Stop has changed then set the new stop loss
                          
      if (CurrentTrail_Stop != previousTrail_Stop)
                              
      SetStopLoss(CalculationMode.PriceCurrentTrail_Stop);
                          
                          
                      }
                      else if (
      Position.MarketPosition == MarketPosition.Short)
                      {
                          
      // before changing the current trailing stop price, keep a copy
                          
      previousTrail_Stop CurrentTrail_Stop;
                          
                          if (
      IsFirstTickOfBar)
                          {
                              if (
      BarsSinceEntryExecution(0""0) > Bars_Since_Entry)
                              {
                                  if (
      ParabolicSAR1[1] > High[1])
                                  {
                                      if (
      ParabolicSAR1[1] < (Position.AveragePrice + (Init_Stop_short TickSize)) )
                                      {
                                          if (
      Close[0] < ParabolicSAR1[1])
                                          {
                                              
      CurrentTrail_Stop ParabolicSAR1[0];
                                          }
                                      }
                                  }
                              }
                          }
                          
                          
      // if the CurrentTrail_Stop has changed then set the new stop loss
                          
      if (CurrentTrail_Stop != previousTrail_Stop)
                              
      SetStopLoss(CalculationMode.PriceCurrentTrail_Stop);&#8203; 
      There is or was nothing for the stop profit, it is just the amount of ticks, if reached trade will be closed if not, the PSAR Stop is closing the trade
      Last edited by Triple_M; 03-18-2024, 08:02 AM.

      Comment


        #4
        Hello Triple_M,

        If the stop loss is dynamic it needs to be reset before calling the entry method. Your current code has SetStopLoss() happening after the entry.

        This sample script demonstrates modifying the price of stop loss and profit target orders:



        Have you edited the code to make sure the fromEntrySignal of your SetProfitTarget matches the signal name of the entry order?


        Debugging using prints and TraceOrders would help us understand why the script is behaving the way it is.

        Yes, it is possible that the stop loss or a profit target could be rejected due to market volatility. There is no way to 100% avoid this occurring, as in volatile markets the market could move so far and fast that this would occur.

        Gaby V.NinjaTrader Customer Service

        Comment


          #5
          I have seen this example, it is a good question, if i should call the Stop Loss and Profit Target at the State.Set.Default and not in the OnBarUpdate. May be thats works better? But I even want to have different approaches and tick sizes for Long and Short. And as you can see, i set a stop loss as an initial stop, but 3 bars later it change to a parabolic SAR stop. What is about the stop loss then? Does it change or do i have then two stops.

          Yes, I call "bool isFlat = Position.MarketPosition == MarketPosition.Flat;" as a condition before a new position is done. And yes i updated the Signal Name that they match.
          Last edited by Triple_M; 03-18-2024, 12:24 PM.

          Comment


            #6
            Hello Triple_M,

            If your set method is static (meaning will not be updated dynamically), then it is best to call it from OnStateChange(). If it is being called/updated dynamically, you can call it from OnBarUpdate().

            I recommend debugging using prints and TraceOrders. TraceOrders in particular will give us information regarding whether the order was ignored, rejected, submitted, etc.

            If you need assistance with creating a print or enabling TraceOrders, please let me know.
            Gaby V.NinjaTrader Customer Service

            Comment


              #7
              yes please, tell me how to trace orders, never done before. How to set and how and where to check

              Comment


                #8
                Hello,

                You can set TraceOrders to true in State.SetDefaults.



                Output from prints and TraceOrders will appear in a NinjaScript Output window (Control Center > New > NinjaScript Output).

                Please let me know if you have any further questions.
                Gaby V.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Option Whisperer, Today, 09:55 AM
                1 response
                11 views
                0 likes
                Last Post bltdavid  
                Started by port119, Today, 02:43 PM
                0 responses
                1 view
                0 likes
                Last Post port119
                by port119
                 
                Started by Philippe56140, Today, 02:35 PM
                0 responses
                3 views
                0 likes
                Last Post Philippe56140  
                Started by 00nevest, Today, 02:27 PM
                0 responses
                2 views
                0 likes
                Last Post 00nevest  
                Started by Jonafare, 12-06-2012, 03:48 PM
                5 responses
                3,987 views
                0 likes
                Last Post rene69851  
                Working...
                X