2017-03-08 7 views
0

私はコードに働く元の値を与えるなら、私は各x-tickにその値からのパーセント変化を表示したい。matplotlibの各xtickの隣に計算値を表示する方法は?

I.E 10を元の値に設定した場合、5のグリッド線を5(-50%)のように読み取り、20を20(+100%)としたいとします。

括弧内の用語の色は追加ボーナスになります。

編集:ソースコード

ここにコードがあります。特定の役割を果たすために何度も変更されているので、それは最良ではありませんが、あなたはそのアイデアを得ることができます。手数料や配当金を考慮した取引のための単なる計算機です。

私はあなたは

from colorama import Fore, Style 
import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.widgets import Cursor 


name='XYZ' 
current_stock_price=8.66 

closing_price=current_stock_price  #price at which you close the position 

#first trade 
entrance_price1=9.55 
number_shares1=1500 
dividend1=0.0 
longshort1='short' #is it a long or short position? 

#second trade 
entrance_price2=8.93 
number_shares2=1200 
dividend2=0.0 
longshort2='short' 

def commission_stock(commission_rate, cost, commission_minimum,  number_shares): 

    com=commission_rate/100*cost 
    #com=commission_rate*number_shares 

    if com<commission_minimum: 
     com=commission_minimum 

    return com 

def pl_calculator(entrance_price, closing_price, number_shares, \ 
       commission_shares, commission_minimum, dividend_amount, name,\ 
       longshort): 

    name=name 
    longshort=longshort 

    entrance_price= entrance_price 
    closing_price= closing_price 

    stock_change=closing_price-entrance_price #change in stock price 

    number_shares=number_shares 

    commission_shares=commission_shares#percent or per/number of shares 
    commission_minimum=commission_minimum 

    dividend_amount=dividend_amount #dollars per share 
    dividend_cost=dividend_amount*number_shares #how much you pay in dividends 

    margin_req=1 

    nominal_value=number_shares*entrance_price #value of shares (not necessarily how much we own if margin is involved) 
    cost_to_exit_share=number_shares*closing_price 

    outlay=nominal_value*margin_req #amount of money required up front 

    movement=float(closing_price)/entrance_price*100-100 #stock price movement 

    stock_pl=number_shares*(closing_price-entrance_price) #numerical value of profit or loss 

    commission_shares_enter=commission_stock(commission_shares, nominal_value, commission_minimum, number_shares) 
    commission_shares_exit=commission_stock(commission_shares, cost_to_exit_share, commission_minimum, number_shares) 
    commission=commission_shares_enter+commission_shares_exit 

    if longshort=='long': 
     stock_pl=stock_pl-commission_shares_enter-  commission_shares_exit+dividend_cost 
    elif longshort=='short': 
     stock_pl=-stock_pl-commission_shares_enter-commission_shares_exit-dividend_cost 
    else: 
     raise AttributeError 

    pl_perc=stock_pl/nominal_value*100 
    pl_perc_outlay=stock_pl/outlay*100 #profit or loss on outlaid cash. If margin=1 then pl_perc_outlay=pl_perc 

    return [stock_pl, pl_perc, commission, dividend_cost] 


def print_graph(current_stock_price, profit_current, profit_chosen, closing_price, name): 

#creates a list of stock prices either side of current stock price and substitutes each of these into the p/l calculator to give the rough idea of a p/l graph. 

    fig=plt.figure() 
    ax=fig.add_subplot(111) 

    a=float(current_stock_price)*0.6 
    b=float(current_stock_price)*1.4 


    #create list of the stockprices to be tested 
    data=np.arange(a, b, 0.25) 

    #create empty list to populate with theh profit value at each point 
    profit_points=np.zeros(len(data), dtype='f') 

    i=0 

    while i <= (len(data)-1): 

     profit1=pl_calculator(entrance_price=entrance_price1, closing_price=data[i], number_shares=number_shares1,\ 
       commission_shares=0.1, commission_minimum=8, dividend_amount=dividend1,\ 
       name=name, longshort=longshort1) 

     profit2=pl_calculator(entrance_price=entrance_price2, closing_price=data[i], number_shares=number_shares2,\ 
       commission_shares=0.1, commission_minimum=8, dividend_amount=dividend2,\ 
       name=name, longshort=longshort2) 


     #append the profit at this particular price to a list 
     profit_points[i]=profit1[0]+profit2[0] 



     i+=1 

    ax.plot(data, profit_points) 
    ax.plot(current_stock_price, profit_current, 'ro', label='Current Position')     
    ax.plot(closing_price, profit_chosen, 'g^', label='Chosen Closing Position') 

    cursor = Cursor(ax, useblit=True, color='k', linewidth=1) 

    ax.grid(axis='both') 


    plt.xlabel('%s($)' %name) 
    plt.ylabel('P/L ($)') 
    plt.title('P/L') 
    plt.legend(loc='upper right', numpoints=1) 

    plt.show() 







#run cases for current stock price and chosen stock price 
trade1_current=pl_calculator(entrance_price=entrance_price1,  closing_price=current_stock_price, number_shares=number_shares1,\ 
       commission_shares=0.1, commission_minimum=8, dividend_amount=dividend1,\ 
       name=name, longshort=longshort1) 

trade2_current=pl_calculator(entrance_price=entrance_price2, closing_price=current_stock_price, number_shares=number_shares2,\ 
       commission_shares=0.1, commission_minimum=8, dividend_amount=dividend2,\ 
       name=name, longshort=longshort2) 

trade1_chosen=pl_calculator(entrance_price=entrance_price1, closing_price=closing_price, number_shares=number_shares1,\ 
       commission_shares=0.1, commission_minimum=8, dividend_amount=dividend1,\ 
       name=name, longshort=longshort1) 

trade2_chosen=pl_calculator(entrance_price=entrance_price2, closing_price=closing_price, number_shares=number_shares2,\ 
       commission_shares=0.1, commission_minimum=8, dividend_amount=dividend2,\ 
       name=name, longshort=longshort2) 





profit_current=trade1_current[0]+trade2_current[0] 
profit_chosen=trade1_chosen[0]+trade2_chosen[0] 

dividend=trade1_current[3]+trade2_current[3] 

movement=closing_price/current_stock_price*100-100 

print '%s: $%.2f' %(name, current_stock_price) 
print '@ $%.2f (%.2f%%)' %(closing_price, movement) 
print 'Dividend Payment: $%.2f' %dividend 
print 'Profit: $%.2f'%(trade1_current[0]+trade2_current[0]) 


print_graph(current_stock_price, profit_current, profit_chosen, closing_price, name) 
+0

サンプルコードを投稿してください。 –

+0

物自体を見ずに何かを修正する方法について助言するのは難しいです。 –

+0

私は答えを待っていますが、それが立っているので、私は質問を閉じるために投票しなければなりません。 –

答えて

0

「現在の株価」から変化率を示すように、グラフ上でその両側「現在の株価」や株価であることをグラフの中心が欲しいです次のようにパーセンテージ番号を追加するmatplotlib.ticker.ScalarFormatterをサブクラス化することができます

import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.ticker 

class MyPercentageFormatter(matplotlib.ticker.ScalarFormatter): 
    def __init__(self,origval, offset=False, mathText=True): 
     self.origval = float(origval) 
     matplotlib.ticker.ScalarFormatter.__init__(self,useOffset=offset,useMathText=mathText) 
    def pprint_val(self, x): 
     val = matplotlib.ticker.ScalarFormatter.pprint_val(self, x) 
     perc = x/self.origval*100 
     newval = val + "({perc:.0f}%)".format(perc=perc) 
     return newval 


x = np.arange(50) 
y = np.sin(x/10.) 

fig, ax = plt.subplots() 
ax.xaxis.set_major_formatter(MyPercentageFormatter(20)) 
ax.plot(x,y) 

plt.show() 

enter image description here

0

あなたはカスタムを使用する必要がこれを行うにはクラス。外部の「元の」値に頼っているので、私はFuncFormatterのようなものをお勧めします。あなたは

from matplotlib.ticker import FuncFormatter 
ax.xaxis.set_major_formatter(FuncFormatter(lambda x, pos: '{}({:d}%)'.format(x, int(x/original * 100.0 + 0.5)))) 

のような色は、部分的にLaTeXを用いて実現することができる何かを行うことができますが、あなたは、レンダリングの並べ替えをサポートする裏打ちを使用する場合のみ。また、軸に独自のラベルを作成し、それらを動かしてダニをシミュレートすることもできますが、これは限られた利益のためにはあまりにも多くの作業であると思われます。該当する投稿は Partial coloring of text in matplotlibです。

マイナーニックピックの場合、2行で行う代わりにfig, ax = plt.subplots()を使用することを検討してください。

関連する問題