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


Having multiple 'Take Profit' based off of PnL - Just can't get it to work reliably..

  • Filter
  • Time
  • Show
Clear All
new posts

    Having multiple 'Take Profit' based off of PnL - Just can't get it to work reliably..

    Hi, what I need a little help with is a strategy (Scripted not Builder).

    I'm using MTF with the main Data being 15 Mins and the Secondary being 1 Tick, Unrealized Account PnL is updated in 'BarsInProgress == 1' so at a 1 tick resolution. This is looking for an Unrealized PnL of $100+ then when that is seen to have the strategy 'bank' that $100 by exiting the order and then put a Market order back in.

    Normal order entry and exit is conducted under the 15 Min time frame and works as it should with a fixed SL and a coded TP point (If Position Long && other criteria then exit) and the same for short, this all works as expected. (Except I've not hit an SL yet to make sure that part works).

    Where my issue lies is with the orders at the +$100 PnL mark, I've scripted to check order direction, check PnL, and if conditions are met then to Exit DataSeries[0]'s Order and place a new order and new SL, this is where I am running into trouble.

    Exiting the original order seems to go OK, but the new order doesn't get entered...
    Sometimes the PnL keeps running, is this Position PnL or is it from when the Strategy is first activated?
    Sometimes the PnL just sits at zero, regardless of what the actual PnL is, restarting the Strategy TWICE is needed to fix this.
    And once the new order is placed, I want it to repeat, EG +$100 Exit, Place Order, + another $100 Exit, Place Order Etc and repeat until either the SL is hit, OR the original closing criteria is met.

    This leads me to a couple more questions if I submit a Market Order for DataSeries[0] from DataSeries[1], will it wait up to 15 minutes until the next bar close or will it be submitted when called? Would it better to just submit the subsequent orders on DataSeries[1] and just set a flag so it knows where to ExitOrders from? Is this even an issue if the orders are from the same strategy?

    This is what I have so far for the subsequent order handling... Any help or ideas would be very much appreciated...

    Thanks in advance, Paul

    edit: 'earlyProfit' is just a bool flag which in this case is always set true, 'contractQty' is a fixed number.

    else if (BarsInProgress == 1)
    if ((Position.MarketPosition == MarketPosition.Long) && (earlyProfit == true) && (PositionAccount.GetUnrealizedProfitLoss(Performan ceUnit.Currency) > 100.00))
    ExitLong(0, contractQty,"MR0NEW_LONG", "MR0NEW_LONG");
    SetStopLoss("MR0NEW_LONG", CalculationMode.Currency, StopLoss, false);
    EnterLong(contractQty, "MR0NEW_LONG");
    if ((Position.MarketPosition == MarketPosition.Short) && (earlyProfit == true) && (PositionAccount.GetUnrealizedProfitLoss(Performan ceUnit.Currency) > 100.00))
    ExitShort(0, contractQty, "MR0NEW_SHORT", "MR0NEW_SHORT");
    SetStopLoss("MR0NEW_SHORT", CalculationMode.Currency, StopLoss, false);
    EnterShort(contractQty, "MR0NEW_SHORT");
    Print(" MR0NEW: " + PositionAccount.GetUnrealizedProfitLoss(Performanc eUnit.Currency));

    Hello PaulJC,

    Thanks for your post.

    There are a few things we should clear up before addressing additional questions.

    1. If you are adding a single tick data series for intrabar granularity, it is best to have all orders submitted to this data series.
    2. Mixing use of Set methods between different BarsInProgress may lead to unexpected behavior
    3. Set methods should also be submitted before the entry order is submitted to ensure that the initial stop/target are prepared for when the associated Entry order fills. (set methods prep NinjaTrader to submit target and stop orders once the entry order fills.)
    4. PositionAccount represents the account position, which is separate from the strategy position. Position represents the strategy position.

    Since you are working with a Multi Time Frame script, I may suggest the following approach:

    1. Ensure you are submitting orders to the single tick data series
    2. Since you are mixing logic that operates on BarsInProgress == 0 and BarsInProgress == 1, you could consider to use OnExecutionUpdate and use Exit methods for your target and stop so the orders are always sent to the single tick data series.

    Our backtesting with Intrabar granularity example demonstrates submitting orders to a single tick data series.

    Backtesting with Intrabar granularity -

    Our SampleOnOrderUpdate example demonstrates how you may use Exit methods in OnExecutionUpdate to submit target/stop orders. You can combine the concepts from the SampleIntrabarBacktest example so these orders are submitted to the single tick data series.

    SampleOnOrderUpdate -

    For more information on strategy vs. account positions and working with multi time frame scripts, please see our documentation below.

    Strategy Position vs. Account Position -

    Multi Time Frame and Instruments -

    I would suggest considering this approach and notes above. If you have any follow up questions, please don't hesitate to write back.
    JimNinjaTrader Customer Service


      Thanks for such a comprehensive reply! I shall have a read though everything and take a look at the examples and come back if i can't work it out


      Latest Posts


      Topics Statistics Last Post
      Started by terkk, Today, 01:16 PM
      1 response
      Last Post NinjaTrader_Heath  
      Started by TraderElegante, Today, 12:14 PM
      1 response
      Last Post NinjaTrader_PaulH  
      Started by dgee949, Today, 11:44 AM
      1 response
      Last Post NinjaTrader_JustinD  
      Started by NinjaTrader_Kate, Today, 12:01 PM
      0 responses
      Last Post NinjaTrader_Kate  
      Started by efeuvejota01, Today, 11:42 AM
      1 response
      Last Post NinjaTrader_ChrisL