Trade object: why is sell size 0?
-
First problem I had is identifying whether a trade is a buy or sell. Order has
isbuy()
method, but what about trades?I can now see from correlation with orders that trades that are sell have a size of 0, which is odd. Is there a way to see the sell amount? And is there some method to get the trade type (buy / sell)?
-
Sells are neither
buy
norsell
. They canlong
orshort
. This may seem like a stupid distinction, but aTrade
is composed of the combination of1 or more buy
operations and1 or more sell
operations (which may be executed through auxiliary methods likeclose
ororder_target_xxxx
)This is explained in the docs: Docs - Trade
That's why
Trade
has along
attribute. Or you can check whethersize
is positive o negative. -
@duality_ said in Trade object: why is sell size 0?:
I can now see from correlation with orders that trades that are sell have a size of 0
No. They have a negative size.
-
Hmm I think they are 0. I use this code to debug:
def notify_order(self, o): print("Order {} {} {} {} {}".format(o.data._name, 'buy ' if o.isbuy() else 'sell', o.size, o.getstatusname(), o.getordername())) def notify_trade(self, t): print("Trade {} {}*{} = {}".format(t.getdataname(), t.size, t.price, t.value))
Here's a part of the output:
Order ETH/BTC buy 667356 Submitted Market Order ETH/BTC buy 667356 Accepted Market Order ETH/BTC buy 667356 Completed Market Trade ETH/BTC 667356*0.05344 = 35663.50464 Order BTC/USD sell -25 Submitted Market Order BTC/USD sell -25 Accepted Market Order BTC/USD sell -25 Completed Market Trade BTC/USD 0*1190.49 = 0.0
Similar with other outputs.
-
So I tried the
.long
attribute of a trade (BTW this is not in the reference innotify_trade
, but all my trades seem to be long. Not sure if this is correct? -
@duality_ said in Trade object: why is sell size 0?:
Trade BTC/USD 0*1190.49 = 0.0
Yes, it s
0
because you have closed the trade.@duality_ said in Trade object: why is sell size 0?:
So I tried the .long attribute of a trade (BTW this is not in the reference in notify_trade, but all my trades seem to be long. Not sure if this is correct?
If your trades look like the one above ... they will all be long. But we cannot know.
-
I've read what you wrote and the docs but this doesn't make much sense to me.
What I need to know is to debug when and how the strategy trades. Currently what I can do based on what I know is use
notify_order
and only output info when the order has been completed. Hope that's okay. -
A trade is the combination of multiple orders and is either long or short. That is determined by the opening order.
- If you buy 25 units, you have opened a long trade with size 25
- You can then buy and sell multiple times, which will change the current size of the trade as long as you don't take the size to 0
- When the size goes down to 0 the trade is closed and the current size remains at 0 and cannot be modified (any futher orders, generate new trades)
Apply the inverse logic for a short trade, which starts with a sell order.
-
@backtrader said in Trade object: why is sell size 0?:
A trade is the combination of multiple orders and is either long or short. That is determined by the opening order.
- If you buy 25 units, you have opened a long trade with size 25
- You can then buy and sell multiple times, which will change the current size of the trade as long as you don't take the size to 0
- When the size goes down to 0 the trade is closed and the current size remains at 0 and cannot be modified (any futher orders, generate new trades)
Apply the inverse logic for a short trade, which starts with a sell order.
Thanks, this is definitely the best behavior. I use this in my framework when constantly re-balancing a trade size.
-
@Laurent-Michelizza so you have one trade open almost indefinitely?
@backtrader how do I know then how much has been traded in a trade if it's closed and the
size
is zero? -
@duality_ said in Trade object: why is sell size 0?:
@backtrader how do I know then how much has been traded in a trade if it's closed and the size is zero?
The problem when answering your questions is that the size is dynamic, i.e.: it can start at 25, move up to 50, go down to 5, move to 17, down to 15, up to 100 and then go down to 0 to close it.
Which of all those size values is the one you want to keep track of?
-
The answer to this should be obvious if you're looking from a trader's point-of-view: how much of what have I traded at what point in time?
An analogy to the real-life food market would be:
- 2018-02-03 traded $3 for 7 apples
- 2018-02-43 traded $4 for 7 apples
- 2018-02-12 traded 3 apples for 2 oranges
- 2018-02-23 traded 1 orange for $2
As you can see there's no traded $0 for 7 apples because a trade is closed or something similar.
So back to your example of a trade going from a size 25 to 50, back to 5 etc., assuming I understand how this works, for me this would mean that I have 1. bought 25, 2. bought 25 (totaling 50), 3. sold 45 (totaling 5).
-
Size
0
doesn't indicate that you have traded anything for0
: it is the final size status of the trade, i.e.: the trade is closed.If you have another expectation, the
Trade
object isn't going to fulfill it.@duality_ said in Trade object: why is sell size 0?:
how much of what have I traded at what point in time?
Check
tradehistory
: Docs - Cerebro -
So is using orders and filtering when they are completed better for what I want?
-
Not sure if I did understand what you want exactly but did you take a look at the
BuySell
observer? It keeps track of all executed orders indenpendently of trades (i.e. position size going to 0). -
@vbs yes this does basically what I would do: check every order and see if it's already executed and if it is, use that.
-
@duality_ said in Trade object: why is sell size 0?:
@Laurent-Michelizza so you have one trade open almost indefinitely?
Yes sort of. I pick a side, and then a forecast adjusts the size on each bar.
-
@duality_ said in Trade object: why is sell size 0?:
So is using orders and filtering when they are completed better for what I want?
It is really unclear (at least here) what you want. You are issuing the orders, so you obviously know the size of each order. If your trades consist of a single
buy/sell
pair, you already know thesize
. You can of course track order notifications and see the size of each completed order. -
I don't know the size because I use
order_target_percent
.