Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Setting Target using a Variable Value

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

    Setting Target using a Variable Value

    Sorry for the question, the library covers very little regarding the SetTarget methods

    SetProfitTarget("Long", CalculationMode.Ticks, 40);

    works just fine of course, as the 40 above is an integer.

    Replacing that integer with anything other than another direct numerical integer value yields random results, mostly 1 to 1/2 point targets.

    I'm sure this can be simplified, but in the moment I have:

    double bodySize = Math.Abs(Open[0] - Close[0]);
    double bodySize1 = Math.Round(bodySize);
    int Profit = System.Convert.ToInt32(bodySize1);


    Trying

    SetProfitTarget("Long", CalculationMode.Ticks, Position.AveragePrice + Profit); or

    SetProfitTarget("Long", CalculationMode.Ticks, Profit);

    still yields random results. ​


    Obviously my conversion method is amiss?

    Setting int Profit to a direct integer value, i.e. int Profit = 40; works just fine...

    Thanx
    JM​
    Last edited by johnMoss; 07-23-2023, 01:49 PM.

    #2
    Hello JM,

    Thanks for your post.

    Are you calling SetProfitTarget() in State.Configure?

    Or, are you calling SetProfitTarget() in OnBarUpdate()?

    If you are trying to use dynamic values for SetProfitTarget(), you should do your calculations in OnBarUpdate() and call SetProfitTarget() in OnBarUpdate() one line above your Entry order method.

    For example:

    //OnBarUpdate()

    double bodySize = Math.Abs(Open[0] - Close[0]);
    double bodySize1 = Math.Round(bodySize);
    int Profit = System.Convert.ToInt32(bodySize1);

    if (condition)
    {
    SetProfitTarget(
    "Long", CalculationMode.Ticks, Profit);
    EnterLong();
    }


    Prints should be added to the script one line after your calculations that print out the value of your calculations so that you can see how it is evaluating.

    For example:

    double bodySize = Math.Abs(Open[0] - Close[0]);
    Print("bodySize: " + bodySize);
    double bodySize1 = Math.Round(bodySize);
    Print("bodySize1: " + bodySize1);
    int Profit = System.Convert.ToInt32(bodySize1);
    ​Print("Profit: " + Profit);


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

    See this help guide page for more information about SetProfitTarget(): https://ninjatrader.com/support/help...ofittarget.htm

    Here is a reference sample demonstrating using SetProfitTarget() in OnBarUpdate(): https://ninjatrader.com/support/help..._lo.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
      Hi Brandon,
      Thank you for taking weekend time to respond. Yes, all of the above is being called in OnBarUpdate. Our code matches so I'll look at the prints...

      Comment


        #4
        Prints:
        bodySize: 0
        bodySize1: 0
        Profit: 0
        bodySize: 0
        bodySize1: 0
        Profit: 0
        bodySize: 0.75
        bodySize1: 1
        Profit: 1
        bodySize: 0.5
        bodySize1: 0
        Profit: 0
        bodySize: 0.5
        bodySize1: 0
        Profit: 0
        bodySize: 0.25
        bodySize1: 0
        Profit: 0
        bodySize: 0.5
        bodySize1: 0
        Profit: 0
        bodySize: 0.25
        bodySize1: 0
        Profit: 0
        bodySize: 0.75
        bodySize1: 1
        Profit: 1
        bodySize: 0.25
        bodySize1: 0
        Profit: 0
        bodySize: 0.25
        bodySize1: 0
        Profit: 0
        bodySize: 0
        bodySize1: 0
        Profit: 0
        bodySize: 0.5
        bodySize1: 0​

        Changed Double bodysize to var bodySize..
        Running another output but no values so far

        Comment


          #5
          Hello JM,

          Thanks for your notes.

          bodySize should not be changed to var. You should use double bodySize in your script.

          The value argument of SetProfitTarget() is 'double value'.

          SetProfitTarget(CalculationMode mode, double value)

          SetProfitTarget(): https://ninjatrader.com/support/help...etprofittarget

          A whole number needs to be passed into the SetProfitTarget() method when using CalculationMode.Ticks. Decimals should not be used for the double value argument in the SetProfitTarget() method when CalculationMode is set to CalculationMode.Ticks.

          This means that you would need to make sure to pass in a whole number, such as 1, 5, 10, etc. for the double value argument in the SetProfitTarget() method.




          <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


            #6
            Thank you Brandon. On examination I'm seeing errors in my use of logic. I'm gonna dive into that next.

            Comment


              #7
              Fixed... All good thank you...
              JM

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by NullPointStrategies, Yesterday, 05:17 AM
              0 responses
              56 views
              0 likes
              Last Post NullPointStrategies  
              Started by argusthome, 03-08-2026, 10:06 AM
              0 responses
              132 views
              0 likes
              Last Post argusthome  
              Started by NabilKhattabi, 03-06-2026, 11:18 AM
              0 responses
              73 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
              49 views
              0 likes
              Last Post TheRealMorford  
              Working...
              X