devxpy

MACD histogram relative open/close

devxpy 已更新   
Prelude

This script makes it easy to capture MACD Histogram open/close for automated trading.

There seems to be no "magic" value for MACD Histogram that always works as a cut-off for trade entry/exit, because of the variation in market price over time.

The idea behind this script is to replicate the view of the MACD graph we (humans) see on the screen, in mathematics, so the computer can approximately detect when the curve is opening/closing.

Math

The maths for this is composed of 2 sections -

1. Entry -

i. To trigger entry, we normalize the Histogram value by first determining the lowest and highest values on the MACD curves (MACD, Signal & Hist).
ii. The lowest and highest values are taken over the "Frame of reference" which is a hyperparameter.
iii. Once the frame of reference is determined, the entry cutoff param can be defined with respect to the values from (i) (10% by default)


2. Exit

To trigger an exit, a trader searches for the point where the Histogram starts to drop "steeply".

To convert the notion of "steep" into mathematics -

i. Take the max histogram value reached since last MACD curve flip
ii. Define the cutoff with reference to the value from (i) (30% by default)

Plots

Gray - Dead region
Blue - Histogram opening
Red - Histogram is closing


Notes

A good value for the frame of reference can be estimated by looking at the timescale of the graph you generally work with during manual trading.
For me, that turned out to be ~2.5 hours. (as shown in the above graph)
For a 3-minute ticker, frame of reference = 2.5 * 60 / 3 = 50
Which is the default given in this script.

Ultimately, it is up to you to do grid search and find these hyperparams for the stock and ticker size you're working with.

Also, this script only serves the purpose of detecting the Histogram curve opening/closing.
You may want to add further checks to perform proper trading using MACD.
版本注释:
MACD is a good way to confirm a trend and trade on it.

The problem is that often the trend is weak. When we are watching it manually, during the entry, we can see that that gap has sufficiently opened for confirmation of a possible potential entry. However, during the algo trading, the MACD will trigger a trade entry on a minuscule gap. Often such a scenario would result in sideways movement of the market. And a sideways movement might result in loss.

To prevent this, in this script there is a confirmation on the next candle after the MACD flip, that the gap is sufficiently wide. This gives you some safety from false entry signals and saves some losses. You can change the default values to suit your trade based on backtesting.

Same way, during the exit, if you wait for the MACD crossover flip, you lose some profit, often turning profit into a loss. So, you might want to exit just before the MACD trend reversal crossover. This script offers you to exit when the MACD divergence gap is the lowest just before a possible MACD trend reversal flip over. You can change the default values to suit your trade based on backtesting.
开源脚本

本着真正的TradingView精神,该脚本的作者将其开源发布,以便交易者可以理解和验证它。为作者喝彩!您可以免费使用它,但在出版物中重复使用此代码受网站规则的约束。 您可以收藏它以在图表上使用。

免责声明

这些信息和出版物并不意味着也不构成TradingView提供或认可的金融、投资、交易或其它类型的建议或背书。请在使用条款阅读更多信息。

想在图表上使用此脚本?