Skip to content

Commit 5243958

Browse files
authored
Merge pull request #14 from CAFECA-IO/fix/tradebot_fail_cfdorder
Fix/tradebot fail cfdorder
2 parents fa70ab1 + 62c50f1 commit 5243958

File tree

4 files changed

+260
-7
lines changed

4 files changed

+260
-7
lines changed

src/strategies/strategies.service.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ describe('StrategiesService', () => {
125125
// Info: (20240320 - Jacky) this is aim to sum the profit of all trades
126126
const profitArray = tradeArray.map((trade) => trade.profit);
127127
const sum = profitArray.reduce((total, profit) => total + profit, 0);
128-
expect(sum).toBe(-2396.2045136100032);
128+
expect(sum).toBe(-2093.879039695004);
129129
});
130130

131131
it('should backtest use api', async () => {
@@ -178,6 +178,6 @@ describe('StrategiesService', () => {
178178
// Info: (20240320 - Jacky) this is aim to sum the profit of all trades
179179
const profitArray = tradeArray.map((trade) => trade.profit);
180180
const sum = profitArray.reduce((total, profit) => total + profit, 0);
181-
expect(sum).toBe(-39.96050000000014);
181+
expect(sum).toBe(-347.9295500000012);
182182
});
183183
});

src/strategies/strategy/autoArima.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ export function suggestion(data: { priceArray: number[]; spreadFee: number }) {
55
const arima = new ARIMA('auto');
66
arima.train(data.priceArray);
77
const AbsspreadFee = Math.abs(data.spreadFee);
8-
const arimaPredict = arima.predict(15);
8+
const arimaPredict = arima.predict(36);
99
const predict: number[] = arimaPredict[0];
1010
const predictMax = Math.max(...predict);
1111
const predictMin = Math.min(...predict);
12-
const predictProfit = AbsspreadFee * 2.5;
12+
const predictProfit = AbsspreadFee * 2.2;
1313
const currentPrice = data.priceArray[data.priceArray.length - 1];
1414
if (predictMax - currentPrice > predictProfit) {
1515
suggestion = 'BUY';
@@ -50,7 +50,7 @@ export function takeProfit(data: {
5050
const AbsOpenSpreadFee = Math.abs(data.spreadFee);
5151
const openPrice = data.openPrice;
5252
const currentPrice = data.currentPrice;
53-
const takeProfit = AbsOpenSpreadFee * 2.2;
53+
const takeProfit = AbsOpenSpreadFee * 2;
5454
const holdingStatus = data.holdingStatus;
5555
if (holdingStatus === 'BUY') {
5656
if (currentPrice - openPrice > takeProfit) {

src/tradebot/tradebot.controller.spec.ts

Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { UserService } from '../user/user.service';
1515

1616
describe('TradebotController', () => {
1717
let controller: TradebotController;
18+
// let tradebotService: TradebotService;
1819

1920
beforeEach(async () => {
2021
const module: TestingModule = await Test.createTestingModule({
@@ -38,9 +39,261 @@ describe('TradebotController', () => {
3839
}).compile();
3940

4041
controller = module.get<TradebotController>(TradebotController);
42+
// tradebotService = module.get<TradebotService>(TradebotService);
4143
});
4244

4345
it('should be defined', () => {
4446
expect(controller).toBeDefined();
4547
});
4648
});
49+
// TODO: (20240325 - Jacky) Should be finish in the future for higher quality
50+
// describe('create', () => {
51+
// it('should create a tradebot and return the tradebot details', async () => {
52+
// const createTradebotResult = {
53+
// returnDeposit: true,
54+
// tradebot: {
55+
// id: '1',
56+
// created_at: '2022-01-01',
57+
// wallet: {
58+
// address: 'address',
59+
// privateKey: 'privateKey',
60+
// },
61+
// currentAsset: {
62+
// data: {
63+
// balance: {
64+
// available: 100,
65+
// },
66+
// },
67+
// },
68+
// },
69+
// };
70+
// jest
71+
// .spyOn(tradebotService, 'create')
72+
// .mockImplementation(async);
73+
74+
// const result = await controller.create();
75+
76+
// expect(result).toEqual(
77+
// 'Tradebot 1 created at 2022-01-01 public address is address private key is privateKey and received deposit is true and currentAvailable = 100',
78+
// );
79+
// });
80+
81+
// it('should return an error message if tradebot creation fails', async () => {
82+
// jest.spyOn(tradebotService, 'create').mockResolvedValue(null);
83+
84+
// const result = await controller.create();
85+
86+
// expect(result).toEqual('Tradebot not created');
87+
// });
88+
89+
// it('should return an error message if deposit is not successful', async () => {
90+
// const createTradebotResult = {
91+
// returnDeposit: false,
92+
// tradebot: {
93+
// id: '1',
94+
// created_at: '2022-01-01',
95+
// wallet: {
96+
// address: 'address',
97+
// privateKey: 'privateKey',
98+
// },
99+
// currentAsset: {
100+
// data: {
101+
// balance: {
102+
// available: 100,
103+
// },
104+
// },
105+
// },
106+
// },
107+
// };
108+
// jest
109+
// .spyOn(tradebotService, 'create')
110+
// .mockResolvedValue(createTradebotResult);
111+
112+
// const result = await controller.create();
113+
114+
// expect(result).toEqual('Tradebot is created, but deposit is not successful');
115+
// });
116+
// });
117+
118+
// describe('getTradebot', () => {
119+
// it('should return all tradebots if id is not provided', async () => {
120+
// const tradebotArray = [
121+
// {
122+
// id: '1',
123+
// toJSON: () => ({
124+
// id: '1',
125+
// wallet: {
126+
// privateKey: 'privateKey',
127+
// },
128+
// }),
129+
// },
130+
// {
131+
// id: '2',
132+
// toJSON: () => ({
133+
// id: '2',
134+
// wallet: {
135+
// privateKey: 'privateKey',
136+
// },
137+
// }),
138+
// },
139+
// ];
140+
// jest.spyOn(tradebotService, 'getAllTradebots').mockResolvedValue(tradebotArray);
141+
142+
// const result = await controller.getTradebot();
143+
144+
// expect(result).toEqual([
145+
// {
146+
// tradebot: {
147+
// id: '1',
148+
// wallet: {
149+
// privateKey: 'privateKey',
150+
// },
151+
// },
152+
// 'private key': 'privateKey',
153+
// },
154+
// {
155+
// tradebot: {
156+
// id: '2',
157+
// wallet: {
158+
// privateKey: 'privateKey',
159+
// },
160+
// },
161+
// 'private key': 'privateKey',
162+
// },
163+
// ]);
164+
// });
165+
166+
// it('should return a tradebot by id', async () => {
167+
// const tradebot = {
168+
// id: '1',
169+
// toJSON: () => ({
170+
// id: '1',
171+
// wallet: {
172+
// privateKey: 'privateKey',
173+
// },
174+
// }),
175+
// };
176+
// jest.spyOn(tradebotService, 'getTradebotById').mockResolvedValue(tradebot);
177+
178+
// const result = await controller.getTradebot('1');
179+
180+
// expect(result).toEqual({
181+
// tradebot: {
182+
// id: '1',
183+
// wallet: {
184+
// privateKey: 'privateKey',
185+
// },
186+
// },
187+
// 'private key': 'privateKey',
188+
// });
189+
// });
190+
// });
191+
192+
// describe('updateTradebot', () => {
193+
// it('should update a tradebot and return the updated tradebot details', async () => {
194+
// const tradebot = {
195+
// id: '1',
196+
// toJSON: () => ({
197+
// id: '1',
198+
// }),
199+
// };
200+
// const updatedTradebot = {
201+
// id: '1',
202+
// toJSON: () => ({
203+
// id: '1',
204+
// }),
205+
// };
206+
// const data = {
207+
// suggestion: 'suggestion',
208+
// tradeStrategy: 'tradeStrategy',
209+
// stopLoss: 'stopLoss',
210+
// takeProfit: 'takeProfit',
211+
// };
212+
// jest.spyOn(tradebotService, 'getTradebotById').mockResolvedValue(tradebot);
213+
// jest.spyOn(tradebotService, 'updateTradebot').mockResolvedValue(updatedTradebot);
214+
215+
// const result = await controller.updateTradebot('1', data);
216+
217+
// expect(result).toEqual('1 is updated and tradebot = {"id":"1"}');
218+
// });
219+
220+
// it('should return an error message if tradebot is not found', async () => {
221+
// jest.spyOn(tradebotService, 'getTradebotById').mockResolvedValue(null);
222+
223+
// const result = await controller.updateTradebot('1', {});
224+
225+
// expect(result).toEqual('Tradebot not found');
226+
// });
227+
// });
228+
229+
// describe('command', () => {
230+
// it('should run a command to start the tradebot', async () => {
231+
// const tradebot = {
232+
// id: '1',
233+
// };
234+
// jest.spyOn(tradebotService, 'getTradebotById').mockResolvedValue(tradebot);
235+
// jest.spyOn(tradebotService, 'run').mockReturnValue('Run command executed');
236+
237+
// const result = await controller.command('1', { strategy: 'strategy', command: 'run' });
238+
239+
// expect(result).toEqual('Run command executed');
240+
// });
241+
242+
// it('should run a command to stop the tradebot', async () => {
243+
// const tradebot = {
244+
// id: '1',
245+
// };
246+
// jest.spyOn(tradebotService, 'getTradebotById').mockResolvedValue(tradebot);
247+
// jest.spyOn(tradebotService, 'stop').mockReturnValue('Stop command executed');
248+
249+
// const result = await controller.command('1', { strategy: 'strategy', command: 'stop' });
250+
251+
// expect(result).toEqual('Stop command executed');
252+
// });
253+
254+
// it('should run a command to receive deposit for the tradebot', async () => {
255+
// const tradebot = {
256+
// id: '1',
257+
// };
258+
// jest.spyOn(tradebotService, 'getTradebotById').mockResolvedValue(tradebot);
259+
// jest.spyOn(tradebotService, 'receiveDeposit').mockReturnValue('Deposit command executed');
260+
261+
// const result = await controller.command('1', { strategy: 'strategy', command: 'deposit' });
262+
263+
// expect(result).toEqual('Deposit command executed');
264+
// });
265+
266+
// it('should return an error message for an invalid command', async () => {
267+
// const tradebot = {
268+
// id: '1',
269+
// };
270+
// jest.spyOn(tradebotService, 'getTradebotById').mockResolvedValue(tradebot);
271+
272+
// const result = await controller.command('1', { strategy: 'strategy', command: 'invalid' });
273+
274+
// expect(result).toEqual('invalid is invalid command');
275+
// });
276+
277+
// it('should return an error message if tradebot is not found', async () => {
278+
// jest.spyOn(tradebotService, 'getTradebotById').mockResolvedValue(null);
279+
280+
// const result = await controller.command('1', { strategy: 'strategy', command: 'run' });
281+
282+
// expect(result).toEqual('Tradebot not found');
283+
// });
284+
285+
// it('should return an error message if an exception occurs', async () => {
286+
// const tradebot = {
287+
// id: '1',
288+
// };
289+
// jest.spyOn(tradebotService, 'getTradebotById').mockResolvedValue(tradebot);
290+
// jest.spyOn(tradebotService, 'run').mockImplementation(() => {
291+
// throw new Error('An error occurred');
292+
// });
293+
294+
// const result = await controller.command('1', { strategy: 'strategy', command: 'run' });
295+
296+
// expect(result).toEqual('An error occurred');
297+
// });
298+
// });
299+
// });

src/transaction/transaction.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export class TransactionService {
8989
): Promise<ReturnCreateCFDOrderDto> {
9090
const typeData = CFDOrderCreate;
9191
// Info: (20240315 - Jacky) this is aim to copy the object without any references
92-
typeData.message = createCFDDto;
92+
typeData.message = JSON.parse(JSON.stringify(createCFDDto));
9393
typeData.message.quotation.price = SafeMath.toSmallestUnit(
9494
typeData.message.quotation.price,
9595
10,
@@ -163,7 +163,7 @@ export class TransactionService {
163163
): Promise<ReturnCloseCFDOrderDto> {
164164
const typeData = CFDOrderClose;
165165
// Info: (20240315 - Jacky) this is aim to copy the object without any references
166-
typeData.message = closeCFDOrderDto;
166+
typeData.message = JSON.parse(JSON.stringify(closeCFDOrderDto));
167167
typeData.message.quotation.price = SafeMath.toSmallestUnit(
168168
typeData.message.quotation.price,
169169
10,

0 commit comments

Comments
 (0)