Загрузка...

Rolling Computation of Bid and Ask Prices in R

Learn how to perform rolling computations iteratively for bid and ask prices using R and the `dplyr` and `purrr` packages in this in-depth guide.
---
This video is based on the question https://stackoverflow.com/q/66787554/ asked by the user 'shoonya' ( https://stackoverflow.com/u/1471708/ ) and on the answer https://stackoverflow.com/a/66796202/ provided by the user 'AnilGoyal' ( https://stackoverflow.com/u/2884859/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.

Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Rolling computation of two simultaneous variables iteratively or rowwise, using three other given variables

Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/licensing
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/by-sa/4.0/ ) license.

If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Rolling Computation of Bid and Ask Prices in R

In data analysis, particularly in finance, rolling computations are crucial for maintaining up-to-date information about variable states based on historical data. This post will showcase a method for calculating two simultaneous variables, Bid and Ask, by iteratively leveraging three other variables. We will be using R’s powerful dplyr and purrr packages, which streamline these types of computations.

Understanding the Dataset

Before we dive into the computations, let’s take a look at our sample dataset named crass. It consists of the following columns:

WT_TRADE_PRICE: The price at which the trade occurs.

min and max: The minimum and maximum trade values for a given period.

Bid: The highest known price a buyer is willing to pay for a security.

Ask: The lowest known price a seller is willing to accept for a security.

Here's a glimpse of the crass dataset:

[[See Video to Reveal this Text or Code Snippet]]

The Problem Statement

The issue at hand is to fill the initial Bid and Ask values iteratively based on conditions reliant upon the other observed values in our dataset. Specifically, we want to assess the WT_TRADE_PRICE against the previous values of the Ask and the average of the Bid and Ask. The logic we will use is as follows:

If WT_TRADE_PRICE is greater than or equal to either the previous value of Ask or the average of Bid and Ask, we will update Ask to be the current row's max, while keeping the Bid unchanged.

Otherwise, we will set Bid to the min and keep Ask as the previous Ask value.

Pseudo Code Representation

Here’s how we can express the above logic in a pseudo code format:

[[See Video to Reveal this Text or Code Snippet]]

The Rolling Computation Solution

To accomplish the task in R, we utilize the purrr::accumulate function, which allows us to roll through the data. The strategy involves several steps:

Step 1: Reshape Data

Initially, we need to rearrange our data into row-wise tibbles, ensuring each column operates as a singular input. This can be done by creating a dummy column id.

Step 2: Nesting the Data

Using tidyr::nest_by(), we can prepare our data for processing:

[[See Video to Reveal this Text or Code Snippet]]

Step 3: Execute the Accumulate Function

Now, we can implement the accumulation logic. Here’s the crucial bit of code that fills in Bid and Ask based on the provided rules:

[[See Video to Reveal this Text or Code Snippet]]

Final Output

After running through these steps, the expected output should look similar to the crass tibble, with the Bid and Ask columns filled appropriately, as seen below:

[[See Video to Reveal this Text or Code Snippet]]

Summary

We’ve demonstrated an effective method for performing rolling computations of the Bid and Ask prices using R’s dplyr and purrr packages. By employing purrr::accumulate, combined with strategic data manipulation through dplyr and tidyr, we can perform this task iteratively without the need for traditional loops, providing a more efficient and cleaner solution.

Feel free to explore these techniques further and adapt them to your own datasets and analysis needs!

Видео Rolling Computation of Bid and Ask Prices in R канала vlogize
Яндекс.Метрика

На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.

Об использовании CookiesПринять