Skip to content

Commit

Permalink
Base Strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeykosinski committed Jul 15, 2021
1 parent 11978f4 commit d9d1618
Show file tree
Hide file tree
Showing 23 changed files with 490 additions and 431 deletions.
Binary file removed article/apiqueue.webp
Binary file not shown.
6 changes: 3 additions & 3 deletions config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
"development": {
"username": "root",
"password": "example",
"database": "bot",
"database": "tradebot",
"host": "127.0.0.1",
"dialect": "postgres"
},
"test": {
"username": "root",
"password": "example",
"database": "bot",
"database": "tradebot",
"host": "127.0.0.1",
"dialect": "postgres"
},
"production": {
"username": "root",
"password": "example",
"database": "bot",
"database": "tradebot",
"host": "127.0.0.1",
"dialect": "postgres"
}
Expand Down
11 changes: 11 additions & 0 deletions constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,19 @@ const SYMBOL = {
ETHUSDT: 'ETHUSDT',
BTCUSDT: 'BTCUSDT',
}


const STRATEGY = {
STATUS:{
WAIT_ENTRY_POINT:'WAIT_ENTRY_POINT',
IN_PROGRESS:'IN_PROGRESS',
COMPLETED:'COMPLETED'
}
}

module.exports = {
ORDER,
K_LINE,
SYMBOL,
STRATEGY
}
17 changes: 0 additions & 17 deletions core/index.js

This file was deleted.

3 changes: 2 additions & 1 deletion db/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const defineModels = require('../models');
const defineProviders = require('../providers');
const { Sequelize } = require('sequelize');
const config = require('../config');

async function initDB(config) {
const sequelize = new Sequelize({
Expand All @@ -26,4 +27,4 @@ async function initDB(config) {
});
}

module.exports = initDB;
module.exports = initDB(config);
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ services:
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: example
POSTGRES_DB: bot
POSTGRES_DB: tradebot
ports:
- 5432:5432
networks:
Expand Down
42 changes: 42 additions & 0 deletions helpers/asynQueue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class AsyncQueue {
constructor(options = {}) {
const { log, error } = options;
this.log = log || console.log;
this.error = error || console.error;
this.queue = [];
this.isRunning = false;
}

addToQueue(func, data) {
this.queue.push({ func, data });
}

emitInQueue = (func, data) => {
this.addToQueue(func, data);
this.runQueue();
};

runQueue() {
if (this.isRunning) {
return;
}
this.isRunning = true;
this.runNext().catch((e) => this.error('runQueue error', e));
}

async runNext() {
if (this.queue.length === 0) {
this.isRunning = false;
return;
}
const { func, data } = this.queue.splice(0, 1)[0];
if (typeof func === 'function') {
await func(data);
} else {
this.log('function in queue not found', { func, data });
}
await this.runNext();
}
}

module.exports = AsyncQueue;
17 changes: 17 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const path = require('path')
const initConfig = require('./config')
const initDB = require('./db')

const ROOT_PATH = process.cwd();

(async function () {
const config = await initConfig(path.resolve(ROOT_PATH, 'config'))

const db = await initDB()
application.services = { db, config }


await application.start(true)

logger.info('Application started')
})()
25 changes: 6 additions & 19 deletions migrations/20210602202235-order.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,23 @@ module.exports = {
allowNull: true
},
price: {
type: Sequelize.REAL, // ???
type: Sequelize.REAL,
allowNull: true
},
avg_price: {
type: Sequelize.REAL, // ???
type: Sequelize.REAL,
allowNull: true
},
activate_price: {
type: Sequelize.REAL, // ???
type: Sequelize.REAL,
allowNull: true
},
price_rate: {
type: Sequelize.REAL, // ???
type: Sequelize.REAL,
allowNull: true
},
stop_price: {
type: Sequelize.REAL, // ???
type: Sequelize.REAL,
allowNull: true
},
side: {
Expand Down Expand Up @@ -83,7 +83,7 @@ module.exports = {
allowNull: true
},
update_time: {
type: Sequelize.STRING, // ??? DATE
type: Sequelize.STRING,
allowNull: true
},
working_type: {
Expand All @@ -94,19 +94,6 @@ module.exports = {
type: Sequelize.JSONB,
allowNull: true
},
created_at: {
type: Sequelize.DATE,
allowNull: true,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
},
updated_at: {
type: Sequelize.DATE,
allowNull: true
},
deleted_at: {
type: Sequelize.DATE,
allowNull: true
},
commission: {
type: Sequelize.REAL,
allowNull: true
Expand Down
68 changes: 68 additions & 0 deletions migrations/20210703122401-strategy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict';

module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('strategy', {
id: {
autoIncrement: true,
type: Sequelize.BIGINT,
allowNull: false,
primaryKey: true
},
status: {
type: Sequelize.STRING,
allowNull: false
},
profit: {
type: Sequelize.REAL,
allowNull: true
},
position_side: {
type: Sequelize.STRING,
allowNull: true
},
symbol: {
type: Sequelize.STRING,
allowNull: true
},
type: {
type: Sequelize.STRING,
allowNull: true
},
options:{
type: Sequelize.JSONB,
allowNull: true,
},
user_id: {
type: Sequelize.BIGINT,
allowNull: false,
references: {
model: {
tableName: 'user'
},
key: 'id'
},
onUpdate: 'cascade',
onDelete: 'cascade'
},
createdAt: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
updatedAt: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
deletedAt: {
type: Sequelize.DATE,
allowNull: true,
},
})
},

down: (queryInterface) => {
return queryInterface.dropTable('strategy');
}
};
2 changes: 2 additions & 0 deletions models/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'
const user = require('./user')
const order = require('./order')
const strategy = require('./strategy')

/**
*
Expand All @@ -11,6 +12,7 @@ function defineModels(sequelize) {
[
order(sequelize),
user(sequelize),
strategy(sequelize),
].map((model) => {
if (model.associate) {
model.associate(sequelize.models)
Expand Down
66 changes: 66 additions & 0 deletions models/strategy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const { Model, DataTypes } = require('sequelize')

class Strategy extends Model {
static associate(models) {
Strategy.hasMany(models.Order, {
foreignKey: 'strategyId',
sourceKey: 'id',
as: 'orders'
});
}
}
module.exports = function (sequelize) {
return Strategy.init(
{
id: {
autoIncrement: true,
type: DataTypes.BIGINT,
allowNull: false,
primaryKey: true
},
status: {
type: DataTypes.STRING,
allowNull: false
},
profit: {
type: DataTypes.REAL,
allowNull: true
},
positionSide: {
type: DataTypes.STRING,
allowNull: true
},
symbol: {
type: DataTypes.STRING,
allowNull: true
},
type: {
type: DataTypes.STRING,
allowNull: true
},
options:{
type: DataTypes.JSONB,
allowNull: true,
},
userId: {
type: DataTypes.BIGINT,
allowNull: false,
references: {
model: {
tableName: 'user'
},
key: 'id'
},
onUpdate: 'cascade',
onDelete: 'cascade'
},
},
{
sequelize,
tableName: 'strategy',
timestamps: true,
},
)
}

module.exports.Strategy = Strategy
4 changes: 2 additions & 2 deletions models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ module.exports = function (sequelize) {
type: DataTypes.STRING,
allowNull: false
},
binance_api_key: {
binanceApiKey: {
type: DataTypes.STRING,
allowNull: true
},
binance_api_secret: {
binanceApiSecret: {
type: DataTypes.STRING,
allowNull: true
},
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"migrate": "node_modules/sequelize-cli/lib/sequelize db:migrate",
"migrate:undo": "sequelize-cli db:migrate:undo",
"migrate:undo:all": "sequelize-cli db:migrate:undo:all",
"migrate:add": "sequelize-cli migration:generate --name x",
"seed": "sequelize-cli db:seed:all",
"seed:undo": "sequelize-cli db:seed:undo",
"seed:undo:all": "sequelize-cli db:seed:undo:all"
Expand Down
Loading

0 comments on commit d9d1618

Please # to comment.