• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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 CriticalMind, 11-29-2015, 12:39 PM
    3 responses
    1,321 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by novel168, 06-14-2019, 09:20 PM
    1 response
    7 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by johnnybegoode, Yesterday, 08:00 PM
    3 responses
    8 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by digibob, Yesterday, 03:00 PM
    1 response
    6 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by yeshujbp, Yesterday, 02:50 PM
    1 response
    11 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Working...
    X