/*
* 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:
*
* 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.Retest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.strategyquant.lib.*;
import com.strategyquant.datalib.*;
import com.strategyquant.tradinglib.*;
@ClassConfig(name="Randomize history data", display="Randomize history data, with probability #Probability# % and max price change #MaxChange# % of ATR")
@Help("<b>Note!</b>If you choose this option then robustness tests will be tested with Selected Timeframe precision, while original strategy will be tested with configured precision.<br/>This might cause difference in results between original strategy and simulations because of different precision.<br/>To avoid this, use Selected Timeframe precision in Setings : Data.")
public class RandomizeHistoryData extends MonteCarloRetest {
public static final Logger Log = LoggerFactory.getLogger(RandomizeHistoryData.class);
@Parameter(name="Probability", defaultValue="20", minValue=1, maxValue=100, step=1)
public int Probability;
@Parameter(name="Max change", defaultValue="10", minValue=1, maxValue=100, step=1)
public int MaxChange;
private int barsProcessed = 0;
private double atrValue = 0;
private int atrPeriod = 14;
//------------------------------------------------------------------------
//------------------------------------------------------------------------
//------------------------------------------------------------------------
public RandomizeHistoryData() {
super(MonteCarloTestTypes.ModifyData);
}
//------------------------------------------------------------------------
public void modifyData(IRandomGenerator rng, TickEvent tickEvent, double globalATR) {
double dblProbability = ((double) Probability/ 100.0d);
if(rng.probability(dblProbability)) {
// we should change this price
double ask = tickEvent.getAsk();
double bid = tickEvent.getBid();
double spread = ask - bid;
int change = rng.nextInt(MaxChange);
double dblChange = ((double) change)/ 100.0d;
double priceChange = 2 * globalATR * dblChange;
bid = (rng.nextInt(2) == 0 ? bid + priceChange : bid - priceChange);
tickEvent.setBid(bid);
tickEvent.setAsk(bid + spread);
}
}
}