Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Last position statistics

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

    Last position statistics

    Hi,
    I'm implementing a strategy via Ninjascript in NT8, which trades within OnBarUpdate function (EnterLong/Short, ExitLongShort). On closing of every trade I try to highlight the background of the closing bar according to the result of the trade (win or loss).
    Basically I need a callback if the MarketPosition goes flat - but I need access to last enter/exit price, net profit and so on.

    Don't really understand the concept behind OnPositionUpdate, OnOrderUpdate and OnExecutionUpdate. OnPositionUpdate reflects the current market position (long, short or flat) - but in flat case, the Position attributes are empty.
    OnExecutionUpdate seems to be only called if entering the market, but not exiting via ExitLong/Short calls.

    Could you please provide an example, where an ExitLong/Short call trigger a callback where the last position/trade stats can be accessed to set BackBrushAll.

    Thanks, Frank

    #2
    Hello,

    Thank you for the question.

    The OnPositionUpdate method I believe is what you would need to use for what you have described, for flat you would need to store the prior order information as the flat position would not reflect those orders.

    The exit methods would call the OnExecutionUpdate override just like an entry would, I will provide a simple example below that uses Prints to display this information. This example places a single long entry, and then exits that entry on the next bar. The prints show the various overrides and what they report for this simple test. Also I have demonstrated using the Order objects to Store order information.

    Code:
    private bool doOnce;
    private Order entryOrder;
    private Order exitOrder;
    protected override void OnBarUpdate()
    {
    	if(State == State.Historical || doOnce) return;
    	if(Position.MarketPosition == MarketPosition.Flat)
    	{
    		entryOrder = EnterLong();
    	}
    	if(Position.MarketPosition == MarketPosition.Long)
    	{
    		exitOrder = ExitLong();
    		doOnce = true;
    	}
    }
    
    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
    {
    	if(execution.Order == null) return;
    	if(execution.Order == entryOrder)
    	{
    		//store any values from the execution that is needed:
    		Print(execution.Order.AverageFillPrice);
    	}
    	else if(execution.Order == exitOrder)
    	{
    		//store any values from the execution that is needed:
    		Print(execution.Order.AverageFillPrice);
    	}
            Print("OnExecutionUpdate " + marketPosition);
    }
    
    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
    {
    	Print("OnOrderUpdate" + order.ToString());
    }
    
    protected override void OnPositionUpdate(Position position, double averagePrice, int quantity, MarketPosition marketPosition)
    {
    	
    	Print("OnPositionUpdate " + marketPosition);
    }
    The result of this logic was the following output:
    2096.75
    OnExecutionUpdate Long
    OnPositionUpdate Long

    2096.5
    OnExecutionUpdate Short
    OnPositionUpdate Flat
    In this case we can see OnPositionUpdate reflects the position the strategy has taken based on the orders that were completed. OnExecutionUpdate would not directly match the Position update although you are referencing the marketPosition value, this was the market position of the order executed, not the virtual position in the strategy.

    Regarding accessing the last Entry and Exit prices, depending on the script and its complexity you could do this in multiple ways. One way would be to store variables when you submit the orders. I have included this in the above sample showing the use of the Order objects. They are used in OnExecutionUpdate to know when the order has Filled, you could then complete logic based on that event.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi Jesse,

      thanks for your response. What if I'm not using ExitLong/Short calls and instead only use Entry calls. In this case I have no exitorder object. Just asking, because in my opinion the whole thing is too complicated.
      I want to access an historic order, e.g. in OnPositionUpdate, detecting marketposition==flat, then what? How can I access historical (specifically the last) trade. I'd like to access net profit, entry/exit dates and so on.

      Thanks, Frank

      Comment


        #4
        How can I access historical (specifically the last) trade.
        There's an example here
        Code:
         if (SystemPerformance.AllTrades.Count > 1)
          {
              Trade [COLOR="DarkGreen"]lastTrade [/COLOR]= SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by judysamnt7, 03-13-2023, 09:11 AM
        4 responses
        59 views
        0 likes
        Last Post DynamicTest  
        Started by ScottWalsh, Today, 06:52 PM
        4 responses
        36 views
        0 likes
        Last Post ScottWalsh  
        Started by olisav57, Today, 07:39 PM
        0 responses
        7 views
        0 likes
        Last Post olisav57  
        Started by trilliantrader, Today, 03:01 PM
        2 responses
        22 views
        0 likes
        Last Post helpwanted  
        Started by cre8able, Today, 07:24 PM
        0 responses
        10 views
        0 likes
        Last Post cre8able  
        Working...
        X