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

changing targets when averaging contracts

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

    changing targets when averaging contracts

    I'm trying to do the following:
    Enter long and set a target. No problem there. If I receive another entry condition before the target is hit, I want to add another contract but reduce the target. I can't figure out how to modify the target when >= 2 contracts are open. I tried
    Code:
    protected override void OnBarUpdate()
            {
                // Condition set 1
                if (Position.GetProfitLoss(Close[0], PerformanceUnit.Points) >= 10)
                {
                    ExitLong("Exit Long", "Long Entry");
                }
            }}
    but it doesn't seem to work. The open position never exits using this. Any thoughts how to achieve changing the target once >1 contracts are open?

    #2
    Tdschulz, to scale out of a position you would need to scale in first when using the Managed approach in NinjaScript, please take a look at this reference sample showing you how to achieve this - http://www.ninjatrader.com/support/f...ead.php?t=3751
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Thank you for the info but I'm not trying to scale out of the position. I will scale into a position but exit at once. I have no problem if I use the same target always by use "SetProfitTarget("", CalculationMode.Ticks, 10) in the initialize section. However, I want to use a large target if I only have one contract on and use a small target if I have more than one contract on. I tried putting "SetProfitTarget" command in the OnBarUpdate section but it still doesn't seem to be working. What method do you suggest to achieve this goal?

      Comment


        #4
        Thanks for the clarification, the reason it's not triggering then for you is this line here -

        if (Position.GetProfitLoss(Close[0], PerformanceUnit.Points) >= 10)

        Points would be taken literally as 10 full points, so that's different from your 10 ticks set, as that would for example equal 2.5 points on the Emini SP.

        When working with SetProfitTarget() you would want to set the target dynamically, correct - depending on for example what Position.Quantity you current manage via the strategy - http://www.ninjatrader.com/support/h...7/quantity.htm
        BertrandNinjaTrader Customer Service

        Comment


          #5
          I did notice that at some point last night and corrected it but still no success. Entries are fine, stop loss is fine, but can't get the targets working. this example is for instruments with .01 tick increments. I have the following:
          Code:
           protected override void Initialize()
                  {
          
                      SetStopLoss(stoploss, false);
          		    EntriesPerDirection = MaxContracts; 
              		EntryHandling = EntryHandling.AllEntries; 
          			
          
                      CalculateOnBarClose = true;
                  }
          
                  /// <summary>
                  /// Called on each bar update event (incoming tick)
                  /// </summary>
                  protected override void OnBarUpdate()
          	{
          			
          			if (ToTime(Time[0]) >= (starttime) && ToTime(Time[0]) < stoptime)
          				Print(Time.ToString()+"Time= "+Time[0]);
          			Print(Time.ToString()+"MarketPosition= "+Position.MarketPosition);
          		
                      // Condition set 1
          		
          			if (ToTime(Time[0]) >= (starttime) && ToTime(Time[0]) < (stoptime)) 
          			if (Position.MarketPosition == MarketPosition.Flat)
          				
          		
          				if (....................
          					
                      {
                          EnterLong(1, "LongEntry");
          		
                      }
          		
          			
          		//	Logic for adding when already in a long position
          			if (Position.MarketPosition == MarketPosition.Long)
          				if (...................
          				
                      {
                          EnterLong(1, "Buy");
          		//		SetProfitTarget("", CalculationMode.Ticks, Target2);
                      }
          
          		
                      // Condition set 2
          			
          				
          				if (ToTime(Time[0]) >= (starttime) && ToTime(Time[0]) < (stoptime))
          			if (Position.MarketPosition == MarketPosition.Flat)
          			
          					if (.............
          						
                      {
                          EnterShort(1, "ShortEntry");
          		
                      }
          			
          		
          			
          			if (Position.MarketPosition == MarketPosition.Short)
                                  if ................
          	
          					
          //					
                      {
                          EnterShort(1, "Sell");
          	
                      }
                  	
          
          			if ((Position.GetProfitLoss(Close[0], PerformanceUnit.Points) >= .2)
                          && Position.Quantity == 1)
                      {
                          ExitLong("LongTarget", "");
          		
                      }
          			if ((Position.GetProfitLoss(Close[0], PerformanceUnit.Points) >= .1)
                          && Position.Quantity >= 2)
                      {
                          ExitLong("LongTarget2", "");
          			
                      }
          			if ((Position.GetProfitLoss(Close[0], PerformanceUnit.Points) >= .2)
                          && Position.Quantity == -1)
                      {
                    
          				ExitShort("ShortTarget","");
                      }
          			if ((Position.GetProfitLoss(Close[0], PerformanceUnit.Points) >= .1)
                          && Position.Quantity < -1)
                      {
                     
          				ExitShort("ShortTarget2","");
                      }

          Comment


            #6
            I would suggest running with TraceOrders - http://www.ninjatrader.com/support/f...ead.php?t=3627 to see if you're getting any ignored orders here because your signal naming tags would be off.
            BertrandNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by warpinator, 05-16-2024, 10:44 AM
            10 responses
            54 views
            0 likes
            Last Post NinjaTrader_Gaby  
            Started by Austiner87, Today, 11:19 AM
            3 responses
            9 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by ETFVoyageur, Today, 10:34 AM
            5 responses
            17 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by rodriguesdanny, 05-15-2024, 12:06 PM
            7 responses
            31 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by StefanA, Today, 11:24 AM
            6 responses
            15 views
            0 likes
            Last Post StefanA
            by StefanA
             
            Working...
            X