Skip to content

joshualawson/profit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Profit

Profit calculates a set of historical data to find the most profitable trade for that specific day.

Scenario

Suppose we could access yesterday's stock prices as a list, where:

  • The indices are the time in minutes past trade opening time, which was 10:00am local time.
  • The values are the price in dollars of the Latitude Financial stock at that time.
  • So if the stock cost $5 at 11:00am, stock_prices_yesterday[60] = 5.

Write an efficient function that takes an array of stock prices and returns the best profit I could have made from 1 purchase and 1 sale of 1 Latitude Financial stock yesterday.

For example:

var stock_prices_yesterday = [10, 7, 5, 8, 11, 9];

get_max_profit(stock_prices_yesterday)
# returns 6 (buying for $5 and selling for $11)

You must buy before you sell. You may not buy and sell in the same time step (at least 1 minute must pass).

Usage

$ go get github.com/joshualawson/profit

Basic Usage

import (
    "fmt"
    "github.com/joshualawson/profit"
)

data := []int{
    10, 5, 2, 22, 8, 9, 12
}

profitableTrades := profit.Profitable(data)

maxProfit := profit.MaxProfit(profitableTrades)

fmt.Printf("The most profitable trade will have a profit of: %v\n", maxProfit)

Dependencies

Dependency management is done using dep

Dep can be found here with instruction on how to install dep

To load all of the packages dependencies run the following command from within the packages root dir

$ dep ensure

Testing

To test the library simply run the following command

$ go test ./...

Benchmark

To run benchmark results yourself use the following command

$ go test -bench=.

Benchmark Results

Benchmark results using my MacBook Air

BenchmarkProfitable1-4                  2000000000               0.00 ns/op
BenchmarkProfitable2-4                  2000000000               0.00 ns/op
BenchmarkMaxProfit1-4                   2000000000               0.00 ns/op
BenchmarkMaxProfit2-4                   2000000000               0.00 ns/op
BenchmarkMaxProfitWithProfitable-4      2000000000               0.00 ns/op

Coverage

Coverage is currently at 100% to see the results yourself run the following command

$ go test -cover

Coverage Results

PASS
coverage: 100.0% of statements

Reasoning

The task was to find the most profitable trade from a series of numbers where the first value was the opening trade for the data at 10:00 am local time and every value past that was +60mins.

I decided to create two methods, one to generate a slice that would contain every profitable trade for the data set given to it and one to find the single most profitable trade.

Finding the profitable trades could of been implemented into a go routine to iterate over the data in a go routine faster but I decided to leave it simple as the benchmarks were quite fast with the data set given and the extra complexity was not really needed.

As a bonus I added a method to calculate the profit made on all profitable trades.

About

Code test for Latitude finance

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages