Comments (2)
Lets see if i get it right, but first lets define whats what,
A long limit order is placed when you expect the price to rise above the limit price after your purchase. You're buying with the expectation of selling at a higher price later.
A short limit order involves selling at the limit price with the expectation that you can buy back the asset at a lower price in the future. Short selling aims at profiting from the decline in the asset's price.
The issue i can probably see in the code is when placing a short limit order by just using the .sell() function and passing the same limit_price_long as the price, you're attempting to open a short position at a price that is less than the current market price. This is conceptually the same as a long limit order because a sell limit order aims to sell at a price higher than the current market price, not lower.
maybe something like this should work
# Assuming limit_price_short is calculated correctly and is above the current market price
order_short = self.sell(size=self.trade_size, limit=limit_price_short)
# Now, calculate tp for both long and short positions
for trade in self.trades:
if trade.is_long:
trade.tp = tp_price_long # Ensure this is above your entry for long
elif trade.is_short:
trade.tp = tp_price_short # Ensure this is below your entry for short
from backtesting.py.
I'm trying to play against market trend. So yes I want to place an order long or short with the determinant price, even the current price is less or higher. If I place limit order, but current price is less than expected (or higher) is executes straight away. I discovered that there are stop-limit orders and now it works
order_short = self.sell(size=self.trade_size, stop=limit_price_long, limit=limit_price_long) ==
order_long = self.buy(size=self.trade_size, stop=limit_price_long, limit=limit_price_long)
but entry prices are still a little bit different, not sure why. I didn't place any tp, sl level here
Size EntryBar ExitBar EntryPrice ExitPrice PnL ReturnPct \
8 1 2906 27322 2287.173831 3585.46 1298.286169 0.567638
7 1 5713 27322 2169.623927 3585.46 1415.836073 0.652572
6 1 9650 27322 2200.865225 3585.46 1384.594775 0.629114
5 1 10101 27322 2226.207870 3585.46 1359.252130 0.610568
4 1 21484 27322 2593.182104 3585.46 992.277896 0.382649
3 1 22358 27322 2752.453069 3585.46 833.006931 0.302642
2 1 25842 27322 3277.345012 3585.46 308.114988 0.094014
1 1 26132 27322 3373.465502 3585.46 211.994498 0.062842
0 1 26873 27322 3362.545455 3585.46 222.914545 0.066293
Size EntryBar ExitBar EntryPrice ExitPrice PnL ReturnPct \
8 -1 2906 27322 2285.344823 3585.46 -1300.115177 -0.568892
7 -1 5713 27322 2167.888922 3585.46 -1417.571078 -0.653895
6 -1 9650 27322 2199.105236 3585.46 -1386.354764 -0.630418
5 -1 10101 27322 2224.427616 3585.46 -1361.032384 -0.611857
4 -1 21484 27322 2591.108388 3585.46 -994.351612 -0.383755
3 -1 22358 27322 2750.251987 3585.46 -835.208013 -0.303684
2 -1 25842 27322 3274.724185 3585.46 -310.735815 -0.094889
1 -1 26132 27322 3370.767808 3585.46 -214.692192 -0.063692
0 -1 26873 27322 3359.856495 3585.46 -225.603505 -0.067147
The same goes with tp/sl. If now is better price, than tp's it order executes at the next candle.
So to close long/short at the same time I should invert tp-sl
for trade in self.trades:
if trade.is_long:
trade.tp = price
elif trade.is_short:
trade.sl = price
Size EntryBar ExitBar EntryPrice ExitPrice PnL ReturnPct \
0 1 2906 2923 2287.173831 2247.0990 -40.074831 -0.017522
1 1 5713 5726 2169.623927 2190.5855 20.961573 0.009661
2 1 9650 9666 2200.865225 2222.6290 21.763775 0.009889
3 1 10101 10116 2226.207870 2242.8200 16.612130 0.007462
4 1 21484 21491 2593.182104 2641.1740 47.991896 0.018507
5 1 22358 22369 2752.453069 2787.5600 35.106931 0.012755
6 1 25842 25860 3277.345012 3321.9190 44.573988 0.013601
7 1 26132 26152 3373.465502 3377.4735 4.007998 0.001188
8 1 26873 26886 3362.545455 3394.7985 32.253045 0.009592
Size EntryBar ExitBar EntryPrice ExitPrice PnL ReturnPct \
0 -1 2906 2923 2285.344823 2247.0990 38.245823 0.016735
1 -1 5713 5726 2167.888922 2190.5855 -22.696578 -0.010469
2 -1 9650 9666 2199.105236 2222.6290 -23.523764 -0.010697
3 -1 10101 10116 2224.427616 2242.8200 -18.392384 -0.008268
4 -1 21484 21491 2591.108388 2641.1740 -50.065612 -0.019322
5 -1 22358 22369 2750.251987 2787.5600 -37.308013 -0.013565
6 -1 25842 25860 3274.724185 3321.9190 -47.194815 -0.014412
7 -1 26132 26152 3370.767808 3377.4735 -6.705692 -0.001989
8 -1 26873 26886 3359.856495 3394.7985 -34.942005 -0.010400
from backtesting.py.
Related Issues (20)
- Waiting a couple bars or minutes after a trade is closed before getting into another one? HOT 1
- TypeError: '_Broker' object is not callable
- Is there a way to enter a bracket order?
- what about multiple stocks/data inputs? HOT 2
- Expectancy calculation
- entry exit check condition problem
- As long as I set a lot of data, it will take a long time to execute the program, but I see that the system resources are not occupied to 10%. Is there any way to make it run at full load?
- Support for backtesting multi-assets strategy
- TypeError: Index.get_loc() got an unexpected keyword argument 'method' HOT 7
- Avoid opening position that gaps down with limit order
- Code citation
- self.buy(size=10.15) `size` can it be a floating point number greater than 1?
- parallel trades
- A few questions about execution on lower timeframes and weekly vwap
- Implement 2 strategies at the same time
- --
- Friendly Note Regarding Your GitHub & backtesting Profile Picture HOT 1
- Assertion Error due to negative stop loss using Optimize
- how to get correct 15min value by using resample_apply with 1min data
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from backtesting.py.