Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
I really need help in calculating percentage change of my open positions!

So basically, I'm new to Quantopian and Python. I used to code on TD Ameritrade's ThinkScript platform and I need your help in creating a code that helps me to calculate the percentage change of my OPEN positions!

On ThinkScript it was as simple as this :

def percentChg = 100 * (open - EntryPrice()) / EntryPrice();

'open' denotes the market price of the security, and 'EntryPrice()' indicates the entry price of my open position. e.g. I entered aapl at $85.05 (entry price), and its trading at $87 (open).

Would appreciate your help in helping me find out how to retrieve the current market price of a security & how to denote the Entry price on quantopian.

5 responses
def percentChg(context, data, your_symbol):  
    open = data.current(your_symbol, 'open')  
    entryprice = context.portfolio.positions[your_symbol].cost_basis  
    return 100 * (open - entryprice)/entryprice  

Note: your_symbol should be a symbol object in Quantopian, e.g. symbol('AAPL') / sid(24)

OMG THANK YOU SO MUCH YOU GENIUS !!!!

Hey Louis, and everyone else who can help out.

I've tried infusing your code into my code, but I'm still getting an error of 'ZeroDivisionError: float division by zero'.

Care to advise on where I have gone wrong in my code? Thanks!

It is because your don't hold any NUGT before the PctChange function was called, hence the cost_basis returned 0. To prevent the stateless state of your algo, here is an example on how to do it.

that's interesting!

What does the context.start mean?
and how do I know in which areas of the codes I may apply it to?