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

Get the entry execution order bar index from the execution time

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

    Get the entry execution order bar index from the execution time

    I would like to get the bar index from the execution order time. I used the following function but sometime it return the previous bar of the execution order bar on the live trading.

    I'm using 60 mins time frame.


    Please advise.

    int orderIndex = CurrentBars[0] - BarsArray[0].GetBar(execution.Time);
    Last edited by matrixxx; 06-11-2018, 09:44 AM.

    #2
    Hello matrixxx,

    Thanks for your post.

    You aren't far from reaching your goal, but there may be some indexing issues.

    BarsArray[0].GetBar(execution.Time) will give the bar index of the order execution, however, this would reference a "future bar" since we may see this execution before the iteration of the next bar and CurrentBar would not yet be updated. Thus, using CurrentBars[0] - BarsArray[0].GetBar(execution.Time) will often return a -1 for a barsago reference.

    You could set a bool when the the execution is seen and then get the appropriate reference in OnBarUpdate() or you could correct the reference in OnExecutionUpdate() for when the execution is seen before the OnBarUpdate() iteration.

    If the order gets submitted intra-bar using Calculate.OnPriceChange/Calculate.OnEachTick or using a secondary series, you may see instances where the primary bar may close before the execution is seen.

    You may use some code similar to what I have attached to better visualize these indexes:

    Code:
    protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity, 
    	Cbi.MarketPosition marketPosition, string orderId, DateTime time)
    {
    	
    	int orderIndex = BarsArray[0].GetBar(execution.Time);
    	Print("Exec index: " + orderIndex + " Bar: " + CurrentBars[0] + " Close: " + Close[0] + " Price: " + price);
    	Print("Exec barsago: " + (CurrentBars[0]-orderIndex) );
    	Print("Exec barsago corrected: " + (CurrentBars[0]-orderIndex+1) );
    }
    
    protected override void OnBarUpdate()
    {
    	if(Position.MarketPosition == MarketPosition.Flat)
    	{
    		EnterLong();
    		Print("Entry: " + CurrentBar);
    	}
    	else if (BarsSinceEntryExecution() > 3)
    	{
    		ExitLong();
    		Print("Exit: " + CurrentBar);
    	}
    	Print("   NewBar: " + CurrentBar + " Close: " + Close[0]);
    }
    Please let us know if we can be of further assistance.
    JimNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    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
    22 views
    0 likes
    Last Post xiinteractive  
    Started by Pattontje, Yesterday, 02:10 PM
    2 responses
    21 views
    0 likes
    Last Post Pattontje  
    Started by flybuzz, 04-21-2024, 04:07 PM
    17 responses
    230 views
    0 likes
    Last Post TradingLoss  
    Working...
    X