Advertisement
Guest User

CRYPTO FORECASTING TOOL 2.6

a guest
Oct 24th, 2017
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.71 KB | None | 0 0
  1. #☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀ CRYPTO FORECASTING TOOL ☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀
  2.  
  3. '''
  4. Contact: https://steemit.com/@profitgenerator
  5.  
  6. Copyright 2017 Profitgenerator
  7.  
  8. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  9.  
  10. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  11.  
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  13. '''
  14.  
  15. #☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀ ▶ dependencies ◀ ☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀
  16.  
  17. import os.path
  18. import math
  19.  
  20. #☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀ ▶ parameters ◀ ☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀
  21.  
  22. version  = "2.6"
  23. ARRAY    = []
  24. FORECAST = []
  25. file_name= "btc_usd.csv"
  26. period   = 840
  27. forward_steps =1
  28. best_period   = 0
  29. min_error     = 999999999999.0
  30.  
  31. #☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀ ▶ functions ◀ ☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀
  32.  
  33. def process_file():
  34.     with open("data/"+file_name,'r') as f:
  35.         lines=f.readlines()
  36.     ARRAY.append(['dummytime', 'dummyprice'])
  37.     FORECAST.append('dummyforecast')
  38.     for i in range(0,len(lines)):
  39.         ARRAY.append(lines[i].strip().split(','))
  40.         FORECAST.append("dummy")
  41.     return (len(ARRAY) -1)
  42.  
  43. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  44.  
  45. def linear_forecast_diff(limit,f_period):
  46.       f_diff=0.0
  47.       for i in range(limit-f_period+1,limit+1): # make it [,]
  48.            f_diff=f_diff +   (float(ARRAY[i][1])-float(ARRAY[i-1][1]))
  49.       f_diff/=f_period
  50.       return (f_diff)
  51.  
  52. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  53.  
  54. def verification(limit,f_period_corrector,f_error_ignore,f_period):
  55.    f_diff=linear_forecast_diff(limit,f_period)
  56.    last_real    =float(ARRAY[limit+1][1])
  57.    last_forecast=float(ARRAY[limit  ][1])+(f_diff*forward_steps)
  58.    # manual correction for negative log values or zero division
  59.    if(last_forecast<=0):
  60.         f_period_corrector+=1
  61.         f_error_ignore=True
  62.         FORECAST[limit+1]="error"
  63.        # print("here "+str(f_period)+" "+str(f_diff)+" "+str(limit))
  64.         return (0,f_period_corrector,f_error_ignore)
  65.    else:
  66.         FORECAST[limit+1]=last_forecast
  67.         error=abs(math.log(float(last_real/last_forecast)))  
  68.         return (error,f_period_corrector,f_error_ignore)
  69.  
  70. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  71.  
  72. # formula: https://docs.oracle.com/cd/E17236_01/epm.1112/cb_statistical/frameset.htm?ch07s02s03s04.html
  73. def theil_u():
  74.    upper=0.0
  75.    lower=0.0
  76.    for x in range(1+period,arraysize-forward_steps): # N-1 for Theil's U
  77.        if(ARRAY[x+1][1]!="error" and ARRAY[x+2][1]!="error" and FORECAST[x+1]!="error" and FORECAST[x+2]!="error"):
  78.           cur_real       =float(ARRAY   [x+1][1])
  79.           cur_forecast   =float(FORECAST[x+1])
  80.           future_real    =float(ARRAY   [x+2][1])
  81.           future_forecast=float(FORECAST[x+2])
  82.           upper+= ((future_forecast-future_real)/cur_real)**2
  83.           lower+= ((future_real-cur_real)       /cur_real)**2
  84.    if(upper!=0.0 and lower!=0.0):          
  85.        theil=math.sqrt(upper/lower)
  86.        return (theil)
  87.  
  88.    else:
  89.        return (1)      
  90.  
  91. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  92.  
  93. def user_interaction():
  94.    print ("\n")
  95.    print ("Choose Option!")
  96.    print ("Backtest: 0")
  97.    print ("Forecast: 1")
  98.    print ("Brute Force: 2")
  99.    return(input())
  100.  
  101. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  102.  
  103. def main_function(f_period,f_best_period,f_min_error):
  104.    if(userinput=="0" or userinput=="2" ):
  105.         error=0.0
  106.         period_corrector=0
  107.         error_ignore=False
  108.         theilu=0.0
  109.         for k in range(1+f_period,arraysize-forward_steps+1): # +1 to fill the [] zone , -1 to go until the prev value
  110.             output=verification(k,period_corrector,error_ignore,f_period)
  111.             if(output[2]==False):
  112.               error+=output[0]
  113.             error_ignore=False
  114.         error/= (arraysize-f_period-forward_steps-output[1])
  115.  
  116.         if(userinput=="2" ):
  117.           if (error<f_min_error):
  118.             f_min_error=error
  119.             f_best_period=f_period
  120.  
  121.         if(userinput=="0"):
  122.             theilu=theil_u()
  123.             print ("    "+'\033[103m'+'\033[1m'  +"Profitgenerator's CRYPTO FORECASTING TOOL v"+version+ '\033[0m')
  124.             print ("\n")
  125.             print ("    Mode:              "+"Backtest")
  126.             print ("    File Name:         "+file_name)
  127.             print ("    From:              "+str(ARRAY[1][0])+" to "+str(ARRAY[arraysize][0]))
  128.             print ("    Array Size:        "+str(arraysize))
  129.             print ("    Period:            "+str(f_period))
  130.             print ("    Forward Steps:     "+str(forward_steps))
  131.             print ("    Iterations:        "+str((arraysize-f_period-forward_steps)))
  132.             print ("    "+'\033[106m' +'\033[1m'  +"LN Error:          "+str(error)+ '\033[0m'    )
  133.             print ("    Theil's U:         "+str(theilu))
  134.             if(theilu<1):
  135.                 print ("    "+'\033[102m'+'\033[1m'  +"Predictive Edge:   "+str((1-theilu)) + '\033[0m')
  136.             else:
  137.                 print ("    "+'\033[101m'+'\033[1m'  +"Predictive Edge:   "+str((1-theilu)) + '\033[0m')
  138.  
  139.    if(userinput=="1"):
  140.         difference=linear_forecast_diff(arraysize,f_period)
  141.         lastprice=float(ARRAY[arraysize][1])
  142.         forecast=lastprice+(difference*forward_steps)
  143.         print ("    "+'\033[103m'+'\033[1m'  +"Profitgenerator's CRYPTO FORECASTING TOOL v"+version+ '\033[0m')
  144.         print ("\n")
  145.         print("    Mode:                  "+"Forecast")
  146.         print("    File Name:             "+file_name)
  147.         print("    Array Size:            "+str(arraysize))
  148.         print("    Period:                "+str(f_period))
  149.         print("    Forward Steps:         "+str(forward_steps))
  150.         print("    Last Price:            "+str(lastprice))
  151.         print("    "+'\033[106m' +'\033[1m'  +"Forecasted Next Price: "+str(forecast)+ '\033[0m')
  152.   # print(f_best_period,f_min_error)
  153.    return (f_best_period,f_min_error)
  154. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  155.  
  156.  
  157. #☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀ ▶ main ◀ ☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀☀
  158.  
  159.  
  160. arraysize=process_file()
  161.  
  162.  
  163. if((period+forward_steps)>arraysize-2):
  164.    period=1
  165.    forward_steps=1
  166. userinput     = user_interaction()
  167.  
  168.  
  169. print ("\n\n")
  170. if(userinput=="0" or userinput=="1" ):
  171.    main_function(period,best_period,min_error)
  172. if(userinput=="2" ):
  173.    for period in range (1,arraysize-forward_steps):
  174.      main_output=main_function(period,best_period,min_error) # +1 to fill the [] zone
  175.      best_period=main_output[0]
  176.      min_error  =main_output[1]
  177.    print ("    Mode:                  "+"Brute Force")
  178.    print ("    Forward Steps:         "+str(forward_steps))
  179.    print ("    Best Period:           "+str(best_period))
  180.    print ("    Error:                 "+str(min_error))  
  181. print ("\n\n")
  182.  
  183.  
  184.  
  185. '''
  186. for x in range (0,200):
  187.     print('\033['+str(x)+'m' +"    testcolors12345678: "+ '\033[0m'+str(x))
  188. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement