Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Set Profit Target based on candlesize?

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

    Set Profit Target based on candlesize?

    Can someone help me set the profit target based on the size of the most recent candle. Currently I have the following but the profit target is set when the first candle closes in profit instead of what I want.

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 1)
    return;

    candlesize = High[0] - Low[0];

    if ((High[1] < EMA1[0])
    && (Close[0] > EMA1[0]))
    {

    EnterLong();

    SetStopLoss("", CalculationMode.Price,(Low[0]), false);

    SetProfitTarget("" , CalculationMode.Ticks, candlesize);


    }

    #2
    Hello omermizra,

    Thanks for your post.

    I see that you are calling Set methods after the entry order method is called. Set methods should be called before the entry method if you are calling the Set methods in OnBarUpdate. The reason for this is Set methods prep NinjaTrader to submit a profit target or stop loss order once an entry order method is filled. For example, see below.

    Code:
    protected override void OnBarUpdate()
    {
        if (Close[0] > Open[0])
        {
            SetStopLoss(string fromEntrySignal, CalculationMode mode, double value, bool isSimulatedStop);
            SetProfitTarget(string fromEntrySignal, CalculationMode mode, double value);
    
            EnterLong();
        }
    }
    Also, note the following tip from the Tips section of the SetStopLoss/SetProfitTarget help guide pages: 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.

    Based on the code you shared, if the previous High price and current Close price are greater than EMA1, your strategy will place an entry order at the current High price - current Low price and calls SetStopLoss/SetProfitTarget to place a stop and target.

    Ultimately, if the strategy is not placing orders where expected then debugging steps would need to be target to determine how the strategy is placing orders so that you could modify the script accordingly.

    See the link below to a forum post that demonstrates how to use prints to understand behavior.
    https://ninjatrader.com/support/foru...121#post791121

    See the help guide documentation below for more information.
    SetStopLoss: https://ninjatrader.com/support/help...etstoploss.htm
    SetProfitTarget: https://ninjatrader.com/support/help...ofittarget.htm

    Let us know if we may assist further.
    Last edited by NinjaTrader_BrandonH; 02-07-2022, 11:56 AM.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      What should the reset values be set too?

      I've tried the following:

      candlesize = (High[0] - Low[0]);
      SetStopLoss("", CalculationMode.Price,(Low[0]), false);
      SetProfitTarget("" , CalculationMode.Ticks, candlesize);

      if ((High[1] < EMA1[0])
      && (Close[0] > EMA1[0]))
      {

      EnterLong();
      }


      if (Position.MarketPosition == MarketPosition.Flat)
      {
      SetStopLoss("", CalculationMode.Currency, 0, false);
      SetProfitTarget("" , CalculationMode.Currency, 0);


      }

      Comment


        #4
        Hello omermirza,

        Thanks for your note.

        What should the reset values be set too?

        Are you referring to resetting the target price/offset value when your strategy is flat?

        If so, see the reference sample linked below from our help guide which demonstrates resetting the target price/offset value of a Target/Stop when the strategy is flat. This reference sample also demonstrates modifying the price of a stop loss/profit target order placed by Set methods in OnBarUpdate.



        Let us know if we may assist further.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Yesterday, 05:17 AM
        0 responses
        79 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        148 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        79 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        52 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        58 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X