Coder Social home page Coder Social logo

teddykoker / blog Goto Github PK

View Code? Open in Web Editor NEW
178.0 178.0 106.0 20.73 MB

Source code for my personal blog

Home Page: https://teddykoker.com

License: MIT License

HTML 0.08% Ruby 0.04% TeX 0.14% Jupyter Notebook 99.55% Shell 0.01% Python 0.03% Smarty 0.01% CSS 0.14%
blog machine-learning

blog's People

Contributors

dependabot[bot] avatar teddykoker avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

blog's Issues

picking stocks with the highest momentum

Hi Teddy, great post. I'm a bit confused with the following code though:

self.rankings.sort(key=lambda d: self.inds[d]["momentum"][0])

and then
# buy stocks with remaining cash for i, d in enumerate(self.rankings[:int(num_stocks * 0.2)]): cash = self.broker.get_cash() value = self.broker.get_value() if cash <= 0: break if not self.getposition(self.data).size: size = value * 0.001 / self.inds[d]["atr20"] self.buy(d, size=size)

Seems to be picking stocks with the lowest momentum? And It's supposed to be picking stocks with the highest momentum in Andreas F. Clenow's book?

Thanks!

Stock on the move: momentum

I can't seem to be able to reproduce your results with my own back-testing program.

hence I installed the backtester yesterday, trying to run your program, and found some problems,

in below code, rebalance portfolio never executed correctly.
if self.getposition(self.data).size: ==> should be ==> if self.getposition(d).size: huge bug...

after fixing this bug, I still have some problems... hence I'm contacting you to see if you have fixed any other bugs on this program. or there are some issues in Backtester itself. I am not an expert on Backtester. just started to use it.

my own testers seem to be working correctly.
thanks in advance.

def rebalance_portfolio(self):
# only look at data that we can have indicators for
self.rankings = list(filter(lambda d: len(d) > 100, self.stocks))
self.rankings.sort(key=lambda d: self.inds[d]["momentum"][0])
num_stocks = len(self.rankings)

    # sell stocks based on criteria
    for i, d in enumerate(self.rankings):
        if self.getposition(self.data).size:
            if i > num_stocks * 0.2 or d < self.inds[d]["sma100"]:
                self.close(d)
    
    if self.spy < self.spy_sma200:
        return
    
    # buy stocks with remaining cash
    for i, d in enumerate(self.rankings[:int(num_stocks * 0.2)]):
        cash = self.broker.get_cash()
        value = self.broker.get_value()
        if cash <= 0:
            break
        if not self.getposition(self.data).size:
            size = value * 0.001 / self.inds[d]["atr20"]
            self.buy(d, size=size)

feedback request (RL IN TRADING )

HI teddy

I would like to know as to who should I replace the following part ii , if I whant say instead want to maximize for other variant quantities say like calmar ratio

A = np.mean(R)
B = np.mean(np.square(R))
S = A / np.sqrt(B - A ** 2)    # under function gradient  

the above is just for sharpe ratio , that means that I have to write down the equivalent quantity to be maximized , then is it just enough to the following

def calc_calmar(rets):
# Peaks and bottoms indexes in sequence
mins = np.ravel(argrelmin(rets))
maxs = np.ravel(argrelmax(rets))
extrema = np.concatenate((mins, maxs))
extrema.sort()
return -rets.median()/np.diff(rets[extrema]).min()

then the above is just the another definition just like the one you introduced in your article i.e.

def sharpe_ratio(rets):
return rets.mean() / rets.std()

but now shall I add the quantity under gradient as well

mins = np.ravel(argrelmin(rets))
maxs = np.ravel(argrelmax(rets))
extrema = np.concatenate((mins, maxs)) 
extrema.sort()
return -rets.mean()/np.diff(rets[extrema]).min()

then finding derivatives w.r.t. the parameters of the calamr ratio , which are argmin and argmax ? is that right
your input is highly appreciated
best regards

Blog

Just a quick note to thank you for a splendid series of posts on Backtrader.

They are a great read!

Splendid code too.

grad calculation question

hello, Thanks for your great work. I learned a lot from your posts

I have a question.

On post

trading-with-reinforcement-learning-in-python-part-two-application.ipynb

each time step, the flowing grad is calculated
(dRdF * dFdtheta + dRdFp * dFpdtheta)

my question is: it seems this is only part of grad of each step?

Example of the csv file

Hi Teddy,

I was trying to implement the code you have written.
Can you provide a sample csv file so that I can cross check with my csv file whether they are same or not. Because with my csv file, I am getting a lot of error.

Prediction Signal?

I admit your project and math are over my head. I'm still struggling to understand your position function and with that I'm trying to come up with a simple

  • input format
  • that yields a simple output format (that I can interprete as a signal to buy, sell or hold)

How would that be done in your code?

Momentum stock selection

best30 = momentums30.iloc[-1:].max().sort_values(ascending=False).index[:5]
plt.figure(figsize=(12, 9))
plt.xlabel('Days')
plt.ylabel('Stock Price')
for best in best30:
print ("best = ", best, " momentum = ", momentums30[best].iloc[-1 :])
rets = np.log(stocks[best].iloc[-30 :])
x = np.arange(len(rets))
slope, intercept, r_value, p_value, std_err = linregress(x, rets)
plt.plot(np.arange(90), stocks[best][-90:]);
y = np.arange(60, 90)
z = np.e ** (intercept + slope*x)
plt.plot(y, z)
plt.show();

Hello, great work. But, I just read thru here and found some trouble understanding this. Don't you want to find the best 5 momentum stocks of the the current day, not anyday in the past?
hence, I changed the code to
best30 = momentums30.iloc[-1:].max().sort_values(ascending=False).index[:5]
to get the current day best 5 momentum candidates. ( i used 30 days momentum in this example)

Also changed the plot on regression line from the current day back to 30 days ago.

a. I plotted the 90 day stock prices from 90 days ago to today
b. plotted the regression line only for the last 30 days of the stock prices.

I believe this makes much more sense.

Comments? thanks in advance.

Momentum Equity Clenow List Sorting

Thanks for the bt version of Clenow momentum equity strategy.

QQ: Should the rankings be reverse sorted, as we want higher values first?

The line

self.rankings.sort(key=lambda d: self.inds[d]["momentum"][0])

would become

self.rankings.sort(key=lambda d: self.inds[d]["momentum"][0], reverse=True)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.