Posts Tagged ‘F#’

This is part of a series on technical analysis indicators in F#, based on the multi-language TA-Lib.

Some base studies we’ll use in other studies

namespace Trading.Studies

module internal BaseStudies =

  let wmomLookback (period:int) = period

  let wmom coeffToday coeffLag period (data:float[]) =
    Study.checkPositiveIntMin1 period
    let lookbackTotal = wmomLookback period
    let startIdx = lookbackTotal
    let endIdx = data.Length - 1
    Study.lazyCompute startIdx endIdx (fun outLen ->
      let f =
        match coeffToday, coeffLag with
        | 0.0, 0.0 -> fun today -> 0.0
        | 0.0, 1.0 -> fun today -> -data.[today - lookbackTotal]
        | 0.0, beta -> fun today -> -beta * data.[today - lookbackTotal]
        | 1.0, 0.0 -> fun today -> data.[today]
        | 1.0, 1.0 -> fun today -> data.[today] - data.[today - lookbackTotal]
        | alpha, 0.0 -> fun today -> alpha * data.[today]
        | alpha, beta -> fun today -> alpha * data.[today] - beta * data.[today - lookbackTotal]

      Array.init outLen (fun i -> f (startIdx + i))
    )

  let momLookback period = wmomLookback period

  let mom period data = wmom  1.0 1.0 period data

  let rocLookback (period:int) = period

  let roc isBase100 divByZeroDefaultValue period (data:float[]) =
    Study.checkPositiveIntMin1 period
    let lookbackTotal = wmomLookback period
    let startIdx = lookbackTotal
    let endIdx = data.Length - 1
    Study.lazyCompute startIdx endIdx (fun outLen ->
      if isBase100 then
        Array.init outLen (fun i ->
          let today = startIdx + i
          let lagValue = today - period
          if data.[lagValue] <> 0.0 then
            100.0 * (data.[today] / data.[lagValue] - 1.0)
          else
            divByZeroDefaultValue
        )
      else
        Array.init outLen (fun i ->
          let today = startIdx + i
          let lagValue = today - period
          if data.[lagValue] <> 0.0 then
            data.[today] / data.[lagValue] - 1.0
          else
            divByZeroDefaultValue
        )
    )

The bitfield tracks which pieces of the transfer are already downloaded, and which aren’t. We therefore only need a structure which can track two states per value, which is why we use a bitarray library. In fact, we use only a subset of these functions, and rename a few to make them more expressive.

We also need to be able to translate the bitfield from/to the network where the high bit (right-most in network order) in the first byte corresponds to piece index 0.

Read more »

Once the Metainfo file can be read, the client ought to know which files compose the transfer, and how to connect to sources for the transfer. In this post, we will define the elements we need to perform the I/O operations, that is, to read data from (and write it to) the disk.
Read more »

This post describes the F# implementation of the implicit queue from Chris Okasaki’s “Purely functional data structures”.

Read more »

This post describes the F# implementation of the bootstrapped queue from Chris Okasaki’s “Purely functional data structures”.

Read more »

To exchange files, a BitTorrent client needs some information about the exchanged file(s). This information is contained within a file named the “metainfo” file.

More details are explaind in the Metainfo File Structure of the BitTorrent specification web page.

In short, the structure is a UTF-8 bencoded dictionary containing some required and optional key/value pairs.

Read more »

This post describes the F# implementation of the alternative binary random access list from Chris Okasaki’s “Purely functional data structures”.

Read more »

This post describes the F# implementation of the Bankers double-ended queue from Chris Okasaki’s “Purely functional data structures”.

Read more »

This post describes the F# implementation of the skew binomial heap from Chris Okasaki’s “Purely functional data structures”.

Read more »

Bencoding values for the BitTorrent protocol is explained in a Wikipedia article on Bencoding or in the more general BitTorrent protocol page.

Briefly, there are four types :

  • byte strings : [string length encoded in base ten ASCII]:[string data] ;
  • integers : i[integer encoded in base ten ASCII]e ;
  • lists : l[bencoded values]e ;
  • and dictionaries : d[bencoded string][bencoded element]e.

Byte strings can be strings represented as byte strings (such as the description of the exchanged data), as well as an array of bytes (such as a hash). Representing them with the .Net strings can thus lead to errors. Hence we shall keep the byte array representation.

Integers can be longs, hence we represent them with int64.

Read more »

This post describes the F# implementation of the real-time double-ended queue from Chris Okasaki’s “Purely functional data structures”.

Read more »

This post describes the F# implementation of the skew binary random access list from Chris Okasaki’s “Purely functional data structures”.

Read more »

This post describes the F# implementation of the binary random access list from Chris Okasaki’s “Purely functional data structures”.

Read more »

We had previously given a way to implement two-way traffic control on a strict basis using a function which looped constantly to see if any data was available o read/write, and which processed it accordingly.

The following approach is based on a per-request basis. That is, an actors asks for n tokens to read (or write), and the manager sends it an answer when the actor is allowed perform the action. In effect, if the actor asks for 100 bytes and only 50 bytes can be exchanged per second, it will get a green light only after two seconds have elapsed.

Read more »

The following code allows us to measure (and keep track of) the exchange rate (whether upload or download), and the time needed to exchange n additional bytes (assuming the rate is stable).

Read more »