Yes, you can vary your leverage, but typically start with a fixed leverage (of 1). Change that later if you want to fine tune your algorithm. You will realistically only be adjusting it between 1-3 (3 is the max that Quantopian allows for their contest and brokers will only allow most of us mortals leverage less than 2).
One approach is to not think in terms of quantity of shares or dollars when ordering securities. Rather, your algorithm determines the percent of the total portfolio value, or weight, you want your security to represent. Orders can then be easily placed with desired leverage and stock weight using the 'order_target_percent' method.
order_target_percent(my_stock, leverage * my_stock_weight )
'leverage' and 'my_stock_weight' can be constants or can be dynamically adjusted by your algorithm. As long as you ensure the sum of all the 'my_stock_weight's equals 1 you will keep your leverage to the desired level.
One caution. The 'order_target_percent' method does NOT account for any outstanding orders. It's generally good practice to not place an order for a security if there are already orders open. If you trade once a day, this isn't an issue since all orders on Quantopian are canceled at the end of the day.