package jforex.orders.sl;
import java.awt.Color;
import java.text.DecimalFormat;
import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
import com.dukascopy.api.drawings.IHorizontalLineChartObject;
public class SlTpUpdateWithOrigLines implements IStrategy {
@Configurable("ストップロスとリミット価格との乖離幅")
public double priceDistance = 0.0003;
@Configurable("通貨ペア")
public Instrument instrument = Instrument.EURUSD;
private IEngine engine;
private IOrder order;
private IHistory history;
private IChart chart;
private IContext context;
private IConsole console;
private IHorizontalLineChartObject slLine, tpLine;
private boolean origTpBroken, origSlBroken;
long lastTickTime = 0;
DecimalFormat df = new DecimalFormat("0.00000");
@Override
public void onStart(IContext context) throws JFException {
this.context= context;
engine= context.getEngine();
history = context.getHistory();
console = context.getConsole();
context.setSubscribedInstruments(java.util.Collections.singleton(instrument), true);
chart = context.getChart(instrument);
double lastPrice = history.getLastTick(instrument).getBid();
lastTickTime = history.getLastTick(instrument).getTime();
double slPrice = lastPrice - priceDistance;
double tpPrice = lastPrice + priceDistance;
order = engine.submitOrder("order1", instrument, OrderCommand.BUY, 0.001, 0, 5, slPrice, tpPrice);
slLine = chart.getChartObjectFactory().createHorizontalLine();
tpLine = chart.getChartObjectFactory().createHorizontalLine();
slLine.setPrice(0, slPrice);
tpLine.setPrice(0, tpPrice);
slLine.setColor(Color.RED);
tpLine.setColor(Color.GREEN);
slLine.setText("Original SL");
tpLine.setText("Original TP");
chart.addToMainChartUnlocked(slLine);
chart.addToMainChartUnlocked(tpLine);
}
@Override
public void onTick(Instrument instrument, ITick tick) throws JFException {
if(instrument != this.instrument || tick.getTime() - lastTickTime < 1000){
return;
}
if(!engine.getOrders().contains(order)){
context.stop();
return;
}
order.setStopLossPrice(tick.getBid() - priceDistance);
order.setTakeProfitPrice(tick.getBid() + priceDistance);
if(!origSlBroken && tick.getBid() < slLine.getPrice(0)){
console.getOut().println("最初のストップロス価格を抜けました。Bid価格 = " + df.format(tick.getBid()));
origSlBroken = true;
}
if(!origTpBroken && tick.getBid() > tpLine.getPrice(0)){
console.getOut().println("最初のリミット価格を抜けました。Bid価格 = " + df.format(tick.getBid()));
origTpBroken = true;
}
lastTickTime = tick.getTime();
}
@Override
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {}
@Override
public void onMessage(IMessage message) throws JFException {}
@Override
public void onAccount(IAccount account) throws JFException {}
@Override
public void onStop() throws JFException {
chart.removeAll();
for(IOrder o:engine.getOrders()){
o.close();
}
}
}