• 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 XimoTrader, Today, 12:57 PM
    1 response
    12 views
    0 likes
    Last Post NinjaTrader_PatrickG  
    Started by FxInception, Today, 12:27 PM
    2 responses
    12 views
    0 likes
    Last Post FxInception  
    Started by siroya, Today, 10:34 AM
    1 response
    20 views
    0 likes
    Last Post NinjaTrader_ShawnB  
    Started by Leeroy_Jenkins, Today, 11:22 AM
    2 responses
    12 views
    0 likes
    Last Post Leeroy_Jenkins  
    Started by RedDuke, Today, 10:27 AM
    2 responses
    15 views
    0 likes
    Last Post RedDuke
    by RedDuke
     
    Working...
    X