Back to Community
Transactions all over the place in backtesting

for some reason the transactions are being printed at random times all over the day. Possible fix/cause?
I've attached a buy/sell loop to illustrate the issue.

Clone Algorithm
2
Loading...
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
import quantopian.algorithm as algo
from quantopian.pipeline import Pipeline
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.filters import QTradableStocksUS


def initialize(context): 
    set_slippage(slippage.FixedSlippage(spread=0))
#     toggle when ready to order again
    context.buylock = False
    context.selllock= False
    context.stock = sid(24)
    context.filled_list={}
    context.order_id= dict.fromkeys(['sell_1'])
    # context.order_id = {"sell_1":None}

def handle_data(context, data):
    c=context 
    holdings=c.portfolio.positions[sid(24)].amount

#   buys 100 at market
    if holdings==0 and c.buylock==False: 
       buy_order_id=(order(context.stock, 100))
#   adds the current buy to the order_id dictionary
       context.order_id["buy1"]=buy_order_id
#   clears the previous sell orders from the dictionary     
       context.order_id["sell_1"]= None
#   turns the buy lock on
       c.buylock=True
  
#     checks if buy order was filled and then records the fill price in the dictionary
    if holdings==100 and context.order_id["sell_1"]==None and c.selllock==False: 
#     turns the buy fill number into an object an places the object into a dictionary
       filled_price=(c.portfolio.positions[sid(24)].cost_basis)
       c.filled_list["fill_1"]=filled_price
# when using limit orders:    sell_price1 = filled_price 
       log.info(holdings)
        
#    sells at the sell_price1 
       selling=order(c.stock,-100)
# for limit orders use: selling=order(c.stock,-100,style=LimitOrder(sell_price1))
#    adds the sale to the dictionary
       context.order_id["sell_1"]=selling
#    turns the sell lock on
       c.selllock==True

#   checks if the sell order was ordered and placed in the dictionary
    if "sell_1" in c.order_id and context.order_id["sell_1"]!=None:
#   assigns the sale an order_id value in the dictionary
        sale1=get_order(c.order_id['sell_1'])
#   checks if the sale has gone through
        if sale1.status==1:
#   allows new buying
            c.buylock=False
            log.info(holdings)
    
   
    
    
    
    
    
       # profit_filled = bool(get_order(context.order_id2)['filled'])
There was a runtime error.
2 responses

To clarify: when loading transactions in the backtester, the transactions are being recorded out of order like at 3:15 pm then 9:26 am.

Did you ever receive an update about this?