TradingView
A3Sh
16 apr 2023 08:13

LowFinder_PyraMider_V2 

XRPUSDT Perpetual ContractBybit

Descrizione

This strategy is a result of an exploration to experiment with other ways to detect lows / dips in the price movement, to try out alternative ways to exit and stop positions and a dive into risk management. It uses a combination of different indicators to detect and filter the potential lows and opens multiple positions to spread the risk and opportunities for unrealized losses or profits. This script combines code developed by fellow Tradingview community_members.

LowFinder
The lows in the price movement are detected by the Low finder script by RafaelZioni. It finds the potential lows based on the difference between RSI and EMA RSI. The MTF RSI formula is part of the MTFindicators library developed by Peter_O and is integrated in the Low finder code to give the option to use the RSI of higher timeframes. The sensitivity of the LowFinder is controlled by the MA length. When potential lows are detected, a Moving Average, a MTF Stochastic (based the the MTFindiicators by Peter_O) and the average price level filter out the weak lows. In the settings the minimal percentage needed for a low to be detected below the average price can be specified.

Order Sizing and Pyramiding
Pyramiding, or spreading multiple positions, is at the heart of this strategy and what makes it so powerful. The order size is calculated based on the max number of orders and portfolio percentage specified in the input settings. There are two order size modes. The ‘base’ mode uses the same base quantity for each order it opens, the ‘multiply’ mode multiplies the quantity with each order number. For example, when Long 3 is opened, the quantity is multiplied by 3. So, the more orders the bigger the consecutive order sizes. When using ‘multiply’ mode the sizes of the first orders are considerably lower to make up for the later bigger order sizes. There is an option to manually set a fixed order size but use this with caution as it bypasses all the risk calculations.

Stop Level, Take Profit, Trailing Stop
The one indicator that controls the exits is the Stop Level. When close crosses over the Stop Level, the complete position is closed and all orders are exited. The Stop Level is calculated based on the highest high given a specified candle lookback (settings). There is an option to deviate above this level with a specified percentage to tweak for better results. You can activate a Take Profit / Trailing Stop. When activated and close crosses the specified percentage, the Stop Level logic changes to a trailing stop to gain more profits. Another option is to use the percentage as a take profit, either when the stop level crosses over the take profit or close. With this option active, you can make this strategy more conservative. It is active by default.
And finally there is an option to Take Profit per open order. If hit, the separate orders close. In the current settings this option is not used as the percentage is 10%.

Stop Loss
I published an earlier version of this script a couple of weeks ago, but it got hidden by the moderators. Looking back, it makes sense because I didn’t pay any attention to risk management and save order sizing. This resulted in unrealistic results. So, in this script update I added a Stop Loss option. There are two modes. The ‘average price’ mode calculates the stop loss level based on a given percentage below the average price of the total position. The ‘equity’ mode calculates the stop loss level based on a given percentage of your equity you want to lose. By default, the ‘equity’ mode is active. By tweaking the percentage of the portfolio size and the stop loss equity mode, you can achieve a quite low risk strategy set up.

Variables in comments
To sent alerts to my exchange I use a webhook server. This works with a sending the information in the form of a comment. To be able to send messages with different quantities, a variable is added to the comment. This makes it possible to open different positions on the exchange with increasing quantities. To test this the quantities are printed in the comment and the quantities are switched off in the style settings.

This code is a result of a study and not intended for use as a worked out and full functioning strategy. Use it at your own risk. To make the code understandable for users that are not so much introduced into pine script (like me), every step in the code is commented to explain what it does. Hopefully it helps.
Enjoy!

Note di rilascio

Removed the precision in the strategy properties, that caused the chart not working in some cases.

Note di rilascio

Deleted some obsolete code.

Note di rilascio

Added a precision setting. Specifies the number of digits after the floating point of the script's displayed values. If the order sizes are to small and the strategy doesn't generate results, try increasing the precision. The max value is 16.

Note di rilascio

Tiny update. Trailing Stop only gets plotted when higher then Take Profit level.

Note di rilascio

Fixed a flaw with preventing opening too much orders in a row. The counting of the bars logic was flawed. Now when the position size is bigger then 0 the bar starts counting. The counting resets when a consecutive order opens.
Commenti
mw8vsf4sd2
Hello, I have the same situation with other contracts, the order is not placed at 10% of the total capital, but one lot.
A3Sh
Hi @mw8vsf4sd2, did you try to change the precision? If you do 3 for example it will give you a precision of 3 decimals behind the dot.
mw8vsf4sd2
@A3Sh, Thanks for your reply, I tried adjusting the Precision parameter to 16 and then to 3 and didn't see any difference in the backtesting data, still all 1 lot.
A3Sh
@mw8vsf4sd2, Strange...I just copy pasted the above script in a new strategy and the precision is 4 by default. So it seems to work fine. Are you sure you unchecked fixed position size in the settings?
mw8vsf4sd2
Hello, I also saw this on the ETH backtest, the orders are all in units of 1, do we need to adjust this order setting? If the leverage is obviously open on BTC, on ETH, obviously the capital utilization is very low. I look forward to your reply.
mw8vsf4sd2
Assuming 1. the total capital is $10,000 USD
2. 10% of the equity of a single order.
3. The approximate value of each BTC lot is $30,000.
4. The backtest report shows that each order is 1 lot.
According to the Total Funds and Single Order Equity settings, the maximum size of a single BTC lot should be.
$10,000 x 10% / $30,000 per lot = 0.33 lots
But in the backtest all the lots are 1 lot.
This means that in the backtest, although the 10% limit on the equity of a single order was set, in reality, about 3 times the leverage was invariably used to realize the trade of 1 lot.
This is not only a violation of the single order limit, but also makes the backtest results somewhat biased.
A3Sh
@mw8vsf4sd2, 10% of your total funds.
mw8vsf4sd2
Hello and thank you for your open source spirit and contributing to the community. There are two other points I would like to inquire about: 1. Can there be a shorting version as well, which would increase the number of opportunities and capital utilization. 2. Can you elaborate on the pros and cons of the two position management modes, and the impact on return and risk? 3. I see some unfilled orders that are already losing money, which would be catastrophic if they were in a downtrend. 4. Can you set up a BTC/ETH setup based on some common parameters for BTC/ETH?
A3Sh
@mw8vsf4sd2, It is good to point out that the strategy is a result of a research into different logics (see description) and not a fully function and worked out strategy. So use it on on your own risk. ---The risk management was one of these explorations. You can bypass the risk management with manual fixed order sizes, but this is very risky. --- I tried shorts, but didn't give better results and personally find shorting a bit tricky. Feel free to tweak the code and add short options. ---Regarding the two position management modes, one is linear and opens consecutive positions with the same order size. The multiply mode doubles each position. This cause the average price to drop harder and therefore increases the change that the position will close in the following uptrend wave. I would recommend you to tweak these settings based on your own preferences.
EtherMatt
Hey first of all, great work! The Script looks super promising. Automation of the Script would be a bit tricky since the unique risk management style of this script won't work with basic 3rd party automation platforms. Any Ideas on how someone could automate this script?
Altro