vamana2734 wrote:
But Buy and Sell seem to be triggered randomly.
Consider adding more logging statements in the decision points, see:
https://www.dukascopy.com/wiki/#IConsole/Logging_valuesPrevious_Stoch = indicators.stoch(instrument, period, side, fastKPeriod, slowKPeriod, slowKMaType, slowDPeriod, slowDMaType, shift);
Are you aware that here you calculate the stoch on an arbitrary period bar (most likely 10 secs)? See onBar description here:
https://www.dukascopy.com/wiki/#Strategy_APIif ((instrument == Instrument.GBPUSD) && (period == Period.FIFTEEN_MINS)) {
Consider filtering bars the opposite way, see:
https://www.dukascopy.com/wiki/#Filter_Ticks_BarsThis wise you will reduce the code nesting.
Previous_Stoch = indicators.stoch(instrument, period, side, fastKPeriod, slowKPeriod, slowKMaType, slowDPeriod, slowDMaType, shift);
Current_Stoch = indicators.stoch(instrument, Period.FIFTEEN_MINS, side, fastKPeriod, slowKPeriod, slowKMaType, slowDPeriod, slowDMaType, shift);
If you wish to use the same period here, then consider using the candle interval methods for indicator calculation, see:
https://www.dukascopy.com/wiki/#Indicator_Calculation/Calculate_indicator_by_candle_intervalstoploss = Double.parseDouble(myDF.format(bidBar.getOpen() + 10 * myPipValue));
Calculate the SL/TP price distance as it is done here:
https://www.dukascopy.com/wiki/#SMA_Simpleif ((myOrder.getState().equals(IOrder.State.CANCELED)) || (myOrder.getState().equals(IOrder.State.CLOSED)) || (myOrder == null)) {
if myOrder will be null, the condition will fail with the NPE, hence the last OR condition is redundant in either way.
Consider reducing nesting to make the code more manageable, e.g. the block which starts with the upper mentioned line could be simplified to:
if (myOrder == null || myOrder.getState() == IOrder.State.CANCELED || myOrder.getState() == IOrder.State.CLOSED) {
canTrade = true;
return;
}
if ((myOrder.getOrderCommand() == IEngine.OrderCommand.SELL && Previous_Stoch[0] < Current_Stoch[0])
|| myOrder.getOrderCommand() == IEngine.OrderCommand.BUY && Previous_Stoch[0] > Current_Stoch[0]) {
myOrder.close();
console.getOut().println("Order Closed");
canTrade = true;
}