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

SetProfitTarget not working in OnBarUpdate

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

    SetProfitTarget not working in OnBarUpdate

    I have the following bit of code that I am attempting to use to create an entry with associated trailing stops and profit target. The profit target never gets hit. Proving that to myself by commenting out the trailing stop.

    Is this the appropriate use of SetProfitTarget?

    And related, what is the impact to my backtest by setting an entry price for the EnterLongLimit that is less than current Close[0]? Is this making my results look better than reality, or is that limit actually not filled if the next bar does not have tick that would fill as in the real world?

    Code:
     
    void Buy()
    {
    	int size = 0;
    	double entryPrice = 0.0;
    			
    	if (this.TradingStocks) {
    		entryPrice = Close[0] - (_atr[0] * this.ATRMultiple);
    		size = (int) (this.InitialCapital / entryPrice);
    	}
    	else if (this.TradingFutures) {
    		entryPrice = Close[0] - (Math.Truncate(_atr[0] * this.ATRMultiple) / TickSize);
    		size = (int) (this.InitialCapital / 20000.0);
    	}
    			
    	if (size > 0) {
    		base.EnterLongLimit(size, entryPrice, "B"+base.CurrentBar);
    		base.SetProfitTarget("B"+base.CurrentBar, CalculationMode.Ticks, Math.Truncate((_atr[0] * this.ATRMultiple) / TickSize), false);
    		//base.ExitLongLimit(entryPrice + (TickSize * Math.Truncate(_atr[0] * this.ATRMultiple)), "B"+base.CurrentBar);
    		base.SetTrailStop("B"+base.CurrentBar, CalculationMode.Ticks, Math.Truncate((_atr[0] * this.ATRMultiple) / TickSize), false);
    	}
    }

    #2
    Hello RandyT,

    Thank you for your note.

    The EnterLongLimit call you have will be canceled if not filled on the bar it’s submitted. If you wanted it to remain working you could you use the syntax which includes isLiveUntilCancelled. See the last syntax at the following link,


    Are you seeing any errors on the log tab of the control center when running the strategy related to your profit target?

    I would suggest the following two links on debugging.

    Using prints to understand behavior:
    Dive into manipulating C# code from within an unlocked NinjaScript strategy using the NinjaScript Editor.NinjaTrader 7 is an award winning end to end online ...


    Link covering debugging which you may find helpful.
    Debugging: http://ninjatrader.com/support/forum...ead.php?t=3418

    I look forward to your reply.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Alan, thanks for your response.

      I believe that the issue was more caused by my miscalculation of entry, exit and target prices.

      Can you tell me how will the system respond when trading futures contracts with specific tick values if you give it an order for something that does not round to a specific tick value? For example, sending an order to buy ES at 2800.28 vs. on the tick boundary of 2800.25?

      Comment


        #4
        Hello RandyT,

        The price will be rounded to the nearest tick. It would be good practice to avoid this though, at the very least for debugging down the road.

        Please let us know if you need further assistance.
        Alan P.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Javierw.ok, Today, 04:12 PM
        0 responses
        4 views
        0 likes
        Last Post Javierw.ok  
        Started by timmbbo, Today, 08:59 AM
        2 responses
        10 views
        0 likes
        Last Post bltdavid  
        Started by alifarahani, Today, 09:40 AM
        6 responses
        40 views
        0 likes
        Last Post alifarahani  
        Started by Waxavi, Today, 02:10 AM
        1 response
        18 views
        0 likes
        Last Post NinjaTrader_LuisH  
        Started by Kaledus, Today, 01:29 PM
        5 responses
        15 views
        0 likes
        Last Post NinjaTrader_Jesse  
        Working...
        X