Commit c7d7abda authored by Ahmad Nemati's avatar Ahmad Nemati

git pull

parent a74b91b0
{
"apps": [
{
"name": "torobche2",
"script": "./torobche2.js",
"merge_logs": true,
"log_date_format":"YY-MM-DD HH:mm",
"instances" : "max",
"exec_mode" : "cluster",
"cron_restart": "*/3 * * * *"
}
]
}
const fs = require('fs')
let moment = require('moment-timezone')
let _ = require('lodash')
const uuid = require('uniqid')
let pair = 'BNB'
let res
let arr = []
let lastBalance = 5000
let size = 50
let balance = lastBalance
let files = ['newCom1.json',
'newCom2.json',
'newCom3.json']
init()
let base = 1
let coms
//profitLossString
//drawDownString
let year = ['2017', '2018', '2019', '2020', '2021', '2022']
let month = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']
let dates = []
async function init() {
try {
await fs.mkdirSync('torob')
} catch (e) {
}
coms = await fs.readFileSync('t.json', 'utf8');
let bad = await fs.readFileSync('bad.txt', 'utf8');
bad = JSON.parse(bad)
base = parseFloat(bad.bad)
//console.log(bad)
coms = JSON.parse(coms)
run()
}
async function lastCheck(param1, param2, param3, arr5) {
let bad = await fs.readFileSync('bad.txt', 'utf8');
bad = JSON.parse(bad)
base = parseFloat(bad.bad)
if (param3 > base) {
let z = {maxReserve: param1, profit: param2, bad: param3}
await createfileADV(JSON.stringify(z))
await createfileTorob(param3.toString(), JSON.stringify(arr5, null, 2))
}
}
function resetDates() {
dates = []
for (let i = 0; i < year.length; i++) {
for (let j = 0; j < month.length; j++) {
let t = year[i] + '-' + month[j]
if (!(t === '2017-01' || t === '2017-02' || t === '2017-03' || t === '2017-04' || t === '2017-05' || t === '2017-06' || t === '2017-07' || t === '2017-08' || t === '2022-05' || t === '2022-06' || t === '2022-07' || t === '2022-08' || t === '2022-09' || t === '2022-10' || t === '2022-11' || t === '2022-12'))
dates.push({date: t, profit: 0, count: 0, longCount: 0, shortCount: 0, longProfit: 0, shortProfit: 0})
}
}
}
async function sleep(millis) {
return new Promise(resolve => setTimeout(resolve, millis));
}
async function run() {
coms = _.shuffle(coms)
let com = _.uniqBy(coms, 'platform');
balance = lastBalance
arr = []
resetDates()
for (let i = 0; i < size; i++) {
let closeDate = com[i].closeDate.split('_')
let profits = com[i].profitLossString.split('_')
let side = com[i].sideString.split('_')
for (let j = 0; j < closeDate.length - 1; j++)
add(closeDate[j], parseFloat(profits[j]) - 0.1, side[j])
}
arr = _.orderBy(arr, ['date'])
for (let i = 0; i < arr.length; i++) {
balance = balance + arr[i].profit
arr[i].balance = balance
}
let arrNew = []
for (let i = 0; i < arr.length; i++) {
let sums = 0
for (let j = i + 1; j < arr.length; j++) {
sums = sums + parseFloat(arr[j].profit)
if (arrNew.length === 0)
arrNew.push({sum: sums, index: i, startDate: arr[i + 1].date, date: arr[j].date})
else if (arrNew[arrNew.length - 1].sum > sums)
arrNew.push({sum: sums, index: i, startDate: arr[i + 1].date, date: arr[j].date})
}
}
arrNew = _.orderBy(arrNew, ['sum'])
for (let i = 0; i < arr.length; i++)
addDate(arr[i])
let arrDate = []
for (let i = 0; i < dates.length; i++) {
let sums = 0
for (let j = i + 1; j < dates.length; j++) {
sums = sums + parseFloat(dates[j].profit)
if (arrDate.length === 0)
arrDate.push({sum: sums, index: i, startDate: dates[i + 1].date, date: dates[j].date})
else if (arrDate[arrDate.length - 1].sum > sums)
arrDate.push({sum: sums, index: i, startDate: dates[i + 1].date, date: dates[j].date})
}
}
arrDate = _.orderBy(arrDate, ['sum'])
balance = lastBalance
for (let i = 0; i < dates.length; i++) {
balance = balance + dates[i].profit
dates[i].balance = balance
}
// console.log(arr)
// console.log('Balance ->',balance)
// console.log('Worth ->',arrNew[0])
if (arrDate[0].sum === 0)
return run()
// let worth = (balance - lastBalance) / (arrDate[0].sum)
let allMonthLoss = 0
for (let i = 0; i < dates.length; i++)
if (dates[i].profit < 0)
return run()
let worth = balance-lastBalance+allMonthLoss
if ( worth > base) {
base = worth
console.log(arrDate[0].sum, balance - lastBalance, worth)
let arr5 = []
for (let f = 0; f < size; f++) {
arr5.push(com[f])
}
// let z={maxReserve:arrNew[0].sum,balance:balance,bad:base}
//
// await createfileADV(JSON.stringify(z))
await lastCheck(arrDate[0].sum, balance - lastBalance, base, arr5)
}
// let start=false
// let arr2=[]
// for (let i=0;i<arr.length;i++)
// {
// if (arr[i].date ===arrNew[0].startDate)
// {
// start=true
// arr2.push(arr[i])
// }
// // else if (arr[i].date === arrNew[0].date )
// // {
// // arr2.push(arr[i])
// // break
// // }
// else if (start)
// arr2.push(arr[i])
// }
//
// balance=lastBalance
// for (let i=0;i<arr2.length;i++)
// {
// balance=balance+arr2[i].profit
// arr2[i].balance=balance
// }
// createfileADV(JSON.stringify(arr2, null, 2))
// let index=arrNew[0].index
// arrNew=[]
// for (let i = index; i < index+1; i++) {
// let sums = 0
// for (let j = i + 1; j < arr.length ; j++) {
//
// sums = sums + parseFloat(arr[j].profit)
//
// // if (arrNew.length ===0)
// // arrNew.push({sum: sums, index: i,date:arr[j].date})
// // else if (arrNew[arrNew.length-1].sum > sums)
// arrNew.push({sum: sums, index: i,date:arr[j].date})
//
//
// }
//
// }
return run()
}
function addDate(data) {
for (let i = 0; i < dates.length; i++)
if (data.date.includes(dates[i].date)) {
dates[i].profit = dates[i].profit + data.profit
dates[i].profit = parseFloat(parseFloat(dates[i].profit).toFixed(2))
dates[i].count = dates[i].count + data.count
dates[i].longCount = dates[i].longCount + data.longCount
dates[i].longProfit = dates[i].longProfit + data.longProfit
dates[i].shortCount = dates[i].shortCount + data.shortCount
dates[i].shortProfit = dates[i].shortProfit + data.shortProfit
return
}
}
function add(date, profit, side) {
profit = parseFloat(parseFloat(profit).toFixed(2))
for (let i = 0; i < arr.length; i++)
if (arr[i].date === date) {
arr[i].profit = arr[i].profit + profit
arr[i].count++
if (side === 'LONG') {
arr[i].longCount++
arr[i].longProfit = arr[i].longProfit + profit
} else {
arr[i].shortCount++
arr[i].shortProfit = arr[i].shortProfit + profit
}
return
}
if (side === 'LONG')
arr.push({
date: date,
profit: profit,
count: 1,
longCount: 1,
shortCount: 0,
longProfit: profit,
shortProfit: 0
})
else
arr.push({
date: date,
profit: profit,
count: 1,
longCount: 1,
shortCount: 1,
longProfit: 0,
shortProfit: profit
})
}
function get(date) {
for (let i = 0; i < arr.length; i++)
if (arr[i].date === date) {
return arr[i]
}
}
function createfileADV(data) {
// console.log(data)
return new Promise(function (resolve, reject) {
fs.writeFile('bad.txt', data, 'utf8', function (err) {
if (err) reject(err);
else resolve(data);
});
});
}
function createfileTorob(name, data) {
// console.log(data)
return new Promise(function (resolve, reject) {
fs.writeFile('torob/' + name + '.json', data, 'utf8', function (err) {
if (err) reject(err);
else resolve(data);
});
});
}
function randomInt(low, high) {
return Math.floor(Math.random() * (high - low) + low)
}
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