Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problem with adding to position

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

    Problem with adding to position

    Hi!
    I am trying to add to a position for the second time. I copied the code that works well for the first increase and thought I could just adjust it a little bit and it will also work for the second one.
    Unfortunately it does not. And it looks to me like it is simply ignoring some conditions in the if statement. The script is calculated OnPriceChange

    if (Position.MarketPosition == MarketPosition.Short
    && secondAddOnDirectEntryPoint > 0
    && secondAddOnPullbackEntryPoint == 0
    && Close[0] <= (initialEntryPrice - (secondAddOnDirectEntryPoint * TickSize))
    && isSecondAdd == false
    && isFirstAdd == true || isFirstAddOnPullback == true)

    {
    //Print("-----------------------------------------------------------------------------");
    //Print("isSecondAdd");
    //Print(isSecondAdd.ToString());
    isSecondAdd = true;
    //Print("secondAddOnDirectEntryPoint (76)");
    //Print(secondAddOnDirectEntryPoint.ToString());
    //Print("secondAddOnPullbackEntryPoint (0)");
    //Print(secondAddOnPullbackEntryPoint.ToString());
    //Print("Close (0)");
    //Print(Close[0].ToString());
    //Print("initialEntryPrice");
    //Print(initialEntryPrice.ToString());
    //Print("initialEntryPrice - (secondAddOnDirectEntryPoint * TickSize)");
    //var test = (initialEntryPrice - (secondAddOnDirectEntryPoint * TickSize));
    //Print(test.ToString());


    EnterShort(Convert.ToInt32(DefaultQuantity * 2), "Addon two"); //3rd Entry
    SetStopLoss(CalculationMode.Price, initialEntryPrice - (secondAddOnStopSave * TickSize));
    SetProfitTarget(CalculationMode.Price, takeProfitPrice);

    isSecondAddOnLevelMet = true;
    return;
    }​


    I printed all of the values to the output window. Clearly Close[0] is not <= (initialEntryPrice - (secondAddOnDirectEntryPoint * TickSize)) when the entry happens.
    But for some reason it ignores this and places the entry at the same price and at the same time where and when already the earlier entry took place?

    Click image for larger version

Name:	Screenshot 2023-09-13 203947.png
Views:	258
Size:	52.0 KB
ID:	1268868

    Also I do not understand why it is printing several times because after isSecondAdd = true, it should not execute again, since the Boolean expression at least then evaluates to false.

    Any help is very much appreciated!
    Thank you

    #2
    Hello ravendo,

    Thanks for your post.

    I have moved your post to the Strategy Development section of the Forums since your question is related to programming a NinjaScript strategy. The NinjaScript File Sharing Discussion section of the Forums would be for if you are looking for an existing NinjaScript.

    That said, I see you are calling Set methods after your Enter method in the code you shared. Set methods should be called prior to the Enter order method as noted on the SetStopLoss() and SetProfitTarget() help guide documentation.

    For example:

    SetProfitTarget();
    SetStopLoss();
    EnterLong();


    From the SetStopLoss() and SetProfitTarget() help guides: Since they are submitted upon receiving an execution, the Set method should be called prior to submitting the associated entry order to ensure an initial level is set.

    SetProfitTarget(): https://ninjatrader.com/support/help...ofittarget.htm
    SetStopLoss(): https://ninjatrader.com/support/help...etstoploss.htm

    Your Prints for the condition to submit the order should be placed outside of the condition. One line above the condition to place the order, add your print to see how the values in the conditions are evaluating.

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

    Scaling into a position is controlled with the signal names of your strategy, and properties EntryHandling and EntriesPerDirection. This reference sample can help with scaling in and out of a strategy.
    https://ninjatrader.com/suppport/helpGuides/nt8/en-us/scaling_out_of_a_position.htm

    This sample can assist further with managing multiple exit/entry signals:
    https://ninjatrader.com/support/helpGuides/nt8/en-us/using_multiple_entry_exit_sign.htm
    <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
      Hello Brandon,

      Thanks for your quick reply and for moving the post to the right section.

      And thank you for all the links you provided.Will look into it tomorrow.

      But the reason why I use Set methods after my Enter method is because only after the next entry has taken place will I then have a new Position.AveragePrice.
      And once a position is increased my stop has to save at least some ticks in profit.

      So I think I can only use the Set method for the stoploss to save these ticks, after the new Position.AveragePrice is clear and in place?

      Comment


        #4
        Hello ravendo,

        Thanks for your notes.

        Set methods prep NinjaTrader to submit the protective stop loss and profit target orders which is why they should be called prior to the Enter order method.

        In your case, you may consider using Exit methods such as ExitLongLimit() and ExitLongStopLimit() in your script for profit target and stop loss orders instead of Set methods since you want to scale into position and then place the stop loss and profit target orders to the new average price of the position.

        See this help guide page for more information about Exit methods available to use: https://ninjatrader.com/support/help...d_approach.htm
        <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


          #5
          Hello Brandon,

          Thank you for the information about Exit Methods. I will dive into it, as I want to learn as much as possible about all of this stuff.

          In my case SetProfitTarget and SetStopLoss is already called in State == State.DataLoaded. So when I later call EnterShort I am not just sending a naked market order.

          The Set methods called after the Enter method then only adjust the already existing Takeprofit and Stoploss order to my desired price level.

          I hope doing it like this is ok. So far when testing it in replay mode, everything looks reliable.

          In the meantime I was able to figure out by myself why adding the second time did not take place where I wanted it to be.

          Until yesterday I never had to use || operators in an if statement.
          So today I took a hard look at how conditions with || operators are really being read.

          What i wanted to express was:
          if a + b + c + d is true AND EITHER e or f, do...

          But my code from yesterday reads: if a + b + c + d + e is true, or f is true, do....

          Well f was always true but nothing else ...


          Last edited by ravendo; 09-14-2023, 06:13 AM.

          Comment

          Latest Posts

          Collapse

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