• 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

SetProfitTarget Not working

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

    SetProfitTarget Not working

    I can not make it work. this is my code:


    Code:
     protected override void Initialize()
            {		
    			SetStopLoss( CalculationMode.Percent, 0.08);
    			SetProfitTarget( CalculationMode.Percent, 0.03);	
    			
            }
    
    //Example code
     protected override void OnBarUpdate()
            {
    			
    			if( RSI( slowSMA, 3)[0] > 30 && RSI( slowSMA,3)[0]<70)
    			{			
    				if ( SMA(fastSMA)[0] > SMA(slowSMA)[0] )
    				{
    					EnterLong( 1);	
    				}
    				
    				if(SMA(fastSMA)[0] < SMA(slowSMA)[0] )
    				{
    					EnterShort( 1);						
    				
    				}
    			}			
            }
    As you can see in the next image it show 0.11%, it is over 0.03!!.

    #2
    Hello hectorzx,

    Thank you for writing in and welcome to the NinjaTrader Support Forum!

    When using CalculationMode.Percent, this will be placing the stop loss/profit target at a price that is the specified percentage of your entry.

    For example, let's say the strategy enters at 2038.

    3% of 2038 is 61.14.

    2038 + 61.14 is 2099.14. Rounding up to the ticksize, the profit target would be placed at 2099.25.

    To place the profit target as you have intended, you will want to use the CalculationMode of Price instead and set it to Position.AvgPrice * 1.0003.

    You will be unable to use Position.AvgPrice in Initialize(), so you will have to do this in OnBarUpdate() and ensure that you are in a position before doing so.

    Example:
    Code:
    protected override void OnBarUpdate()
    {
         if (Position.MarketPosition == MarketPosition.Long)
              SetProfitTarget(CalculationMode.Price, Position.AvgPrice * 1.0003);
    }
    This value was obtained by using the GetProfitLoss() method with PerformanceUnit.Percent: https://ninjatrader.com/support/help...profitloss.htm

    Please, let us know if we may be of further assistance.
    Last edited by NinjaTrader_ZacharyG; 06-24-2016, 11:48 AM.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      answer + issues on exiting on profit targets

      OP, for one, try using different code. Rather than calculate, set the profit target as a double:

      private double profit = 0; // setting it to $0
      private double profitTarget = 0.03; // that's 3% you're looking for

      THEN:

      profit = Position.AveragePrice + (profitTarget * Position.AveragePrice); // if your entry price was $10, profit will become $10.30

      However, if someone can then help me, as this code below should exit positions... The way I'm understanding... IF I'm long, AND most recent High is Above profit, it should exit. I understand I don't really need to check if it's above my average, if it's above profit, but I kept it in there, since it will exit the trade if it's above average price, but won't if it's above profit... So then I see the hypothetical price go above $10.40 for most recent high, and no exit. What's going on here?

      if (Position.MarketPosition == MarketPosition.Long && High[0] > profit )
      {
      Print(" PROFIT HIT, EXIT");
      ExitLong();
      }

      THE TRAIL STOP DOES work, and position DOES EXIT
      else if (Low[0] <= trailStop && Position.MarketPosition == MarketPosition.Long && Position.AveragePrice < trailStop)
      {
      Print(" TRAIL STOP HIT: " + trailStop + " " + Close[0] + " symbol " + Instrument.FullName);
      ExitLong();
      }

      profit and trailStop are both updated on every bar count

      Comment


        #4
        Hello lmatiukas,

        Have you ensured that the value of High[0] is, indeed, more than your profit variable?

        I would suggest placing a print before your condition to check the value of High[0] and profit.
        Zachary G.NinjaTrader Customer Service

        Comment


          #5
          good point. I've added print everywhere, and I do see that High[0] or Close[0] simply is usually below profit level... However, then I'm confused as to why it doesn't seem to update on every bar.

          I'm noticing that I'm only getting these print outs, whenever an entry OR and exit of the position took place, but I feel like I should get a data dump on EVERY tick, no? Here is the bar update script

          protected override void OnBarUpdate()
          {
          // Condition set 1 - Upward Price Breakout
          if (CurrentBar < BarsRequiredToTrade)
          return;
          // if not entered yet, first time's a charm
          if (PriceBreakoutRectangularBase(upwardBreakout,Volum eMultiple).Plot0[0] == 1 &&
          Position.MarketPosition == MarketPosition.Flat && smaFast[0] > smaSlow[0])
          {
          EnterLong();
          justEntered = true;
          }
          // now the position is long, and should update on bar update, every bar, based on time?
          if (Position.MarketPosition == MarketPosition.Long && justEntered == true)
          {
          if (trailingStopPct == 0)
          {
          trailStop = Position.AveragePrice - (chandelierRangeMultiple * ATR(atrPeriod)[0]);
          }
          else
          {
          trailStop = Position.AveragePrice - (trailingStopPct * Position.AveragePrice);
          }

          StopLoss = Position.AveragePrice - (stopLossPct * Position.AveragePrice);
          profit = Position.AveragePrice + (profitTarget * Position.AveragePrice);
          Print("profit target is" + profit);
          Print("close is" + Close[0]);
          Print("high is" + High[0]);
          Print("Low is" + Low[0]);
          Print("trail is" + trailStop);
          Print("stop is" + StopLoss);
          prevValue = Position.AveragePrice;

          justEntered = false;

          //Print(" TRAIL STOP TRACKING: " + trailStop + " symbol " + Instrument.FullName);
          }

          In one week of PCLN I only got one entry, and only got two output prints.

          Comment


            #6
            Hello lmatiukas,

            Your prints are within the if (Position.MarketPosition == MarketPosition.Long && justEntered == true) statement.

            The prints will not occur again unless both conditions are true; I see that you change justEntered to false within this if statement. The prints will not print again until justEntered becomes true again.
            Zachary G.NinjaTrader Customer Service

            Comment


              #7
              I see what you mean, took me a while to realize it. It only re-calculates that one single time, and then after it closes the position, as it satisfies the original condition. VALID POINT!

              I'll take re-calculation of trailstop, profit stop outside of this loop, at the beginning of onBarUpdate function, so I can get them new numbers every time!

              Thanks a bunch.. Still such a nightmare learning this fresh with no C coding, and trying to figure out how to incorporate custom indicators into strategies.

              Comment


                #8
                Hello lmatiukas,

                I would suggest taking a look at the Educational Resources page of the help guide for additional information for learning basic through intermediate skills in building custom indicators and strategies within NinjaTrader: https://ninjatrader.com/support/help..._resources.htm
                Last edited by NinjaTrader_ChelseaB; 02-28-2018, 02:19 PM.
                Zachary G.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Leeroy_Jenkins, Today, 06:54 AM
                0 responses
                3 views
                0 likes
                Last Post Leeroy_Jenkins  
                Started by rainbow47, Today, 06:10 AM
                0 responses
                8 views
                0 likes
                Last Post rainbow47  
                Started by stenberg98, Today, 04:53 AM
                2 responses
                7 views
                0 likes
                Last Post NinjaTrader_Marco  
                Started by wukefuha, Today, 03:20 AM
                1 response
                7 views
                0 likes
                Last Post NinjaTrader_Marco  
                Started by mbcmbc, Yesterday, 10:12 PM
                1 response
                10 views
                0 likes
                Last Post NinjaTrader_ChrisSch  
                Working...
                X