Commit 82e7eaf3 authored by Ahmad Nemati's avatar Ahmad Nemati

init

parent 8028eb6f
...@@ -7,16 +7,22 @@ let zarib = 'zarib' ...@@ -7,16 +7,22 @@ let zarib = 'zarib'
let base = 'base' let base = 'base'
let dynamic = 'dynamic' let dynamic = 'dynamic'
let elsa = 'elsa' let elsa = 'elsa'
let candles = []
let timeFrame = 0
function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, maxTradeDays, targetPercentage) { function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, maxTradeDays, targetPercentage, candlesData) {
let type = config.type + ' param:' + config.param + ' base:' + config.base + ' target:' + config.target + ' targetPercentage:' + targetPercentage.enable let type = config.type + ' param:' + config.param + ' base:' + config.base + ' target:' + config.target + ' targetPercentage:' + targetPercentage.enable
let saveData = [] let saveData = []
candles = candlesData
timeFrame = candles[1].t - candles[0].t
let needPrintData = shouldPrint(detail, data[0].signal + '&' + type) let needPrintData = shouldPrint(detail, data[0].signal + '&' + type)
// console.log(needPrintData) // console.log(needPrintData)
let commision = config.commision let commision = config.commision
let money = config.base let money = config.base
let unixStart = 0
let unixMid = 0
let total = 0 let total = 0
let maxDays = 0 let maxDays = 0
let startDays = 0 let startDays = 0
...@@ -40,7 +46,7 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max ...@@ -40,7 +46,7 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max
let lastMoney = 0 let lastMoney = 0
let maxMetaStep = 0 let maxMetaStep = 0
let levels = [] let levels = []
let targets=[] let targets = []
let detailLevel = [] let detailLevel = []
let durDetail = [] let durDetail = []
...@@ -66,9 +72,11 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max ...@@ -66,9 +72,11 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max
return null return null
let obj let obj
if (step === 0) { if (step === 0) {
unixStart = moment.tz(data[i].openDate, 'YYYY.MM.DD HH:mm:ss', 'GMT').unix()
startDays = data[i].openDate startDays = data[i].openDate
money = config.base money = config.base
} else { } else {
if (config.type === zarib) if (config.type === zarib)
money = lastMoney * config.param money = lastMoney * config.param
if (config.type === base) if (config.type === base)
...@@ -128,8 +136,8 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max ...@@ -128,8 +136,8 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max
obj = d obj = d
obj.signal = data[i] obj.signal = data[i]
obj.signal.openTimeStamp=moment.tz(data[i].openDate, 'YYYY.MM.DD HH:mm:ss','GMT').unix() obj.signal.openTimeStamp = moment.tz(data[i].openDate, 'YYYY.MM.DD HH:mm:ss', 'GMT').unix()
obj.signal.closeTimeStamp= moment.tz(data[i].closeDate, 'YYYY.MM.DD HH:mm:ss','GMT').unix() obj.signal.closeTimeStamp = moment.tz(data[i].closeDate, 'YYYY.MM.DD HH:mm:ss', 'GMT').unix()
if (d.meta < 0) if (d.meta < 0)
...@@ -176,6 +184,14 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max ...@@ -176,6 +184,14 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max
if (endDays > maxDays) if (endDays > maxDays)
maxDays = endDays maxDays = endDays
let runnigTime = 0
// runnigTime = calculateRunnigTime(data[i].side, moment.tz(data[i].openDate, 'YYYY.MM.DD HH:mm:ss', 'GMT').unix(), moment.tz(data[i].closeDate, 'YYYY.MM.DD HH:mm:ss', 'GMT').unix(), d.targetPrice)
// if (step !== 0)
// runnigTime = runnigTime + (unixMid - unixStart)
//
//
// if (needPrintData)
// obj.runnigTime = runnigTime
listDays.push(endDays) listDays.push(endDays)
...@@ -184,6 +200,8 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max ...@@ -184,6 +200,8 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max
// console.log(maxDays) // console.log(maxDays)
levels.push(step) levels.push(step)
step = 0 step = 0
unixStart = 0
unixMid = 0
reserv = 0 reserv = 0
profit = 0 profit = 0
maxMetaStep = 0 maxMetaStep = 0
...@@ -195,7 +213,7 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max ...@@ -195,7 +213,7 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max
total++ total++
} else { } else {
unixMid = moment.tz(data[i].closeDate, 'YYYY.MM.DD HH:mm:ss', 'GMT').unix()
step++ step++
profit = profit + d.profit profit = profit + d.profit
lastMoney = d.lastMoney lastMoney = d.lastMoney
...@@ -317,11 +335,10 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max ...@@ -317,11 +335,10 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max
// } // }
let sum = Math.abs(maxAllocatedMoney) let sum = Math.abs(maxAllocatedMoney)
for (let i = 0; i < total; i++) for (let i = 0; i < total; i++)
sum = sum + (sum * getperp(targets[i],maxAllocatedMoney)) sum = sum + (sum * getperp(targets[i], maxAllocatedMoney))
sum = sum - Math.abs(maxAllocatedMoney) sum = sum - Math.abs(maxAllocatedMoney)
let newCompund = sum / dur let newCompund = sum / dur
...@@ -383,8 +400,7 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max ...@@ -383,8 +400,7 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max
// console.log(data) // console.log(data)
} }
function getperp(target,maxAllocatedMoney) function getperp(target, maxAllocatedMoney) {
{
let perp = (target * 100) / Math.abs(maxAllocatedMoney) let perp = (target * 100) / Math.abs(maxAllocatedMoney)
perp = perp / 100 perp = perp / 100
return perp return perp
...@@ -453,7 +469,7 @@ function inlineCheck(money, com, data, profit, lastCom, tar, riskFree, targetPer ...@@ -453,7 +469,7 @@ function inlineCheck(money, com, data, profit, lastCom, tar, riskFree, targetPer
let reserve = percentage(money, data.drawDown) let reserve = percentage(money, data.drawDown)
let profitMoney = percentageMinus(money, data.profit) let profitMoney = percentageMinus(money, data.profit)
let targetPrice=getTargetPrice(money,target,data.openPrice,data.side) let targetPrice = getTargetPrice(money, target, data.openPrice, data.side)
let diff = reserve - money let diff = reserve - money
diff = diff - com - lastCom + profit diff = diff - com - lastCom + profit
...@@ -475,7 +491,7 @@ function inlineCheck(money, com, data, profit, lastCom, tar, riskFree, targetPer ...@@ -475,7 +491,7 @@ function inlineCheck(money, com, data, profit, lastCom, tar, riskFree, targetPer
profit: profitMoney, profit: profitMoney,
lastCom: lastCom, lastCom: lastCom,
com: com, com: com,
targetPrice:targetPrice, targetPrice: targetPrice,
target: target, target: target,
targetDetail: targetDetail targetDetail: targetDetail
} }
...@@ -491,19 +507,13 @@ function inlineCheck(money, com, data, profit, lastCom, tar, riskFree, targetPer ...@@ -491,19 +507,13 @@ function inlineCheck(money, com, data, profit, lastCom, tar, riskFree, targetPer
com: com, com: com,
target: target, target: target,
targetDetail: targetDetail, targetDetail: targetDetail,
targetPrice:targetPrice targetPrice: targetPrice
} }
} }
function getTargetPrice(money,target,basePrice,side) {
let diffMoney = calPer(money, target)
if (side === 'LONG')
return percentageAdv(basePrice, diffMoney)
else
return percentageAdv(basePrice, (-1 * diffMoney))
}
function percentageAdv(base, per) { function percentageAdv(base, per) {
...@@ -565,15 +575,53 @@ function getNewMinDiff(s, c) { ...@@ -565,15 +575,53 @@ function getNewMinDiff(s, c) {
} }
function getTargetPrice(side,money,targetMoney,basePrice) { function getTargetPrice(side, money, targetMoney, basePrice) {
let diffMoney = calPer(money, targetMoney) let diffMoney = calPer(money, targetMoney)
if (side ==='LONG') if (side === 'LONG')
return percentageAdv(basePrice, diffMoney) return percentageAdv(basePrice, diffMoney)
else else
return percentageAdv(basePrice, (-1 * diffMoney)) return percentageAdv(basePrice, (-1 * diffMoney))
} }
function calculateRunnigTime(side, startTime, endTime, targetPrice) {
let arr = grabData(startTime, endTime)
for (let i = 0; i < arr.length; i++) {
if (side === 'LONG') {
if (targetPrice <= arr[i].h)
return timeFrame * (i + 1)
} else {
if (targetPrice >= arr[i].l) {
return timeFrame * (i + 1)
}
}
}
}
function grabData(start, end) {
let arr = []
let indexStart = false
for (let i = 0; i < candles.length; i++) {
if (indexStart && candles[i].t <= end)
arr.push(candles[i])
else if (candles[i].t === start) {
indexStart = true
arr.push(candles[i])
}
}
return arr
}
module.exports = module.exports =
{ {
......
...@@ -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,targetPercentage) { async function parse(directory, name, configs, risk, pfTrade, side, riskFreeLevel,detail,onlyShortLong,loss,dur,maxTradeDays,targetPercentage,candles) {
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,targetPercentage))); .on('end', rowCount => resolve(initData(arr, configs, name, risk, pfTrade, side, riskFreeLevel,detail,onlyShortLong,loss,dur,maxTradeDays,targetPercentage,candles)));
}); });
} }
function initData(data, config, 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,targetPercentage,candles) {
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,targetPercentage) let d = ai.run(arr, config[i], risk, pfTrade, riskFreeLevel,detail,loss,dur,maxTradeDays,targetPercentage,candles)
if (d !== null) if (d !== null)
cz.push(d) cz.push(d)
} }
......
...@@ -43,7 +43,7 @@ async function run() { ...@@ -43,7 +43,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,configs.targetPercentage)) 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,arr5))
} }
arr = await Promise.all(arr) arr = await Promise.all(arr)
......
...@@ -20,11 +20,32 @@ async function reaD() { ...@@ -20,11 +20,32 @@ async function reaD() {
} }
candles = arr candles = arr
grabData(1547635200,1547636700) let d = calculateRunnigTime('LONG', 1547632800, 1577653500, 2.04)
console.log(d)
// //
} }
function calculateRunnigTime(side, startTime, endTime, targetPrice) {
let arr = grabData(startTime, endTime)
let timeFrame = arr[1].t - arr[0].t
for (let i = 0; i < arr.length; i++) {
if (side === 'LONG') {
if (targetPrice <= arr[i].h)
return timeFrame * (i + 1)
} else {
if (targetPrice >= arr[i].l) {
return timeFrame * (i + 1)
}
}
}
}
function grabData(start, end) { function grabData(start, end) {
let arr = [] let arr = []
let indexStart = false let indexStart = false
...@@ -39,6 +60,6 @@ function grabData(start, end) { ...@@ -39,6 +60,6 @@ function grabData(start, end) {
} }
console.log(arr) return arr
} }
\ 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