Support and Resistance: Triangles [YinYangAlgorithms]Overview:
Triangles have always been known to be the strongest shape. Well, why wouldn’t that likewise apply to trading? This Indicator will create Upwards and Downwards Triangles which in turn create Support and Resistance locations. For example, we find 2 highs that meet the criteria (within deviation %, Minimum Distance and Lookback Distance). We calculate the distance between these two and create an Equilateral Triangle Downwards (You can adjust the % if you want more of an Isosceles Triangle). The midpoint (tip) of this triangle is the Support and the bottom (base) of it is the Resistance. The exact opposite applies for an Upwards Triangle.
The reason why Triangles may make for good Support and Resistance locations is the % 's used, much like the fibonacci, use ratios relevant in nature and everywhere in the world around us, so why not for trading too?
Tutorial:
If you look at the locations we’ve circled above, all of them exhibit strong rejections are predictive Support and Resistance locations plotted by the triangles created. There can only ever be 1 Upward and 1 Downward Triangle at a time, so when a new one is created, the Support and Resistance locations are moved.
If you scroll back far enough you’ll notice the Triangles disappear but their Support and Resistance locations are still plotted. This has to do with the fact you are allowed only so many Lines plotted and when a new Triangle is created, an old one will be removed. The Support and Resistance locations however will stay.
If we look at the example above, you can see the Support and Resistance locations the Triangles made here may have helped predict where the price would struggle to surpass.
By default the Look Back Distance is set to 50 and the Min Distance is 10 (settings used in all previous examples). However, you can modify these to make Triangles more ‘Rare’ and therefore the Support and Resistance locations change less. In the example above for Instance we left Look Back Distance to 50 but changed Min Distance from 10 to 25. This results in Support and Resistance locations that may hold better in the long term.
If we scroll back a bit, we can see the settings ‘Look Back Distance’ 50 and ‘Minimum Distance’ 25 had done a decent job at predicting the ATH resistance and many Support and Resistance locations around it. Keep in mind, previous results don’t mean future results, but Triangles may create ratios which apply well to trading.
We will conclude our Tutorial here. Hopefully you can see the benefit to the ratio Triangles make when predicting Support and Resistance locations.
Settings:
Show Triangles: If all you want to know is the Support and Resistance locations, there’s no need to draw the Triangles.
Triangle Zones: What types of triangles should we create our zones for? Options are Upward, Downward, Both, None.
Max Deviation Allowed: Maximum Deviation up or down from the last bars High/Low for potential to create a Triangle.
Lookback Distance: How far back we look to see for potential of a High/Low within Deviation range.
Min Distance: This is so triangles are spaced properly and not from 2 bars beside each other. Min distance allocated between 2 points to create a Triangle.
Bar Percent Increase: How much % multiplier do we apply for each bar spacing of the triangle. 0.005 creates a close to Equilateral Triangle, but other values like 0.004 and 0.006 seem to work well too.
If you have any questions, comments, ideas or concerns please don't hesitate to contact us.
HAPPY TRADING!
Algorithms
YinYang Bar ForecastOverview:
YinYang Bar Forecast is a prediction indicator. It predicts the movement for High, Low, Open and Close for up to 13 bars into the future. We created this Indicator as we felt the TradingView community could benefit from a bar forecast as there wasn’t any currently available.
Our YinYang Bar Forecast is something we plan on continuously working on to better improve it, but at its current state it is still very useful and decently accurate. It features many calculations to derive what it thinks the future bars will hold. Let’s discuss some of the logic behind it:
Each bar has its High, Low, Open and Close calculated individually for highest accuracy. Within these calculations we first check which bar it is we are calculating and base our span back length that we are getting our data from based on the bar index we are generating. This helps us get a Moving Average for this bar index.
We take this MA and we apply our Custom Volume Filter calculation on it, which is essentially us dividing the current bars volume over the average volume in the last ‘Filtered Length’ (Setting) length. We take this decimal and multiply it on our MA and smooth it out with a VWMA.
We take the new Volume Filtered MA and apply a RSI Filter calculation on it. RSI Filter is where we take the difference between the high and low of this bar and we multiply it with an RSI calculation using our Volume Filtered MA. We take the result of that multiplication and either add or subtract it from the Volume Filtered MA based on if close > open. This makes our RSI Filtered MA.
Next, we do an EMA Strength Calculation which is where we check if close > ema(close, ‘EMA Averaged Length’) (Setting). Based on this condition we assign a multiplier that is applied to our RSI Filtered MA. We divide by how many bars we are predicting and add a bit to each predictive bar so that the further we go into the future the stronger the strength is.
Next we check RSI and RSI MA levels and apply multiplications based on its RSI levels and if it is greater than or less than the MA. Also it is affected by if the RSI is <= 30 and >= 70.
Finally we check the MFI and MFI MA levels and like RSI we apply multiplications based on its MFI levels and if it is greater than or less than the MA. It is also affected by if the MFI is <= 30 and >= 70.
Please note the way we calculate this may change in the future, this is just currently what we deemed works best for forecasting the future bars. Also note this script uses MA calculations out of scope for efficiency but there is potential for inconsistencies.
Innately it’s main use is the projection it provides. It only draws the bars for realtime bars and not historical ones, so the best way to backtest it is with TradingView’s Replay Tool.
Well, enough of the logic behind it, let's get to understanding how to use it:
Tutorial:
So unfortunately we aren’t able to plot legit bars/candles into the future so we’ve had to do a bit of a work around using lines and fills. As you can see here we have 4 Lines and 3 Zones:
Lines:
Green: Represents the High
Orange: Represents the Open
Teal: Represents the Close
Red: Represents the Low
Zones:
High Zone: This zone is from either Open or Close to the High and is ALWAYS filled with Green.
Open/Close Zone: This zone is from the Open to the Close and is filled with either Green or Red based on if it's greater than the previous bar (real or forecasted).
Low Zone: This zone is from either Open or Close to the Low and is ALWAYS filled with Red.
As you can see generally the Forecasted bars are generally within strong pivot locations and are a good estimation of what will likely go on. Please note, the WHOLE structure of the prediction can change based on the current bars movements and the way it affects the calculations.
Let's look 1 bar back from the current bar just so we can see what it used to Forecast:
As you can see it has changed quite a bit from the previous bar, but if you look close, we drew horizontal lines around where its projecting the next bar to be (our current realtime bar), if we go back to the live chart:
Its projections were pretty close for the high and low. Generally, right now at least, it does a much better job at predicting the high and low than it does the open and close, however we will do our best to fine tune that in future updates.
Remember, this indicator is not meant to base your trades on, but rather give you a Forecast towards the general direction of the next few bars. Somewhat like weather, the farther the bar (or day for weather), the harder it is to predict. For this reason we recommend you focusing on the first few bars as they are more accurate, but review the further ones as they may help show the trend and the way that pair will move.
We will conclude this tutorial here, hopefully this Predictive Indicator can be of some help and use to you. If you have any questions, comments, ideas or concerns please let us know.
Settings:
Forecast Length: How many bars should we predict into the Future? Max 13
Each Bar Length Multiplier: For each new Forecast bar, how many more bars are averaged? Min 2
VWMA Averaged Length: All Forecast bars are put into a VWMA, what length should we use?
EMA Averaged Length: All Forecast bars are put into a EMA, what length should we use?
Filtered Length: What length should we use for Filtered Volume and RSI?
EMA Strength Length: What length should we use for the EMA Strength
HAPPY TRADING!
YinYang RSI Volume Trend StrategyThere are many strategies that use RSI or Volume but very few that take advantage of how useful and important the two of them combined are. This strategy uses the Highs and Lows with Volume and RSI weighted calculations on top of them. You may be wondering how much of an impact Volume and RSI can have on the prices; the answer is a lot and we will discuss those with plenty of examples below, but first…
How does this strategy work?
It’s simple really, when the purchase source crosses above the inner low band (red) it creates a Buy or Long. This long has a Trailing Stop Loss band (the outer low band that's also red) that can be adjusted in the Settings. The Stop Loss is based on a % of the inner low band’s price and by default it is 0.1% lower than the inner band’s price. This Stop Loss is not only a stop loss but it can also act as a Purchase Available location.
You can get back into a trade after a stop loss / take profit has been hit when your Reset Purchase Availability After condition has been met. This can either be at Stop Loss, Entry or None.
It is advised to allow it to reset in case the stop loss was a fake out but the call was right. Sometimes it may trigger stop loss multiple times in a row, but you don’t lose much on stop loss and you gain lots when the call is right.
The Take Profit location is the basis line (white). Take Profit occurs when the Exit Source (close, open, high, low or other) crosses the basis line and then on a different bar the Exit Source crosses back over the basis line. For example, if it was a Long and the bar’s Exit Source closed above the basis line, and then 2 bars later its Exit Source closed below the basis line, Take Profit would occur. You can disable Take Profit in Settings, but it is very useful as many times the price will cross the Basis and then correct back rather than making it all the way to the opposing zone.
Longs:
If for instance your Long doesn’t need to Take Profit and instead reaches the top zone, it will close the position when it crosses above the inner top line (green).
Please note you can change the Exit Source too which is what source (close, open, high, low) it uses to end the trades.
The Shorts work the same way as the Long but just opposite, they start when the purchase source crosses under the inner upper band (green).
Shorts:
Shorts take profit when it crosses under the basis line and then crosses back.
Shorts will Stop loss when their outer upper band (green) is crossed with the Exit Source.
Short trades are completed and closed when its Exit Source crosses under the inner low red band.
So, now that you understand how the strategy works, let’s discuss why this strategy works and how it is profitable.
First we will discuss Volume as we deem it plays a much bigger role overall and in our strategy:
As I’m sure many of you know, Volume plays a huge factor in how much something moves, but it also plays a role in the strength of the movement. For instance, let’s look at two scenarios:
Bitcoin’s price goes up $1000 in 1 Day but the Volume was only 10 million
Bitcoin’s price goes up $200 in 1 Day but the Volume was 40 million
If you were to only look at the price, you’d say #1 was more important because the price moved x5 the amount as #2, but once you factor in the volume, you know this is not true. The reason why Volume plays such a huge role in Price movement is because it shows there is a large Limit Order battle going on. It means that both Bears and Bulls believe that price is a good time to Buy and Sell. This creates a strong Support and Resistance price point in this location. If we look at scenario #2, when there is high volume, especially if it is drastically larger than the average volume Bitcoin was displaying recently, what can we decipher from this? Well, the biggest take away is that the Bull’s won the battle, and that likely when that happens we will see bullish movement continuing to happen as most of the Bears Limit Orders have been fulfilled. Whereas with #2, when large price movement happens and Bitcoin goes up $1000 with low volume what can we deduce? The main takeaway is that Bull’s pressured the price up with Market Orders where they purchased the best available price, also what this means is there were very few people who were wanting to sell. This generally dictates that Whale Limit orders for Sells/Shorts are much higher up and theres room for movement, but it also means there is likely a whale that is ready to dump and crash it back down.
You may be wondering, what did this example have to do with YinYang RSI Volume Trend Strategy? Well the reason we’ve discussed this is because we use Volume multiple times to apply multiplications in our calculations to add large weight to the price when there is lots of volume (this is applied both positively and negatively). For instance, if the price drops a little and there is high volume, our strategy will move its bounds MUCH lower than the price actually dropped, and if there was low volume but the price dropped A LOT, our strategy will only move its bounds a little. We believe this reflects higher levels of price accuracy than just price alone based on the examples described above.
Don’t believe us?
Here is with Volume NOT factored in (VWMA = SMA and we remove our Volume Filter calculation):
Which produced -$2880 Profit
Here is with our Volume factored in:
Which produced $553,000 (55.3%)
As you can see, we wen’t from $-2800 profit with volume not factored to $553,000 with volume factored. That's quite a big difference! (Please note previous success does not predict future success we are simply displaying the $ amounts as example).
Now how about RSI and why does it matter in this strategy?
As I’m sure most of you are aware, RSI is one of the leading indicators used in trading. For this reason we figured it would only make sense to incorporate it into our calculations. We fiddled with RSI for quite awhile and sometimes what logically seems to be the right way to use it isn’t. Now, because of this, our RSI calculation is a little odd, but basically what we’re doing is we calculate the RSI, then turn it into a percentage (between 0-1) that can easily be multiplied to the price point we need. The price point we use is the difference between our high purchase zone and our low purchase zone. This allows us to see how much price movement there is between zones. We multiply our zone size with our RSI multiplication and we get the amount we will add +/- to our basis line (white line). This officially creates the NEW high and low purchase zones that we are actually using and displaying in our trades.
If you found that confusing, here are some examples to why it is an important calculation for this strategy:
Before RSI factored in:
Which produced 27.8% Profit
After RSI factored in:
Which produced 553% Profit
As you can see, the RSI makes not only the purchase zones more accurate, but it also greatly increases the profit the strategy is able to make. It also helps ensure an relatively linear profit slope so you know it is reliable with its trades.
This strategy can work on pretty much anything, but you should tweak the values a bit for each pair you are trading it with for best results.
We hope you can find some use out of this simple but effective strategy, if you have any questions, comments or concerns please let us know.
HAPPY TRADING!
Emulating binary operations and several values in one variableBinary operations and storing several numbers in one variable.
It's useful when you need to pass a values numbers to another study. Study-on-study (SOS) in Tradingview allows passing only one value, which is not always convinient. So if we put all those numbers in one - we can pass more values from one study to another.
In Pine we can use up to 52 bits for our data. Because of that we can put 6 1-byte (8-bit) values in one float. Or 12 4-bit values. Or 52 1-bit values.
MACD, backtest 2015+ only, cut in half and doubledThis is only a slight modification to the existing "MACD Strategy" strategy plugin!
found the default MACD strategy to be lacking, although impressive for its simplicity. I added "year>2014" to the IF buy/sell conditions so it will only backtest from 2015 and beyond ** .
I also had a problem with the standard MACD trading late, per se. To that end I modified the inputs for fast/slow/signal to double. Example: my defaults are 10, 21, 10 so I put 20, 42, 20 in. This has the effect of making a 30min interval the same as 1 hour at 10,21,10. So if you want to backtest at 4hr, you would set your time interval to 2hr on the main chart. This is a handy way to make shorter time periods more useful even regardless of strategy/testing, since you can view 15min with alot less noise but a better response.
Used on BTCCNY OKcoin, with the chart set at 45 min (so really 90min in the strategy) this gave me a percent profitable of 42% and a profit factor of 1.998 on 189 trades.
Personally, I like to set the length/signals to 30,63,30. Meaning you need to triple the time, it allows for much better use of shorter time periods and the backtests are remarkably profitable. (i.e. 15min chart view = 45min on script, 30min= 1.5hr on script)
** If you want more specific time periods you need to try plugging in different bar values: replace "year" with "n" and "2014" with "5500". The bars are based on unix time I believe so you will need to play around with the number for n, with n being the numbers of bars.