-
Notifications
You must be signed in to change notification settings - Fork 78
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
1 parent
1ec9dca
commit bc96c6d
Showing
15 changed files
with
271 additions
and
115 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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
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
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,14 @@ | ||
import { candles } from './spec.helper' | ||
import { AroonDown } from './aroon-down' | ||
|
||
describe('AroonDown', () => { | ||
it('should calculate the lowest close', () => { | ||
const lowest = AroonDown.getLowestPeriodIndex(candles) | ||
expect(lowest).toBe(16) | ||
}) | ||
|
||
it('should calculate correctly', () => { | ||
const lowest = AroonDown.calculate(candles) | ||
expect(lowest).toBe(36) | ||
}) | ||
}) |
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,17 @@ | ||
import { PeriodItem } from '@lib' | ||
|
||
export class AroonDown { | ||
public static calculate(allPeriods: PeriodItem[], length = 25) { | ||
const usedPeriods = [...allPeriods] | ||
usedPeriods.length = length | ||
|
||
return ((length - this.getLowestPeriodIndex(usedPeriods)) / length) * 100 | ||
} | ||
|
||
public static getLowestPeriodIndex(periods: PeriodItem[]) { | ||
const mapped = periods.map(({ close }, idx) => ({ close, idx })) | ||
const sorted = mapped.sort((a, b) => (a.close < b.close ? -1 : 1)) | ||
|
||
return sorted[0].idx | ||
} | ||
} |
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,14 @@ | ||
import { candles } from './spec.helper' | ||
import { AroonUp } from './aroon-up' | ||
|
||
describe('AroonUp', () => { | ||
it('should calculate the lowest low', () => { | ||
const lowest = AroonUp.getHighestPeriodIndex(candles) | ||
expect(lowest).toBe(58) | ||
}) | ||
|
||
it('should calculate correctly', () => { | ||
const lowest = AroonUp.calculate(candles) | ||
expect(lowest).toBe(88) | ||
}) | ||
}) |
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,17 @@ | ||
import { PeriodItem } from '@lib' | ||
|
||
export class AroonUp { | ||
public static calculate(allPeriods: PeriodItem[], length = 25) { | ||
const usedPeriods = [...allPeriods] | ||
usedPeriods.length = length | ||
|
||
return ((length - this.getHighestPeriodIndex(usedPeriods)) / length) * 100 | ||
} | ||
|
||
public static getHighestPeriodIndex(periods: PeriodItem[]) { | ||
const mapped = periods.map(({ close }, idx) => ({ close, idx })) | ||
const sorted = mapped.sort((a, b) => (a.close > b.close ? -1 : 1)) | ||
|
||
return sorted[0].idx | ||
} | ||
} |
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
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,75 @@ | ||
import { PeriodItem } from '@lib' | ||
import { Signal } from '@m8bTypes' | ||
import { AroonDown, AroonUp } from '../../indicators' | ||
import { BaseStrategy, StrategyField } from '../base-strategy' | ||
import { logger } from '../../../util' | ||
|
||
export interface AroonOptions { | ||
period?: string | ||
} | ||
|
||
export interface AroonPeriod { | ||
up: number | ||
down: number | ||
} | ||
|
||
export class Aroon extends BaseStrategy<AroonOptions, void> { | ||
public static description = '' | ||
|
||
public static fields: StrategyField[] = [] | ||
|
||
public options: AroonOptions = {} | ||
|
||
private periods: AroonPeriod[] = [ | ||
{ | ||
up: null, | ||
down: null, | ||
}, | ||
] | ||
|
||
constructor(exchange: string, symbol: string, options?: Partial<AroonOptions>) { | ||
super('aroon', exchange, symbol) | ||
this.options = { ...this.options, ...options } | ||
} | ||
|
||
public calculate(periods: PeriodItem[]) { | ||
if (!periods.length) return | ||
|
||
const up = AroonUp.calculate(periods, 60) | ||
const down = AroonDown.calculate(periods, 60) | ||
|
||
this.periods[0] = { up, down } | ||
} | ||
|
||
public onPeriod() { | ||
const signal = this.getSignal() | ||
const [{ up, down }] = this.periods | ||
|
||
if (!this.isPreroll) logger.debug(`Aroon: up(${up}) down(${down})`) | ||
|
||
return signal | ||
} | ||
|
||
private getSignal(): Signal { | ||
if (this.shouldBuy()) return 'buy' | ||
if (this.shouldSell()) return 'sell' | ||
|
||
return null | ||
} | ||
|
||
private shouldBuy() { | ||
const [{ up, down }] = this.periods | ||
|
||
return up > 50 && down < 50 | ||
} | ||
|
||
private shouldSell() { | ||
const [{ up, down }] = this.periods | ||
|
||
return up < 50 && down > 50 | ||
} | ||
|
||
public newPeriod() { | ||
this.periods.unshift({ up: null, down: null }) | ||
} | ||
} |
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,3 @@ | ||
import { Aroon } from './aroon' | ||
|
||
export const strategy = Aroon |