package jforex;

import com.dukascopy.api.*;
import com.dukascopy.api.Configurable;
import com.dukascopy.api.IAccount;
import com.dukascopy.api.IBar;
import com.dukascopy.api.IConsole;
import com.dukascopy.api.IContext;
import com.dukascopy.api.IMessage;
import com.dukascopy.api.IStrategy;
import com.dukascopy.api.ITick;
import com.dukascopy.api.Instrument;
import com.dukascopy.api.JFException;
import com.dukascopy.api.Period;
import com.pretty_tools.dde.client.DDEClientConversation;
import com.pretty_tools.dde.client.DDEClientException;

@Library("pretty-tools-JDDE-1.0.2.jar")



@RequiresFullAccess
public class UpdateExcel implements IStrategy {


@Configurable("CCY")
public Instrument instrument = Instrument.EURUSD;

@Configurable("Period")
public Period period = Period.TEN_SECS;

@Configurable("Select the starting row")
public int row = 1;
@Configurable("Select the starting column")
public int column = 1;
@Configurable("Type sheet name")
public String sheetName = "Feuil1";


private DDEClientConversation conversation;
private IConsole console;
private int counter;


@Override
public void onStart(IContext context) throws JFException {

conversation = new DDEClientConversation();
console = context.getConsole();
counter = 0;

try {
conversation.connect("Excel", sheetName);

} catch (DDEClientException e) {
print(e.toString());
}



}

public synchronized void onBar(Instrument instrument, Period period, IBar askBar,
IBar bidBar) throws JFException {

if (instrument == this.instrument && period == this.period) {

try {
conversation.poke(getCell(0),String.valueOf(bidBar.getOpen()));
conversation.poke(getCell(1),String.valueOf(bidBar.getHigh()));
conversation.poke(getCell(2),String.valueOf(bidBar.getLow()));
conversation.poke(getCell(3),String.valueOf(bidBar.getClose()));
conversation.poke(getCell(4),String.valueOf(bidBar.getVolume()));
conversation.poke(getCell(5),String.valueOf(bidBar.getTime()));
conversation.poke(getCell(6),String.valueOf(askBar.getOpen()));
conversation.poke(getCell(7),String.valueOf(askBar.getHigh()));
conversation.poke(getCell(8),String.valueOf(askBar.getLow()));
conversation.poke(getCell(9),String.valueOf(askBar.getClose()));
conversation.poke(getCell(10),String.valueOf(askBar.getVolume()));
conversation.poke(getCell(11),String.valueOf(askBar.getTime()));

} catch (DDEClientException e) {

print(e.toString());
} 

counter++;

}


}

@Override
public void onTick(Instrument instrument, ITick tick) throws JFException {



}

@Override
public void onAccount(IAccount account) throws JFException {

}


@Override
public void onMessage(IMessage message) throws JFException {

}


@Override
public void onStop() throws JFException {

}

public void print(String str) {

console.getOut().println(str);

}

public String getCell(int num) {


String str = "L" +(row+counter) + "C" + (column+num);

return str;
}
}
