Skip to content

Commit

Permalink
added more comparators and indicators
Browse files Browse the repository at this point in the history
  • Loading branch information
cangshu888 committed May 5, 2021
1 parent 61d57f1 commit 056b924
Show file tree
Hide file tree
Showing 18 changed files with 503 additions and 57 deletions.
9 changes: 6 additions & 3 deletions backtest.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ async function updateDatabase(backtestResults, strategyID, hasErrors)
backTestResultsID: backtestID,
tradeFrequency: modifiedBacktestResults.tradeFrequency,
alpha: modifiedBacktestResults.alpha,
totalReturn: modifiedBacktestResults.totalReturn,
status: "Backtest complete"
});

Expand Down Expand Up @@ -215,7 +216,7 @@ async function getSPYHistory(date)
{
var SPYresults = [];
var cloud = axios.default.create({});
let url = 'https://api.polygon.io/v2/aggs/ticker/SPY/range/1/month/2019-12-01/' + date + '?unadjusted=false&sort=asc&limit=5000&apiKey=APIKEY';
let url = 'https://api.polygon.io/v2/aggs/ticker/SPY/range/1/month/2019-12-01/' + date + '?unadjusted=false&sort=asc&limit=5000&apiKey=Rm1obTikWQybL7LDoH_yYojQBrrr0SQg';
try
{
let res2 = await cloud.get(url).then(function (data) {
Expand All @@ -226,6 +227,7 @@ async function getSPYHistory(date)
}
catch (err)
{
console.log(err);
return;
}

Expand Down Expand Up @@ -291,7 +293,7 @@ async function runBacktest(strategyParams, availableDates)
for (var i = 0; i < symbols.length; i+=1)
{
var cloud = axios.default.create({});
let url = 'https://api.polygon.io/v2/aggs/ticker/' + symbols[i] + '/range/1/day/2020-01-02/2020-01-02?unadjusted=false&sort=asc&limit=1&apiKey=APIKEY';
let url = 'https://api.polygon.io/v2/aggs/ticker/' + symbols[i] + '/range/1/day/2020-01-02/2020-01-02?unadjusted=false&sort=asc&limit=1&apiKey=Rm1obTikWQybL7LDoH_yYojQBrrr0SQg';

try
{
Expand Down Expand Up @@ -744,6 +746,7 @@ async function runBacktest(strategyParams, availableDates)
trades: trades,
tradeFrequency: tradeFrequency,
sharpeRatio: sharpeRatio,
todayChange: 0,
alpha: alpha,
ROIs: ROIs,
wins: wins,
Expand All @@ -761,7 +764,7 @@ async function getChunk(symbol, start, end, timeframe)
{
let bars = [];
var cloud = axios.default.create({});
let url = 'https://api.polygon.io/v2/aggs/ticker/' + symbol + '/range/' + timeframe.toString() + '/minute/' + start + '/' + end + '?unadjusted=false&sort=asc&limit=50000&apiKey=';
let url = 'https://api.polygon.io/v2/aggs/ticker/' + symbol + '/range/' + timeframe.toString() + '/minute/' + start + '/' + end + '?unadjusted=false&sort=asc&limit=50000&apiKey=Rm1obTikWQybL7LDoH_yYojQBrrr0SQg';
try
{
let res2 = await cloud.get(url).then(function (data) {
Expand Down
5 changes: 5 additions & 0 deletions comparators/BouncesHigherOff.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class BouncesHigherOff extends Comparator {
let currentCandle = this.indicator1.getValue();
let indicator2Value = this.indicator2.getValue();

if (currentCandle == -1)
{
return false;
}

if (indicator2Value == -1)
{
return false;
Expand Down
5 changes: 5 additions & 0 deletions comparators/BouncesLowerOff.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class BouncesLowerOff extends Comparator {
let currentCandle = this.indicator1.getValue();
let indicator2Value = this.indicator2.getValue();

if (currentCandle == -1)
{
return false;
}

if (indicator2Value == -1)
{
return false;
Expand Down
5 changes: 5 additions & 0 deletions comparators/BreaksAbove.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class BreaksAbove extends Comparator {
let currentCandle = this.indicator1.getValue();
let indicator2Value = this.indicator2.getValue();

if (currentCandle == -1)
{
return false;
}

if (indicator2Value == -1)
{
return false;
Expand Down
5 changes: 5 additions & 0 deletions comparators/BreaksBelow.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class BreaksBelow extends Comparator {
let currentCandle = this.indicator1.getValue();
let indicator2Value = this.indicator2.getValue();

if (currentCandle == -1)
{
return false;
}

if (indicator2Value == -1)
{
return false;
Expand Down
54 changes: 49 additions & 5 deletions comparators/Closes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,60 @@ class Closes extends Comparator {
return;
}

checkConditions() {
checkConditions()
{
if (this.indicator2.getName() == "Up")
{
let currentCandle = this.indicator1.getValue();
return (currentCandle.c > currentCandle.o);
if (this.indicator1.getName() == "PreviousNCandles")
{
let priceHistory = this.indicator1.getValue();

for (var i = 1; i < priceHistory.length; i++)
{
if (priceHistory[i].c <= priceHistory[i - 1].c)
{
return false;
}
}

return true;
}
else
{

let currentCandle = this.indicator1.getValue();
if (currentCandle == -1)
{
return false;
}
return (currentCandle.c > currentCandle.o);
}
}
else if (this.indicator2.getName() == "Down")
{
let currentCandle = this.indicator1.getValue();
return (currentCandle.c < currentCandle.o);
if (this.indicator1.getName() == "PreviousNCandles")
{
let priceHistory = this.indicator1.getValue();

for (var i = 1; i < priceHistory.length; i++)
{
if (priceHistory[i].c >= priceHistory[i - 1].c)
{
return false;
}
}

return true;
}
else
{
let currentCandle = this.indicator1.getValue();
if (currentCandle == -1)
{
return false;
}
return (currentCandle.c > currentCandle.o);
}
}

return false;
Expand Down
20 changes: 20 additions & 0 deletions comparators/ClosesAbove.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,31 @@ class ClosesAbove extends Comparator {
let currentCandle = this.indicator1.getValue();
let indicator2Value = this.indicator2.getValue();

if (currentCandle == -1)
{
return false;
}

if (indicator2Value == -1)
{
return false;
}

if (this.indicator1.getName() == "PreviousNCandles")
{
let priceHistory = this.indicator1.getValue();

for (var i = 0; i < priceHistory.length; i++)
{
if (priceHistory[i].c <= indicator2Value)
{
return false;
}
}

return true;
}

if (currentCandle.c > indicator2Value && this.indicator2.getName() == "HighOfFirstNMinutes")
{
this.indicator2.alreadyMetConditions = true;
Expand Down
20 changes: 20 additions & 0 deletions comparators/ClosesBelow.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,31 @@ class ClosesBelow extends Comparator {
let currentCandle = this.indicator1.getValue();
let indicator2Value = this.indicator2.getValue();

if (currentCandle == -1)
{
return false;
}

if (indicator2Value == -1)
{
return false;
}

if (this.indicator1.getName() == "PreviousNCandles")
{
let priceHistory = this.indicator1.getValue();

for (var i = 0; i < priceHistory.length; i++)
{
if (priceHistory[i].c >= indicator2Value)
{
return false;
}
}

return true;
}

if (currentCandle.c < indicator2Value && this.indicator2.getName() == "LowOfFirstNMinutes")
{
this.indicator2.alreadyMetConditions = true;
Expand Down
38 changes: 38 additions & 0 deletions comparators/CrossesAbove.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const Comparator = require("../comparator");

class CrossesAbove extends Comparator {
constructor(indicator1, indicator2) {
super(indicator1, indicator2); // call the super class constructor and pass in the name parameter
this.indicator1 = indicator1;
this.indicator2 = indicator2;
}

initialize() {
return;
}

reset()
{
return;
}

checkConditions() {
let indicator1History = this.indicator1.getHistory();
let indicator2History = this.indicator2.getHistory();

if (indicator1History.length < 2)
{
return false;
}

if (indicator2History.length < 2)
{
return false;
}

return (indicator1History[indicator1History.length - 2] < indicator2History[indicator2History.length - 2]
&& indicator1History[indicator1History.length - 1] > indicator2History[indicator2History.length - 1]);
}
}

module.exports = CrossesAbove;
38 changes: 38 additions & 0 deletions comparators/CrossesBelow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const Comparator = require("../comparator");

class CrossesBelow extends Comparator {
constructor(indicator1, indicator2) {
super(indicator1, indicator2); // call the super class constructor and pass in the name parameter
this.indicator1 = indicator1;
this.indicator2 = indicator2;
}

initialize() {
return;
}

reset()
{
return;
}

checkConditions() {
let indicator1History = this.indicator1.getHistory();
let indicator2History = this.indicator2.getHistory();

if (indicator1History.length < 2)
{
return false;
}

if (indicator2History.length < 2)
{
return false;
}

return (indicator1History[indicator1History.length - 2] > indicator2History[indicator2History.length - 2]
&& indicator1History[indicator1History.length - 1] < indicator2History[indicator2History.length - 1]);
}
}

module.exports = CrossesBelow;
5 changes: 5 additions & 0 deletions comparators/FallsTo.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class FallsTo extends Comparator {
let currentCandle = this.indicator1.getValue();
let indicator2Value = this.indicator2.getValue();

if (currentCandle == -1)
{
return false;
}

if (indicator2Value == -1)
{
return false;
Expand Down
Loading

0 comments on commit 056b924

Please sign in to comment.