potential improvement in skip trades algo


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.

Attachments
  • Votes 0
  • Project StrategyQuant X
  • Type Bug
  • Status Fixed
  • Priority Normal

History

b
#1

bentra

05.12.2020 04:26

Task created

MF
#2

Mark Fric

07.12.2020 08:54

Status changed from New to Refused

yes, it works as supposed, I don't se any bug or potential for improvement
b
#3

bentra

08.12.2020 04:51
Ah I made a modification, I was asking if my mod would work. The potential improvement is speed. It could be almost 100x faster if 1% of trades are skipped. Almost 10x faster if 10% are skipped etc.
MF
#4

Mark Fric

08.12.2020 08:58
the modification is not correct, you use probability to compute number of replaced trades, and then again for each trade.

It will result in probability * probability removals (much less).

b
#5

bentra

08.12.2020 20:16

Attachment Screenshot 108.png added

Screenshot 108.png
(342.07 KiB)
Well I got it working and it works fine now.

It first calculates total trades to be removed then loops that many times and each iteration removes 1 trade (randomly selected) per iteration. I use % of trades to be removed instead of % probability of each trade being removed. The number of trades removed is now static in this new way but other than that it seems to work great and should use much less resources. Not a huge deal because it wasn't using many resources in the first place but I enjoyed the though experiment.

I marked it up nice so it's easier to understand and you can remove the commenting of the debugging to really see what's going on.




b
#6

bentra

08.12.2020 20:29

Attachment Screenshot 109.png added

Screenshot 109.png
(123.00 KiB)
I also answered my own question about what happens if the same trade#50 is removed 2x. Apparently the originalOrders.remove function handles this excellently in the core, trade #51 becomes the new trade#50 automatically so in case #50 gets hit twice it's not a problem because the gaps are closed automatically by this remove function.

To clarify "It will result in probability * probability removals (much less)." is not what I'm doing. I'm doing "removal% * 1 removals" the only thing random is the order# randomly selected for removal if you look carefully at the code. Granted I should've renamed the @Parameter because it doesn't represent probability anymore.

Even when I use a high 95 skip% everything looks fine.

It should be way faster as it loops only once per each removal that is needed as apposed to looping through the entire orders list.

Here's the working code:


/*  * 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); } } }




MF
#7

Mark Fric

09.12.2020 09:07

Status changed from Refused to Fixed

ok, I made an optimization inspired by your code.



Votes: 0

Drop files to upload

or

choose files

Max size: 5MB

Not allowed: exe, msi, application, reg, php, js, htaccess, htpasswd, gitignore

...
Wait please