Commit 69c4141a authored by Ahmad Nemati's avatar Ahmad Nemati

init

parent 448a5dc4
......@@ -6,7 +6,7 @@ let zarib = 'zarib'
let base = 'base'
function run(data, config,risk,pfTrade) {
function run(data, config, risk, pfTrade, riskFreeLevel) {
let commision = config.commision
let money = config.base
let total = 0
......@@ -20,12 +20,12 @@ function run(data, config,risk,pfTrade) {
let profit = 0
let lastCom = 0
let lastMoney = 0
let levels=[]
let levels = []
let dur = getMinDiff(data[0].openDate, data[data.length - 1].closeDate)
let newP=dur/data.length
// console.log(data.length)
// console.log(newP)
if (pfTrade<newP)
let newP = dur / data.length
// console.log(data.length)
// console.log(newP)
if (pfTrade < newP)
return null
for (let i = 0; i < data.length; i++) {
......@@ -39,8 +39,10 @@ function run(data, config,risk,pfTrade) {
else if (config.type === plus)
money = config.base * (step + 1)
}
let d = inlineCheck(money, commision, data[i], profit, lastCom, config.target)
let riskfree = false
if (step > riskFreeLevel)
riskfree = true
let d = inlineCheck(money, commision, data[i], profit, lastCom, config.target,riskfree)
if (Math.abs(d.reserve) > Math.abs(maxReserve))
......@@ -73,39 +75,32 @@ function run(data, config,risk,pfTrade) {
}
}
let firstMaxAllocatedMoney=maxAllocatedMoney
if (config.param !==0)
{
for (let i=0;i<risk;i++)
maxAllocatedMoney=maxAllocatedMoney*config.param
let firstMaxAllocatedMoney = maxAllocatedMoney
if (config.param !== 0) {
for (let i = 0; i < risk; i++)
maxAllocatedMoney = maxAllocatedMoney * config.param
}
let newLevel=[]
let newLevelText=''
for (let i=0;i<(maxLevel+1);i++)
{
let count=counter(i,levels)
// newLevel.push({level:i,count:count})
let per=parseFloat((count/total)*100).toFixed(1)
let newLevel = []
let newLevelText = ''
for (let i = 0; i < (maxLevel + 1); i++) {
let count = counter(i, levels)
// newLevel.push({level:i,count:count})
let per = parseFloat((count / total) * 100).toFixed(1)
newLevelText=newLevelText+i+':'+count+':'+per+'%'+','
if ((i+2) %12 ===0)
{
newLevelText+newLevelText+'\\n'
newLevelText = newLevelText + i + ':' + count + ':' + per + '%' + ','
if ((i + 2) % 12 === 0) {
}
newLevelText + newLevelText + '\\n'
}
}
// console.log(newLevelText)
// for (let i=0;i<newLevel)
// console.log(newLevelText)
// for (let i=0;i<newLevel)
let totalFinal = total * config.target
......@@ -136,7 +131,7 @@ function run(data, config,risk,pfTrade) {
for (let i = 0; i < total; i++)
sum = sum + (sum * perp)
sum=sum-Math.abs(maxAllocatedMoney)
sum = sum - Math.abs(maxAllocatedMoney)
let newCompund = sum / dur
let compoundPerMonth = (newCompund * 30 * 100) / Math.abs(maxAllocatedMoney)
......@@ -154,23 +149,23 @@ function run(data, config,risk,pfTrade) {
profitPerMonth: Math.abs(profitPerMonth),
compoundPerMonth: compoundPerMonth,
firstMaxAllocatedMoney:firstMaxAllocatedMoney,
firstMaxAllocatedMoney: firstMaxAllocatedMoney,
maxAllocatedMoney: maxAllocatedMoney,
avgAllocatedMoney: maxAllocatedMoney / total,
drawDown: drawDown,
CompoundPerMonthDrawDown:compoundPerMonth/drawDown,
profitPerMonthDrawDown:Math.abs(profitPerMonth)/drawDown,
CompoundPerMonthDrawDown: compoundPerMonth / drawDown,
profitPerMonthDrawDown: Math.abs(profitPerMonth) / drawDown,
maxReseve: maxReserve,
avgReserve: maxReserve / total,
percentage: percentage,
avgProfitPerDay: newT,
allocatedReserve: (maxAllocatedMoney) / Math.abs(maxReserve),
minus: minus,
risk:risk,
levels:newLevelText,
pftrade:newP,
countOfTrade:data.length
risk: risk,
levels: newLevelText,
pftrade: newP,
countOfTrade: data.length
}
......@@ -183,10 +178,9 @@ function run(data, config,risk,pfTrade) {
}
function counter(count,array)
{
let sum=0
for (let i=0;i<array.length;i++)
function counter(count, array) {
let sum = 0
for (let i = 0; i < array.length; i++)
if (array[i] === count)
sum++
......@@ -204,12 +198,15 @@ function percentage2(base, per) {
}
function inlineCheck(money, com, data, profit, lastCom, tar) {
function inlineCheck(money, com, data, profit, lastCom, tar,riskFree) {
// console.log(profit)
com = com * money
let target = money + Math.abs(profit) + com + lastCom + tar
let target = money + Math.abs(profit) + com + lastCom
if (!riskFree)
target=target+tar
let reserve = percentage(money, data.drawDown)
......
......@@ -6,7 +6,7 @@ let moment = require('moment-timezone')
let ai = require('./ai')
async function parse(directory, name, configs, risk, pfTrade, side) {
async function parse(directory, name, configs, risk, pfTrade, side,riskFreeLevel) {
return new Promise(function (resolve, reject) {
let arr = []
// console.log(name)
......@@ -14,12 +14,12 @@ async function parse(directory, name, configs, risk, pfTrade, side) {
.pipe(csv.parse({headers: true}))
.on('error', error => reject(error))
.on('data', row => arr.push(row))
.on('end', rowCount => resolve(initData(arr, configs, name, risk, pfTrade, side)));
.on('end', rowCount => resolve(initData(arr, configs, name, risk, pfTrade, side,riskFreeLevel)));
});
}
function initData(data, config, name, risk, pfTrade, side) {
function initData(data, config, name, risk, pfTrade, side,riskFreeLevel) {
data = JSON.stringify(data)
......@@ -107,7 +107,7 @@ function initData(data, config, name, risk, pfTrade, side) {
let cz = []
for (let i = 0; i < config.length; i++) {
cz.push(ai.run(arr, config[i], risk, pfTrade))
cz.push(ai.run(arr, config[i], risk, pfTrade,riskFreeLevel))
}
return cz
......
......@@ -20,7 +20,7 @@ run()
async function run()
{
let obj={sort:'profitPerMonthDrawDown',alocatedReserve:3,alocatedReserveCompound:3,risk:0,start:10000,pfTrade:2,side:'both'}
let obj={sort:'profitPerMonthDrawDown',alocatedReserve:3,alocatedReserveCompound:3,risk:0,start:10000,pfTrade:2,side:'both',riskFreeLevel:5}
let all=[]
for (let i=0;i<arr.length;i++)
all.push(add(arr[i]))
......
......@@ -20,7 +20,7 @@ async function run() {
for (let i = 0; i < files.length; i++)
if (files[i].includes('.csv')) {
csvFile = files[i]
arr.push(csv.parse(directory, files[i], configs.data, configs.risk, configs.pfTrade,configs.side))
arr.push(csv.parse(directory, files[i], configs.data, configs.risk, configs.pfTrade,configs.side,configs.riskFreeLevel))
}
arr = await Promise.all(arr)
......
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