Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.8K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
I need help with my first genetic algorithm

Hi I develop open source cryptocurrency trading software at www.tradewave.net. I have written python versions of many financial technical indicators, you can find them in the tradewave forums.

This is my first attempt at a genetic algo and I'm not sure where I went wrong. When I optimize the bot manually... backtesting each possible allele state I get better results than when I let the algo run through my evolve() definition and do the same optimization automatically on the same data set.

You can backtest the strategy free here (you have to create an account - its free):

https://tradewave.net/strategy/unlgoZdiYF

or just read the syntax highlighted code here:

http://pastebin.com/nLdy2CiY

The whole algo is about 400 lines, but I'm pretty sure the problem is in `def evolve()` which is less than 50 lines without comments. The rest of the code works fine if I pre train the genome and skip the evolution process. There is a description of the algo at the top of the pastebin and everything is well commented.

Can you help my find my error?

much thanks!

`def evolve():`


log('evolve() attempt at locus: %s' % storage.locus)

# Define simulated backtest window
depth = 5000; width = 200

# create deep closing price arrays for each of 3 trading pairs
ltcbtc_dc, ltcusd_dc, btcusd_dc = close_arrays(depth)

#matrix of closing price numpy arrays
price_matrix = [ltcbtc_dc, ltcusd_dc, btcusd_dc]

# find the current value of locus to be mutated
original_state = storage.genome[storage.locus]

# create 3 simulated starting portfolios w/ same start balance
portfolio = np.array([ [0.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 1.0, 0.0] ])
# [usd, btc, ltc]

# empty any previous data in storage.macd_n_smooth from previous evolution
storage.macd_0_smooth = [[],[]] #ltcbtc
storage.macd_1_smooth = [[],[]] #ltcusd
storage.macd_2_smooth = [[],[]] #btcusd
# log('DEBUG 1 %s' % portfolio)

# run a simulated backtest for each allele state
# possible allele states are 0=usd; 1=btc; 2=ltc

for allele in range(0,3): # do this for each possible allele state (0,1,2)
#log('DEBUG 2 %s' % allele)
# for every possible slice[-200:] of 2000 deep closing array; oldest first:
for cut in range(depth, width, -1):
#if cut == depth: log('DEBUG 3 %s' % allele)
# create 2d matrix for market slices and macd for each currency pair
market_slice = [[],[],[]]
macd = [[],[],[]]
# create 1d matrix for last price
last = [0,0,0]
for n in range (0,3): # do this for each currency pair
#if (n == 0) and (cut==depth): log('DEBUG 4 %s' % allele)
#take a slice of the market for each currency pair
# slice notation reference
# z[-3:] # only the last 3
# z[:3] # only the first 3
market_slice[n] = (price_matrix[n][:cut])[-width:]
#calculate an macd value, and previous macd value for each currency pair
#try:
macd[n] = ta.MACD(market_slice[n], PERIOD1, PERIOD2)[-1]
#except Exception as e: log('talib fail %s' % e)
# smooth the macd for each currency pair
label = 'macd_' + str(n)
macd[n] = smooth(label, macd[n], AGGREGATION, 10)
# price normalize each macd by sma30
mean = (sum((market_slice[n])[-30:])/30)/100
macd[n] = macd[n]/mean
#extract last closing price
last[n] = market_slice[n][-1]

# calculate all_bull and all_bear from sim macd arrays for each instrument
all_bull, all_bear = all_bull_all_bear(macd[0], macd[1], macd[2])
#if (cut == depth): log('normalized macd %.5f %
What is up with this return redirect line?

main is just my homepage and / is the url. For some reason, I can't get signUp to return back to the homepage after finishing.
return redirect(function) goes return redirect(main) (since thats the name of home being rendered.

I can also confirm 'here' and 'Registered' get printed. Also, in my terminal, i get

127.0.0.1 - - [23/Apr/2018 23:13:51] "GET / HTTP/1.1" 200 -

Which means it SHOULD be going to home, because I get the same line when I just click the home link on the page. It redirects too. What happens with the flask is it just stays on the login page. Nothing else, no errors thrown. Just sits there.

@app.route("/")
def main():
return render_template('Senty.html')

@app.route('/signUp',methods=['POST','GET'])
def signUp():
# still need to create signup class and transfer below code to new file
conn = mysql.connect()
cur = conn.cursor()

try:
_name = request.form['inputName']
_email = request.form['inputEmail']
_password = request.form['inputPassword']

# validate the received values
if _name and _email and _password:

cur.callproc('sp_createUser',(_name,_email,_password,))
print ("Registered")
data = cur.fetchall()

conn.commit()
json.dumps({'message':'User created successfully !'})
print('here')
return redirect(url_for('main'))

#else:
#return json.dumps({'html':'<span>Enter the required fields</span>'})

#except Exception as e:
#return json.dumps({'error':str(e)})

finally:
cur.close()
conn.close()

@app.route('/showSignIn')
def showSignIn():
return(render_template('login.html'))

I'm not sure where to go. I feel like I've tried everything.


/r/flask
https://redd.it/8ei5hq