Click here to Login

How to Calculate and Display the Bid/Ask Spread on a Chart

Updated on 2014-03-29

A new function introduced in version 3.0.1 of QuantShare allows you to create custom functions/indicators based on bid/ask data.
This function cannot be accessed directly from the QuantShare programming language but we can use it in the custom functions tool to create new functions (C# based) that can be referenced later by the QuantShare programming language.

Calculate the Average Bid/Ask Spread

The indicator we are going to implement now, calculates the average ask price for a specific bar then subtracts it to the average bid price for that same bar. If you are displaying a one-minute chart then the average ask/bid is calculated based on all ask/bid updates that occurred during that bar.

- Select "Tools -> Create Function"
- Click on "Add" then type "AvgBidAskSpread"

In the formula editor, type the following code:

// Get the bid vector (history) for the current symbol
VectorCustomDouble bid = cFunctions.GetBidAskData(cFunctions.Symbol, "bid");
// Get the bid vector (history) for the current symbol
VectorCustomDouble ask = cFunctions.GetBidAskData(cFunctions.Symbol, "ask");
// Check if we have bid/ask data the current symbol
if(bid.Length > 0 && ask.Length > 0)
   // Loop through each bar (we can also replace result.Length by ask.Length or bid.Length)
   for(int i=0;i<result.Length;i++)
      // Initialize the average ask variable
      double avgAsk = 0;
      if(ask[i].Length > 0)
         // Loop thought each ask update that occurred within the bar i
         for(int j=0;j<ask[i].Length;j++)
            // Add the current ask to the average ask variable
            avgAsk += ask[i][j];
         // Calculate the average by dividing the sum by the number of ask updates
         avgAsk = avgAsk / ask[i].Length;
         // Set average ask to zero since there were no ask updates for bar i
         avgAsk = 0;
      // Perform the same logic for bid
      double avgBid = 0;
      if(bid[i].Length > 0)
         for(int j=0;j<bid[i].Length;j++)
            avgBid += bid[i][j];
         avgBid = avgBid / bid[i].Length;
         avgBid = 0;
      // Calculate spread and add it to result vector (this vector is returned by the function)
      result[i] = avgAsk- avgBid;

- The description of each instruction is included in the code.
- Bid/Ask updates are loaded from memory. QuantShare currently stores in memory the last 1000 tick/bid/ask updates. This means that only the bid/ask updates that occurred during that period will be included in the calculation.
- This indicator is available in the sharing server: Average Bid/Ask Spread

Plot the Bid/Ask Spread on a Chart

Let us plot the Bid/Ask spread on a chart now.

- Open a new chart
- Right click on it then select "Create New Pane"
- Right click on the new pane then select "Edit Formula"

Since the name of the custom function we created previously is "AvgBidAskSpread", we can reference it in QS programming language just by typing it. Example:

a = AvgBidAskSpread();
plot(a, "", colorGreen);

no comments (Log in)

QuantShare Blog
Search Posts

Recent Posts

Create Graphs using the Grid Tool
Posted 1298 days ago

Profile Graphs
Posted 1403 days ago

Previous Posts

How Does QuantShare Work?
Posted 3775 days ago

Troubleshooting a Trading System
Posted 3816 days ago

More Posts


Create an account
Affiliate Program
Contact Us
Trading Forum
How-to Lessons
About Us
Terms of Use

Copyright 2024
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Follow us on Google+
RSS Trading Items

Trading financial instruments, including foreign exchange on margin, carries a high level of risk and is not suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to invest in financial instruments or foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with trading and seek advice from an independent financial advisor if you have any doubts.