Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

AvgPrice

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

    AvgPrice

    I'm getting errors when using AvgPrice in my SetStopLoss. I have been testing the code for a few hours and it seems that AvgPrice works when it is access after a position opens AND the bar the position occured in is closed, but when during the first bar if I use it, I always get an error referring to my StopLoss or target indicating I'm my order was rejected because it was on the wrong side of the price. Here is a section of the code I'm using;

    {
    EnterShort(
    "Short");
    SetProfitTarget(
    "Short", CalculationMode.Price,Position.AvgPrice - (MyShortTarget*TickSize));
    SetStopLoss(
    "Short", CalculationMode.Price,Position.AvgPrice + (MyShortStop*TickSize), false);
    MyEntryPrice = Close[
    0];
    }

    I'm using this with CalculateOnBarClose = false. I have a section of code inside FirstTickOfBar and it will work from within there, but it appears to me that Position.AvgPrice is not assigned a value until the close of the first bar of the trade. Is this correct, or am I possibly doing something else wrong?

    Thanks

    Safetrading

    #2
    Hello,

    Postion.AvgPrice should be available as soon as your are in position. However if you're working in OnBarUpdate, the value will not update until the OnBarUpdate event has completed.

    If you would like to immediately obtain your AvgPrice, you can work in OnPositionUpdate() which is called as soon as the position has changed.

    MatthewNinjaTrader Product Management

    Comment


      #3
      Sorry, but I'm not an advanced programmer, would you please clarify what you said..."the value will not update until the OnBarUpdate event has completed". Does this mean that all of the instructions in the strategy must be cycled through once before the value is updatad? If so, does this also refer to any variables I have in the strategy?

      Comment


        #4
        If you're calling this inside OnBarUpdate and a block using FirstTickOfBar, the value will not update until the next bar has opened. Any variables which are in this block would not update until this occurs.

        If you would like to keep working in OBU, you can set these variables outside of the FirstTickOfBar block
        MatthewNinjaTrader Product Management

        Comment


          #5
          Assistance

          Originally posted by NinjaTrader_Matthew View Post
          If you're calling this inside OnBarUpdate and a block using FirstTickOfBar, the value will not update until the next bar has opened. Any variables which are in this block would not update until this occurs.

          If you would like to keep working in OBU, you can set these variables outside of the FirstTickOfBar block
          I'm using variables in OBU, with CacculateOnBarClose = false, outside of FirstTickOfBar, but they do not update until the next bar. These variables are tied to the Position.AvgPrice. Earlier you said the OBU event must be completed before AvgPrice will update. I'm trying to understand "what event" must be completed before the AvgPrice will update? When or where within my strategy can I access AvgPrice before the bar completes?

          Thanks,

          Safetrading

          Comment


            #6
            OnBarUpdate() is the event that is completed when a bar has been updated. Setting it to false should ensure that OnBarUpdate is ran once you have received an incoming tick. What I'm getting at is that AvgPrice should be available to you immediately when the order if filled - however there may be other blocks of code where are preventing this variable from updating when OBU has completed, such as using FirstTickOfBar, or perhaps some other statement.

            You will want to use plenty of print statements in your code to debug why this is not updating until the bar completes. Trying printing your variable in various sections of OBU and see where the values might be changing.

            You can also check for AvgPrice in other events such as OnPositionUpdate or OnExecution.
            MatthewNinjaTrader Product Management

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by rdtdale, Yesterday, 01:02 PM
            2 responses
            16 views
            0 likes
            Last Post rdtdale
            by rdtdale
             
            Started by TradeSaber, Today, 07:18 AM
            0 responses
            7 views
            0 likes
            Last Post TradeSaber  
            Started by PaulMohn, Today, 05:00 AM
            0 responses
            10 views
            0 likes
            Last Post PaulMohn  
            Started by ZenCortexAuCost, Today, 04:24 AM
            0 responses
            6 views
            0 likes
            Last Post ZenCortexAuCost  
            Started by ZenCortexAuCost, Today, 04:22 AM
            0 responses
            3 views
            0 likes
            Last Post ZenCortexAuCost  
            Working...
            X