Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to code and alert trades above ask or below bid

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

    How to code and alert trades above ask or below bid

    I have been trying to create a condition that will alert when a trade occurs above the ask or below the bid. I guess I don't have a full understanding of MarketDataType usage.

    The idea is to increment a variable when there are 2 trades above the ask or below the bid and increment a second variable if there are 8 or 10 trades above the ask or below the bid.

    I know I need to use MarketDataType.Ask and MarketDataType.Last and e.Price but what I have put together so far is not working.

    I don't mind using COBC and just DrawText and mark the bar AA for AboveAsk or BB for BelowBid. This way I can incorporate in an existing strategy that uses COBC. If I have to create a separate strategy where COBC is false, I'm ok with that too and actually I probably should do this because I want to know right away when this happens.

    Here is the last code I tried but it doesn't work, any help with this would be most appreciated !

    /* The below condition is not working, figure it out
    protected override void OnMarketData(MarketDataEventArgs e)
    {
    if (e.MarketDataType == MarketDataType.Ask)
    {
    CurrentAsk = e.Price;
    }

    if (e.MarketDataType == MarketDataType.Last && Close[0] > CurrentAsk)
    {
    TradeAboveAsk++;
    }

    if (e.MarketDataType == MarketDataType.Last && e.Price < GetCurrentBid())
    {
    TradeBelowBid++;
    }


    }
    */

    #2
    gdavis74, it looks like items are mixed up here in your code. The Close price updating is tied solely to OnBarUpdate(), same would go for the GetCurrentBid / GetCurrentAsk - since there's no guaranteed sequence for OnBarUpdate vs OnMarketData calls (called whenever we get the event to do so) - I would work in either method with your logic however not in both concurrently as you've attempted.

    Comment


      #3
      I want guaranteed sequence so OnMarketData() is where this code needs to be. So how do I code that the last trade was above the ask?

      OnMarketData() is based on each incoming event, right? So does this mean that if the best ask price goes up, for instance, and I want to detect that, I would use if (...MarketDataType.Ask)?

      Any tips on how to program this and also understand it would help. Is there a video that describes more about how to use OnMarketData()?

      thanks

      Comment


        #4
        Correct, OnMarketData() is called for each change in Level1 data. You need to filter which change / event you want to listen to with your code, that's what the if (e.MarketDataType) for, you see which event called OnMarketData() and could act then.

        To compare the prices, you would need to store the last known trade then if the last update calls OnMarketData(), it's your tracking variable you keep as reference and updated.

        This means you can then compare to the e.Price you get when the bid / ask updates are coming in and calling your logic.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        669 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        378 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        111 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        575 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        580 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X