Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Time since last bid/ask change

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

    Time since last bid/ask change

    Hello all,

    I'd like to keep a running exponential moving average of the time since the last bid/ask change.

    Would the following work?

    Code:
    double currentBid = 0;
    double currentAsk = 0;
    double priorBid = 0;
    double priorAsk = 0;
    double xmaBidImproveTime = 0;
    double xmaAskImproveTime = 0;
    double currentTime = 0;
    double priorTime = 0;
    
    protected override void OnMarketData(MarketDataEventArgs e)
    {
        if (e.MarketDataType == MarketDataType.Bid) currentBid = e.Price;
        else if (e.MarketDataType == MarketDataType.Ask) currentAsk = e.Price;
    
        currentTime = e.Time;
    
        if (currentBid != priorBid and currentAsk == priorAsk){
            if (xmaBidImproveTime == 0 && priorTime != 0) xmaBidImproveTime = currentTime - priorTime;
            else if (xmaBidImproveTime != 0 && priorTime != 0) xmaBidImproveTime = xmaBidImproveTime * .9 + (currentTime - priorTime) * .1;
    
            priorBid = currentBid;
            priorTime = currentTime;
    
        }
        else if (currentBid == priorBid and currentAsk != priorAsk){
            if (xmaAskImproveTime == 0 && priorTime != 0) xmaAskImproveTime = currentTime - priorTime;
            else if (xmaAskImproveTime != 0 && priorTime != 0) xmaAskImproveTime = xmaAskImproveTime * .9 + (currentTime - priorTime) * .1;
    
            priorAsk = currentAsk;
            priorTime = currentTime;
    
        }
    }
    Last edited by kaydgee; 10-05-2010, 04:44 PM.

    #2
    It seems you are applying just 2 weights for the current and last values? From a glance over it seems like it would be okay. You would need to test this on your end to ensure this to be the case though.
    Josh P.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    67 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    36 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    59 views
    1 like
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    62 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    53 views
    0 likes
    Last Post CarlTrading  
    Working...
    X