Swingtrade
New member
HI
I am creating a strategy to buy when the moving average 9 crosses 14 (Exponential), below is the code I have written. I can not find any trades not sure which part of the code is wrong. Can you kindly help with correcting the code
Input tradesize = 10;
input price = close;
input averagetype = AverageType.exponential;
input percentgaingoal = 5;
input percentlossgoal = -2;
input avglength = 9;
input avglength1= 14;
def movingaverage = MovingAverage(averagetype, price, avglength>avglength1);
def percentchange = 10 * (close - EntryPrice()) / EntryPrice() ;
def buysignal = close crosses above movingaverage ;
def exitgood = percentchange > percentgaingoal ;
def exitbad = percentchange < percentlossgoal ;
AddOrder(OrderType.BUY_TO_OPEN, buysignal, open[-1], tradesize, Color.CYAN, Color.CYAN);
AddOrder(OrderType.SELL_TO_CLOSE, exitgood, open[-1], tradesize, Color.GREEN, Color.GREEN);
AddOrder(OrderType.SELL_TO_CLOSE, exitbad, open[-1], tradesize, Color.RED, Color.RED);
plot movingavgplot = MovingAverage (averagetype, price, avglength>avglength1);
Alert (buysignal, "SMA_Price_Cross");
I am creating a strategy to buy when the moving average 9 crosses 14 (Exponential), below is the code I have written. I can not find any trades not sure which part of the code is wrong. Can you kindly help with correcting the code
Input tradesize = 10;
input price = close;
input averagetype = AverageType.exponential;
input percentgaingoal = 5;
input percentlossgoal = -2;
input avglength = 9;
input avglength1= 14;
def movingaverage = MovingAverage(averagetype, price, avglength>avglength1);
def percentchange = 10 * (close - EntryPrice()) / EntryPrice() ;
def buysignal = close crosses above movingaverage ;
def exitgood = percentchange > percentgaingoal ;
def exitbad = percentchange < percentlossgoal ;
AddOrder(OrderType.BUY_TO_OPEN, buysignal, open[-1], tradesize, Color.CYAN, Color.CYAN);
AddOrder(OrderType.SELL_TO_CLOSE, exitgood, open[-1], tradesize, Color.GREEN, Color.GREEN);
AddOrder(OrderType.SELL_TO_CLOSE, exitbad, open[-1], tradesize, Color.RED, Color.RED);
plot movingavgplot = MovingAverage (averagetype, price, avglength>avglength1);
Alert (buysignal, "SMA_Price_Cross");