Announcement

Collapse
No announcement yet.

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.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Today, 05:17 AM
    0 responses
    44 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    126 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    65 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    42 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    46 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X