How to make average of buying price

i want to make average of buy price if pnl goes below certain price.
for e.g at particular buy signal if one lot is taken and pnl goes below 500 then add 2 more lots, again if the the total profit goes below 1500 then add 4 more lots.