-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feature/modify_priceticker
- Loading branch information
Showing
24 changed files
with
1,044 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -176,7 +176,7 @@ Parameters | |
|
||
```typescript | ||
|
||
PUT /tradebot?id=tradebotId | ||
GET /tradebot?id=tradebotId | ||
|
||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
import { Controller } from '@nestjs/common'; | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { StrategiesService } from './strategies.service'; | ||
|
||
@Controller('strategies') | ||
export class StrategiesController { | ||
constructor(private readonly strategiesService: StrategiesService) {} | ||
|
||
@Get() | ||
async trainDqn() { | ||
await this.strategiesService.trainDqn(); | ||
return 'Training DQN finished'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* @license | ||
* Copyright 2019 Google LLC. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* ============================================================================= | ||
*/ | ||
|
||
import * as tf from '@tensorflow/tfjs-node'; | ||
|
||
export function createDeepQNetwork(numActions) { | ||
const model = tf.sequential(); | ||
model.add( | ||
tf.layers.dense({ units: 32, activation: 'relu', inputShape: [104] }), | ||
); | ||
model.add(tf.layers.dense({ units: 64, activation: 'relu' })); | ||
model.add(tf.layers.dense({ units: 128, activation: 'relu' })); | ||
model.add(tf.layers.dropout({ rate: 0.25 })); | ||
// model.add(tf.layers.dense({ units: numActions })); | ||
model.add(tf.layers.dense({ units: numActions, activation: 'sigmoid' })); | ||
return model; | ||
} | ||
|
||
/** | ||
* Copy the weights from a source deep-Q network to another. | ||
* | ||
* @param {tf.LayersModel} destNetwork The destination network of weight | ||
* copying. | ||
* @param {tf.LayersModel} srcNetwork The source network for weight copying. | ||
*/ | ||
export function copyWeights(destNetwork, srcNetwork) { | ||
// https://github.com/tensorflow/tfjs/issues/1807: | ||
// Weight orders are inconsistent when the trainable attribute doesn't | ||
// match between two `LayersModel`s. The following is a workaround. | ||
// TODO(cais): Remove the workaround once the underlying issue is fixed. | ||
let originalDestNetworkTrainable; | ||
if (destNetwork.trainable !== srcNetwork.trainable) { | ||
originalDestNetworkTrainable = destNetwork.trainable; | ||
destNetwork.trainable = srcNetwork.trainable; | ||
} | ||
|
||
destNetwork.setWeights(srcNetwork.getWeights()); | ||
|
||
// Weight orders are inconsistent when the trainable attribute doesn't | ||
// match between two `LayersModel`s. The following is a workaround. | ||
// TODO(cais): Remove the workaround once the underlying issue is fixed. | ||
// `originalDestNetworkTrainable` is null if and only if the `trainable` | ||
// properties of the two LayersModel instances are the same to begin | ||
// with, in which case nothing needs to be done below. | ||
if (originalDestNetworkTrainable != null) { | ||
destNetwork.trainable = originalDestNetworkTrainable; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
export class Environment { | ||
current_step: number; | ||
initial_balance: number; | ||
balance: number; | ||
action_space: any; | ||
done: boolean; | ||
spreadFee: number; | ||
holdingStatus: number; | ||
priceArray: number[]; | ||
openPrice: number; | ||
currentPrice: number; | ||
profitLog: number[]; | ||
actionLog: any[]; | ||
rewardLog: any[]; | ||
|
||
constructor(priceArray) { | ||
this.current_step = 48; | ||
this.initial_balance = 10000; | ||
this.spreadFee = 30; | ||
this.balance = this.initial_balance; | ||
this.priceArray = priceArray; | ||
this.action_space = [0, 1, 2, 3]; // WAIT, BUY, SELL, CLOSE | ||
this.done = false; | ||
this.holdingStatus = 0; | ||
this.profitLog = []; | ||
this.actionLog = []; | ||
this.rewardLog = []; | ||
} | ||
|
||
reset() { | ||
this.current_step = 48; | ||
this.initial_balance = 10000; | ||
// this.spreadFee = 30; | ||
this.balance = this.initial_balance; | ||
this.done = false; | ||
this.holdingStatus = 0; | ||
this.profitLog = []; | ||
return this; | ||
} | ||
|
||
// return { | ||
// currentPrice: currentPrice, | ||
// openPrice: this.openPrice, | ||
// spreadFee: spreadFee, | ||
// profit: profit, | ||
// holdingStatus: 'WAIT', | ||
// }; | ||
step(action: any) { | ||
let profit = 0; | ||
let reward = 0; | ||
this.currentPrice = this.priceArray[this.current_step]; | ||
this.spreadFee = 0.005 * this.currentPrice; | ||
if (action === 3) { | ||
if (this.holdingStatus === 0) { | ||
reward = -40; | ||
} | ||
if (this.holdingStatus === 1) { | ||
profit = this.currentPrice - this.spreadFee - this.openPrice; | ||
reward = profit; | ||
reward += 10; | ||
this.holdingStatus = 0; | ||
} | ||
if (this.holdingStatus === 2) { | ||
profit = this.openPrice - (this.currentPrice + this.spreadFee); | ||
reward = profit; | ||
reward += 10; | ||
this.holdingStatus = 0; | ||
} | ||
} | ||
if (action === 1) { | ||
if (this.holdingStatus === 0) { | ||
const currentPriceStr = JSON.stringify(this.currentPrice); | ||
this.openPrice = JSON.parse(currentPriceStr); | ||
this.holdingStatus = 1; | ||
reward = 25; | ||
if (this.openPrice < this.priceArray[this.current_step + 10]) { | ||
reward += 24; | ||
} else { | ||
reward += -14; | ||
} | ||
} else { | ||
reward = -35; | ||
} | ||
} | ||
if (action === 2) { | ||
if (this.holdingStatus === 0) { | ||
const currentPriceStr = JSON.stringify(this.currentPrice); | ||
this.openPrice = JSON.parse(currentPriceStr); | ||
this.holdingStatus = 2; | ||
reward = 25; | ||
if (this.openPrice > this.priceArray[this.current_step + 10]) { | ||
reward += 20; | ||
} else { | ||
reward += -20; | ||
} | ||
} else { | ||
reward = -35; | ||
} | ||
} | ||
this.current_step += 1; | ||
if (this.current_step >= this.priceArray.length) { | ||
this.done = true; | ||
} | ||
if (action === 0) { | ||
if (this.actionLog.slice(-24).every((a) => a === 0)) { | ||
reward = -30; | ||
} | ||
} | ||
return { | ||
previousPrice: this.priceArray.slice(-100), | ||
currentPrice: this.currentPrice, | ||
openPrice: this.openPrice, | ||
spreadFee: this.spreadFee, | ||
holdingStatus: this.holdingStatus, | ||
reward: reward, | ||
}; | ||
} | ||
getState() { | ||
return [ | ||
this.priceArray.slice(-100), | ||
this.currentPrice, | ||
this.openPrice, | ||
this.spreadFee, | ||
this.holdingStatus, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"modelTopology":{"class_name":"Sequential","config":{"name":"sequential_1","layers":[{"class_name":"Dense","config":{"units":32,"activation":"relu","use_bias":true,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"normal","seed":null}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"dense_Dense1","trainable":true,"batch_input_shape":[null,104],"dtype":"float32"}},{"class_name":"Dense","config":{"units":64,"activation":"relu","use_bias":true,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"normal","seed":null}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"dense_Dense2","trainable":true}},{"class_name":"Dense","config":{"units":128,"activation":"relu","use_bias":true,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"normal","seed":null}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"dense_Dense3","trainable":true}},{"class_name":"Dropout","config":{"rate":0.25,"noise_shape":null,"seed":null,"name":"dropout_Dropout1","trainable":true}},{"class_name":"Dense","config":{"units":4,"activation":"sigmoid","use_bias":true,"kernel_initializer":{"class_name":"VarianceScaling","config":{"scale":1,"mode":"fan_avg","distribution":"normal","seed":null}},"bias_initializer":{"class_name":"Zeros","config":{}},"kernel_regularizer":null,"bias_regularizer":null,"activity_regularizer":null,"kernel_constraint":null,"bias_constraint":null,"name":"dense_Dense4","trainable":true}}]},"keras_version":"tfjs-layers 4.17.0","backend":"tensor_flow.js"},"weightsManifest":[{"paths":["weights.bin"],"weights":[{"name":"dense_Dense1/kernel","shape":[104,32],"dtype":"float32"},{"name":"dense_Dense1/bias","shape":[32],"dtype":"float32"},{"name":"dense_Dense2/kernel","shape":[32,64],"dtype":"float32"},{"name":"dense_Dense2/bias","shape":[64],"dtype":"float32"},{"name":"dense_Dense3/kernel","shape":[64,128],"dtype":"float32"},{"name":"dense_Dense3/bias","shape":[128],"dtype":"float32"},{"name":"dense_Dense4/kernel","shape":[128,4],"dtype":"float32"},{"name":"dense_Dense4/bias","shape":[4],"dtype":"float32"}]}],"format":"layers-model","generatedBy":"TensorFlow.js tfjs-layers v4.17.0","convertedBy":null} |
Binary file not shown.
Oops, something went wrong.