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

Position.AveragePrice Error on First Entry

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

    Position.AveragePrice Error on First Entry

    I have a very simple code to buy if a certain price pattern occurs. This strategy continues to buy each time the logic is true.

    The profit target is a function of what my average entry price is. The problem I am having is Ninja is not reading my average entry price correctly on the first trade, and then it messes up the trades thereafter

    I get a log error that states "Calculated target order price was smaller/equal 0. No target order placed",

    It seems to me, if there is only 1 entry made, ninjatrader's average price is not working correctly.

    Below is the code, and a picture of the chart. You can clear see in the chart the entry price and target don't print until the second trade, when they should start printing on the first. And you will see that the issue causes a problem on the second trade.

    Thanks.

    if (Close[6]<Close[7]
    && Close[5]<Close[6]
    && Close[4]<Close[5]
    && Close[3]<Close[4]
    && Close[2]<Close[3]
    && Close[1]>Close[2]
    && Close[0]>Close[1]
    && (BarsSinceExitExecution() >= 1 || BarsSinceExitExecution() == -1))



    {
    EnterLong(0,DefaultQuantity, "Long");
    longOrderBar = CurrentBars[0];
    pftpcnt = 25/100;

    averagePrice = Position.AveragePrice;
    takeProfit = (Position.AveragePrice+Position.AveragePrice/100*pftpcnt);

    SetProfitTarget("Long", CalculationMode.Price,takeProfit,true);
    Draw.Line(this,"averagePrice"+ CurrentBars[0].ToString(),false,0,averagePrice,-15,averagePrice,Brushes.Green,DashStyleHelper.Dash ,1);
    Draw.Line(this,"takeProfit"+ CurrentBars[0].ToString(),false,0,takeProfit,-15,takeProfit,Brushes.Red,DashStyleHelper.Dash,1);


    }
    Attached Files
    -EC_Chris-
    NinjaTrader Ecosystem Vendor - Emergent Cybernetics

    #2
    Hello EC_Chris,

    Thanks for opening the thread.

    It is generally advised to reset the Profit Target when it is in a flat position and then to update the Profit Target with SetProfitTarget() once you see that the order has been placed and you are in a position.

    From the help guide:
    Should you call this method to dynamically change the target price in the strategy OnBarUpdate() method, you should always reset the target price / offset value when your strategy is flat otherwise, the last price/offset value set will be used to generate your profit target order on your next open position
    At the time you call EnterLong(), it is not guaranteed that the order is filled at that exact moment. This would also mean that Position.AveragePrice would not be guaranteed until the order gets filled.

    As a solution, you could reset your Profit Target when in a flat position, and then when you are no longer in a flat position, you can update the profit target.

    Position.MarketPosition can be used to tell if you are long, short, or flat, and you can use BarsSinceEntryExecution() with signal name of your entry order to detect if you are in a position. Order objects may be used as well to track fill prices for orders to be used later in the strategy.

    I have provided links to our publicly available documentation on the subjects above.

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

    BarsSinceEntryExecution() - https://ninjatrader.com/support/help...yexecution.htm

    Order objects - https://ninjatrader.com/support/help...n-us/order.htm

    Another solution would be to create your own Profit Target and Stop Loss using orders submitted in OnExecutionUpdate(), so you can use the AverageFillPrice of the Order object that just filled. The SampleOnOrderUpdate() strategy provides a good demonstration on how to do this.

    SampleOnOrderUpdate strategy - https://ninjatrader.com/support/foru...ead.php?t=7499

    If there is anything else I can do to help out, please let me know.
    JimNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by habeebft, Today, 07:27 AM
    1 response
    12 views
    0 likes
    Last Post NinjaTrader_ChristopherS  
    Started by AveryFlynn, Today, 04:57 AM
    1 response
    12 views
    0 likes
    Last Post NinjaTrader_Erick  
    Started by Max238, Today, 01:28 AM
    4 responses
    37 views
    0 likes
    Last Post Max238
    by Max238
     
    Started by r68cervera, Today, 05:29 AM
    1 response
    10 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by geddyisodin, Today, 05:20 AM
    1 response
    14 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Working...
    X