I do not know why it crashes, but I would try a different programming approach: Create an array of int of size (num bar in the period )fill it with "0" and StrategyTrades random "1". For each bar check if the array contains 1, open the trade. I guess it will work