I am using a simple moving average crossover system below. I am trying to write code to add to winners (I have only attempted to do so for Long positions at this time). I will put my questions before the code I have questions with.
Here is my code:
var study9 = new MAStudy(9, 0, "Close", MAStudy.SIMPLE);
var study16 = new MAStudy(16, 0, "Close", MAStudy.SIMPLE);
function preMain() {
setPriceStudy(true);
setColorPriceBars(true);
setDefaultPriceBarColor(Color.black);
}
function main() {
var v9 = study9.getValue(MAStudy.MA);
var v16 = study16.getValue(MAStudy.MA);
var nOriginalEntry;
/* Once trade moves in favor this much, add to position*/
var nIncrementAmount;
/* Once trade is entered,this variable is set to one and is incremented each time I add to my position*/
var nCounter;
nIncrementAmout = .10;
if(v9 == null)
return;
if(v16 == null)
return;
/*Here I am trying to add to a long position if I am already long and the high of the bar has increased by .10 of my original entry point (for the first time executed. Is there a better way to do this?*/
if(Strategy.isLong && high > (nOriginalEntry + (nCounter * nIncrementAmount)))
/*Here I am trying to add to a position. Is this how I would do it?*/
Strategy.doLong("Crossing Up",nOriginalEntry + (nCounter * nIncrementAmount))
if(v9 > v16 && !Strategy.isLong())
Strategy.doLong("Crossing Up", Strategy.MARKET, Strategy.THISBAR);
/* I am trying to assign my original entry price to this variable. Is this correct?*/
nOriginalEntry = Strategy.MARKET;
nCounter = 0;
nCounter = nCounter + 1;
if(v9 < v16 && !Strategy.isShort())
nCounter = 0;
Strategy.doSell("Sell",Strategy.MARKET, Strategy.THISBAR,Strategy.ALL);
Strategy.doShort("Crossing Down", Strategy.MARKET, Strategy.THISBAR);
if(Strategy.isLong())
setPriceBarColor(Color.lime);
else if(Strategy.isShort())
setPriceBarColor(Color.red);
return v9;
}
Thanks,
fan27
Here is my code:
var study9 = new MAStudy(9, 0, "Close", MAStudy.SIMPLE);
var study16 = new MAStudy(16, 0, "Close", MAStudy.SIMPLE);
function preMain() {
setPriceStudy(true);
setColorPriceBars(true);
setDefaultPriceBarColor(Color.black);
}
function main() {
var v9 = study9.getValue(MAStudy.MA);
var v16 = study16.getValue(MAStudy.MA);
var nOriginalEntry;
/* Once trade moves in favor this much, add to position*/
var nIncrementAmount;
/* Once trade is entered,this variable is set to one and is incremented each time I add to my position*/
var nCounter;
nIncrementAmout = .10;
if(v9 == null)
return;
if(v16 == null)
return;
/*Here I am trying to add to a long position if I am already long and the high of the bar has increased by .10 of my original entry point (for the first time executed. Is there a better way to do this?*/
if(Strategy.isLong && high > (nOriginalEntry + (nCounter * nIncrementAmount)))
/*Here I am trying to add to a position. Is this how I would do it?*/
Strategy.doLong("Crossing Up",nOriginalEntry + (nCounter * nIncrementAmount))
if(v9 > v16 && !Strategy.isLong())
Strategy.doLong("Crossing Up", Strategy.MARKET, Strategy.THISBAR);
/* I am trying to assign my original entry price to this variable. Is this correct?*/
nOriginalEntry = Strategy.MARKET;
nCounter = 0;
nCounter = nCounter + 1;
if(v9 < v16 && !Strategy.isShort())
nCounter = 0;
Strategy.doSell("Sell",Strategy.MARKET, Strategy.THISBAR,Strategy.ALL);
Strategy.doShort("Crossing Down", Strategy.MARKET, Strategy.THISBAR);
if(Strategy.isLong())
setPriceBarColor(Color.lime);
else if(Strategy.isShort())
setPriceBarColor(Color.red);
return v9;
}
Thanks,
fan27
Comment