import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.strategyquant.lib.*; import com.strategyquant.datalib.*; import com.strategyquant.tradinglib.*; @ClassConfig(name="Randomly skip trades", display="Randomly skip trades, with probability #Probability# %") @Help("Randomly skip trades") public class SkipRandomTrades extends MonteCarloManipulation { public static final Logger Log = LoggerFactory.getLogger(SkipRandomTrades.class); @Parameter(name="Probability", defaultValue="10", minValue=1, maxValue=100, step=1) public int Probability; //------------------------------------------------------------------------ //------------------------------------------------------------------------ //------------------------------------------------------------------------ @Override public void modifyTrades(IRandomGenerator rng, OrdersList originalOrders) throws Exception { double dblProbability = ((double) Probability/ 100.0d); int tradesToRemove = (originalOrders.size() * dblProbability); //for(int i=originalOrders.size()-1; i>=0; i--) { for(int i=tradesToRemove-1; i>=0; i--) { // remove one trade randomly int tradeToRemove = rng.nextInt(tradesToRemove-(tradesToRemove-i)); originalOrders.remove(tradeToRemove); } } }
Aside from any bugs, would something like this work? Does the remove function automatically close the gap left in the order list? I mean does order#51 become the new order #50 automatically when we remove order #50? Or if we remove order #50 and then remove order #50 again later is it going to be trying to remove the same order?
If it works you can see the loop has potential to be much smaller.
Status changed from New to Refused
It will result in probability * probability removals (much less).
Attachment Screenshot 108.png added
Attachment Screenshot 109.png added
/* * Copyright (c) 2017-2018, StrategyQuant - All rights reserved. * * Code in this file was made in a good faith that it is correct and does what it should. * If you found a bug in this code OR you have an improvement suggestion OR you want to include * your own code snippet into our standard library please contact us at: * https://roadmap.strategyquant.com * * This code can be used only within StrategyQuant products. * Every owner of valid (free, trial or commercial) license of any StrategyQuant product * is allowed to freely use, copy, modify or make derivative work of this code without limitations, * to be used in all StrategyQuant products and share his/her modifications or derivative work * with the StrategyQuant community. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */ package SQ.MonteCarlo.Manipulation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.strategyquant.lib.*; import com.strategyquant.datalib.*; import com.strategyquant.tradinglib.*; @ClassConfig(name="Skip x trades (FAST by bendx)", display="Skip #Probability# % trades randomly") @Help("Skip x% trades randomly") public class SkipRandomTrades extends MonteCarloManipulation { public static final Logger Log = LoggerFactory.getLogger(SkipRandomTrades.class); @Parameter(name="Probability", defaultValue="10", minValue=1, maxValue=100, step=1) public int Probability; //------------------------------------------------------------------------ //------------------------------------------------------------------------ //------------------------------------------------------------------------ @Override public void modifyTrades(IRandomGenerator rng, OrdersList originalOrders) throws Exception { double dblProbability = ((double) Probability/ 100.0d); //calc how many trades need to be removed. int OrderListSize = originalOrders.size(); int tradesToRemove = (int)Math.round((OrderListSize * dblProbability)); //debug("Skip Randomly"," tradestoremove " + tradesToRemove); //loop only once per removal needed (to save resources) for(int i=tradesToRemove; i>=1; i--) { //debug("Skip Randomly"," i loop " + i); // determine RANDOMLY which single trade to remove. int tradeToRemove = rng.nextInt(OrderListSize-(tradesToRemove-i)); //debug("Skip Randomly"," skip trade# " + tradeToRemove); //remove the randomly selected trade. originalOrders.remove(tradeToRemove); } } }
Status changed from Refused to Fixed