-3

I am writing a paper trading system, and what I have works, but I can't help but feel that there's a better way to partially close a stock position; what I currently have seems a little overboard.

Partially Close Scenario: Customer A buys 100 shares of stock X at $1 then, later the customer sells 80 shares at $2; leaving 20 shares on the table.

Close plus Open Scenario: Customer A buys 100 shares of stock X at $1 then later the customer sells 120 shares at $2; opening a new short position with 20 shares.

Pseudo Code

I think posting the actual code is irrelevant for answering this question.

if (customer has active trade) {
    if (quantity to close >= quantity open) {
        close open trade
        if (quantity to close > quantity open)
            create new open trade with remaining quantity (close - open)
    } else {
        // This is where I think it could be better.
        close old position at entry price
        open new position at old entry price (close quantity)
        close new position at specified exit price
        open new position at old entry price (old quantity - close quantity)
    }
} else {
    create new open trade with specified quantity
}

Is there a more efficient way to write the logic above? Both scenarios can be executed as a single trade on most platforms.

NOTE: The community may feel this question has a better home over at Code Review; if this happens to be the case, can it be migrated? I thought it was a better fit here since there's no actual code involved.

2
  • For the section you are trying to improve, is the worry that more operations could get added later, or that the separate operations are a bit clunky? Also, are you looking to only improve the section under that else block with the comment or the overall snippet? Commented May 18, 2020 at 3:16
  • Mostly just focused on that small section because it feels a bit clunky IMO. Commented May 18, 2020 at 19:05

2 Answers 2

1

Came up with a couple ideas. One thing to note is that the existing snippet seems fine to me and keeping things simple sometimes feels clunky. Also, go easy on me for the PseudoCode wording, it has been a while.

For idea 1, this is a little more complex looking, but it lends itself to easier maintenance with positions and operations against them. The main premise is utilizing collections/maps.

        // idea 1 start

        define collection positionOperationsMap(position, operation)
        define list operationsList(operation)

        add operation (close at entry price) to operationsList
        add operation (open at close quantity) to operationsList
        add operation (close at specific exit price) to operationsList
        add operation (open at old quantity - close quantity) to operationsList

        add positionOperation (old position, operationsList[0]) to positionOperationsMap
        add positionOperation (new position, operationsList[1]) to positionOperationsMap
        add positionOperation (new position, operationsList[2] to positionOperationsMap
        add positionOperation (new position, operationsList[3]) to positionOperationsMap

        for each positionOperation in positionOperationsMap
                execute positionOperation
        next

        // idea 1 end

For the second idea, I will be using code here, because that makes more sense in my mind and feels fairly simple. This uses the idea that new positions are based on the previous position.

        // idea 2 start

        Postion OldPosition; // this is defined somewhere above

        // some method for closing that returns an instance of a new 
        // Open Position; assuming OldPosition is holding quantity
        // determining CloseQuantity can be within Position, but it makes more 
        // sense since you have a "specified" exit price/quantity coming up

        Position NewPositionTemp = OldPosition.Close(CloseQuantity);

        Position NewPosition = NewPositionTemp.Close(ExitPrice);

        // idea 2 end

    }
} else {
    create new open trade with specified quantity
}

I believe this still keeps the general concept of the expected functionality so hopefully this helps some.

1

The concerned code does look chunky and repetitive. The answer though would depend on implementation. I have worked with an application like this before and we were able to "modify" positions. If that is possible then consider this:

modify old position quantity to (old quantity - close quantity)
open new position at old entry price (close quantity)
close new position at specified exit price

This makes the changes to old position atomic and less complex. It also introduces brevity.

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.