This line is suspicious:

Lots = max(1, total_usd_exposure/ConversionPrice/LotAmount/num_algos/((MaxLong+MaxShort)/2));

If any of the denominators is small or zero, you'll get an insane lot amount.