Git Product home page Git Product logo

Comments (17)

happynoom avatar happynoom commented on July 3, 2024 1

@EmbraceLife I got why comes this difference. The output of the Deeptrade is the position in rate or persent. That means when it output 0.1, we will hold previous_day_capital * 0.1 dollars of stock share. So how much profit we get is previous_day_capital * 0.1 * price_change_rate. Note that the label of the sample is also change rate, not value. And the cumulative return I calculated is profit rate. It means I put 1 unit at beginning and got 4.1 unit at the end, and profit rate is (4.1-1)/1=310%

from deeptrade_keras.

Masquerade0097 avatar Masquerade0097 commented on July 3, 2024 1

@happynoom Can you please post the answer to @EmbraceLife comment above about "peeking into the future problem" so that is easier for everyone to conclude from the discussion. Thanks.

from deeptrade_keras.

happynoom avatar happynoom commented on July 3, 2024

model.30.best is one of my best model according to validation loss, because you know I train many times, and keep several version of model. And all I kept reach the -0.16 on validation set on loss. I have draw cumulative return curve on the website http://deeplearning.xin/ using this model.30.best model. You could get to see. Thank you!

from deeptrade_keras.

EmbraceLife avatar EmbraceLife commented on July 3, 2024

The profit curve looks fantastic! However, Strangely I can't reproduce the same profit curve with model.30.best in repo. Could you include your profit_calculation function in your repo too?

When you have time, could you also try the model.30.best directly from your repo and see whether it gets your the same profit curve showing on your web visualization?

Thanks!

from deeptrade_keras.

happynoom avatar happynoom commented on July 3, 2024

@EmbraceLife , Did you calculate the profit using compound interest. In my webpage, I calculate the profit using compound interest. That means if I have 1 dollar as principal and 0 percent of profit at the beginning. Then if I got profit, it will be put into the principal to next investment. Suppose the i(th) day's price change rate is ri, and the position advice is pi, so how much I have after i(th) day's investment will be Ci = 1 dollar * (1.0 + r0 * p0) * (1.0 + r1 * p1) * ... *(1.0 + ri * pi) . So, the cumulative return of i(th) day will be (Ci - 1.0) . That's how I calculated the cumulative return curve, if I'm wrong, let me know.

from deeptrade_keras.

happynoom avatar happynoom commented on July 3, 2024

@EmbraceLife , Ok, I will submit a profit calculation function.

from deeptrade_keras.

happynoom avatar happynoom commented on July 3, 2024

@EmbraceLife use python gossip.py predict to calculate cumulative return. If I went wrong, let me know. Thank you!

from deeptrade_keras.

EmbraceLife avatar EmbraceLife commented on July 3, 2024

Thanks @happynoom

With this updated repo, I can replicate the same cumulative return. But I still have some concerns on using compound interest to calculate cumulative returns.

As you described above, you seem to take compound interest approach to calc return. Here is your code below, and I added some comment to describe my understanding of your approach

def calculate_cumulative_return(labels, pred):
    cr = []
    if len(labels) <= 0:
        return cr
   # first day's capital = original capital + today's profit = original_capital * (1 + today_price_change * today_position) 
    cr.append(1. * (1. + labels[0] * pred[0]))

    for l in range(1, len(labels)):
        # from day2 onward, each_day_capital = previous_day_capital + today_profit = previous_day_capital * (1 + today_price_change * today_position) 
        cr.append(cr[l-1] * (1 + labels[l] * pred[l]))
    for i in range(len(cr)):
        cr[i] = cr[i] - 1
    return cr

If my two comments above are correct in describing what your intention of the code above, then here is a problem:
a) it is always the case: each_day_capital == previous_day_capital + today_profit (No problem)
b) but it is not always the case (Real problem): previous_day_capital + today_profit == previous_day_capital * (1 + today_price_change * today_position) why?
c) if yesterday DeepTrade sold 90% of my position, then previous_day_capital + today_profit == previous_day_capital + today_price_change * today_position *previous_day_price != previous_day_capital * (1 + today_price_change * today_position)

So, if you agree with the approach of each_day_capital == previous_day_capital + today_profit == previous_day_capital + previous_day_close_pricetoday_positiontoday_price_change, then a fake code could be something like:

def calculate_cumulative_return(labels, pred, closes):
    cr = []
    if len(labels) <= 0:
        return cr
    cr.append(1. * (1. + labels[0] * pred[0]))

    norm_closes = closes/closes[0]
    for l in range(1, len(labels)):
        cr.append(cr[l-1] + (norm_closes[l-1]* labels[l] * pred[l]))
    for i in range(len(cr)):
        cr[i] = cr[i] - 1
    return cr

Please let me know whether this makes sense.

from deeptrade_keras.

EmbraceLife avatar EmbraceLife commented on July 3, 2024

In fact, I also have a concern about which day (previous, current or next day) should prediction or position values be applied to.

Here, I try to calculate every day's profit, only daily profit, not daily capital. (cumulative_profit = cumulative_capital - original_capital)

Suppose we have following arrays:

  1. test_norm_closes = closes/closes[0], it is 700 days (from day_1 to day_700) normalized close prices, day_1's close price is made 1
  2. test_predictions, 700 (from day_1 to day_700) predicted positions for next day; according to your design, predictions[0] is position to be hold for day_2, not day_1
  3. test_targets, 700 days' close_price_changes (from day_1 to day_700), test_targets[0] is day_1's price change compared to day_0's close price

Therefore,

  1. Given test_predictions[0] is 1, DeepTrade will take full position at the end of day_1, and day_2_position will be full, which is 1; and there is 0 profit for day_1;
  2. at the end of day_2, day_2_profit == day_1_norm_close_price * day_2_position * day_2_price_change == test_norm_closes[0] * test_predictions[0] * test_targets[1]

Accordingly, the pseudo code is below

daily_profit=[0] # day_1_profit == 0
for idx in range(1, len(target_pos_price_change)): # start from day_2
	daily_profit.append(test_norm_closes[idx-1]*test_predictions[idx-1]*test_targets[idx])
accum_profit1 = np.cumsum(daily_profit)

Does it make sense? Let me know if anything is wrong.

from deeptrade_keras.

happynoom avatar happynoom commented on July 3, 2024

Like today, 14:57pm, my DeepTrade output 1 for 2017/06/27, so I decide hold all my stock share to tomorrow(2017/06/27), sell nothing today. Therefore, if the price change rate of tomorrow is 1%, I will get the profit: today 's capital * 1 * 1%. "1" is the output of Deeptrade ,also position rate, 1% is the price change rate of tomorrow, also the sample 's label.

from deeptrade_keras.

happynoom avatar happynoom commented on July 3, 2024

friend me on QQ(1550070188),if problem remains.

from deeptrade_keras.

EmbraceLife avatar EmbraceLife commented on July 3, 2024

Thanks @happynoom

predictions is about how much percentage of capital (not position) to hold for next day is essential. I got it now.

I have just one question left. For example,
on day_1:

  • close_price: 1.0
  • prediction (how much percentage of capital to hold for day_2): 1.0
  • price_change (compared to day before day_1): 0.1
  • original_capital to invest: 1.0 (suppose we have to invest before the end of day_1)
    So, day_1_capital = 1, because we open position before close of market on day_1

on day_2:

  • close_price: 1.2
  • price_change: (1.2-1.0)/1.0 = 0.2
  • prediction: 0.5 (before the end of day_2, we have to sell 50% of day_1_capital)
  • day_2_capital = day_1_capital * (1 + day_1_prediction * day_2_price_change)

Accordingly, your code would become. please see my comments inside the code below for my understanding.

def calculate_cumulative_return(labels, pred):
    cr = []
    if len(labels) <= 0:
        return cr
    # day_1_capital becomes 1
    cr.append(1)

    # from day_2 onward
    for l in range(1, len(labels)):
        # pred[l-1] is how much percentage of capital to hold for next day, but pred[l] is how much percentage of capital to hold for next next day
        cr.append(cr[l-1] * (1 + labels[l] * pred[l-1]))
    for i in range(len(cr)):
        cr[i] = cr[i] - 1
    return cr

I can't get my head around why you use pred[l] rather than pred[l-1]. using pred[l] seems like peeking into the future already.

Thanks

from deeptrade_keras.

EmbraceLife avatar EmbraceLife commented on July 3, 2024

by the way, failed to add you as friend on QQ , as it requires to know your real name to pass by system. :)

from deeptrade_keras.

happynoom avatar happynoom commented on July 3, 2024

Sorry,I forgot to tell my name:房晓宇 . The output of the Deeptrade is the percentage of the capital we should hold to tomorrow, it is given before today's close. It means pred[i] is the output for day_i, but given on day_(i-1). Suppose pred[i]=0.5, we should sold 0.5captal_(i-1), and keep 0.5captal_(i-1). Therefore, on day_i, price change rate is 0.2. We got profit 0.5*captal_(i-1)*0.2. So you know, pred [i] take effect on day_i, though is pre-given on day_(i-1). Sorry for the mistake of "percentage of position ", I got wrong for this concept.

from deeptrade_keras.

happynoom avatar happynoom commented on July 3, 2024

I know it's confusing pred[i] is for day_i but is pre-given on day_(i-1). It's because we should take action before price changes. And what we have today will return tomorrow. Thank you

from deeptrade_keras.

EmbraceLife avatar EmbraceLife commented on July 3, 2024

Thanks @happynoom
According to your responses above, I think we are on the same page about the issues. And I think in order to make your fantastic return curve robust, we have to avoid peeking into the future or avoid cheating from future.

To really make sure we are on the same page, could you double check the following points with me?

  1. current_day_capital == previous_day_capital * ( 1 + prediction_made_on_previous_day * current_day_price_change) , (agree?)
  2. if point above agreed, then we should also agree on the following equation:
    current_day_capital == daily_capital[current_index-1] * (1 + predictions[current_index - 1] * daily_price_changes[current_index]), (agree?)

If we agree on first point, then we agree on the logic which can avoid the problem of peeking into the future, which is good; however,
if we disagree on second point, to be specific, if you insist on using current_day_capital == daily_capital[current_index-1] * (1 + predictions[current_index ] * daily_price_changes[current_index]), then we are having the problem of peeking into the future.

from deeptrade_keras.

iamyb avatar iamyb commented on July 3, 2024

hi, What's the conclusion of those discussion? @EmbraceLife 's comment looks quite reasonable.

Thanks @happynoom
According to your responses above, I think we are on the same page about the issues. And I think in order to make your fantastic return curve robust, we have to avoid peeking into the future or avoid cheating from future.

To really make sure we are on the same page, could you double check the following points with me?

  1. current_day_capital == previous_day_capital * ( 1 + prediction_made_on_previous_day * current_day_price_change) , (agree?)
  2. if point above agreed, then we should also agree on the following equation:
    current_day_capital == daily_capital[current_index-1] * (1 + predictions[current_index - 1] * daily_price_changes[current_index]), (agree?)

If we agree on first point, then we agree on the logic which can avoid the problem of peeking into the future, which is good; however,
if we disagree on second point, to be specific, if you insist on using current_day_capital == daily_capital[current_index-1] * (1 + predictions[current_index ] * daily_price_changes[current_index]), then we are having the problem of peeking into the future.

from deeptrade_keras.

Related Issues (20)

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.