Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 794981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:24:14+00:00 2026-05-14T22:24:14+00:00

I would like to create a function for On Balance Volume (SQL Function). This

  • 0

I would like to create a function for On Balance Volume (SQL Function).
This is too complex of a calculation for met to figure out but here is the outline of the User Defined Table Function. If someone could help me to fill in the blanks I would appreciate it.
Craig

   CREATE FUNCTION [dbo].[GetStdDev3] (@TKR VARCHAR(10))
   RETURNS @results TABLE (
    dayno   SMALLINT IDENTITY(1,1) PRIMARY KEY
    , [date]  DATETIME
    , [obv] FLOAT
    )
    AS BEGIN

    DECLARE @rowcount SMALLINT
     INSERT @results ([date], [obv])

// CREATE A FUNCTION FOR ON BALANCE VOLUME
// On Balance Volume is the Summ of Volume for Total Periods
// OBV = 1000 at Period = 0
// OBV = OBV Previous + Previous Volume if Close > Previous Close
// OBV = OBV Previous - Previous Volume if Close < Previous Close
//  OBV = OBV Previous if Close = Previous Close

//  The actual Value of OBV is not important so to keep the ratio low we reduce the 
// Total Value of Tickers by 1/10th or 1/100th
// For Value of Volume = Volume * .01 if Volume < 999
// For Value of Volume = Volume * .001 If Volume >= 999
    FROM Tickers

   RETURN

    END

This is the Tickers table

 [dbo].[Tickers](
 [ticker] [varchar](10) NULL,
 [date] [datetime] NULL,
 [high] [float] NULL,
 [low] [float] NULL,
 [open] [float] NULL,
 [close] [float] NULL,
 [volume] [float] NULL,
 [time] [datetime] NULL,
 [change] [float] NULL
 ) 

Here is an example of the data

   ticker  date  close  volume 
   pzi:  5-10-10  10.94    805 
   pzi;  5-11-10  11.06    444 
   pzi:  5-12-10  11.42    236 
   pzi:  5-13-10  11.3    635 
   pzi:  5-14-10  11    316 

   date  obv 
   5-10  996.38 
   5-11  996.82 
   5-12  997.06 
   5-13  996.42 
   5-14  996.11 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:24:15+00:00Added an answer on May 14, 2026 at 10:24 pm

    Here’s a working inline table valued function (most efficient for the optimizer):

    CREATE FUNCTION [dbo].[GetStdDev3] (@TKR VARCHAR(10))
        RETURNS TABLE
        AS RETURN (
        WITH    Y AS ( SELECT   *
                       ,OBV_Change = ISNULL(SIGN(currclose - prevclose)
                                            * volume, 1000)
               FROM     ( SELECT    curr.date
                                   ,curr.[CLOSE] AS currclose
                                   ,prev.[CLOSE] AS prevclose
                                   ,curr.volume
                          FROM      Tickers AS curr
                          LEFT JOIN Tickers AS prev
                                    ON prev.ticker = @TKR
                                       AND prev.date = ( SELECT MAX(date)
                                                         FROM   Tickers
                                                         WHERE  ticker = @TKR
                                                                AND date < curr.date
                                                       )
                          WHERE     curr.ticker = @TKR
                        ) AS X
             )
    SELECT  y1.date
           ,SUM(y2.OBV_Change) AS OBV
           ,ROW_NUMBER() OVER(ORDER BY y1.date) AS dayno
    FROM    Y AS y1
    LEFT JOIN Y AS y2
            ON y2.date <= y1.date
    GROUP BY y1.date
    )
    

    I wasn’t sure about the normalization – I left that out – adding it in might need you to make this in to a multi-statement TVF.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 392k
  • Answers 392k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you want to just notify the user about the… May 15, 2026 at 1:47 am
  • Editorial Team
    Editorial Team added an answer What about something like this: javascript:function f(){return 42}; var r… May 15, 2026 at 1:47 am
  • Editorial Team
    Editorial Team added an answer You need to commit (and close) the connection (and statement)… May 15, 2026 at 1:47 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.