forked from askmike/gekko
-
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.
Merge pull request #15 from askmike/develop
Merge
- Loading branch information
Showing
24 changed files
with
694 additions
and
50 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
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,80 @@ | ||
// wraps around a low level trigger and feeds | ||
// it live market data. | ||
|
||
const _ = require('lodash'); | ||
|
||
const exchangeUtils = require('./exchangeUtils'); | ||
const bindAll = exchangeUtils.bindAll; | ||
|
||
const triggers = require('./triggers'); | ||
|
||
// @param api: a gekko broker wrapper instance | ||
// @param type: type of trigger to wrap | ||
// @param props: properties to feed to trigger | ||
class Trigger { | ||
constructor({api, type, props, onTrigger}) { | ||
this.onTrigger = onTrigger; | ||
this.api = api; | ||
|
||
this.isLive = true; | ||
|
||
// note: we stay on the safe side and trigger | ||
// as soon as the bid goes below trail. | ||
this.tickerProp = 'bid'; | ||
|
||
if(!_.has(triggers, type)) { | ||
throw new Error('Gekko Broker does not know trigger ' + type); | ||
} | ||
|
||
this.CHECK_INTERVAL = this.api.interval * 10; | ||
|
||
bindAll(this); | ||
this.trigger = new triggers[type]({ | ||
onTrigger: this.propogateTrigger, | ||
...props | ||
}) | ||
|
||
this.scheduleFetch(); | ||
} | ||
|
||
scheduleFetch() { | ||
this.timout = setTimeout(this.fetch, this.CHECK_INTERVAL); | ||
} | ||
|
||
fetch() { | ||
if(!this.isLive) { | ||
return; | ||
} | ||
this.api.getTicker(this.processTicker) | ||
} | ||
|
||
processTicker(err, ticker) { | ||
if(!this.isLive) { | ||
return; | ||
} | ||
|
||
if(err) { | ||
return console.log('[GB/trigger] failed to fetch ticker:', err); | ||
} | ||
|
||
this.price = ticker[this.tickerProp]; | ||
|
||
this.trigger.updatePrice(this.price); | ||
this.scheduleFetch(); | ||
} | ||
|
||
cancel() { | ||
this.isLive = false; | ||
clearTimeout(this.timout); | ||
} | ||
|
||
propogateTrigger(payload) { | ||
if(!this.isLive) { | ||
return; | ||
} | ||
this.isLive = false; | ||
this.onTrigger(payload); | ||
} | ||
} | ||
|
||
module.exports = Trigger; |
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 @@ | ||
const trailingStop = require('./trailingStop'); | ||
|
||
module.exports = { trailingStop }; |
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,62 @@ | ||
const EventEmitter = require('events'); | ||
|
||
// Note: as of now only supports trailing the price going up (after | ||
// a buy), on trigger (when the price moves down) you should sell. | ||
|
||
|
||
// @param initialPrice: initial price, preferably buy price | ||
// @param trail: fixed offset from the price | ||
// @param onTrigger: fn to call when the stop triggers | ||
class TrailingStop extends EventEmitter { | ||
constructor({trail, initialPrice, onTrigger}) { | ||
super(); | ||
|
||
this.trail = trail; | ||
this.isLive = true; | ||
this.onTrigger = onTrigger; | ||
|
||
this.previousPrice = initialPrice; | ||
this.trailingPoint = initialPrice - this.trail; | ||
} | ||
|
||
updatePrice(price) { | ||
if(!this.isLive) { | ||
return; | ||
} | ||
|
||
if(price > this.trailingPoint + this.trail) { | ||
this.trailingPoint = price - this.trail; | ||
} | ||
|
||
this.previousPrice = price; | ||
|
||
if(price <= this.trailingPoint) { | ||
this.trigger(); | ||
} | ||
} | ||
|
||
updateTrail(trail) { | ||
if(!this.isLive) { | ||
return; | ||
} | ||
|
||
this.trail = trail; | ||
this.trailingPoint = this.previousPrice - this.trail; | ||
// recheck whether moving the trail triggered. | ||
this.updatePrice(this.previousPrice); | ||
} | ||
|
||
trigger() { | ||
if(!this.isLive) { | ||
return; | ||
} | ||
|
||
this.isLive = false; | ||
if(this.onTrigger) { | ||
this.onTrigger(this.previousPrice); | ||
} | ||
this.emit('trigger', this.previousPrice); | ||
} | ||
} | ||
|
||
module.exports = TrailingStop; |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.