Hello! I am new to using backtrader and I do not have a strong technical analysis background, so I am having some difficulty understanding what is happening behind the code. I am currently attempting to implement a simple Moving Average Crossover strategy with some additional conditions.
As far as I am aware, the signals are appearing when I want them to and they are triggering bracket orders correctly (as can be seen in the attached screenshot). However, I am struggling to understand why the second trade, of the ones defined below, is being triggered.
In this case, the first signal attempts to open a short position of size 139 at a price of 175.67 (139175.67 =24418.13). Since the available cash is lower than the trade value (22780.51), the position is not opened. On the next step, we attempt to open a position of size 135 at 175.48 (135175.48 =23689.8). This number is still greater than the available cash, yet the order is still being accepted and executed.
2022-12-19 -> order margin
current cash: 22780.51
openPositions : 360
value: 86021.714
close price: 175.67
stopPrice: 181.13
target: 2.8
targetPrice: 160.3833454
size = 139
2022-12-20 -> order completed
current cash: 22780.51
openPositions: 360
value: 85953.31461826575
close price: 175.48
stopPrice: 181.13
target: 2.8
targetPrice: 159.6613454
size = 135
As far as I am aware, the leverage is set to 1 (default value) and no specific margin has been set. I am currently using a simple commission scheme, which is defined as follows:
cerebro.broker.setcommission(commission=0.002)
Additionally, I have also set cerebro.broker.set_coc(True).
In general, I am curious about the default logic that determines whether a Short trade is accepted or rejected based on the available cash.
I would appreciate it if you could help me understand what is happening. Thank you very much in advance!