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

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.
    Brandon H.NinjaTrader Customer Service

    Comment


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

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Skifree, Today, 03:41 AM
      1 response
      2 views
      0 likes
      Last Post Skifree
      by Skifree
       
      Started by usazencort, Today, 01:16 AM
      0 responses
      1 view
      0 likes
      Last Post usazencort  
      Started by kaywai, 09-01-2023, 08:44 PM
      5 responses
      603 views
      0 likes
      Last Post NinjaTrader_Jason  
      Started by xiinteractive, 04-09-2024, 08:08 AM
      6 responses
      23 views
      0 likes
      Last Post xiinteractive  
      Started by Pattontje, Yesterday, 02:10 PM
      2 responses
      23 views
      0 likes
      Last Post Pattontje  
      Working...
      X