Simple Moving Average Cross Over with a Stop Loss and Profit Target


Category:
0
0

Hello Pete, I would like to know how to use two moving averages cross over, ( i.e 20 and 50sma)with a stop loss and price target and want to see it is viable to test it in real life. I have the script for a simple buy or sell, but I need a guide to add a stop loss and price target, please.
input ShortMALength=20;
input LongMALength=50;
def ShortMA=simplemovingavg(length=ShortMALength);

def LongMA=simplemovingavg(length=LongMALength);

def buy =ShortMA[1]LongMA;

addOrder(OrderType.Buy_to_Open, buy, name=”Crossoverbuy”, tickColor = Color.DARK_GREEN);

def sell =ShortMA<LongMA;
AddOrder(orderType.Sell_to_Close, sell, name="Crossoverexit", tickColor = Color.DARK_RED);

addOrder(OrderType.BUY_AUTO, no);

Attached file contained the same code above.

RESOLVED
Marked as spam
Posted by (Questions: 2, Answers: 1)
Asked on April 27, 2018 2:20 pm
1621 views
0
Private answer

One screenshot shows the solution. No code is required because all the pieces needed to achieve the stated goals are included with Thinkorswim. But that depends on the method you want to use to compute the profit target and stop loss exit. You can examine the code for each piece and combine them into one. Or just use these as is and save the effort.

I will take the time provide the description listed for each of these, viewable by clicking the question mark icon to the right of each study name.

ProfitTargetLX: …generates a Long Exit signal when the price grows by a certain amount relative to the entry price.

MovAvgTwoLinesStrat: The Moving Average Two Lines strategy is based upon crossovers of fast and slow averages of price. The fast average’s length is significantly less than that of the slow one. By default, crossovers are found for Exponential Moving Averages (EMAs), however, you are free to use other average types: simple, weighted, Wilder’s, or Hull. A simulated Buy order is added when the fast average crosses above the slow one, while the simulated Sell order is shown when it crosses below.

StopLossLX: The StopLossLX strategy generates a Long Exit signal when the price falls by a certain amount relative to the entry price.

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on April 27, 2018 2:57 pm