Briefly the system is based on 3 optimization params: stop-loss (SL),
take-profit(TP) and delta (D) (which is the absolute value of the
difference between the current price and the maximum price in Long
Position or minimum price in Short. So you're always in market once
you enter it and then you only monitor stop-loss or take-profit
signals.
1) If LONG (Position=1) AND CurrentPrice < BuyPrice - SL => Position=
-1
2) If LONG (Position=1) AND MaxPrice > BuyPrice+TP AND MaxPrice-
CurrentPrice>Delta => Position= -1
3) If SHORT (Position= -1) AND CurrentPrice > SellPrice + SL =>
Position= 1
4) If SHORT (Position= -1) AND MinPrice < SellPrice-TP AND
CurrentPrice-MinPrice>Delta => Position= 1
I didn't specified any condition for initial entry when Position=0.
For example simple 2MA rule can be used: Long when fastMA>slowMA and
vice versa or simply enter long or short without any condition.
*******************STRATEGY**********************************************
package com.jbooktrader.strategy;
import com.jbooktrader.indicator.maxmin.*;
import com.jbooktrader.indicator.currentprice.*;
import com.jbooktrader.platform.indicator.*;
import com.jbooktrader.platform.marketbook.MarketBook;
import com.jbooktrader.platform.model.*;
import com.jbooktrader.platform.optimizer.*;
import com.jbooktrader.platform.position.*;
import com.jbooktrader.platform.strategy.*;
/**
*
*/
public class SLTP extends StrategyES {
// Strategy parameters names
private static final String STOP_LOSS = "StopLoss";
private static final String TAKE_PROFIT = "TakeProfit";
private static final String DELTA = "Delta";
// Strategy parameters values
private final Indicator MaxMin, Change, CurrentPrice;
private final int SL;
private final int TP;
private final int delta;
public SLTP(StrategyParams optimizationParams) throws
JBookTraderException {
super(optimizationParams);
SL=getParam(STOP_LOSS);
TP=getParam(TAKE_PROFIT);
delta=getParam(DELTA);
}
public int getPosition(){
return position;
}
/**
* Adds parameters to strategy. Each parameter must have 5 values:
* name: identifier
* min, max, step: range for optimizer
* value: used in backtesting and trading
*/
@Override
public void setParams() {
addParam(STOP_LOSS, 30, 500, 5, 120);
addParam(TAKE_PROFIT, 30, 500, 5, 40);
addParam(DELTA, 10, 200, 5, 30);
}
/**
* This is where the strategy itself should be defined.
*/
@Override
public void onBookChange() {
double maxmin = MaxMin.getValue();
double CPrice = CurrentPrice.getValue();
double AvgFillPrice=getAvgFillPrice();;
if (position=0) {
setPosition(1);
}
} else if(position > 0 && CPrice < AvgFillPrice-SL){
setPosition(-1);
} else if (position> 0 && maxmin > AvgFillPrice+TP && maxmin-
CPrice > delta) {
setPosition(-1);
} else if(position<0 && CPrice > AvgFillPrice+SL){
setPosition(1);
} else if (position< 0 && maxmin < AvgFillPrice-TP && maxmin-
CPrice > delta) {
setPosition(1);
}
}
}
***********************MAXMIN-IND*******************************
package com.jbooktrader.indicator.maxmin;
import com.jbooktrader.platform.indicator.*;
/**
* Maximum price in Long Position, Minimum Price in Short Position
*/
public class MaxMin extends Indicator {
private double previousPrice;
private final int position;
public int getPosition() {
return position;
}
@Override
public void calculate() {
double Price = marketBook.getSnapshot().getPrice();
if (previousPrice != 0 && position> 0 && Price >
previousPrice) {
value = Price;
} else if (previousPrice != 0 && position> 0 && Price <
previousPrice) {
value = previousPrice;
}else if (previousPrice != 0 && position< 0 && Price >
previousPrice) {
value = previousPrice;
}else if (previousPrice != 0 && position> 0 && Price <
previousPrice) {
value = Price;
}
previousPrice = Price;
}
@Override
public void reset() {
calculate();
}
}
**************CURRENT-PRICE******************************
package com.jbooktrader.indicator.currentprice;
import com.jbooktrader.platform.indicator.*;
public class CurrentPrice extends Indicator {
@Override
public void calculate() {
value = marketBook.getSnapshot().getPrice();
}
@Override
public void reset() {
calculate();
}
}
********************************
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"JBookTrader" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/jbooktrader?hl=en
-~----------~----~----~----~------~----~------~--~---