I am trying to use DynaOrder as an API between FXCM and Esignal. I have downloaded a simplw MA crossover file from DynaOrder. The problem is that, when the lines cross and consolidate, the system places multiple trades in the same side. Can a check be places in the code that when the lines cross only one trade should be placed and that trade should be reversed when the lines cross again. Like in CMS's Visual Trading Software. The code from Dynaorder is;
// Dynaorder DLL declaration
var fxdots5 = new DLL ("fxdots5.dll");
// Moving Averages:
var MAFast = new MAStudy( 9, 0, "Close", MAStudy.SIMPLE);
var MASlow = new MAStudy(17, 0, "Close", MAStudy.SIMPLE);
// variables defining the instrument to trade:
var TradingName = "USD/JPY";
var Account = "";
var entryLevel;
var vLONG = 1;
var vSHORT = -1;
var vNONE = 0;
var nDirection = vNONE;
var vTime;
var thisTime = -1;
var prevTime = -1;
var diffTime = 0;
var oneBarTime = -1;
var prevFast;
var prevSlow;
var LotSize = 1;
var ShareMultiplier = 1;
var nArrow = 0;
var startupOK = false;
function preMain()
{
doInit();
setPriceStudy(true);
setDefaultBarFgColor(Color.blue, 0);
setDefaultBarFgColor(Color.red, 1);
setDefaultBarFgColor(Color.green, 2);
setPlotType(PLOTTYPE_LINE, 0);
setPlotType(PLOTTYPE_LINE, 1);
setPlotType(PLOTTYPE_DOT, 2);
setDefaultBarThickness(2, 2);
setCursorLabelName("FastMA", 0);
setCursorLabelName("SlowMA", 1);
setCursorLabelName("EntryLevel", 2);
setStudyTitle("fxMA Crossover");
}
function main()
{
if (startupOK==false)
{
doGoFlat();
defineInterval();
startupOK=true;
}
if (oneBarTime==-1)
return; // not supported time frame
vtime = getValue("Time");
if (vtime == null)
return;
var Fast_MA = MAFast.getValue(MAStudy.MA);
var Slow_MA = MASlow.getValue(MAStudy.MA);
thisTime = vtime.getHours()*10000 + vtime.getMinutes()*100 + vtime.getSeconds();
if (prevTime==(-1))
{
prevTime = thisTime;
prevFast = Fast_MA;
prevSlow = Slow_MA;
return;
}
if (nDirection==vNONE)
entryLevel = null;
if (thisTime>=prevTime)
diffTime = thisTime-prevTime;
else
diffTime = 240000+thisTime-prevTime;
if (diffTime>=oneBarTime)
{
if (getCurrentBarIndex()==0)
{
if ((Fast_MA>Slow_MA) && (prevFast<=prevSlow))
{
switch (nDirection)
{
case vSHORT: // reverse from short to long
if (doOpenLong(LotSize*ShareMultiplier*2, close())>0)
nDirection = vLONG;
break;
case vNONE: // open long
if (doOpenLong(LotSize*ShareMultiplier, close())>0)
nDirection = vLONG;
break;
}
}
else if ((Fast_MA<Slow_MA) && (prevFast>=prevSlow))
{
switch (nDirection)
{
case vLONG: // reverse from long to short
if (doOpenShort(LotSize*ShareMultiplier*2, close())>0)
nDirection = vSHORT;
break;
case vNONE: // open short
if (doOpenShort(LotSize*ShareMultiplier, close())>0)
nDirection = vSHORT;
break;
}
}
}
prevTime = thisTime;
prevFast = Fast_MA;
prevSlow = Slow_MA;
}
return new Array(Fast_MA, Slow_MA, entryLevel);
}
function defineInterval()
{
var sInterval = getInterval();
var c;
if (sInterval=="D" || sInterval=="W" || sInterval=="M" || sInterval=="Y")
oneBarTime = -1;
else
{
c = sInterval.search("S");
if (c!=-1)
oneBarTime = sInterval.substring(0, c);
else
{
c = sInterval.search("T");
if (c!=-1)
oneBarTime = -1;
else
oneBarTime = sInterval*100;
}
}
}
function doGoFlat()
{
conn = fxdots5.call ("IsHostConnected");
if (conn==1)
{
fxdots5.call ("ClosePositionFor", Account, TradingName);
}
else
return 0;
}
function doOpenLong(num, price)
{
conn = fxdots5.call ("IsHostConnected");
if (conn==1)
{
lastOrderID = fxdots5.call ("BuyMarket", TradingName, Account, num);
if (lastOrderID==0)
return 0;
else
{
drawShapeRelative (0, price, Shape.UPARROW, "", Color.blue, Image.ONTOP, "ARROW"+nArrow);
nArrow++;
entryLevel = price;
return num;
}
}
else
return 0;
}
function doOpenShort(num, price)
{
conn = fxdots5.call ("IsHostConnected");
if (conn==1)
{
lastOrderID = fxdots5.call ("SellMarket", TradingName, Account, num);
if (lastOrderID==0)
return 0;
else
{
drawShapeRelative (0, price, Shape.DOWNARROW, "", Color.red, Image.ONTOP, "ARROW"+nArrow);
nArrow++;
entryLevel = price;
return num;
}
}
else
return 0;
}
function doInit()
{
fxdots5.addFunction ("IsHostConnected", DLL.SHORT, DLL.STDCALL,"ISHOSTCONNECTED");
fxdots5.addFunction ("BuyMarket", DLL.INT, DLL.STDCALL, "BUYMARKET", DLL.STRING, DLL.STRING, DLL.FLOAT);
fxdots5.addFunction ("SellMarket", DLL.INT, DLL.STDCALL, "SELLMARKET", DLL.STRING, DLL.STRING, DLL.FLOAT);
fxdots5.addFunction ("ClosePositionFor", DLL.INT, DLL.STDCALL, "CLOSEPOSITIONFOR", DLL.STRING, DLL.STRING);
}
// Dynaorder DLL declaration
var fxdots5 = new DLL ("fxdots5.dll");
// Moving Averages:
var MAFast = new MAStudy( 9, 0, "Close", MAStudy.SIMPLE);
var MASlow = new MAStudy(17, 0, "Close", MAStudy.SIMPLE);
// variables defining the instrument to trade:
var TradingName = "USD/JPY";
var Account = "";
var entryLevel;
var vLONG = 1;
var vSHORT = -1;
var vNONE = 0;
var nDirection = vNONE;
var vTime;
var thisTime = -1;
var prevTime = -1;
var diffTime = 0;
var oneBarTime = -1;
var prevFast;
var prevSlow;
var LotSize = 1;
var ShareMultiplier = 1;
var nArrow = 0;
var startupOK = false;
function preMain()
{
doInit();
setPriceStudy(true);
setDefaultBarFgColor(Color.blue, 0);
setDefaultBarFgColor(Color.red, 1);
setDefaultBarFgColor(Color.green, 2);
setPlotType(PLOTTYPE_LINE, 0);
setPlotType(PLOTTYPE_LINE, 1);
setPlotType(PLOTTYPE_DOT, 2);
setDefaultBarThickness(2, 2);
setCursorLabelName("FastMA", 0);
setCursorLabelName("SlowMA", 1);
setCursorLabelName("EntryLevel", 2);
setStudyTitle("fxMA Crossover");
}
function main()
{
if (startupOK==false)
{
doGoFlat();
defineInterval();
startupOK=true;
}
if (oneBarTime==-1)
return; // not supported time frame
vtime = getValue("Time");
if (vtime == null)
return;
var Fast_MA = MAFast.getValue(MAStudy.MA);
var Slow_MA = MASlow.getValue(MAStudy.MA);
thisTime = vtime.getHours()*10000 + vtime.getMinutes()*100 + vtime.getSeconds();
if (prevTime==(-1))
{
prevTime = thisTime;
prevFast = Fast_MA;
prevSlow = Slow_MA;
return;
}
if (nDirection==vNONE)
entryLevel = null;
if (thisTime>=prevTime)
diffTime = thisTime-prevTime;
else
diffTime = 240000+thisTime-prevTime;
if (diffTime>=oneBarTime)
{
if (getCurrentBarIndex()==0)
{
if ((Fast_MA>Slow_MA) && (prevFast<=prevSlow))
{
switch (nDirection)
{
case vSHORT: // reverse from short to long
if (doOpenLong(LotSize*ShareMultiplier*2, close())>0)
nDirection = vLONG;
break;
case vNONE: // open long
if (doOpenLong(LotSize*ShareMultiplier, close())>0)
nDirection = vLONG;
break;
}
}
else if ((Fast_MA<Slow_MA) && (prevFast>=prevSlow))
{
switch (nDirection)
{
case vLONG: // reverse from long to short
if (doOpenShort(LotSize*ShareMultiplier*2, close())>0)
nDirection = vSHORT;
break;
case vNONE: // open short
if (doOpenShort(LotSize*ShareMultiplier, close())>0)
nDirection = vSHORT;
break;
}
}
}
prevTime = thisTime;
prevFast = Fast_MA;
prevSlow = Slow_MA;
}
return new Array(Fast_MA, Slow_MA, entryLevel);
}
function defineInterval()
{
var sInterval = getInterval();
var c;
if (sInterval=="D" || sInterval=="W" || sInterval=="M" || sInterval=="Y")
oneBarTime = -1;
else
{
c = sInterval.search("S");
if (c!=-1)
oneBarTime = sInterval.substring(0, c);
else
{
c = sInterval.search("T");
if (c!=-1)
oneBarTime = -1;
else
oneBarTime = sInterval*100;
}
}
}
function doGoFlat()
{
conn = fxdots5.call ("IsHostConnected");
if (conn==1)
{
fxdots5.call ("ClosePositionFor", Account, TradingName);
}
else
return 0;
}
function doOpenLong(num, price)
{
conn = fxdots5.call ("IsHostConnected");
if (conn==1)
{
lastOrderID = fxdots5.call ("BuyMarket", TradingName, Account, num);
if (lastOrderID==0)
return 0;
else
{
drawShapeRelative (0, price, Shape.UPARROW, "", Color.blue, Image.ONTOP, "ARROW"+nArrow);
nArrow++;
entryLevel = price;
return num;
}
}
else
return 0;
}
function doOpenShort(num, price)
{
conn = fxdots5.call ("IsHostConnected");
if (conn==1)
{
lastOrderID = fxdots5.call ("SellMarket", TradingName, Account, num);
if (lastOrderID==0)
return 0;
else
{
drawShapeRelative (0, price, Shape.DOWNARROW, "", Color.red, Image.ONTOP, "ARROW"+nArrow);
nArrow++;
entryLevel = price;
return num;
}
}
else
return 0;
}
function doInit()
{
fxdots5.addFunction ("IsHostConnected", DLL.SHORT, DLL.STDCALL,"ISHOSTCONNECTED");
fxdots5.addFunction ("BuyMarket", DLL.INT, DLL.STDCALL, "BUYMARKET", DLL.STRING, DLL.STRING, DLL.FLOAT);
fxdots5.addFunction ("SellMarket", DLL.INT, DLL.STDCALL, "SELLMARKET", DLL.STRING, DLL.STRING, DLL.FLOAT);
fxdots5.addFunction ("ClosePositionFor", DLL.INT, DLL.STDCALL, "CLOSEPOSITIONFOR", DLL.STRING, DLL.STRING);
}