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

init

parents
let plus='plus'
let zarib='zarib'
function run(data,config) {
let commision = config.commision
let money = config.base
let total = 0
let maxReserve = 0
let maxAllocatedMoney = 0
let maxLevel = 0
// console.log(data)
let step = 0
let reserv=0
let profit=0
let lastCom=0
for (let i = 0; i < data.length; i++) {
if (step === 0)
money = config.base
else
{
if (config.type ===zarib)
money = config.base * config.param * step
else if (config.type ===plus)
money = config.base +(config.param * step)
}
let d = inlineCheck(money, commision, data[i],profit,lastCom)
if (Math.abs(d.reserve) > Math.abs(maxReserve))
maxReserve = d.reserve
if (money > maxAllocatedMoney)
maxAllocatedMoney = money
// console.log(data[i])
// console.log( 'step->'+step)
// console.log(d)
if (step>maxLevel)
maxLevel=step
if (d.done === true) {
step = 0
reserv=0
profit=0
lastCom=0
total++
} else {
step++
profit=profit+d.profit
lastCom=lastCom+d.com
}
}
let percentage=maxReserve+total
percentage=maxAllocatedMoney/percentage
let minus=0
if (percentage<0) {
minus=percentage
percentage = null
}
let opt = {
platform: data[0].signal,
type:config.type +' param:'+config.param+' base:'+config.base,
total: total,
avgLevel: maxLevel/total,
maxLevel: maxLevel,
maxAllocatedMoney: maxAllocatedMoney,
avgAllocatedMoney: maxAllocatedMoney/total,
maxReseve: maxReserve,
avgReserve: maxReserve/total,
percentage:percentage,
minus:minus
}
return opt
// console.log(data)
}
function inlineCheck(money, com, data,profit,lastCom) {
// console.log(profit)
com = com * money
let target = money +Math.abs(profit)+ com+lastCom + 1
let reserve = percentage(money, data.drawDown)
let profitMoney = percentageMinus(money, data.profit)
let diff=reserve - money
diff=diff-com-lastCom+profit
// console.log('reserve->'+reserve + ' money->'+money +' com->'+com +' lastCom->'+lastCom + 'profit->'+profit +' diff->'+diff)
if (percentage(money,data.runUp) > target)
return {done: true, reserve: diff}
else
return {done: false, reserve: diff,profit:profitMoney,com:com}
}
function calPer(entry, last) {
return ((last - entry) / entry) * 100
}
function percentage(base, per) {
base = parseFloat(base)
per = parseFloat(per)
let percentager = per / 100
let temp = base * percentager
return base + temp
}
function percentageMinus(base, per) {
base = parseFloat(base)
per = parseFloat(per)
let percentager = per / 100
let temp = base * percentager
return temp
}
module.exports =
{
run:run,
}
\ No newline at end of file
{
"sort": "platform",
"data": [
{
"type": "plus",
"commision": 0.0008,
"base": 100,
"param": 1000
},
{
"type": "zarib",
"commision": 0.0008,
"base": 100,
"param": 2
}
]
}
\ No newline at end of file
const fs = require('fs');
const csv = require('fast-csv');
let _ = require('lodash')
let ai=require('./ai')
async function parse(name,configs) {
return new Promise(function (resolve, reject) {
let arr = []
fs.createReadStream('./work/'+name)
.pipe(csv.parse({headers: true}))
.on('error', error => reject(error))
.on('data', row => arr.push(row))
.on('end', rowCount => resolve(initData(arr,configs)));
});
}
function initData(data,config) {
// console.log(data)
data = JSON.stringify(data)
// data.replace(/Trade #/g, 'id').replace(/Profit %/g, 'profitPer')
data = _.replace(data, new RegExp('Trade #', 'g'), 'id')
data = _.replace(data, new RegExp('Profit %', 'g'), 'profit')
data = _.replace(data, new RegExp('Run-up %', 'g'), 'runUp')
data = _.replace(data, new RegExp('Drawdown %', 'g'), 'drawDown')
data = _.replace(data, new RegExp('Price', 'g'), 'price')
data = _.replace(data, new RegExp('Date/Time', 'g'), 'date')
data = JSON.parse(data)
// console.log(data)
let arr = []
for (let i = 0; i < data.length; i = i + 2) {
if (data[i].runUp === '')
continue
let obj = {}
obj.id=parseInt(data[i].id)
obj.signal=data[i].Signal
if (data[i].Type.includes('Long'))
obj.side = 'LONG'
else
obj.side = 'SHORT'
obj.openPrice = parseFloat(data[i].price)
obj.closePrice=parseFloat(data[i+1].price)
obj.openDate = data[i].date
obj.closeDate=data[i+1].date
obj.profit=parseFloat(data[i].profit)
obj.runUp=parseFloat(data[i].runUp)
obj.drawDown=parseFloat(data[i].drawDown)
obj.drawDown = obj.drawDown * -1
arr.push(obj)
// let arr=
}
//console.log(arr)
let cz=[]
for (let i=0;i<config.length;i++)
{
cz.push(ai.run(arr,config[i]))
}
return cz
}
module.exports =
{
parse:parse,
}
let fs = require('fs')
let csv = require('./csv')
let _ = require('lodash')
test()
async function test() {
let configs = await fs.readFileSync('./config.json', 'utf8')
configs = JSON.parse(configs)
let arr = []
let files = await fs.readdirSync('./work')
for (let i = 0; i < files.length; i++)
if (files[i].includes('.csv'))
arr.push(csv.parse(files[i], configs.data))
arr = await Promise.all(arr)
let arr2 = []
for (let i = 0; i < arr.length; i++) {
let inline = arr[i]
for (let j = 0; j < inline.length; j++)
arr2.push(inline[j])
}
console.log(arr2)
arr2 = _.orderBy(arr2, [configs.sort], ['asc']);
// for (let i=0;i<files.length;i++)
createfile(JSON.stringify(arr2, null, 2))
console.log('Done')
// console.log(d)
}
function createfile(data) {
return new Promise(function (resolve, reject) {
fs.writeFile('work/result.json', data, 'utf8', function (err) {
if (err) reject(err);
else resolve(data);
});
});
}
async function test2() {
let configs = await fs.readFileSync('./config.json', 'utf8')
configs = JSON.parse(configs)
console.log(configs)
}
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