I have set up automatic trades using buy(), sell(), buyMarket()...I am pretty much accomplishing what I need.
However, every time I load up my script, it executes tons of orders to my integrated broker (Interactive Broker). I assume, as the script loads, and my orders are triggered for historical data, the orders are sent in (simulated of course at this point). I have tried limiting this several ways, but have not had any luck. I attempted using BARSTATE_ALLBARS (i.e. script is initializing) and only trigger orders when the variable vState == BARSTATE_ALLBARS. However, when I add this code, no orders are triggered at all.
Any help would be greatly appreciated, and thanks for all of the help to date, it has quickly move me forward with EFS.
Regards,
ZMendel
However, every time I load up my script, it executes tons of orders to my integrated broker (Interactive Broker). I assume, as the script loads, and my orders are triggered for historical data, the orders are sent in (simulated of course at this point). I have tried limiting this several ways, but have not had any luck. I attempted using BARSTATE_ALLBARS (i.e. script is initializing) and only trigger orders when the variable vState == BARSTATE_ALLBARS. However, when I add this code, no orders are triggered at all.
PHP Code:
var vLastAlert = -1;
function preMain() {
setPriceStudy(false);
setStudyTitle("ADX DI(+) DI(-) v3.1.2");
setCursorLabelName("DI (+)", 0);
setCursorLabelName("DI (-)", 1);
setDefaultBarStyle(PS_SOLID, 0);
setDefaultBarStyle(PS_SOLID, 1);
setDefaultBarFgColor(Color.navy, 0);
setDefaultBarFgColor(Color.red, 1);
setDefaultBarThickness(9, 0);
setDefaultBarThickness(9, 1);
setPlotType(PLOTTYPE_HISTOGRAM, 0);
setPlotType(PLOTTYPE_HISTOGRAM, 1);
var fp1 = new FunctionParameter("ADXr", FunctionParameter.NUMBER);
fp1.setLowerLimit(0);
fp1.setDefault (3);
}
function main(ADXr, Trade) {
var vADXDM = new ADXDMStudy(ADXr);
if (
vADXDM.getValue(ADXDMStudy.PDI) > vADXDM.getValue(ADXDMStudy.NDI)
) onAction1()
else if (
vADXDM.getValue(ADXDMStudy.PDI) < vADXDM.getValue(ADXDMStudy.NDI)
) onAction2();
return new Array(
vADXDM.getValue(ADXDMStudy.PDI),
vADXDM.getValue(ADXDMStudy.NDI)
);
var nState = getBarState();
}
function onAction1(nState) {
var vCurrentHour = getHour(0);
var vCurrentMinute = getMinute(0)/100;
var vCurrentTime = vCurrentHour + vCurrentMinute;
setBarBgColor(Color.RGB(192,255,160));
if (vLastAlert != 1) Alert.playSound ("C:\\ProgramFiles\\eSignal Pro\\Sounds\\Ding.wav");
if (vLastAlert != 1 & vCurrentTime > 9.29 & vCurrentTime < 15.44) Strategy.doCover("", (Strategy.MARKET + .5), Strategy.THISBAR, Strategy.ALL, 0);
if (vCurrentTime > 15.44) Strategy.doCover("", (Strategy.MARKET + .5), Strategy.THISBAR, Strategy.ALL, 0);
if (vLastAlert != 1 & vCurrentTime > 9.29 & vCurrentTime < 15.44) Strategy.doLong("", (Strategy.MARKET + .5), Strategy.THISBAR, 300, 1);
if (vLastAlert != 1 & vCurrentTime > 9.29 & vCurrentTime < 15.44 & nState == BARSTATE_ALLBARS) buy(getSymbol());
vLastAlert = 1;
}
function onAction2(nState) {
var vCurrentHour = getHour(0);
var vCurrentMinute = getMinute(0)/100;
var vCurrentTime = vCurrentHour + vCurrentMinute;
setBarBgColor(Color.RGB(255,255,160));
if (vLastAlert != 2) Alert.playSound ("C:\\ProgramFiles\\eSignal Pro\\Sounds\\Ding.wav");
if (vLastAlert != 2 & vCurrentTime > 9.29 & vCurrentTime <15.44) Strategy.doSell("", (Strategy.MARKET - .5), Strategy.THISBAR, Strategy.ALL, 0);
if (vCurrentTime > 15.44) Strategy.doSell("", (Strategy.MARKET - .5), Strategy.THISBAR, Strategy.ALL, 0);
if (vLastAlert != 2 & vCurrentTime > 9.29 & vCurrentTime < 15.44) Strategy.doShort("", (Strategy.MARKET -.5), Strategy.THISBAR, 300, 1);
if (vLastAlert != 2 & vCurrentTime > 9.29 & vCurrentTime < 15.44 & nState == BARSTATE_ALLBARS) sell(getSymbol());
vLastAlert = 2;
}
Regards,
ZMendel
Comment