For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Order sizing specified in bought units instead of spent currency



  • The AllInSizer calculates the position size as follows size = cash / data.close[0] * (self.params.percents / 100).

    Am I right in believing this is specifying the number of units to buy?

    How does this work for people? I find my orders come back Margin, because the price has moved.

    To me it makes more sense to specify a market buy in units of currency. i.e. buy $1000 worth of ABC at market price.

    ... also commission...

    Thoughts?


  • administrators

    @user-425 said in Order sizing specified in bought units instead of spent currency:

    How does this work for people? I find my orders come back Margin, because the price has moved.

    It doesn't. All All-In strategies are doomed to fail, because the orders cannot be matched due to price gaps. It's the reality of life which people try to ignore.

    The options:

    • use cheat-on-open. See: Docs - Cerebro and Docs - Cheat on Open

      Because you will for sure argue that you can get the opening price from your broker, because you have a special broker. There will be a slippage though (that can be configured in the broker, but you will then hit the same wall)

    or

    • Use order_target_xxx: See: Docs - Target Orders

      You will then specify 100% value and hit the same wall.

    If you google a bit you will find many references to this topic, because man is the only anima who trips twice over the same stone.



  • Hello! Excellent links and thank you for the reply.

    When I buy stocks from my broker I can specify the amount to buy in one of two ways:

    1. the number of the stock to buy
    2. the USD value of the stock to buy

    It seems to me that backtrader only supports the first of these two.

    Is there a reason backtrader doesn't support submitting buy-market orders to the broker in the cash value form?


  • administrators

    @user-425 said in Order sizing specified in bought units instead of spent currency:

    When I buy stocks from my broker I can specify the amount to buy in one of two ways:

    1. the number of the stock to buy
    2. the USD value of the stock to buy

    Wrong. You specify a value and the broker makes the calculation: size = value // current_price, where // is the Python operator for integer division, because you cannot buy fractions of a stock. And you also have to confirm the operation. Over the web.

    @user-425 said in Order sizing specified in bought units instead of spent currency:

    It seems to me that backtrader only supports the first of these two.

    Wrong. The links above show that.



  • I'm trading crypto actually, using the fork to support that, where you can buy fractions of a stock.

    When you issue a market price order on a crypto exchange - and specify USD (or rather BTC) it keeps buying until it has spent approximately that about of BTC buying the coin.

    Obviously as the administrator I would assume you know more about this than me ... but the idea of a current price is something of a misnomer. The exchange will just eat it's way up the order book until the order is filled.


  • administrators

    Above you were talking about stocks. As the original poster it is obvious that if you want a sensible answer, you have to provide the appropriate information.

    current_price is not a misnomer. In your case you are trading a pair (like in Forex), which in this case is USD/BTC. You could also trade XRP/BTC or ETH/BTC. In any case it has a current_price.

    If you trade USD/JPY you do also specify things in JPY, but because the pair needs to be handled in something and cannot be handled in imaginary units. Hence the need to specify things in an actual currency (which is done by selecting one the currencies from the pair: yes, there is a convention for that)

    @user-425 said in Order sizing specified in bought units instead of spent currency:

    Obviously as the administrator I would assume you know more about this than me

    I don't know what you are expecting me to know or not know. If you use a fork, go and ask the people that manage the fork. They will also be glad to have as much information as possible from the beginning to be able to properly answer.



  • I mean that current_price is a misnomer in a market-buy. As the broker will consume the order book and fill the order at (potentially) multiple different prices. This is of course a distinction for live trading and not when backtesting - where you would have a current_price.

    I had a look at the APIs for a few exchanges and it seems that buying a specified quantity of the target stock/coin is the only option - so that matches the implementation of backtrader and answers my "why?" question about the implementation.

    Time for more research I guess.

    Thanks for your pointers in to the docs. If there's a search term you would recommend I google please let me know.

    Ideally brokers would allow orders to be specified in source currency via the API - in the same way I can via their web interface.


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.