Hi everyone!
I’m trying to code the prorealtime supertrend in python with the native tws api.
I noticed my ATR which is essential for calculating the supertrend it sometimes very far from the PRT indicator.
I have tried several different functions for calculating ATR but nothing helps.
Is it possible to find the source code for ATR or supertrend?
Perhaps have you another way of thinking ?
Thanks everyone
Regards,
Ben
There you go:
// Super Trend custom
//
// https://stackoverflow.com/questions/44935269/supertrend-code-using-pandas-python
//
//Multiplier = 3
//Periods = 10
Multiplier = Max(1,Min(999,Multiplier))
Periods = Max(1,Min(999,Periods))
IF BarIndex > Max(Periods,Multiplier) THEN
MyATR = AverageTrueRange[Periods](close) * Multiplier
BasicUPPER = MedianPrice + MyATR
BasicLOWER = MedianPrice - MyATR
IF (BasicUPPER < FinalUPPER[1]) OR (close[1] > FinalUPPER[1]) THEN
FinalUPPER = BasicUPPER
ENDIF
IF (BasicLOWER > FinalLOWER[1]) OR (close[1] < FinalLOWER[1]) THEN
FinalLOWER = BasicLOWER
ENDIF
IF (ST[1] = FinalUPPER[1]) AND (close <= FinalUPPER) THEN
ST = FinalUPPER
ELSE
IF (ST[1] = FinalUPPER[1]) AND (close > FinalUPPER) THEN
ST = FinalLOWER
ELSE
IF (ST[1] = FinalLOWER[1]) AND (close >= FinalLOWER) THEN
ST = FinalLOWER
ELSE
IF (ST[1] = FinalLOWER[1]) AND (close < FinalLOWER) THEN
ST = FinalUPPER
ENDIF
ENDIF
ENDIF
ENDIF
ENDIF
RETURN ST AS "Super Trend"
Hi robertogozz
Yes it’s almost what i need, I would like to have a look inside “AverageTrueRange”
I will try some ideas from your stackoverflow link
Thanks !
Atr (and TR):
// ATR formula
//
// https://en.wikipedia.org/wiki/Average_true_range
// https://www.investopedia.com/terms/a/atr.asp
//
// TR calculations:
//
// MyTR = max(Range,max(abs (high − close[1]),abs (low − close[1])))
//
// The true range is the largest of the:
// - Most recent period's high minus the most recent period's low
// - Absolute value of the most recent period's high minus the
// previous close
// - Absolute value of the most recent period's low minus the
// previous close
//
// Calculation of ATR (of P periods):
//p = 10
p = max(2,min(999,p)) //range 2 - 999
MyTR = max(Range,max(abs(high - close[1]),abs(low - close[1])))
IF BarIndex < p THEN
MyATR = MyTR
ELSE
MyATR = ((MyATR[1] * (p - 1)) + MyTR) / p
ENDIF
RETURN MyATR