Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K 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 %
NiceGUI 1.2.9 with "refreshable" UI functions, better dark mode support and an interactive styling demo

We are happy to announce NiceGUI 1.2.9. NiceGUI is an open-source Python library to write graphical user interfaces which run in the browser. It has a very gentle learning curve while still offering the option for advanced customizations. NiceGUI follows a backend-first philosophy: it handles all the web development details. You can focus on writing Python code.

### New features and enhancements

- Introduce `ui.refreshable`
- Add enable and disable methods for input elements
- Introduce `ui.dark_mode`
- Add min/max/step/prefix/suffix parameters to `ui.number`
- Switch back to Starlette's StaticFiles
- Relax version restriction for FastAPI dependency

### Bugfixes

- Fix `ui.upload` behind reverse proxy with subpath
- Fix hidden label when text is 0

### Documentation

- Add an interactive demo for classes, style and props
- Improve documentation for `ui.timer`
- Add a demo for creating a ui.table from a pandas dataframe

Thanks for the awesome new contributions. We would also point out that in 1.2.8 we have already introduced the capability to use emoji as favicon. Now you can write:

from nicegui import ui

ui.label("NiceGUI Rocks!")

ui.run(favicon="🚀")


/r/Python
https://redd.it/12u2b9w
NiceGUI 1.2.10 with improved bindings, ui.grid, ui.chat_message and more

### New features and enhancements

- Add [autocomplete](https://nicegui.io/documentation/input#autocompletion) for `ui.input`
- Allow binding dictionaries
- Add binding methods for disableable elements
- Introduce [`ui.grid`](https://nicegui.io/documentation/grid)
- Introduce [`ui.chat_message`](https://nicegui.io/documentation/chat_message)
- Support [`ui.refreshable`](https://nicegui.io/documentation/refreshable) with async functions
- Style blockquotes in [`ui.markdown`](https://nicegui.io/documentation/markdown) elements
- Introduce [`seek`](https://nicegui.io/documentation/video#video_start_position) method for `ui.video`
- Support [base64](https://nicegui.io/documentation/run#base64_favicon) and [SVG](https://nicegui.io/documentation/run#svg_favicon) favicons
- Add `mount_path` parameter to `ui.run_with`

### Bugfixes

- Fix layout issue with [`ui.refreshable`](https://nicegui.io/documentation/refreshable)
- Avoid memory leak with [`ui.refreshable`](https://nicegui.io/documentation/refreshable)
- Fix new event registrations not being sent to the client

### Documentation

- Allow choosing from different elements for the [live demo](https://nicegui.io/documentation#try_styling_nicegui_elements_)
- Improve menu accessibility on mobile devices

Thanks for all the great contributions. It is a pleasure working with you.

/r/Python
https://redd.it/130toef