Is it possible to simulate order with size smaller than 1? #942
-
Apologies if this has been answered already, but I couldn't find the same question in the discussions or issues. My question is: is there a way to place orders with a size less than 1 using the backtesting.py package? For example, let's say the BTCUSDT price is 20k, and my balance is 10k. I would like to place an order with a size of 0.5 (10k/20k). As it stands, when the size is set between 0 and 1, it's treated as a fraction of the balance (cash + position P&L - margin), but it doesn't execute orders with a size smaller than 1. I tried the following code, but the backtest stops once self.equity can no longer buy/sell a size of 1, meaning the equity is smaller than the price: class HeikinStrategy(Strategy):
def init(self):
# pos has 1 or -1 for long/short
self.pos = self.I(Heikin, self.data)
def next(self):
price = self.data.Close[-1]
size = self.equity / price if self.equity < price else 0.9999
# Long entry
if self.pos == 1:
self.buy(size=size)
# Short exit
elif self.pos == -1:
self.sell(size=size) So, I was wondering if I can place an order with a size less than 1. Thank you for your help in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
What did you search for, I wonder. See #134. |
Beta Was this translation helpful? Give feedback.
-
Thank you for your answer. However, I'm still having trouble understanding why the Return [%] differs when using or not using μBTC. For example, when I set cash=100,000, Return [%] is 298.1342. But when I follow your suggestion to do df = (df / 1e6).assign(Volume=df.Volume * 1e6), the Return [%] becomes 310.268262 with initial cash 1000. (I am using the same strategy for both and both trade 728 times) Isn't this strange? The return should be the same regardless of the initial cash and BTC price, as long as the number of trades is the same. Otherwise, the strategy seems to be influenced by the initial cash amount and BTC price scale. Please let me know if I'm missing something or if there's a better way to adjust for μBTC. Thank you! |
Beta Was this translation helpful? Give feedback.
What did you search for, I wonder. See #134.