The indicator looks like this:
It has 4 lines and one chart:
1. Displays the number of trades within the candle.
2. Number of trades for selling.
3. Number of trades for buying.
4. Delta - the difference between the buying trades line and the selling trades line.
5. Moving average Delta.
1. Overview of the code line by line.
Lines 1-4:
Necessary namespaces are imported:
Lines 6-8:
The namespace CustomIndicators.Scripts is defined for code organization, and the class DeltaByCandles inherits from Aindicator:
Lines 10-16:
This block is responsible for setting up the datasets and indicator parameters:
Lines 20-36:
Creation of datasets and indicator parameters:
We check if the indicator is used or not:
If the indicator is used, we create series and parameters.
If not, we reset all series.
Lines 40-52:
Dynamic method for updating the indicator values:
Method OnProcess:
We check for the presence of elements in the array; if there are none, we exit.
Next, we add new values to each dataset:
_seriesTrade = total number of trades.
_seriesBuy = we call the GetTradeInfo method specifying the direction Buy.
_seriesSell = we call the GetTradeInfo method specifying the direction Sell.
_seriesDelta = we call the GetTradeInfo method specifying the direction Buy, then call the GetTradeInfo method specifying the direction Sell.
_seriesSmaDelta = we call the Sma method.
Lines 54-82:
The method counts the number of trades of a specific type:
Method GetTradesInfo:
1. A variable value is initialized to store the number of trades, initially set to 0.
2. The side of the trade (side) is checked:
If it's the buying side (Side.Buy):
Iterate through all trades (Trades) within the specified candle (candles[index]).
For each trade, the side of the trade (Trade.Side) is checked:
If the side matches the desired one (buy), the value is increased by 1.
If it's the selling side (Side.Sell):
Iterate through all trades (Trades) within the specified candle (candles[index]).
For each trade, the side of the trade (Trade.Side) is checked:
If the side matches the desired one (sell), the value is increased by 1.
3. The final value representing the number of trades on the desired side (buy or sell) within the specified candle is returned.
Lines 84-99:
Method for finding the moving average:
Method Sma:
1. It checks if there are enough elements in the list to compute the average. If the index index is less than the specified value _lengthSmaDelta.ValueInt, it returns 0, as there is insufficient data.
2. A variable values is initialized to store the sum of values, initially set to 0.
3. Previous elements in the list are iterated:
For each element from value[index - 1] to value[index - (_lengthSmaDelta.ValueInt - 1)], its value is added to values.
4. The average is computed by dividing the sum values by the number of elements _lengthSmaDelta.ValueInt.
5. The computed average is returned.
2. How to correctly enable the indicator.
To operate the indicator in Tester Light, we will need to download tick data in OsData.
How to download tick data for Tester Light.
After downloading, we go into Tester Light and in DataSetting:
We check this box, and everything will work.
To run the indicator in Bot Station Light, we also go into DataSetting and check the box.
Good luck with your algorithms!