Tuesday, May 22, 2012

sidenum function

sidenum function

The sidenum function appends a sign to a number based on a side. The name sidenum was chosen due to it's similarities with signum in spelling and behavior (dealing with signs).

Definition

The sidenum function of a side x and a number y is defined as follows:

note: 'bid' can be replaced with 'buy', and 'ask' can be replaced with 'sell' or 'offer'. The string representation or type of side varies by trading system.

Usage

The sidenum function appears in most trading applications, though not always named 'sidenum'. It can be helpful for both position related math and conserving screen real-estate by displaying 2 concepts (side & quantity) as 1 (a signed quantity).

Examples

Java
    public static int sidenum(String side, int quantity) {
        if (side.equals("ask"))
            return -quantity;
        return quantity;
    }
Clojure
    (defn sidenum [side quantity]
      (if (= side "ask")
        (- quantity)
        quantity))
Ruby
    def sidenum(side, quantity)
      if side == "ask"
        -quantity
      else
        quantity
      end
    end

4 comments:

  1. Anonymous2:04 AM

    I don't understand the purpose of this post.

    The code examples show the difference between if expressions and if statements, in different languages.

    But; Java has conditional expressions, which were not represented.

    How is this post interesting?
    (Negating a number is not interesting)

    ReplyDelete
  2. Why not following?

    (def ops {"ask" -})

    (defn sidenum [side quantity] ((ops side identity) quantity))

    user=> (sidenum "ask" 10)

    -10

    ReplyDelete
  3. @alex, that definitely works too. Classic "do I use an if or a map" situation, right?

    Either is good. I can't see a downside to either one...

    ReplyDelete
  4. in some situations, maps are more extensible - just add new function to map, and new transformation will work automatically

    ReplyDelete

Note: Only a member of this blog may post a comment.