Load parameters from XML
To save configurable parameters to the file, run your strategy with configurable parameters and then save preset with specified name:
Parameters will be saved in your strategies path directory in xml format.
The example below reads configurable parameters from xml file "Parameters.xml" stored on C disk and creates a Buy Limit order with specified instrument, price and take profit.
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.dukascopy.api.*;
import com.dukascopy.api.IEngine.OrderCommand;
@RequiresFullAccess
public class ParametersInXml implements IStrategy {
private IEngine engine;
private IConsole console;
public Instrument selectedInstrument;
public double price ;
public double takeProfit;
public void onStart(IContext context) throws JFException {
this.engine = context.getEngine();
this.console = context.getConsole();
parseFile("C:\\Parameters.xml");
engine.submitOrder("Order1", selectedInstrument, OrderCommand.BUYLIMIT, 0.01,price, 0, 0, takeProfit);
}
public void onAccount(IAccount account) throws JFException {
}
public void onMessage(IMessage message) throws JFException {
}
public void onStop() throws JFException {
for (IOrder order : engine.getOrders()) {
engine.getOrder(order.getLabel()).close();
}
}
public void onTick(Instrument instrument, ITick tick) throws JFException {
}
public void onBar(Instrument instrument, Period period, IBar askBar, IBar bidBar) throws JFException {
}
Method parseFile find all variable tags and parse inner tag name and value. After parsing necessary tag we initialize ParametersInXml class fields
public void parseFile(String fileName) {
try {
File file = new File(fileName);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("variable");
<b>Bold text</b>
// Call initializeVariable method with all variable xml nodes
for (int count = 0; count < nodeList.getLength(); count++) {
Node node = nodeList.item(count);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
NodeList nameTag = element.getElementsByTagName("name");
Element variableName = (Element) nameTag.item(0);
NodeList name = variableName.getChildNodes();
NodeList valueTag = element.getElementsByTagName("value");
Element variableValue = (Element) valueTag.item(0);
NodeList value = variableValue.getChildNodes();
initializeVariable(name.item(0).getNodeValue(), value.item(0).getNodeValue());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Method initializeVariable initializes variable if xml node name match with necessary order parameters (instrument, price and take profit)
private void initializeVariable(String nameNode, String valueNode) {
if (nameNode.equals("selectedInstrument")) {
selectedInstrument = Instrument.fromString(valueNode);
}
if (nameNode.equals("takeProfit")) {
takeProfit = Double.valueOf(valueNode);
}
if (nameNode.equals("price")) {
price = Double.valueOf(valueNode);
}
}
}
The information on this web site is provided only as general information, which may be incomplete or outdated. Click here for full disclaimer.