sdcoffey / techan

Technical Analysis Library for Golang
https://godoc.org/github.com/sdcoffey/techan
MIT License
840 stars 143 forks source link

Add a way to include transaction fees #12

Open Peltoche opened 5 years ago

Peltoche commented 5 years ago

Hello!

Would it be possible to add a way to include transaction fees into the calculations? I see two way to do it:

What do you think of it?

sdcoffey commented 5 years ago

I like it!

Probably the best way to accomplish this is to add a Commision field to Order, and then adjust the logic in the relevant analysis tools to take it into account when calculating profit.

What do you think?

Peltoche commented 5 years ago

I just started to work on the subject and a question appeared: How do we deduce the commission from a position? I see two solution:

  1. We deduce the commission from the Order
  2. We deduce the commission from the wallet

Solution 1

This solution have as downside that the amount ordered will never be the one expected.

For example if I open a position with 100 BTC with a 10% commission I remove the commission from the order and so I will end up with only 90 BTC inside my position.

Solution 2

This solution imply to create a new Analysis tool for the commissions and let the people deduce them at the end of the operations. It have as downside that the commission are not taken into account into the positions max amount.

For example I have 100 BTC and I want to open a position with 10% commission with all my money. Normally I should be able to open a position for only 90 BTC due to the fact that 10 BTC have been paid for the commission. With the solution 2 the position will be opened with 100 BTC and the 10 BTC for the commission will be paid later.

IMHO the solution 2 seems a lot easier to implement and less error prone.

GeorgeGkinis commented 3 years ago

I currently do it this way :

o := techan.Order{
Side: techan.SELL,
Security: Symbol,
Price: nc.ClosePrice,
Amount: big.NewFromInt(amount).Mul(big.ONE.Sub(big.NewDecimal(transactionFeePercent))),
ExecutionTime: time.Now(),
}

On my trading platform there is a transaction fee of 0,075% This seems to be the aforementioned Solution 1.

I am not 100% sure this is correct, any suggestions here?

atljoseph commented 3 years ago

Not sure it needs to be this complicated. The code used to determine when to place an order should figure in the commission before choosing to execute the order. Like, shouldn’t strategy tell your code it is time to buy or sell, then your code double-checks the fees, then if acceptable, do the math to exec the 90 BTC trade? Maybe I’m saying something wrong, but the repo does leave me wanting for more documentation via examples, with comments on every line, and more realistic scenarios. I know this is just a chore, and it’s easier said than done, but needs to be done.