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

init

parent 8028eb6f
......@@ -7,16 +7,22 @@ let zarib = 'zarib'
let base = 'base'
let dynamic = 'dynamic'
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 saveData = []
candles = candlesData
timeFrame = candles[1].t - candles[0].t
let needPrintData = shouldPrint(detail, data[0].signal + '&' + type)
// console.log(needPrintData)
let commision = config.commision
let money = config.base
let unixStart = 0
let unixMid = 0
let total = 0
let maxDays = 0
let startDays = 0
......@@ -40,7 +46,7 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max
let lastMoney = 0
let maxMetaStep = 0
let levels = []
let targets=[]
let targets = []
let detailLevel = []
let durDetail = []
......@@ -66,9 +72,11 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max
return null
let obj
if (step === 0) {
unixStart = moment.tz(data[i].openDate, 'YYYY.MM.DD HH:mm:ss', 'GMT').unix()
startDays = data[i].openDate
money = config.base
} else {
if (config.type === zarib)
money = lastMoney * config.param
if (config.type === base)
......@@ -128,8 +136,8 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max
obj = d
obj.signal = data[i]
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.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()
if (d.meta < 0)
......@@ -176,6 +184,14 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max
if (endDays > maxDays)
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)
......@@ -184,6 +200,8 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max
// console.log(maxDays)
levels.push(step)
step = 0
unixStart = 0
unixMid = 0
reserv = 0
profit = 0
maxMetaStep = 0
......@@ -195,7 +213,7 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max
total++
} else {
unixMid = moment.tz(data[i].closeDate, 'YYYY.MM.DD HH:mm:ss', 'GMT').unix()
step++
profit = profit + d.profit
lastMoney = d.lastMoney
......@@ -317,11 +335,10 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max
// }
let sum = Math.abs(maxAllocatedMoney)
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)
let newCompund = sum / dur
......@@ -383,8 +400,7 @@ function run(data, config, risk, pfTrade, riskFreeLevel, detail, loss, durs, max
// console.log(data)
}
function getperp(target,maxAllocatedMoney)
{
function getperp(target, maxAllocatedMoney) {
let perp = (target * 100) / Math.abs(maxAllocatedMoney)
perp = perp / 100
return perp
......@@ -453,7 +469,7 @@ function inlineCheck(money, com, data, profit, lastCom, tar, riskFree, targetPer
let reserve = percentage(money, data.drawDown)
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
diff = diff - com - lastCom + profit
......@@ -475,7 +491,7 @@ function inlineCheck(money, com, data, profit, lastCom, tar, riskFree, targetPer
profit: profitMoney,
lastCom: lastCom,
com: com,
targetPrice:targetPrice,
targetPrice: targetPrice,
target: target,
targetDetail: targetDetail
}
......@@ -491,19 +507,13 @@ function inlineCheck(money, com, data, profit, lastCom, tar, riskFree, targetPer
com: com,
target: target,
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) {
......@@ -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)
if (side ==='LONG')
if (side === 'LONG')
return percentageAdv(basePrice, diffMoney)
else
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 =
{
......
......@@ -6,7 +6,7 @@ let moment = require('moment-timezone')
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) {
let arr = []
......@@ -15,12 +15,12 @@ async function parse(directory, name, configs, risk, pfTrade, side, riskFreeLeve
.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, 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)
......@@ -138,7 +138,7 @@ function initData(data, config, name, risk, pfTrade, side, riskFreeLevel,detail,
let cz = []
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)
cz.push(d)
}
......
......@@ -43,7 +43,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, 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)
......
......@@ -20,11 +20,32 @@ async function reaD() {
}
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) {
let arr = []
let indexStart = false
......@@ -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