Now let's complicate the example of Three Soldiers by adding a mechanism for adaptation to average intraday volatility. We will get ThreeSoldiersVolatilityAdaptive. We will make the signal strength the same throughout the entire depth of the test, so that both 5 and 10 years have the same signal strength.
The basic idea of the "Three Soldiers" pattern is that we need to find three consecutive rising candles on the chart:
This robot still does exactly that, searching for three consecutive rising (or falling, in which case it's "Three Black Crows") candles at a certain percentage and entering the trend after them. Meanwhile, adapting to current volatility is very important, as it can depend on many factors. Without adaptation to volatility, it is better not to trade at all.
1. Example in the Project.
The ThreeSoldiersVolatilityAdaptive robot is located inside the OsEngine project. Open it and go here:
2. Robot Parameters.
1. Regime – working mode.
a. Off – Disabled.
b. On – Enabled and will enter both long and short.
c. OnlyLong – Enabled and will only enter long.
d. OnlyShort – Enabled and will only enter short.
e. OnlyClosePosition – will only close the position and will not enter.
2. Volume Type – method for choosing volume.
a. Contracts – the number of contracts for the instrument.
b. Contract Currency – currency of the contract.
c. Deposit Percent – percentage of the deposit.
3. Volume – the volume value. The specifics depend on the previous point. In the case of Contracts, the volume of the instrument is indicated. In the case of Contract Currency, the amount in rubles or dollars needed to enter is specified. In the case of Deposit Percent, a percentage of the total deposit that needs to be entered into the contract is indicated.
4. Asset in Portfolio – you need to specify the name of the currency that will be used for volume calculation if you selected the “Deposit Percent” volume type. In the tester, we leave it as "Prime." In crypto, it's usually "USDT." In ALOR, it's "RUB."
5. Slippage % – slippage for order prices.
6. Height Soldiers % – The total height of the three candles, after which we consider the pattern formed.
7. Min Height One Soldier % – The minimum height of the body of each individual candle, after which we consider the pattern formed.
8. Profit % from Height of Pattern – the percentage of the total height of the pattern that will be taken as profit.
9. Stop % from Height of Pattern – the percentage of the total height of the pattern that will be taken as stop size.
10. Days Volatility Adaptive – the number of days over which we take the average volatility.
11. Height Soldiers Volatility Percent – the part in % of the average intraday volatility that will be the new height of all three candles.
12. Min Height One Soldier Volatility – the part in % of the average intraday volatility that will be the new minimum height of each individual candle in the pattern.
3. Logic for Entering and Exiting Positions.
LONG
Entry is made if over three candles we have accumulated a total movement upwards, "Height Soldiers %". Meanwhile, each individual candle was sized at no less than "Min Height One Soldier %".
Exit is tied to the total size of the movement over three candles. Profit is equal to the setting "Profit % from Height of Pattern", taking the total movement over three candles and a specific percentage of that movement is reserved as profit. The stop is equal to the setting "Stop % from Height of Pattern".
SHORT
Entry is made if over three candles we have accumulated a total movement downwards, "Height Soldiers %". Meanwhile, each individual candle was sized at no less than "Min Height One Soldier %".
Exit is tied to the total size of the movement over three candles. Profit is equal to the setting "Profit % from Height of Pattern", taking the total movement over three candles and a specific percentage of that movement is reserved as profit. The stop is equal to the setting "Stop % from Height of Pattern".
4. Logic for Adapting Signal Candle Sizes to Volatility.
The adaptation logic itself is called every morning. In the robot, this method is AdaptSoldiersHeight:
It consists of three stages:
1. Calculate the intraday volatility in percentage for the specified number of days.
2. Average this value.
3. Calculate new values for the height of the pattern and the individual candles within it.
5. What about the profit?
The settings mentioned above yield the following equity:
Wishing you successful algorithms!