This is the complete strategy...
var fpArray = new Array();
var bInit = false;
var bAllowTrading = false;
var sPreviousTime = 0;
var sCurrentTime = 0;
function preMain() {
setPriceStudy(true);
setStudyTitle("Strategy_MA Crossover_3MAs");
//setColorPriceBars(true);
//setDefaultPriceBarColor(Color.black);
setPlotType(PLOTTYPE_SQUAREWAVE,0);
setDefaultBarFgColor(Color.black, 0);
setPlotType(PLOTTYPE_SQUAREWAVE,1);
setDefaultBarFgColor(Color.blue, 1);
setPlotType(PLOTTYPE_SQUAREWAVE,2);
setDefaultBarFgColor(Color.green, 2);
var x=0;
fpArray[x] = new FunctionParameter("MA1", FunctionParameter.STRING);
with(fpArray[x++]){
setName("Fast MA Type");
addOption("kama");
addOption("HullMA");
addOption("ema");
addOption("sma");
setDefault("sma");
}
fpArray[x] = new FunctionParameter("Length", FunctionParameter.NUMBER);
with(fpArray[x++]){
setName("Length Fast MA");
setLowerLimit(1);
setDefault(20);
}
fpArray[x] = new FunctionParameter("MA2", FunctionParameter.STRING);
with(fpArray[x++]){
setName("Slow MA Type");
addOption("kama");
addOption("HullMA");
addOption("ema");
addOption("sma");
setDefault("kama");
}
fpArray[x] = new FunctionParameter("Length2", FunctionParameter.NUMBER);
with(fpArray[x++]){
setName("Length Slow MA");
setLowerLimit(1);
setDefault(40);
}
fpArray[x] = new FunctionParameter("MA3", FunctionParameter.STRING);
with(fpArray[x++]){
setName("Third MA Type");
addOption("kama");
addOption("HullMA");
addOption("ema");
addOption("sma");
setDefault("kama");
}
fpArray[x] = new FunctionParameter("Length3", FunctionParameter.NUMBER);
with(fpArray[x++]){
setName("Length Third MA");
setLowerLimit(1);
setDefault(40);
}
fpArray[x] = new FunctionParameter("sStartTime", FunctionParameter.NUMBER);
with(fpArray[x++]){
setName("StartTime");
setDefault(800);
}
fpArray[x] = new FunctionParameter("sEndTime", FunctionParameter.NUMBER);
with(fpArray[x++]){
setName("EndTime");
setDefault(1900);
}
fpArray[x] = new FunctionParameter("delta", FunctionParameter.NUMBER);
with(fpArray[x++]){
setName("delta");
setDefault(0);
}
fpArray[x] = new FunctionParameter("nStop_Loss", FunctionParameter.NUMBER);
with(fpArray[x++])
{
setName("StopLoss_Ticks");
setLowerLimit(1);
setDefault(15);
}
fpArray[x] = new FunctionParameter("nProfit_Target", FunctionParameter.NUMBER);
with(fpArray[x++])
{
setName("Profit_Ticks");
setLowerLimit(1);
setDefault(999);
}
fpArray[x] = new FunctionParameter("nTick_Size", FunctionParameter.NUMBER);
with(fpArray[x++]){
setName("TickSize");
setDefault(.1);
}
}
var xSMAFast = null;
var xSMASlow = null;
var xSMAThird = null;
var Symbol = null;
var Interval = null;
var vSymbol = null;
var nStop = null;
var nTarget = null;
var bExittrade = false;
function main(Length,Length2,Length3,MA1,MA2,MA3,sStartTime ,sEndTime,delta,nStop_Loss,nProfit_Target,nTick_Si ze) {
var nBarState = getBarState();
var nSMAFast = 0;
var nSMASlow = 0;
var nSMAThird = 0;
var nSMAFast_1 = 0;
var nSMASlow_1 = 0;
var nSMAThird_1 = 0;
if (bInit == false) {
Symbol = getSymbol();
//Interval = getInterval();
vSymbol = Symbol+",1000V";
//xSMAFast = efsExternal(MA1+".efs",Length);//,sym(vSymbol))
//xSMASlow = efsExternal(MA2+".efs",Length2)//efsExternal("kama.efs",20)
//xSMAFast = sma(Length);
//xSMASlow = sma(Length2);
if(MA1 == "sma"){
xSMAFast = sma(Length);
}else if(MA1 == "ema"){
xSMAFast = ema(Length);
}else if(MA1 == "hullma4"){
xSMAFast = efsExternal("hullma4.efs",Length);
}else xSMAFast = efsExternal(MA1+".efs",Length);
if(MA2 == "sma"){
xSMASlow = sma(Length2);
}else if(MA2 == "ema"){
xSMASlow = ema(Length2);
}else if(MA2 =="hullma4"){
xSMASlow = efsExternal("hullma4.efs",Length2);
}else xSMASlow = efsExternal(MA2+".efs",Length2);
if(MA3 == "sma"){
xSMAThird = sma(Length3);
}else if(MA3 == "ema"){
xSMAThird = ema(Length3);
}else if(MA3 == "hullma4"){
xSMAThird = efsExternal("hullma4.efs",Length3);
}else xSMAThird = efsExternal(MA3+".efs",Length3);
//xSMASlow = efsExternal("hullma.efs",Length)
//xSMAFast = efsExternal("kama_of_hma.efs",Length2,5)
bInit = true;
}
nSMAFast = xSMAFast.getValue(-1);
nSMASlow = xSMASlow.getValue(-1);
nSMAThird = xSMAThird.getValue(-1);
nSMAFast_1 = xSMAFast.getValue(-2);
nSMASlow_1 = xSMASlow.getValue(-2);
nSMAThird_1 = xSMAThird.getValue(-2);
if(nSMAFast == null || nSMASlow == null || nSMAThird ==null) return;
if (getCurrentBarIndex() == 0) return;
CheckTradeTime(sStartTime,sEndTime);
if(Strategy.isLong() == true && bAllowTrading==false) {
Strategy.doSell("EOD",Strategy.MARKET,Strategy.THI SBAR);
}
if(Strategy.isShort() == true && bAllowTrading==false) {
Strategy.doCover("EOD",Strategy.MARKET,Strategy.TH ISBAR);
}
StopExit()
ProfitExit()
bExittrade = false;
if(Strategy.isLong() == true && nSMAFast < nSMASlow && nSMAFast_1 >= nSMASlow_1 ){
Strategy.doSell("Market changing direction",Strategy.MARKET,Strategy.THISBAR);
bExittrade = true;
}
if(Strategy.isShort() == true && nSMAFast > nSMASlow && nSMAFast_1 <= nSMASlow_1){
Strategy.doCover("Market changing direction",Strategy.MARKET,Strategy.THISBAR);
bExittrade = true;
}
if(bAllowTrading == true && bExittrade ==false) {
//if(nSMAFast > nSMASlow+delta && nSMAFast_1 <= nSMASlow_1 && !Strategy.isLong() && nSMASlow > nSMAThird) {
if(nSMAFast > nSMASlow+delta && !Strategy.isLong() && nSMASlow > nSMAThird) {
Strategy.doLong("Crossing Up", Strategy.MARKET, Strategy.THISBAR);
nStop= open(0)-nStop_Loss*nTick_Size;
nTarget = open(0)+ nProfit_Target*nTick_Size;
}
//if(nSMAFast < nSMASlow-delta && nSMAFast_1 >= nSMASlow_1 && !Strategy.isShort() && nSMASlow < nSMAThird){
if(nSMAFast < nSMASlow-delta && !Strategy.isShort() && nSMASlow < nSMAThird){
Strategy.doShort("Crossing Down", Strategy.MARKET, Strategy.THISBAR);
nStop= open(0)+nStop_Loss*nTick_Size;
nTarget = open(0)- nProfit_Target*nTick_Size;
}
}
if(Strategy.isLong()) {
setBarBgColor(Color.RGB(0xde,0xb8,0x87), 0); // background color during long position
}
else if(Strategy.isShort()) {
setBarBgColor(Color.RGB(0x8b,0x86,0x82), 0); //background color during short position
}
return new Array( xSMAFast.getValue(0), xSMASlow.getValue(0),xSMAThird.getValue(0));
}
function CheckTradeTime(sStartTime,sEndTime){
sCurrentTime = (getHour(0)*100)+getMinute(0);
if (sCurrentTime >= sStartTime && sCurrentTime < sEndTime){
bAllowTrading = true;
sPreviousTime = sCurrentTime;
}
//----------------------- exit any open trades at end of trading hours ----------------
if (sPreviousTime < sEndTime && sCurrentTime >= sEndTime){
bAllowTrading = false;
sPreviousTime = sCurrentTime;
}
}
function StopExit(){
if(Strategy.isInTrade() == true) {
if(Strategy.isLong() == true) { //Long position Fixed Stop exit
if(open(0) <= nStop) {
Strategy.doSell("Long Stop",Strategy.MARKET,Strategy.THISBAR);
} else if (low(0) <=nStop){
Strategy.doSell("Long Stop",Strategy.STOP,Strategy.THISBAR,Strategy.ALL, nStop);
}
}
if(Strategy.isShort() == true){ //Short position Fixed Stop exit
if(open(0) >= nStop) {
Strategy.doCover("Short Stop",Strategy.MARKET,Strategy.THISBAR);
} else if (high(0) >=nStop){
Strategy.doCover("Short Stop",Strategy.STOP,Strategy.THISBAR,Strategy.ALL, nStop);
}
}
}
}
function ProfitExit(){
if(Strategy.isInTrade() == true) {
if(Strategy.isLong() == true) { //Long position Fixed Profit Booking exit
if(open(0) >=nTarget) {
Strategy.doSell("Long Target",Strategy.MARKET,Strategy.THISBAR);
}else if (high(0) >=nTarget){
Strategy.doSell("Long Target",Strategy.LIMIT,Strategy.THISBAR,Strategy.A LL,nTarget);
}
}
if(Strategy.isShort()==true){
if(open(0)<=nTarget){
Strategy.doCover("Short Target", Strategy.MARKET, Strategy.THISBAR);
}else if (low(0) <= nTarget) {
Strategy.doCover("Short Target", Strategy.LIMIT, Strategy.THISBAR, Strategy.ALL, nTarget);
}
}
}
}
var fpArray = new Array();
var bInit = false;
var bAllowTrading = false;
var sPreviousTime = 0;
var sCurrentTime = 0;
function preMain() {
setPriceStudy(true);
setStudyTitle("Strategy_MA Crossover_3MAs");
//setColorPriceBars(true);
//setDefaultPriceBarColor(Color.black);
setPlotType(PLOTTYPE_SQUAREWAVE,0);
setDefaultBarFgColor(Color.black, 0);
setPlotType(PLOTTYPE_SQUAREWAVE,1);
setDefaultBarFgColor(Color.blue, 1);
setPlotType(PLOTTYPE_SQUAREWAVE,2);
setDefaultBarFgColor(Color.green, 2);
var x=0;
fpArray[x] = new FunctionParameter("MA1", FunctionParameter.STRING);
with(fpArray[x++]){
setName("Fast MA Type");
addOption("kama");
addOption("HullMA");
addOption("ema");
addOption("sma");
setDefault("sma");
}
fpArray[x] = new FunctionParameter("Length", FunctionParameter.NUMBER);
with(fpArray[x++]){
setName("Length Fast MA");
setLowerLimit(1);
setDefault(20);
}
fpArray[x] = new FunctionParameter("MA2", FunctionParameter.STRING);
with(fpArray[x++]){
setName("Slow MA Type");
addOption("kama");
addOption("HullMA");
addOption("ema");
addOption("sma");
setDefault("kama");
}
fpArray[x] = new FunctionParameter("Length2", FunctionParameter.NUMBER);
with(fpArray[x++]){
setName("Length Slow MA");
setLowerLimit(1);
setDefault(40);
}
fpArray[x] = new FunctionParameter("MA3", FunctionParameter.STRING);
with(fpArray[x++]){
setName("Third MA Type");
addOption("kama");
addOption("HullMA");
addOption("ema");
addOption("sma");
setDefault("kama");
}
fpArray[x] = new FunctionParameter("Length3", FunctionParameter.NUMBER);
with(fpArray[x++]){
setName("Length Third MA");
setLowerLimit(1);
setDefault(40);
}
fpArray[x] = new FunctionParameter("sStartTime", FunctionParameter.NUMBER);
with(fpArray[x++]){
setName("StartTime");
setDefault(800);
}
fpArray[x] = new FunctionParameter("sEndTime", FunctionParameter.NUMBER);
with(fpArray[x++]){
setName("EndTime");
setDefault(1900);
}
fpArray[x] = new FunctionParameter("delta", FunctionParameter.NUMBER);
with(fpArray[x++]){
setName("delta");
setDefault(0);
}
fpArray[x] = new FunctionParameter("nStop_Loss", FunctionParameter.NUMBER);
with(fpArray[x++])
{
setName("StopLoss_Ticks");
setLowerLimit(1);
setDefault(15);
}
fpArray[x] = new FunctionParameter("nProfit_Target", FunctionParameter.NUMBER);
with(fpArray[x++])
{
setName("Profit_Ticks");
setLowerLimit(1);
setDefault(999);
}
fpArray[x] = new FunctionParameter("nTick_Size", FunctionParameter.NUMBER);
with(fpArray[x++]){
setName("TickSize");
setDefault(.1);
}
}
var xSMAFast = null;
var xSMASlow = null;
var xSMAThird = null;
var Symbol = null;
var Interval = null;
var vSymbol = null;
var nStop = null;
var nTarget = null;
var bExittrade = false;
function main(Length,Length2,Length3,MA1,MA2,MA3,sStartTime ,sEndTime,delta,nStop_Loss,nProfit_Target,nTick_Si ze) {
var nBarState = getBarState();
var nSMAFast = 0;
var nSMASlow = 0;
var nSMAThird = 0;
var nSMAFast_1 = 0;
var nSMASlow_1 = 0;
var nSMAThird_1 = 0;
if (bInit == false) {
Symbol = getSymbol();
//Interval = getInterval();
vSymbol = Symbol+",1000V";
//xSMAFast = efsExternal(MA1+".efs",Length);//,sym(vSymbol))
//xSMASlow = efsExternal(MA2+".efs",Length2)//efsExternal("kama.efs",20)
//xSMAFast = sma(Length);
//xSMASlow = sma(Length2);
if(MA1 == "sma"){
xSMAFast = sma(Length);
}else if(MA1 == "ema"){
xSMAFast = ema(Length);
}else if(MA1 == "hullma4"){
xSMAFast = efsExternal("hullma4.efs",Length);
}else xSMAFast = efsExternal(MA1+".efs",Length);
if(MA2 == "sma"){
xSMASlow = sma(Length2);
}else if(MA2 == "ema"){
xSMASlow = ema(Length2);
}else if(MA2 =="hullma4"){
xSMASlow = efsExternal("hullma4.efs",Length2);
}else xSMASlow = efsExternal(MA2+".efs",Length2);
if(MA3 == "sma"){
xSMAThird = sma(Length3);
}else if(MA3 == "ema"){
xSMAThird = ema(Length3);
}else if(MA3 == "hullma4"){
xSMAThird = efsExternal("hullma4.efs",Length3);
}else xSMAThird = efsExternal(MA3+".efs",Length3);
//xSMASlow = efsExternal("hullma.efs",Length)
//xSMAFast = efsExternal("kama_of_hma.efs",Length2,5)
bInit = true;
}
nSMAFast = xSMAFast.getValue(-1);
nSMASlow = xSMASlow.getValue(-1);
nSMAThird = xSMAThird.getValue(-1);
nSMAFast_1 = xSMAFast.getValue(-2);
nSMASlow_1 = xSMASlow.getValue(-2);
nSMAThird_1 = xSMAThird.getValue(-2);
if(nSMAFast == null || nSMASlow == null || nSMAThird ==null) return;
if (getCurrentBarIndex() == 0) return;
CheckTradeTime(sStartTime,sEndTime);
if(Strategy.isLong() == true && bAllowTrading==false) {
Strategy.doSell("EOD",Strategy.MARKET,Strategy.THI SBAR);
}
if(Strategy.isShort() == true && bAllowTrading==false) {
Strategy.doCover("EOD",Strategy.MARKET,Strategy.TH ISBAR);
}
StopExit()
ProfitExit()
bExittrade = false;
if(Strategy.isLong() == true && nSMAFast < nSMASlow && nSMAFast_1 >= nSMASlow_1 ){
Strategy.doSell("Market changing direction",Strategy.MARKET,Strategy.THISBAR);
bExittrade = true;
}
if(Strategy.isShort() == true && nSMAFast > nSMASlow && nSMAFast_1 <= nSMASlow_1){
Strategy.doCover("Market changing direction",Strategy.MARKET,Strategy.THISBAR);
bExittrade = true;
}
if(bAllowTrading == true && bExittrade ==false) {
//if(nSMAFast > nSMASlow+delta && nSMAFast_1 <= nSMASlow_1 && !Strategy.isLong() && nSMASlow > nSMAThird) {
if(nSMAFast > nSMASlow+delta && !Strategy.isLong() && nSMASlow > nSMAThird) {
Strategy.doLong("Crossing Up", Strategy.MARKET, Strategy.THISBAR);
nStop= open(0)-nStop_Loss*nTick_Size;
nTarget = open(0)+ nProfit_Target*nTick_Size;
}
//if(nSMAFast < nSMASlow-delta && nSMAFast_1 >= nSMASlow_1 && !Strategy.isShort() && nSMASlow < nSMAThird){
if(nSMAFast < nSMASlow-delta && !Strategy.isShort() && nSMASlow < nSMAThird){
Strategy.doShort("Crossing Down", Strategy.MARKET, Strategy.THISBAR);
nStop= open(0)+nStop_Loss*nTick_Size;
nTarget = open(0)- nProfit_Target*nTick_Size;
}
}
if(Strategy.isLong()) {
setBarBgColor(Color.RGB(0xde,0xb8,0x87), 0); // background color during long position
}
else if(Strategy.isShort()) {
setBarBgColor(Color.RGB(0x8b,0x86,0x82), 0); //background color during short position
}
return new Array( xSMAFast.getValue(0), xSMASlow.getValue(0),xSMAThird.getValue(0));
}
function CheckTradeTime(sStartTime,sEndTime){
sCurrentTime = (getHour(0)*100)+getMinute(0);
if (sCurrentTime >= sStartTime && sCurrentTime < sEndTime){
bAllowTrading = true;
sPreviousTime = sCurrentTime;
}
//----------------------- exit any open trades at end of trading hours ----------------
if (sPreviousTime < sEndTime && sCurrentTime >= sEndTime){
bAllowTrading = false;
sPreviousTime = sCurrentTime;
}
}
function StopExit(){
if(Strategy.isInTrade() == true) {
if(Strategy.isLong() == true) { //Long position Fixed Stop exit
if(open(0) <= nStop) {
Strategy.doSell("Long Stop",Strategy.MARKET,Strategy.THISBAR);
} else if (low(0) <=nStop){
Strategy.doSell("Long Stop",Strategy.STOP,Strategy.THISBAR,Strategy.ALL, nStop);
}
}
if(Strategy.isShort() == true){ //Short position Fixed Stop exit
if(open(0) >= nStop) {
Strategy.doCover("Short Stop",Strategy.MARKET,Strategy.THISBAR);
} else if (high(0) >=nStop){
Strategy.doCover("Short Stop",Strategy.STOP,Strategy.THISBAR,Strategy.ALL, nStop);
}
}
}
}
function ProfitExit(){
if(Strategy.isInTrade() == true) {
if(Strategy.isLong() == true) { //Long position Fixed Profit Booking exit
if(open(0) >=nTarget) {
Strategy.doSell("Long Target",Strategy.MARKET,Strategy.THISBAR);
}else if (high(0) >=nTarget){
Strategy.doSell("Long Target",Strategy.LIMIT,Strategy.THISBAR,Strategy.A LL,nTarget);
}
}
if(Strategy.isShort()==true){
if(open(0)<=nTarget){
Strategy.doCover("Short Target", Strategy.MARKET, Strategy.THISBAR);
}else if (low(0) <= nTarget) {
Strategy.doCover("Short Target", Strategy.LIMIT, Strategy.THISBAR, Strategy.ALL, nTarget);
}
}
}
}
Comment