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

Variable Profit Calculation Not Working

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

    Variable Profit Calculation Not Working

    I am trying to create a programmatic profit target based on some portion of the range between the recent highest high and lowest low,(maxprofit0 in the code below.

    The code compiles, and executes but the execution I get when running this code is essentially at the entry price. I think that means that my calculation of maxprofit0 is incorrect or the subsequent calculation of the actual profit target (profit0) is off. Can you give me any advice on what to fix in the example code below?
    Thanks
    DaveN

    Code:
    		protected override void OnBarUpdate()
    		{
    			if (CurrentBars[0] < DP_Period || CurrentBars[0] < V_Period)
    			return;
    			
    			double maxhigh0 = MAX(High, MM_Period)[0];
    			double minlow0 = MIN(Low, MM_Period)[0];
    			double maxprofit0 = (int)(maxhigh0 - minlow0);
    			double profit0 = Instrument.MasterInstrument.RoundToTickSize(maxprofit0 * Profit_Factor);
    			
    			if(Position.MarketPosition == MarketPosition.Long && BarsSinceEntryExecution() <= 2)
    				{
    				SetProfitTarget(@"BSDV_L", CalculationMode.Price, (Position.AveragePrice + profit0));
    				}
    			else if(Position.MarketPosition == MarketPosition.Short && BarsSinceEntryExecution() <= 2)
    				{
    				SetProfitTarget(@"BSDV_S", CalculationMode.Price, (Position.AveragePrice - profit0));
    				}

    #2
    Hello DaveN,

    I am not able to assist with correcting your logic or calculations, but I would recommend taking it one step at a time using prints.

    What is the value of the maxhigh0 and minlow0?
    When printing these are you getting expected values?

    What is the value of maxprofit0?
    Is this showing a correct difference between maxhigh0 and minlow0?

    What is the value of Profit_Factor?
    When multiplied with maxprofit0, does profit0 have an expected value?

    And finally, what is the Position.AveragePrice?
    What is the final value being set for the order (Position.AveragePrice + profit0)?

    To understand why the script is behaving as it is, 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 that print the values of every variable used for this calculation.

    This will print to the output window. 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 from both computers to your reply.

    Below I am providing a link to videos that demonstrate adding prints to a script to get further information about the behavior of the script.
    NT8 — http://www.screencast.com/t/TvUGSvfxdXt
    NT7 — https://www.youtube.com/watch?v=K8v_...tu.be&t=48m35s
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Rapine Heihei, 04-23-2024, 07:51 PM
    2 responses
    30 views
    0 likes
    Last Post Max238
    by Max238
     
    Started by Shansen, 08-30-2019, 10:18 PM
    24 responses
    943 views
    0 likes
    Last Post spwizard  
    Started by Max238, Today, 01:28 AM
    0 responses
    9 views
    0 likes
    Last Post Max238
    by Max238
     
    Started by rocketman7, Today, 01:00 AM
    0 responses
    7 views
    0 likes
    Last Post rocketman7  
    Started by wzgy0920, 04-20-2024, 06:09 PM
    2 responses
    28 views
    0 likes
    Last Post wzgy0920  
    Working...
    X