I'm implementing a backtesting strategy with the following approach:
We buy a stock at today's opening price.
We set stop-loss and target boundaries.
If the stop-loss boundary is hit first, we sell the stock at the stop-loss price.
If the target boundary is hit first, we hold the stock until the end of the day and sell at the closing price (i.e., remove the stop order after the target is hit).
If neither boundary is triggered, we hold the stock until the end of the day and sell at the closing price.
Since I don't have intraday data, I assume a uniform distribution for intraday price movements. Although this is my current implementation, I am uncertain about the realism of these assumptions for backtesting purposes.
target_profit = 1.05
stop_loss = 0.95
target_price = target_profit * today_open
stop_price = stop_loss * today_open
if today_high >= target_price and today_low <= stop_price:
if random.random() > (target_profit-1)/(target_profit-stop_loss): # a guess based on uniform distribution to determine if the stop or the target are hit the first
sale_proceeds = shares * today_close
else:
sale_proceeds = shares * stop_price
elif today_high >= target_price:
sale_proceeds = shares * today_close
elif today_low <= stop_price:
sale_proceeds = shares * stop_price
else:
sale_proceeds = shares * today_close