当前位置: 首页>>代码示例>>Python>>正文


Python Position.getPNL方法代码示例

本文整理汇总了Python中position.Position.getPNL方法的典型用法代码示例。如果您正苦于以下问题:Python Position.getPNL方法的具体用法?Python Position.getPNL怎么用?Python Position.getPNL使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在position.Position的用法示例。


在下文中一共展示了Position.getPNL方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: run_data

# 需要导入模块: from position import Position [as 别名]
# 或者: from position.Position import getPNL [as 别名]
    def run_data(self):
        symAPosition = Position(self.symbolA)
        symBPosition = Position(self.symbolB)
        
        last_spread = 0
        date = None
        armedPlus   = False
        armedMinus  = False
        stop_trading= False
        totalDays   = 0
        lastTentPNL = 0
        ## Invest $50,000 In Pair A, and the apropriate numer of shares in B
        sharesA = int(50000/self.symAData.symData[0][1])
        sharesB = int(sharesA*self.hedge_beta)

        i=0

        for spread in self.spread:
            priceA  = self.symAData.symData[i][1]
            priceB  = self.symBData.symData[i][1]
            date    = self.symAData.symData[i][0]

            ## Test getin conditions
            if symAPosition.getShares() == 0 and spread[1] > self.trade_sd*2:
                armedPlus = True
            if symBPosition.getShares() == 0 and spread[1] < -self.trade_sd*2:
                armedMinus = True

            ##TODO: Add condition not to getin within 30 days of end of trading period
            if symAPosition.getShares() == 0 and armedPlus == True and spread[1] < self.trade_sd*2 and not stop_trading:
                ## Open Position
                print date,"|1","Short A (",symAPosition.getSymbol(),") / Buy B (",symBPosition.getSymbol(),")" 
                #print priceA,priceB
                ordA = symAPosition.createOrder("S","MARKET",priceA,sharesA)
                symAPosition.executeOrder(ordA, priceA)
                ordB = symBPosition.createOrder("B","MARKET",priceB,sharesB)
                symBPosition.executeOrder(ordB, priceB)
                armedPlus = False

            if symBPosition.getShares() == 0 and armedMinus == True and spread[1] < -self.trade_sd*2 and not stop_trading:
                ## Open Position
                print date,"|2","Buy A (",symAPosition.getSymbol(),") / Short B (",symBPosition.getSymbol(),")" 
                #print priceA,priceB
                ordA = symAPosition.createOrder("B","MARKET",priceA,sharesA)
                symAPosition.executeOrder(ordA, priceA)
                ordB = symBPosition.createOrder("S","MARKET",priceB,sharesB)
                symBPosition.executeOrder(ordB, priceB)
                armedMinus = False

            ## TODO: Add condition to getout if exceeds 3.5SD's, and stop trading that pair
            ## Test getout conditions
            if symAPosition.getShares() < 0 and (spread[1] < 0 or spread[1] > self.trade_sd*4):
                if (spread[1] > self.trade_sd*4):
                    stop_trading = True
                print date,"|3","Buy A (",symAPosition.getSymbol(),") / Sell B (",symBPosition.getSymbol(),")" 
                #print priceA,priceB
                ordA = symAPosition.createOrder("B","MARKET",priceA,sharesA)
                symAPosition.executeOrder(ordA, priceA)
                ordB = symBPosition.createOrder("S","MARKET",priceB,sharesB)
                symBPosition.executeOrder(ordB, priceB)

            if symAPosition.getShares() > 0 and (spread[1] > 0 or spread[1] < -self.trade_sd*4):
                if (spread[1] < -self.trade_sd*4):
                    stop_trading = True
                print date,"|4","Sell A (",symAPosition.getSymbol(),") / Buy B (",symBPosition.getSymbol(),")" 
                #print priceA,priceB
                ordA = symAPosition.createOrder("S","MARKET",priceA,sharesA)
                symAPosition.executeOrder(ordA, priceA)
                ordB = symBPosition.createOrder("B","MARKET",priceB,sharesB)
                symBPosition.executeOrder(ordB, priceB)


            ## If we have a position open, add it to total days.
            if symAPosition.getShares() != 0:
                totalDays += 1

            ## Add returns to dict for that day
            tentPNL = symAPosition.getPNL() + symBPosition.getPNL() + symAPosition.getTentPNL(priceA) + symBPosition.getTentPNL(priceB)
            self.returns.append(tentPNL - lastTentPNL)
            self.dates.append(date)
            lastTentPNL = tentPNL
            #print date,priceA,priceB,self.returns[-1]

            ## Update last_spread
            last_spread = spread[1]
            #print date,spread[0],spread[1]
            i+=1

        ## Unload positions at the end of trading
        if symAPosition.getShares() > 0:
            print date,"|","Sell A (",symAPosition.getSymbol(),") / Buy B (",symBPosition.getSymbol(),")" 
            #print priceA,priceB
            ordA = symAPosition.createOrder("S","MARKET",priceA,sharesA)
            symAPosition.executeOrder(ordA, priceA)
            ordB = symBPosition.createOrder("B","MARKET",priceB,sharesB)
            symBPosition.executeOrder(ordB, priceB)
        
        if symAPosition.getShares() < 0:
            print date,"|","Buy A (",symAPosition.getSymbol(),") / Sell B (",symBPosition.getSymbol(),")" 
            #print priceA,priceB
#.........这里部分代码省略.........
开发者ID:giladbi,项目名称:Pairs-Trading,代码行数:103,代码来源:trade_pairs.py


注:本文中的position.Position.getPNL方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。