# Strategy coding help

##### 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 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 ;

plot movingavgplot = MovingAverage (averagetype, price, avglength>avglength1);

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 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 ;

plot movingavgplot = MovingAverage (averagetype, price, avglength>avglength1);

I am not a coder so i cant fully help u buy i "fixed (to best of my abilities)" your buysignal condition to reflect what you are looking for. So when I run this code I do get buy order but I am not able to figure out exit strategy because I am struggling with same issue with my own strategy lol

Code:
``````def MA = MovAvgExponential()."AvgExp" crosses above MovAvgExponential("length" = 14)."AvgExp";

def buysignal =close is greater than MovAvgExponential()."AvgExp" and ma is true ;``````

what i would suggest is change "def buysignal" to "plot buysignal" so u can see at least what i have provided plots correct buy signals and hopefully someone smarter than i can help you with your exit strategy.

hope this help,

I am not a coder so i cant fully help u buy i "fixed (to best of my abilities)" your buysignal condition to reflect what you are looking for. So when I run this code I do get buy order but I am not able to figure out exit strategy because I am struggling with same issue with my own strategy lol

Code:
``````def MA = MovAvgExponential()."AvgExp" crosses above MovAvgExponential("length" = 14)."AvgExp";

def buysignal =close is greater than MovAvgExponential()."AvgExp" and ma is true ;``````

what i would suggest is change "def buysignal" to "plot buysignal" so u can see at least what i have provided plots correct buy signals and hopefully someone smarter than i can help you with your exit strategy.

hope this help,
Hi

I changed the code but I don't see any buy order

input price = close;
input averagetype = AverageType.expoNENTIAL;
input percentgaingoal = 5;
input percentlossgoal = -2;
input avglength = 9;
input avglength1= 14;

def MA = MovAvgExponential()."AvgExp" crosses above MovAvgExponential("length" = 14)."AvgExp";

def percentchange = 10 * (close - EntryPrice()) / EntryPrice() ;

def buysignal =close is greater than MovAvgExponential()."AvgExp" and ma is true ;
def exitgood = percentchange > percentgaingoal ;
def exitbad = percentchange < percentlossgoal ;

plot movingavgplot = MovingAverage (averagetype, price, avglength>avglength1);

Hi

I changed the code but I don't see any buy order

input price = close;
input averagetype = AverageType.expoNENTIAL;
input percentgaingoal = 5;
input percentlossgoal = -2;
input avglength = 9;
input avglength1= 14;

def MA = MovAvgExponential()."AvgExp" crosses above MovAvgExponential("length" = 14)."AvgExp";

def percentchange = 10 * (close - EntryPrice()) / EntryPrice() ;

def buysignal =close is greater than MovAvgExponential()."AvgExp" and ma is true ;
def exitgood = percentchange > percentgaingoal ;
def exitbad = percentchange < percentlossgoal ;

plot movingavgplot = MovingAverage (averagetype, price, avglength>avglength1);

So i do see an order for TSLA chart for example:

The reason you/we are only seeing one order is that the exit strategy is not working/correct. That is why I was suggesting to change "def Buysignal" to "plot buysignal" so u can see all the times your buy order will trigger in an ideal world when your exit strategy gets fixed. All of the arrows in the image below is when your buysignal will trigger:

87k+ Posts
335 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?