import numpy as np
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Input, Dense, Conv2D, Concatenate, Dropout, Subtract, \
Flatten, MaxPooling2D, Multiply, Lambda, Add, Dot
from keras.backend import constant
from keras import optimizers
from keras.engine.topology import Layer
from keras.models import Model
from keras.layers import Input
from keras import initializers
from keras.constraints import max_norm
import keras.backend as K
m = 1 # dimension of state space
d = 1 # number of layers in strategy
N = 20 # time discretization
price = Input(shape=(m,))
strategy = Input(shape=(m,))
wealth = Input(shape=(1,))
costs = Input(shape=(1,))
inputs = [price, strategy, costs, wealth]
output_state = []
layers = []
for j in range(N):
for i in range(d):
layer = Dense(m, activation='tanh',trainable=True,
kernel_initializer=initializers.RandomNormal(0,1),#kernel_initializer='random_normal',
bias_initializer='random_normal',
name=str(i)+str(j))
layers = layers + [layer]
for j in range(N):
helper1 = Concatenate()([price,strategy])
for i in range(d):
strategyhelper = layers[i+(j)*d](helper1)
incr = Input(shape=(m,))
change = Subtract()([strategyhelper,strategy])
strategy = strategyhelper
absolutechanges = Lambda(lambda x : K.abs(x))(change)
costs = Multiply()([absolutechanges,price])
costs = Lambda(lambda x : -0.01*K.sum(x,axis=1))(costs)
price = Add()([incr, price])
mult = Multiply()([strategyhelper, incr])
mult = Lambda(lambda x : K.sum(x,axis=1))(mult)
wealth = Add()([mult,wealth])
wealth = Add()([costs,wealth])
inputs = inputs + [incr]
state = wealth
helper = Lambda(lambda x : K.abs(x))(strategy)
costs = Multiply()([helper,price])
costs = Lambda(lambda x : -0.01*K.sum(x,axis=1))(costs)
wealth = Add()([wealth,costs])
state = wealth
model_tradewealth = Model(inputs, outputs=state)