Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjust log format in LogTradesAnalysis Analyze #6

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
21 changes: 15 additions & 6 deletions analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ func (tps TotalProfitAnalysis) Analyze(record *TradingRecord) float64 {
if trade.IsClosed() {
costBasis := trade.CostBasis()
exitValue := trade.ExitValue()

totalProfit = totalProfit.Add(exitValue.Sub(costBasis))
if trade.IsLong() {
totalProfit = totalProfit.Add(exitValue.Sub(costBasis))
} else {
totalProfit = totalProfit.Add(costBasis.Sub(exitValue))
}
}
}

Expand Down Expand Up @@ -60,10 +63,16 @@ type LogTradesAnalysis struct {
// Analyze logs trades to provided io.Writer
func (lta LogTradesAnalysis) Analyze(record *TradingRecord) float64 {
logOrder := func(trade *Position) {
fmt.Fprintln(lta.Writer, fmt.Sprintf("%s - enter with buy %s (%s @ $%s)", trade.EntranceOrder().ExecutionTime.UTC().Format(time.RFC822), trade.EntranceOrder().Security, trade.EntranceOrder().Amount, trade.EntranceOrder().Price))
fmt.Fprintln(lta.Writer, fmt.Sprintf("%s - exit with sell %s (%s @ $%s)", trade.ExitOrder().ExecutionTime.UTC().Format(time.RFC822), trade.ExitOrder().Security, trade.ExitOrder().Amount, trade.ExitOrder().Price))

profit := trade.ExitValue().Sub(trade.CostBasis())
fmt.Fprintln(lta.Writer, fmt.Sprintf("%s - enter with %s %s (%s @ $%s)",
trade.EntranceOrder().ExecutionTime.Format(time.RFC822), trade.EntranceOrder().Side, trade.EntranceOrder().Security, trade.EntranceOrder().Amount, trade.EntranceOrder().Price))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation is off here.

fmt.Fprintln(lta.Writer, fmt.Sprintf("%s - exit with %s %s (%s @ $%s)",
trade.ExitOrder().ExecutionTime.Format(time.RFC822), trade.EntranceOrder().Side, trade.ExitOrder().Security, trade.ExitOrder().Amount, trade.ExitOrder().Price))
profit := big.ZERO
if trade.IsLong() {
profit = trade.ExitValue().Sub(trade.CostBasis())
} else {
profit = trade.CostBasis().Sub(trade.ExitValue())
}
fmt.Fprintln(lta.Writer, fmt.Sprintf("Profit: $%s", profit))
}

Expand Down
11 changes: 11 additions & 0 deletions order.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ const (
SELL
)

func (os OrderSide) String() string {
switch os {
case BUY:
return "BUY"
case SELL:
return "SELL"
default:
return "UNKNOWN"
}
}

// Order represents a trade execution (buy or sell) with associated metadata.
type Order struct {
Side OrderSide
Expand Down