Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
NEW GUY: How to log/record last buying price, so I can intergrate so algo. will only sell if current price > buying price

Question should be explained in title.
I assigned it as ?buying_price?, though not sure if correct name, as you can see I dont know how to do it.
Algo. is pretty basic as I am new to this - so naturally it is based of the introduction algo.

def initialize(context):  
    context.security = symbol('AAPL')

def handle_data(context, data):  
    mavg5 = data[context.security].mavg(5)  
    mavg30 = data[context.security].mavg(30)  
    current_price = data[context.security].price  
    ?buying_price? = data[...?    

    cash = context.portfolio.cash

    if 1.0275*mavg5 > mavg30 and cash > current_price:  
        # Need to calculate how many shares we can buy  
        number_of_shares = int(cash/current_price)  
        # Place the buy order (positive means buy, negative means sell)  
        order(context.security, +number_of_shares)  
        log.info("Buying %s" % (context.security.symbol))

    elif current_price > 1.055*mavg5 and mavg5 > mavg30 and current_price > ?buying_price?:

        # Sell all of our shares by setting the target position to zero  
        order_target(context.security, 0)  
        log.info("Selling %s" % (context.security.symbol))  
    # You can use the record() method to track any custom signal.  
    # The record graph tracks up to five different variables.  
    # Here we record the Apple stock price.  
    record(stock_price=data[context.security].price)  
5 responses

Hello Christian,

As a default, the backtester uses an inaccessible future price as the price for buying/selling. This is to simulate real-world trading, since otherwise the backtest would suffer from 'look-ahead' bias (incorporating unavailable future information into decision making at a given simulation time).

So, you need a model for the 'buying_price' that will use available data to project forward to the next bar. It might be a useful exercise to figure out how to do a straight-line fit to the trailing price data versus wall-clock time (or 'trading time'--skipping nights and weekends).

Grant

Hi again,
Not sure what to make of your post. I'm pretty new in this field, so I don't understand most of the coding terms you are using.

I have been looking around under the portfolio and position object in the API. The only thing I can find is this, and I am not even sure it is the right thing:

last_sale_price
Float: Price at last sale of this security. This is identical to close_price and price.

This gives me the price of the stock right now, but how do I get the price at which I bought the stock at? That way I can make it so my algorithm will only sell if there is an actual profit involved, at the moment the sales are based off of the current_price being higher than the mavg5(times something) - and therefore it sells at a loss overtime if an opportunity does not strike.

Basically I want it to hold the stocks if it cant sell them above the price it has bought them at.

Note: The code above is not valid anymore as it has been changed quite a bit

Hi Christian,

In fact, you don't need to know last buy price. What you need to track is the the average cost price of your stock position and this can be done by calling Position object and its cost_basis property:

Position object
The position object represents a current open position, and is contained inside the positions dictionary. For example, if you had an open AAPL position, you'd access it using context.portfolio.positions[symbol('AAPL')]. The position object has the following properties:

cost_basis
Float: The volume-weighted average price paid (price and commission) per share in this position.

Sorry Christian,

I misunderstood. I thought you were needing the price at which you would buy/sell once an order is submitted. Allen's 'cost_basis' suggestion should work, right?

Grant

Yes I am sure it will work, thanks to both of you :)