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

multiple entries

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

    multiple entries

    hi,

    i have 3 different entries at the same time. they all have three different targets.

    Code:
    if (Position.MarketPosition == MarketPosition.Flat && LinRegSlope1[0] < Entry)
    			{
    			EnterLongLimit(Convert.ToInt32(fxToBuy), GetCurrentBid(0), "Target1");		
    			EnterLongLimit(Convert.ToInt32(fxToBuy), GetCurrentBid(0), "Target2");		
    			EnterLongLimit(Convert.ToInt32(fxToBuy), GetCurrentBid(0), "Target3");						
    			CandleOutlineBrush = Brushes.CornflowerBlue;
    			}
    then i have a condition, where if the realized profit on "last" trade was greater than one, and now the current unrealized p&L is x% of the last profitable trade -> exit all trades

    Code:
    	if (previousTradeCash > 0
    					&& Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0])/previousTradeCash <= -riskProfits)
    					
    				{
    				ExitLong(Convert.ToInt32(Position.Quantity), "ProfitProtect1", "Target1");
    				ExitLong(Convert.ToInt32(Position.Quantity), "ProfitProtect2", "Target2");
    				ExitLong(Convert.ToInt32(Position.Quantity), "ProfitProtect3", "Target3");
    				}
    The problem I am having is that, the above codes are viewing each individual entry as a separate trade. how can I code it so that target1+target2+target3 are all seen as "last trade.". i could look at the total realized P&L of the last three trades but that doesnt work b/c there are scenarios where target1 can be realized and target2 and 3 are still live.

    the reason why i needed to give each entry a unique name was because i have multiple profit targets:

    Code:
    			SetProfitTarget("Target1", CalculationMode.Ticks, Target1*10);
    				SetProfitTarget("Target2", CalculationMode.Ticks, Target2*10);
    				SetProfitTarget("Target3", CalculationMode.Ticks, Target3*10);
    				SetStopLoss(CalculationMode.Ticks, ST*10);
    is there a way to either)

    1) have same entry name and shed that quantity at multiple targets?
    2) tell n8 that target1, target2 and target3 are all "one" trade?
    3) basically I want n8 to be able to recognize that target1,target2,target 3 are all the same name

    thanks

    #2
    oh and i already tried this: doesnt work or i'm not coding it correctly:



    Code:
    		Order Target1Entry = null;
    		Order Target2Entry = null;
    		Order Target3Entry = null;
    STOPLOSS/PROFIT

    Code:
    			else if (State == State.Configure)
    			{
    				AddDataSeries(Data.BarsPeriodType.Day, 1);
    				AddDataSeries(Data.BarsPeriodType.Week, 1);
    				SetProfitTarget("Entry1", CalculationMode.Ticks, Target1*10);
    				SetProfitTarget("Entry2", CalculationMode.Ticks, Target2*10);
    				SetProfitTarget("Entry3", CalculationMode.Ticks, Target3*10);
    				SetStopLoss(CalculationMode.Ticks, ST*10);
    			}

    ONBARUPDATE:

    Code:
    if (Position.MarketPosition == MarketPosition.Flat && LinRegSlope1[0] < Entry
    				&& previousTradereturn >= 0
    				&& Target1Entry == null
    				&& Target2Entry == null
    				&& Target3Entry == null)
    			{
    			Target1Entry=EnterLongLimit(Convert.ToInt32(fxToBuy), GetCurrentBid(0), "Entry1");		
    			Target2Entry=EnterLongLimit(Convert.ToInt32(fxToBuy), GetCurrentBid(0), "Entry2");		
    			Target3Entry=EnterLongLimit(Convert.ToInt32(fxToBuy), GetCurrentBid(0), "Entry3");						
    			CandleOutlineBrush = Brushes.CornflowerBlue;
    			}
    THEN:

    Code:
    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
    {
      // Assign entryOrder in OnOrderUpdate() to ensure the assignment occurs when expected.
      // This is more reliable than assigning Order objects in OnBarUpdate, as the assignment is not guaranteed to be complete if it is referenced immediately        after submitting
      if (order.Name == "Entry1" 
    	  && order.Name == "Entry2"
    	  && order.Name == "Entry3")
          Target1Entry = order;
          Target2Entry = order;
          Target3Entry = order;
      
     
      if (   Target1Entry != null 
    	  && Target2Entry != null
    	  && Target3Entry != null 
    	  && Target1Entry == order 
    	  && Target2Entry == order
    	  && Target3Entry == order
    	  && previousTradeCash > 0
    	  && Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0])/previousTradeCash <= -riskProfits)
      {
    				ExitLong(Convert.ToInt32(Position.Quantity), "ProfitProtect1", "Entry1");
    				ExitLong(Convert.ToInt32(Position.Quantity), "ProfitProtect2", "Entry2");
    				ExitLong(Convert.ToInt32(Position.Quantity), "ProfitProtect3", "Entry3");
              		Target1Entry = null;
              		Target2Entry = null;
              		Target3Entry = null;
    	  }
    }
    strategy analyzer:

    my profit target hits on the first day, and no other trade is ever taken. before I added the above execution stuff, strategy was generating 200+ trades.Either the above code is wrong, or the above code is correct by target1entry etc is not going to null state because profit is hit via SetProfit?

    Comment


      #3
      Code:
      				Trade lastTrade1 = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];
      				previousTrade1 = lastTrade1.ProfitPercent;
      				previousTradeCash1 = lastTrade1.ProfitCurrency;
      				tradename1 = lastTrade1.Entry.Name
      Solved my problem using the above. With it, I can access the entry name, profit of last trade and the trade before last trade and the trade before that trade.

      Cheers

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by f.saeidi, Today, 12:14 PM
      3 responses
      7 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by Russ Moreland, Today, 12:54 PM
      1 response
      3 views
      0 likes
      Last Post NinjaTrader_Erick  
      Started by philmg, Today, 12:55 PM
      1 response
      3 views
      0 likes
      Last Post NinjaTrader_ChristopherJ  
      Started by TradeForge, 04-19-2024, 02:09 AM
      2 responses
      31 views
      0 likes
      Last Post TradeForge  
      Started by aprilfool, 12-03-2022, 03:01 PM
      3 responses
      327 views
      0 likes
      Last Post NinjaTrader_Adrian  
      Working...
      X