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

Profit calculation

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

    Profit calculation

    Question 1)

    If I have multiple entries:
    entry= sma>xyz {enter long}
    entry2 = sma > abc {enter long2}
    entry3 = sma > cvb {enter long3}

    when I

    Code:
    				SetProfitTarget(@"", CalculationMode.Percent, 0.01);
    will it calculate the profit based on average price of the above THREE entries? It seems it's calculating the profit based on individual entry price.

    Question 2)

    Instead of using
    Code:
    SetProfitTarget(@"", CalculationMode.Percent, 0);
    I created a condition:

    Code:
    Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) > 0.01
    ..if true, {exit}

    but the above also seem to behave erratically.

    My expectations are: if I have 3 different long entries, I want to calculate my profit target based on the average price of the three difference entries, then exit all at the same time, but instead, n8 seems to exit all of them at different times

    #2
    Hello calhawk01,

    Thanks for the post.

    Using SetProfitTarget() with no signal name will apply the profit target to each existing order/position individualy based upon the current market price and each average entry price individualy. To calculate a profit percentage based on the average price of your strategy position, you would have to get the AveragePrice from the Position class, calculate a target percentage based on that price, then once you have that percentage, you would use SetProfitTarget() to apply that target to your position. Using a signal name is the only way to have direct control of each order, so when using SetProfitTarget() with a signal name, you can specify custom stops for each order if they need to be different.

    More information on the Position class here (used to get average strategy position):


    To debug your code and make sure everything is happenng the way you want, you can use Print() statements before and after entry/exit to make sure the calculations are producing expected results. Adding Print() statements can also help visualize the flow of control in your script. You can view the output of print statements via the NinjaScript output window (New>NinjaScript output)

    A short example:

    Code:
    OnBarUpdate(){
    
        
        
        Print("About to set profit target");
        if(myCondition){
            Print("Condition met for SetProfitTarget");
            SetProfitTarget(CalculationMode.Percent, myPercentage);
        }
    
        Print("About to exit long");
        if(Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]) > 0.01)
            Print("Condition met for Exit");
            Print(Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0]));
            ExitLong(3);
    
    }
    You can also use trace orders to give detailed information about orders and help you debug anything that might be going wrong at the time of order entry.

    Here is the help guide page on Trace Orders, you just have to set TraceOrders == true in State.SetDefaults to subscribe to trace orders.



    This forum post goes into more detail about trace orders:


    Please see this post that goes into more detail about debugging your code:



    If we may be of any further assistance, please let us know.
    Chris L.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by jclose, Today, 09:37 PM
    0 responses
    6 views
    0 likes
    Last Post jclose
    by jclose
     
    Started by WeyldFalcon, 08-07-2020, 06:13 AM
    10 responses
    1,414 views
    0 likes
    Last Post Traderontheroad  
    Started by firefoxforum12, Today, 08:53 PM
    0 responses
    11 views
    0 likes
    Last Post firefoxforum12  
    Started by stafe, Today, 08:34 PM
    0 responses
    11 views
    0 likes
    Last Post stafe
    by stafe
     
    Started by sastrades, 01-31-2024, 10:19 PM
    11 responses
    169 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Working...
    X