Strategy coding help

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");
 
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 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 tradesize = 10;
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 ;

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");
 
Hi

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

input tradesize = 10;
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 ;

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");
So i do see an order for TSLA chart for example:

bDELu4K.png



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:

zlFpnhn.png
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
335 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

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?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top