Commit 7fec0c95 authored by Ahmad Nemati's avatar Ahmad Nemati

init

parent 3dcc6e4d
let moment = require('moment-timezone') let moment = require('moment-timezone')
const fs = require('fs'); const fs = require('fs');
let _=require('lodash') let _ = require('lodash')
let plus = 'plus' let plus = 'plus'
let zarib = 'zarib' let zarib = 'zarib'
...@@ -9,8 +9,8 @@ let dynamic = 'dynamic' ...@@ -9,8 +9,8 @@ let dynamic = 'dynamic'
let elsa = 'elsa' let elsa = 'elsa'
function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, maxTradeDays) { function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, maxTradeDays, targetPercentage) {
let type = config.type + ' param:' + config.param + ' base:' + config.base + ' target:' + config.target let type = config.type + ' param:' + config.param + ' base:' + config.base + ' target:' + config.target + ' targetPercentage:' + targetPercentage.enable + ' per:' + targetPercentage.per
let saveData = [] let saveData = []
let needPrintData = shouldPrint(detail, data[0].signal + '&' + type) let needPrintData = shouldPrint(detail, data[0].signal + '&' + type)
// console.log(needPrintData) // console.log(needPrintData)
...@@ -28,6 +28,7 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max ...@@ -28,6 +28,7 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max
let curDrawDown = 0 let curDrawDown = 0
let maxAllocatedMoney = 0 let maxAllocatedMoney = 0
let maxLevel = 0 let maxLevel = 0
let realProfit = 0
// console.log(data) // console.log(data)
...@@ -37,10 +38,10 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max ...@@ -37,10 +38,10 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max
let profit = 0 let profit = 0
let lastCom = 0 let lastCom = 0
let lastMoney = 0 let lastMoney = 0
let maxMetaStep=0 let maxMetaStep = 0
let levels = [] let levels = []
let detailLevel = [] let detailLevel = []
let durDetail=[] let durDetail = []
let dur let dur
try { try {
...@@ -96,7 +97,7 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max ...@@ -96,7 +97,7 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max
let riskfree = false let riskfree = false
if (step > riskFreeLevel) if (step > riskFreeLevel)
riskfree = true riskfree = true
let d = inlineCheck(money, commision, data[i], profit, lastCom, config.target, riskfree) let d = inlineCheck(money, commision, data[i], profit, lastCom, config.target, riskfree, targetPercentage)
if (d.reserve < 0 && Math.abs(d.reserve) > Math.abs(maxReserve)) if (d.reserve < 0 && Math.abs(d.reserve) > Math.abs(maxReserve))
...@@ -159,20 +160,25 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max ...@@ -159,20 +160,25 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max
if (d.done === true) { if (d.done === true) {
let endDays = getNewMinDiff(startDays, data[i].closeDate) let endDays = getNewMinDiff(startDays, data[i].closeDate)
detailLevel.push({step: step, dur: endDays}) detailLevel.push({step: step, dur: endDays})
durDetail.push({meta:maxMetaStep,dur:endDays,step:step,days:startDays.split(' ')[0]+'-'+data[i].closeDate.split(' ')[0]}) durDetail.push({
meta: maxMetaStep,
dur: endDays,
step: step,
days: startDays.split(' ')[0] + '-' + data[i].closeDate.split(' ')[0]
})
// console.log(typeof endDays) // console.log(typeof endDays)
//console.log(endDays) //console.log(endDays)
if (endDays > maxDays) if (endDays > maxDays)
maxDays = endDays maxDays = endDays
listDays.push(endDays) listDays.push(endDays)
realProfit = realProfit + d.realProfit
// console.log(maxDays) // console.log(maxDays)
levels.push(step) levels.push(step)
step = 0 step = 0
reserv = 0 reserv = 0
profit = 0 profit = 0
maxMetaStep=0 maxMetaStep = 0
lastCom = 0 lastCom = 0
curDrawDown = 0 curDrawDown = 0
lastMoney = 0 lastMoney = 0
...@@ -242,12 +248,12 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max ...@@ -242,12 +248,12 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max
let newLevelText = '' let newLevelText = ''
for (let i = 0; i < (maxLevel + 1); i++) { for (let i = 0; i < (maxLevel + 1); i++) {
let count = counter(i, levels) let count = counter(i, levels)
let avgDur=getAvgDur(i,detailLevel) let avgDur = getAvgDur(i, detailLevel)
// newLevel.push({level:i,count:count}) // newLevel.push({level:i,count:count})
let per = parseFloat((count / allTotal) * 100).toFixed(1) let per = parseFloat((count / allTotal) * 100).toFixed(1)
newLevelText = newLevelText + i + ':' + count + ':' + per + '%' + ':'+avgDur+'///' newLevelText = newLevelText + i + ':' + count + ':' + per + '%' + ':' + avgDur + '///'
if ((i + 2) % 12 === 0) { if ((i + 2) % 12 === 0) {
newLevelText + newLevelText + '\\n' newLevelText + newLevelText + '\\n'
...@@ -258,29 +264,27 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max ...@@ -258,29 +264,27 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max
} }
durDetail = _.orderBy(durDetail, ['dur'], ['desc']); durDetail = _.orderBy(durDetail, ['dur'], ['desc']);
let durDetailText='' let durDetailText = ''
for (let i=0;i<durDetail.length;i++) for (let i = 0; i < durDetail.length; i++) {
{ if (i > 10)
if (i>10)
break break
durDetailText=durDetailText+durDetail[i].step+':'+parseFloat(durDetail[i].dur).toFixed(2)+':'+durDetail[i].days+':'+parseFloat(durDetail[i].meta).toFixed(2)+'///' durDetailText = durDetailText + durDetail[i].step + ':' + parseFloat(durDetail[i].dur).toFixed(2) + ':' + durDetail[i].days + ':' + parseFloat(durDetail[i].meta).toFixed(2) + '///'
} }
durDetail = _.orderBy(durDetail, ['meta'], ['asc']); durDetail = _.orderBy(durDetail, ['meta'], ['asc']);
let metaDetailText='' let metaDetailText = ''
for (let i=0;i<durDetail.length;i++) for (let i = 0; i < durDetail.length; i++) {
{ if (i > 10)
if (i>10)
break break
metaDetailText=metaDetailText+durDetail[i].step+':'+parseFloat(durDetail[i].dur).toFixed(2)+':'+durDetail[i].days+':'+parseFloat(durDetail[i].meta).toFixed(2)+'///' metaDetailText = metaDetailText + durDetail[i].step + ':' + parseFloat(durDetail[i].dur).toFixed(2) + ':' + durDetail[i].days + ':' + parseFloat(durDetail[i].meta).toFixed(2) + '///'
} }
// console.log(newLevelText) // console.log(newLevelText)
// for (let i=0;i<newLevel) // for (let i=0;i<newLevel)
let totalFinal = total * config.target let totalFinal = realProfit
let totalPlus = totalFinal + allLoss let totalPlus = totalFinal + allLoss
let oldTotalFinal = totalFinal let oldTotalFinal = totalFinal
totalFinal = totalPlus totalFinal = totalPlus
...@@ -347,8 +351,8 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max ...@@ -347,8 +351,8 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max
percentage: percentage, percentage: percentage,
avgProfitPerDay: newT, avgProfitPerDay: newT,
allocatedReserve: (maxAllocatedMoney) / Math.abs(maxReserve), allocatedReserve: (maxAllocatedMoney) / Math.abs(maxReserve),
durDetailText:durDetailText, durDetailText: durDetailText,
metaDetailText:metaDetailText, metaDetailText: metaDetailText,
minus: minus, minus: minus,
risk: risk, risk: risk,
levels: newLevelText, levels: newLevelText,
...@@ -410,17 +414,25 @@ function percentage2(base, per) { ...@@ -410,17 +414,25 @@ function percentage2(base, per) {
} }
function inlineCheck(money, com, data, profit, lastCom, tar, riskFree) { function inlineCheck(money, com, data, profit, lastCom, tar, riskFree, targetPercentage) {
// console.log(profit) // console.log(profit)
com = com * money com = com * money
let minProf = profit * -1 let minProf = profit * -1
let target = money + minProf + com + lastCom let target = money + minProf + com + lastCom
let realProfit = 0
let targetDetail = '(' + money + ')' + '+' + '(' + minProf + ')+(' + com + ')+(' + lastCom + ')' let targetDetail = '(' + money + ')' + '+' + '(' + minProf + ')+(' + com + ')+(' + lastCom + ')'
if (!riskFree) { if (!riskFree) {
target = target + tar
targetDetail = targetDetail + '+(' + tar + ')' if (targetPercentage.enable === true)
realProfit = percentageMinus(target, targetPercentage.per)
else
realProfit = target
target = target + realProfit
targetDetail = targetDetail + '+(' + realProfit + ')'
} }
...@@ -443,6 +455,7 @@ function inlineCheck(money, com, data, profit, lastCom, tar, riskFree) { ...@@ -443,6 +455,7 @@ function inlineCheck(money, com, data, profit, lastCom, tar, riskFree) {
meta: meta, meta: meta,
reserve: diff, reserve: diff,
riskFree, riskFree,
realProfit: realProfit,
profitLastSteps: profit, profitLastSteps: profit,
profit: profitMoney, profit: profitMoney,
lastCom: lastCom, lastCom: lastCom,
......
...@@ -6,7 +6,7 @@ let moment = require('moment-timezone') ...@@ -6,7 +6,7 @@ let moment = require('moment-timezone')
let ai = require('./ai') let ai = require('./ai')
async function parse(directory, name, configs, risk, pfTrade, side, riskFreeLevel,detail,onlyShortLong,loss,dur,maxTradeDays) { async function parse(directory, name, configs, risk, pfTrade, side, riskFreeLevel,detail,onlyShortLong,loss,dur,maxTradeDays,targetPercentage) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
let arr = [] let arr = []
...@@ -15,12 +15,12 @@ async function parse(directory, name, configs, risk, pfTrade, side, riskFreeLeve ...@@ -15,12 +15,12 @@ async function parse(directory, name, configs, risk, pfTrade, side, riskFreeLeve
.pipe(csv.parse({headers: true})) .pipe(csv.parse({headers: true}))
.on('error', error => reject(error)) .on('error', error => reject(error))
.on('data', row => arr.push(row)) .on('data', row => arr.push(row))
.on('end', rowCount => resolve(initData(arr, configs, name, risk, pfTrade, side, riskFreeLevel,detail,onlyShortLong,loss,dur,maxTradeDays))); .on('end', rowCount => resolve(initData(arr, configs, name, risk, pfTrade, side, riskFreeLevel,detail,onlyShortLong,loss,dur,maxTradeDays,targetPercentage)));
}); });
} }
function initData(data, config, name, risk, pfTrade, side, riskFreeLevel,detail,onlyShortLong,loss,dur,maxTradeDays) { function initData(data, config, name, risk, pfTrade, side, riskFreeLevel,detail,onlyShortLong,loss,dur,maxTradeDays,targetPercentage) {
data = JSON.stringify(data) data = JSON.stringify(data)
...@@ -138,7 +138,7 @@ function initData(data, config, name, risk, pfTrade, side, riskFreeLevel,detail, ...@@ -138,7 +138,7 @@ function initData(data, config, name, risk, pfTrade, side, riskFreeLevel,detail,
let cz = [] let cz = []
for (let i = 0; i < config.length; i++) { for (let i = 0; i < config.length; i++) {
let d = ai.run(arr, config[i], risk, pfTrade, riskFreeLevel,detail,loss,dur,maxTradeDays) let d = ai.run(arr, config[i], risk, pfTrade, riskFreeLevel,detail,loss,dur,maxTradeDays,targetPercentage)
if (d !== null) if (d !== null)
cz.push(d) cz.push(d)
} }
......
...@@ -20,7 +20,7 @@ run() ...@@ -20,7 +20,7 @@ run()
async function run() async function run()
{ {
let obj={sort:'profitPerMonthDrawDown',alocatedReserve:3,alocatedReserveCompound:3,risk:0,start:10000,pfTrade:2,side:'both',riskFreeLevel:100,onlyShortLong:false,loss:0,level:200,dur:1500,maxTradeDays:40} let obj={sort:'profitPerMonthDrawDown',alocatedReserve:3,alocatedReserveCompound:3,risk:0,start:10000,pfTrade:2,side:'both',riskFreeLevel:100,onlyShortLong:false,loss:0,level:200,dur:1500,maxTradeDays:40,targetPercentage:{enable:true,per:1}}
let detail=['configs2.json-211021092159_O1_BNC_BTCUSD_Lp1_Y211020a - Copy_Mn.csv&zarib param:1.1 base:200 target:1'] let detail=['configs2.json-211021092159_O1_BNC_BTCUSD_Lp1_Y211020a - Copy_Mn.csv&zarib param:1.1 base:200 target:1']
obj.detail=detail obj.detail=detail
let all=[] let all=[]
......
...@@ -25,7 +25,7 @@ async function run() { ...@@ -25,7 +25,7 @@ async function run() {
for (let i = 0; i < files.length; i++) for (let i = 0; i < files.length; i++)
if (files[i].includes('.csv')) { if (files[i].includes('.csv')) {
csvFile = files[i] csvFile = files[i]
arr.push(csv.parse(directory, files[i], configs.data, configs.risk, configs.pfTrade, configs.side, configs.riskFreeLevel, JSON.parse(checks), configs.onlyShortLong, configs.loss,configs.dur,configs.maxTradeDays)) arr.push(csv.parse(directory, files[i], configs.data, configs.risk, configs.pfTrade, configs.side, configs.riskFreeLevel, JSON.parse(checks), configs.onlyShortLong, configs.loss,configs.dur,configs.maxTradeDays,configs.targetPercentage))
} }
arr = await Promise.all(arr) arr = await Promise.all(arr)
......
let _=require('lodash') let _=require('lodash')
let durDetail=[{dur:-1},{dur:-3},{dur:-4},{dur:-10}]
console.log(percentageMinus(100,1))
durDetail = _.orderBy(durDetail, ['dur'], ['asc']); function percentageMinus(base, per) {
console.log(durDetail)
base = parseFloat(base)
per = parseFloat(per)
let percentager = per / 100
let temp = base * percentager
return temp
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment