forked from sdcoffey/techan
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package techan | ||
|
||
import "github.com/sdcoffey/big" | ||
|
||
type trueRangeIndicator struct { | ||
series *TimeSeries | ||
} | ||
|
||
// NewTrueRangeIndicator returns a base indicator | ||
// which calculates the true rangat the current point in time for a series | ||
// https://www.investopedia.com/terms/a/atr.asp | ||
func NewTrueRangeIndicator(series *TimeSeries) Indicator { | ||
return trueRangeIndicator{ | ||
series: series, | ||
} | ||
} | ||
|
||
func (tri trueRangeIndicator) Calculate(index int) big.Decimal { | ||
candle := tri.series.Candles[index] | ||
previousClose := tri.series.Candles[index-1].ClosePrice | ||
|
||
trueHigh := big.MaxSlice(candle.MaxPrice, previousClose) | ||
trueLow := big.MinSlice(candle.MinPrice, previousClose) | ||
|
||
return trueHigh.Sub(trueLow) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package techan | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestTrueRangeIndicator(t *testing.T) { | ||
ts := mockTimeSeriesOCHL( | ||
[]float64{10, 15, 20, 10}, | ||
[]float64{11, 16, 21, 11}, | ||
[]float64{12, 17, 22, 12}, | ||
[]float64{13, 18, 23, 13}, | ||
[]float64{14, 19, 24, 14}, | ||
[]float64{15, 20, 25, 15}, | ||
) | ||
|
||
trueRangeIndicator := NewTrueRangeIndicator(ts) | ||
|
||
decimalEquals(t, 10, trueRangeIndicator.Calculate(4)) | ||
decimalEquals(t, 10, trueRangeIndicator.Calculate(3)) | ||
decimalEquals(t, 10, trueRangeIndicator.Calculate(2)) | ||
decimalEquals(t, 10, trueRangeIndicator.Calculate(1)) | ||
} |