# MokuNui - Supply n Demand (Auto-Levels)

This indicator aims to identify supply and demand levels in the market by calculating buy and sell pressure based on weighted closing prices.

The indicator starts by defining several variables and input parameters. The 'n' parameter determines the period used for calculations, while the 'DemandIndexValue' parameter sets the threshold for identifying demand levels.

The indicator uses the high, low, close, volume, and bar index data to calculate various values. It calculates the weighted closing price (wC) as the average of the high, low, and twice the closing price divided by four. It also calculates the rate of change of the weighted closing price (wCR) as the difference between the current wC and the previous wC divided by the minimum value between the two.

Next, it calculates the buy pressure (buyP) and sell pressure (sellP) values based on the wCR and volume. If the wCR is positive, buyP is set to vR (volume divided by the average volume), and sellP is set to vPerC (vR divided by the exponential of the minimum value between 88 and cR). Otherwise, if wCR is negative, the values are assigned in the opposite way.

To smooth out the buy and sell pressure values, the indicator uses a rolling average calculation. It maintains two variables, buyPres and sellPres, which are updated for each new data point. These variables store the cumulative sum of the buy and sell pressure values over the specified period 'n' and divide it by 'n' to get the average. This process helps reduce noise and provide a smoother representation of the pressure values.

The indicator then calculates the Demand Index (DI) based on the buyPres and sellPres values. If the difference between the current and previous buyPres and sellPres values is positive, DI is set to - (buyPres / sellPres) if sellPres is not zero; otherwise, it is set to -1. If the difference is negative, DI is set to sellPres / buyPres if buyPres is not zero; otherwise, it is set to 1. The DI value is used to determine the direction of demand in the market, with values less than 0 indicating a downtrend and values greater than 0 indicating an uptrend.

Using the DI values, the indicator calculates the Demand Index (DMI) by assigning a value of -1 - DI for downtrends and 1 - DI for uptrends. These values represent the market sentiment regarding supply and demand.

The indicator then tracks the lowest demand level (DemandL) and the highest demand level (DemandH). When the DMI crosses under the specified DemandIndexValue, it updates the DemandL to the current low price (l). If the DMI is less than -0.2 and the current low price is lower than the previous DemandL, it updates the DemandL; otherwise, it retains the previous value. The indicator also tracks the bar index (DemandX) corresponding to the demand level.

Similarly, the indicator tracks the highest supply level (SupplyH) and the lowest supply level (SupplyL). When the DMI crosses over the DemandIndexValue, it updates the SupplyH to the current high price (h). If the DMI is greater than 0.5 and the current high price is higher than the previous SupplyH, it updates the SupplyH; otherwise, it retains the previous value. The indicator also tracks the bar index (SupplyX) corresponding to the supply level.

Using the recorded demand and supply levels, the indicator draws lines and cloud areas on the price chart to represent them. It creates a shaded area (dcloud) between the DemandL and DemandH using the linefill.new function with a specified color. Similarly, it creates a shaded area (scloud) between the SupplyL and SupplyH to represent the supply zone. Additionally, it creates a shaded area (rcloud) above the SupplyH to indicate potential resistance levels based on the Average True Range (atr).

The indicator also plots two lines, DH and DL, to represent the demand levels. The line.set_xy1 and line.set_xy2 functions are used to set the line's coordinates based on the bar index and the corresponding demand level. The line.set_extend function is used to extend the line to the right of the chart.

Likewise, it plots two lines, SH and SL, to represent the supply levels. These lines are set using the same functions as the demand lines.

Finally, the indicator plots two lines, RH and RL, to represent the potential resistance levels. These lines are set based on the bar index and the calculated resistance value. They are also extended to the right of the chart.

Protected script
This script is published closed-source but you may use it freely. You can favorite it to use it on a chart. You cannot view or modify its source code.
Disclaimer