Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Long Entry Closes Immediately at 1-Tick Loss & Entry Price 0

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

    Long Entry Closes Immediately at 1-Tick Loss & Entry Price 0

    Hi everyone,

    I am fairly new to coding and currently trying to work on this strategy. I'm encountering an issue with my NinjaTrader strategy where long positions I enter are immediately closing at a 1-tick loss. Additionally, the entry price for these positions is being returned as 0.

    ​protected override void OnBarUpdate() {
    if (CurrentBar < BarsRequiredToTrade)
    return;
    if ((Times[0][0].TimeOfDay > new TimeSpan(6, 40, 0))
    && (Times[0][0].TimeOfDay < new TimeSpan(11, 30, 0))
    && (slowEMA1[0] > slowEMA2[0])
    && (CrossAbove(fastEMA1, fastEMA2, 1)))
    {
    EnterLong();

    entryPrice = Position.AveragePrice;
    atr1 = ATR(Close, 14);
    atrPrice = (atr1[0] * 1.5);
    var atrStop = Convert.ToInt32(entryPrice - atrPrice);
    var atrTarget = Convert.ToInt32(entryPrice + atrPrice);


    SetProfitTarget(CalculationMode.Price, atrTarget);
    SetStopLoss(CalculationMode.Price, atrStop);

    Print("Long Entry Triggered");
    Print("Entry Price: " + entryPrice);
    Print("Profit Target: " + atrTarget);
    Print("Stop Loss: " + atrStop);
    Print("ATR Price: " + atrPrice);
    Print("ATR Value: " + atr1);
    }
    }​

    I've verified that the strategy fulfills the entry conditions and calls the EnterLong() function. I've also checked for errors in stop-loss and take-profit calculations within the OnBarUpdate.

    Here is what was outputted from the print statements:

    Long Entry Triggered
    Entry Price: 0
    Profit Target: 1
    Stop Loss: -1
    ATR Price: 1.41738909800935
    ATR Value: ATR(MES JUN24 (1 Minute),14)​

    I am currently running Version 8.1.2.1 of NinjaTrader, and I have tried backtesting this using the playback option in NinjaTrader.

    Any insight and help would be greatly appreciated!

    #2
    Hello Snubsy,

    Thanks for your post and welcome to the Forums!

    Set methods should be called before the Entry order method as noted in the help guide documentation linked below.

    From the help guides:

    "Since they are submitted upon receiving an execution, the Set method should be called prior to submitting the associated entry order to ensure an initial level is set."

    SetStopLoss(): https://ninjatrader.com/support/help...etstoploss.htm
    SetProfitTarget(): https://ninjatrader.com/support/help...ofittarget.htm

    Further, if you are calling Set methods dynamically in OnBarUpdate(), you should always reset the stop loss/ profit target price/offset value when your strategy is flat otherwise, the last price/offset value set will be used to generate your stop loss order on your next open position. This is also noted in the help guide documentation linked above.

    Here is a reference sample demonstrating resetting the stop loss/profit target price/offset when the strategy is flat: https://ninjatrader.com/support/help...of_stop_lo.htm

    You should print out the Entry price after the Entry order has been filled. If the order has not had time to fill yet after being submitted then it could return an AveragePrice value of 0. Note that it will take some time for the entry order to be Submitted, Accepted, Working, Filled after it has been submitted.

    In your script, check if you are in a long position using Position.MarketPosition == MarketPosition.Long and then print out the AveragePrice of the Entry order so that it has had time to fill before accessing that value.

    Position.MarketPosition: https://ninjatrader.com/support/help...etposition.htm

    You might want to consider using Exit methods, such as ExitLongLimit() and ExitLongStopMarket(), for the stops/targets in your script instead of Set methods if you want to calculate a specific value for the stops/targets based on the Entry order

    Please let us know if we may assist further on this topic.
    Last edited by NinjaTrader_BrandonH; 04-03-2024, 07:18 AM.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Thank you for your help! I was able to resolve the issue!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Retsdon, Today, 02:23 AM
      1 response
      12 views
      0 likes
      Last Post NinjaTrader_LuisH  
      Started by Pa_Baz, 12-14-2024, 09:40 PM
      27 responses
      380 views
      1 like
      Last Post backtester831  
      Started by Marty125, Today, 07:00 AM
      1 response
      9 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by meatskin, Yesterday, 03:07 PM
      1 response
      26 views
      0 likes
      Last Post NinjaTrader_LuisH  
      Started by crystalet, 12-06-2022, 06:59 AM
      3 responses
      55 views
      1 like
      Last Post NinjaTrader_Erick  
      Working...
      X