Skip to content
This repository has been archived by the owner on Feb 16, 2020. It is now read-only.

Brute forcer proof of concept (Work in progress or As Inspiration) #1204

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion web/vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"webpack-dev-server": "^2.1.0-beta.0"
},
"dependencies": {
"jade": "^1.11.0"
"jade": "^1.11.0",
"shuffle-array": "^1.0.1"
}
}
78 changes: 69 additions & 9 deletions web/vue/src/components/backtester/backtester.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
p Running backtest..
spinner
result(v-if='backtestResult && backtestState === "fetched"', :result='backtestResult')
bruteForceResults(v-if='topResultsByProfit && bruteForceResultState === "newResult"', :topResultsByProfit='topResultsByProfit', :bruteForcedResultsCount='bruteForcedResultsCount', :bruteForceCombinationsCount='bruteForceCombinationsCount')
</template>

<script>
import configBuilder from './backtestConfigBuilder.vue'
import result from './result/result.vue'
import bruteForceResults from './result/bruteForceResults.vue'
import { post } from '../../tools/ajax'
import spinner from '../global/blockSpinner.vue'

Expand All @@ -24,22 +26,29 @@ export default {
backtestable: false,
backtestState: 'idle',
backtestResult: false,
bruteForcedResultsCount: "0",
bruteForceCombinationsCount: "0",
topResults: { min: undefined, max: undefined },
topResultsByProfit: [],
bruteForceResults: [],
config: false,
}
},
methods: {
check: function(config) {
// console.log('CHECK', config);
this.config = config;

if(!config.valid)
return this.backtestable = false;

this.backtestable = true;
},
run: function() {
this.backtestState = 'fetching';

getReport: function(next, resetFetchingState) {
const self = this;
if (!resetFetchingState) { resetFetchingState = true; }
if (resetFetchingState) {
this.backtestState = 'fetching';
}
const req = {
gekkoConfig: this.config,
data: {
Expand All @@ -50,16 +59,67 @@ export default {
trades: true
}
}

post('backtest', req, (error, response) => {
this.backtestState = 'fetched';
this.backtestResult = response;
});
if (next) {
return post('backtest', req, (error, response) => {
self.bruteForceResultState = 'newResult';
self.backtestState = 'fetched';
self.bruteForcedResultsCount = (parseInt(self.bruteForcedResultsCount)+1).toString();
if (response.report.profit < self.topResults.min || typeof(self.topResults.min == 'undefined')) {
this.topResults.min = response.report.profit;
}
if (response.report.profit > self.topResults.max || typeof(self.topResults.max == 'undefined')) {
// Keep top 10 results by profit
self.topResults.max = response.report.profit;
Object.assign(response, { params: self.config[self.config.tradingAdvisor.method] });
self.topResultsByProfit.push(response);
self.topResultsByProfit.sort(function(a,b) {
return (a.report.profit < b.report.profit) ? 1 : ((b.report.profit < a.report.profit) ? -1 : 0);
});
self.topResultsByProfit = self.topResultsByProfit.slice(0, 10);
}
// Keep all results in order
// self.bruteForceResults.push(response);
next(error, response);
});
} else {
return post('backtest', req, (error, response) => {
self.backtestState = 'fetched';
self.backtestResult = response;
});
}
},
getReports: function(bruteforceParamsPermutations, i) {
const self = this;
if (!i) { i = 0; }
while (i < bruteforceParamsPermutations.length) {
console.log(' + Testing strategy with params: ', bruteforceParamsPermutations[i]['params']);
// Prepare config using a permuted params combination
this.config[this.config.tradingAdvisor.method] = bruteforceParamsPermutations[i]['params'];
return (function(i) {
i++;
self.getReport((err, response) => {
if (!err) {
self.getReports(bruteforceParamsPermutations, i);
}
}, false);
})(i);
}
},
run: function() {
// Are we brute forcing the strategy params?
if (window.bruteForcer && window.bruteForcer.isConfigured()) {
this.bruteForceCombinationsCount = window.bruteForcer.config.bruteforceParamsPermutations.length.toString();
this.getReports(window.bruteForcer.config.bruteforceParamsPermutations);
} else {
// Are we just testing a single set of params?
this.getReport();
}
}
},
components: {
configBuilder,
result,
bruteForceResults,
spinner
}
}
Expand Down
51 changes: 51 additions & 0 deletions web/vue/src/components/backtester/result/bruteForceResults.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<template lang='jade'>
div
.hr.contain
div.contain
h3.top-10 Top 10 by profit
div.count Tested {{ bruteForcedResultsCount }} of {{bruteForceCombinationsCount}} combinations
div(v-for='topResult in topResultsByProfit')
div.contain
div.tested-params
span(v-for="(paramValue, paramName) in topResult.params")
span.param-name {{ paramName }}&nbsp;
span.param-value {{ paramValue }}&nbsp;
result-summary(:report='topResult.report')
</template>

<script>
import resultSummary from './summary.vue'
import chart from './chartWrapper.vue'
import roundtripTable from './roundtripTable.vue'

export default {
props: ['bruteForcedResultsCount', 'bruteForceCombinationsCount', 'topResultsByProfit'],
data: () => {
return {}
},
methods: {},
components: {
roundtripTable,
resultSummary,
chart
}
}
</script>

<style>
.top-10 {
font-weight: 600;
}
.top-10, .count {
text-align: center;
}
.tested-params {
text-align: center;
border: 1px dashed rgba(144,144,144,.99);
margin: 15px 0;
padding: 5px;
}
.param-name {
font-weight: 600;
}
</style>
6 changes: 6 additions & 0 deletions web/vue/src/components/backtester/result/result.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
chart(:data='result', height='500')
.hr.contain
roundtripTable(:roundtrips='result.roundtrips')
div(v-for='result in bruteForceResult')
result-summary(:report='result.report')
.hr.contain
chart(:data='result', height='500')
.hr.contain
roundtripTable(:roundtrips='result.roundtrips')
</template>

<script>
Expand Down
41 changes: 39 additions & 2 deletions web/vue/src/components/global/configbuilder/stratpicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@
h3 Parameters
p {{ strategy }} Parameters:
textarea.params(v-model='rawStratParams')
p.bg--red.p1(v-if='rawStratParamsError') {{ rawStratParamsError.message }}
.bf
.checkbox
label(for='bruteforce') Bruteforce
input(type='checkbox', name='bruteforce', v-model='bruteForce')
em.label-like test all combinations
p.bg--red.p1(v-if='rawStratParamsError') {{ rawStratParamsError.message }}
</template>

<script>
Expand All @@ -51,7 +56,9 @@ export default {
rawStratParamsError: false,

emptyStrat: false,
stratParams: {}
stratParams: {},

bruteForce: false
};
},
created: function () {
Expand All @@ -75,6 +82,11 @@ export default {

this.emitConfig();
},
bruteForce: function(value) {
// Configure the brute forcer
if (value) { this.configBruteForce(); }
this.emitConfig();
},
candleSize: function() { this.emitConfig() },
historySize: function() { this.emitConfig() },
rawStratParams: function() { this.emitConfig() }
Expand Down Expand Up @@ -116,6 +128,12 @@ export default {
this.parseParams();
this.$emit('stratConfig', this.config);
},
configBruteForce: function() {
console.log('Configuring brute force from strategy params :', toml.parse(this.rawStratParams));
window.bruteForcer = new MarketBacktestBruteforcer();
window.bruteForcer.configure(toml.parse(this.rawStratParams));
console.log('bruteforcer config: ', window.bruteForcer.config);
},
parseParams: function() {
try {
this.stratParams = toml.parse(this.rawStratParams);
Expand All @@ -142,4 +160,23 @@ export default {
.align {
padding-left: 1em;
}

.bf .checkbox label {
margin-top: 10px;
margin-left: 10px;
}

.bf .label-like {
display: inline;
}

.bf .checkbox input[type="checkbox"] {
display: inline;
margin-right: 5px;
margin-left: 10px;
}

.bf .checkbox input[type="checkbox"] {
width: auto;
}
</style>
4 changes: 3 additions & 1 deletion web/vue/src/components/global/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import Vue from 'vue'

import { wsPath } from '../../tools/api'
import initializeState from '../../store/init'
import { MarketBacktestBruteforcer } from '../../tools/bruteforcer'
window.MarketBacktestBruteforcer = MarketBacktestBruteforcer

var socket = null;

Expand Down Expand Up @@ -60,4 +62,4 @@ export const connect = () => {
let payload = JSON.parse(message.data);
bus.$emit(payload.type, payload);
};
}
}
Loading