The below code behaves in a way I cannot explain.
The limit orders (sometimes) get filled, which is what I coded,
but they close immediately after one bar.
Anybody has got an idea??
var study = new MAStudy(55, 0, "Close", MAStudy.EXPONENTIAL);
var vEntryLevel =0;
var vLimitLong;
var vLimitShort;
function preMain() {
setPriceStudy(false);
}
function main() {
var v = study.getValue(MAStudy.MA);
vLimitLong = close() + (high() - low())*2/3;
vLimitShort = close + (high() -low()) /3;
if(v == null)
return;
//Strategy.doLong (Description, Fill Type, Fill Bar, LotSize, StopOrLimit)
if(close() >= v)
{
if(Strategy.isShort)
{
Strategy.doCover("cover", Strategy.CLOSE,Strategy.NEXTBAR);
}
if(!Strategy.isLong())
// we try to open one at the limit level
{
Strategy.doLong("limit long", Strategy.LIMIT, Strategy.NEXTBAR, Strategy.getDefaultLotSize(), vLimitLong);
}
}
if(close() < v)
{
if(Strategy.isLong)
{
Strategy.doSell("sell long",Strategy.CLOSE,Strategy.NEXTBAR);
}
if(!Strategy.isShort())
{
// we try to short one at the limit level
Strategy.doShort("limit short", Strategy.LIMIT, Strategy.NEXTBAR, Strategy.getDefaultLotSize(), vLimitShort);
}
}
if(Strategy.isLong())
{
setBarBgColor(Color.green);
}
else if(Strategy.isShort())
{
setBarBgColor(Color.red);
}
return(vLimitLong);
}
The limit orders (sometimes) get filled, which is what I coded,
but they close immediately after one bar.
Anybody has got an idea??
var study = new MAStudy(55, 0, "Close", MAStudy.EXPONENTIAL);
var vEntryLevel =0;
var vLimitLong;
var vLimitShort;
function preMain() {
setPriceStudy(false);
}
function main() {
var v = study.getValue(MAStudy.MA);
vLimitLong = close() + (high() - low())*2/3;
vLimitShort = close + (high() -low()) /3;
if(v == null)
return;
//Strategy.doLong (Description, Fill Type, Fill Bar, LotSize, StopOrLimit)
if(close() >= v)
{
if(Strategy.isShort)
{
Strategy.doCover("cover", Strategy.CLOSE,Strategy.NEXTBAR);
}
if(!Strategy.isLong())
// we try to open one at the limit level
{
Strategy.doLong("limit long", Strategy.LIMIT, Strategy.NEXTBAR, Strategy.getDefaultLotSize(), vLimitLong);
}
}
if(close() < v)
{
if(Strategy.isLong)
{
Strategy.doSell("sell long",Strategy.CLOSE,Strategy.NEXTBAR);
}
if(!Strategy.isShort())
{
// we try to short one at the limit level
Strategy.doShort("limit short", Strategy.LIMIT, Strategy.NEXTBAR, Strategy.getDefaultLotSize(), vLimitShort);
}
}
if(Strategy.isLong())
{
setBarBgColor(Color.green);
}
else if(Strategy.isShort())
{
setBarBgColor(Color.red);
}
return(vLimitLong);
}
Comment