Crossing Moving Averages Strategy


Category:
0
0

I thought it would be easy to take the “MovingAvgCrossover” study and convert it to a strategy, but I wasn’t able to.

What I really want is to have a strategy that buys when theres a bullish sma crossover, and reverses position on a bearish crossover.

Thanks Pete!

Marked as spam
Posted by (Questions: 10, Answers: 14)
Asked on March 4, 2017 11:00 am
354 views
1
Private answer

Boy this is an easy one. Thanks. This strategy is already included with Thinkorswim. It is called “MovAvgTwoLinesStrat”. See screen shot showing how to locate it and a brief description.

As for trying to convert the study into a strategy. You said you tried and were not able to do it. If you want some help with that, just for the sake of learning, I would be very happy to assist. Just post your code representing your best attempt and I’ll debug it for you and explain where you went off course.

Otherwise, consider this a done deal. Don’t forget to up-vote any answers that best solve your question!

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on March 4, 2017 12:07 pm
0
Got it to work! Thank you! Well almost… it doesnt reverse the position. How would I get it to do that?
( at March 4, 2017 2:23 pm)
0

I just added this strategy to my chart and it does reverse the position. This strategy goes from long to short and back to short, always in a position. Did you meaning something else?

( at March 4, 2017 4:46 pm)
0

oh i guess it does! haha thanks

( at March 4, 2017 5:50 pm)
0
Private answer

This is what I did but it doesn’t work

 

#
# TD Ameritrade IP Company, Inc. (c) 2009-2017
#

#wizard input: length1
#wizard text: -period
#wizard input: averageType1
#wizard text: crosses
#wizard input: crossingType
#wizard input: length2
#wizard text: -period
#wizard input: averageType2
#wizard text: Price:
#wizard input: price

input price = close;
input length1 = 15;
input length2 = 30;
input averageType1 = AverageType.Simple;
input averageType2 = AverageType.Simple;
input crossingType = {default above, below};

def avg1 = MovingAverage(averageType1, price, length1);
def avg2 = MovingAverage(averageType2, price, length2);

#deleted plot and added def to this line
def signal = crosses(avg1, avg2, crossingType == CrossingType.above);
def signaldown = crosses(avg1, avg2, crossingType == CrossingType.below);

#didnt think this stuff was necessary
#signal.DefineColor(“Below”, GetColor(7));
#signal.AssignValueColor(if crossingType == CrossingType.above then #signal.color(“Above”) else signal.color(“Below”));

#signal.SetPaintingStrategy(if crossingType == CrossingType.above
# then PaintingStrategy.BOOLEAN_ARROW_UP
# else PaintingStrategy.BOOLEAN_ARROW_DOWN);
addOrder(OrderType.BUY_AUTO, signal);
#added this line to sell
addOrder(OrderType.SELL_AUTO, signal);

Marked as spam
Posted by (Questions: 10, Answers: 14)
Answered on March 4, 2017 1:09 pm
0

without going into much detail, I immediately found you are using the same condition for buy and for sell. the variable named ”signal” is used for both. That will never work. The buy and sell conditions that you use in your AddOrder() statements need to be completely different.

( at March 4, 2017 3:12 pm)