Giter Club home page Giter Club logo

Comments (5)

saleh-old avatar saleh-old commented on May 14, 2024 1

Nice catch. I'll fix it. Thank you

from jesse.

lightyear15 avatar lightyear15 commented on May 14, 2024 1

Thanks a lot,
self.position.entry_price was the bad boy I was looking for.
I agree with you then... if self.position is resuming all info about current position, then self.average_entry_price should report only the average entry price of the last order.
I am closing the issue.
Thanks again

from jesse.

saleh-old avatar saleh-old commented on May 14, 2024

I worked on this for hours today. Turns out it's harder that I expected. The easy fix is to consider average_entry_price as average of open orders. But I don't think that would be enough. How would that be in your use case?

from jesse.

lightyear15 avatar lightyear15 commented on May 14, 2024

For hours!!! I kinda feel sorry for having found the bug now 😅

Well, I'd say that average entry price is the weighted average of all the executed buy (sell) orders; and once the position is liquidated the average_entry_price should be reset.
So let's say I do in sequence:

  1. in go_long() I do
qty = 0.5
self.buy = qty, self.price   
  1. in update_position I do
qty = 0.7
self.buy = qty, self.price
  1. in update_position I do
qty = 0.9
self.buy = qty, self.price

Assuming all 3 buy orders are executed at market price, I expect average_entry_price to be:

0.5 * marketPrice@1. + 0.7 * marketPrice@2. + 0.9 * marketPrice@3. / ( 0.5 + 0.7 + 0.9)

I annotated marketPrice@X. and not self.price@1. just so you know that I am aware of the fact that at the moment when jesse executes the order of type "MARKET" the market price can be different from self.price
The real point of average_entry_price is the weighted average of all the entry prices times the quantities.

I am not sure how the average_entry_price should be updated when you reduce the position 🤔

from jesse.

saleh-old avatar saleh-old commented on May 14, 2024

I'm sorry for the long delay.

Well, in your example you don't need average_entry_price! You could simply use self.position.entry_price.

So I guess I have more time to work on average_entry_price in the future. The problem I found in it after digging into it, is that, it is currently returning the average of OPEN entry orders only. So if you do:

self.buy = [
    (0.5, 6),
    (0.5, 8),
]

average_entry_price gives you 7 which is expected. But if you already have an open position let's say at:

self.buy = 1, 5

And then access in update_position do:

self.buy = [
    (0.5, 6),
    (0.5, 8),
]

No you might expect the average_entry_price to be the average of all three prices weighted by their qty, but it's not. It's only the average of the 2 newly submitted orders.

So since we already have self.position.entry_price for executed orders, maybe it makes sense to leave it as is. But still, we maybe the name of it is confusing and should point to OPEN orders. What do you think?

from jesse.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.