Post by bmdiaz05 on Apr 28, 2010 9:53:27 GMT -5
hello all,
I am new to the forum and thinkscript altogether. I am trying to write a strategy for what I look for in a trade. However, I do not have experience coding so bare with me. Any help would be greatly appreciated!
What I am trying to do is write a strategy that gives a short signal when the following criteria is met:
1) 5 sma crosses under 20 sma
2) adx crosses above 20 OR adx is already above 20 and increasing
3) macd crosses from positive to negative OR macd is already negative and decreasing
4) rsi crosses below 50 OR rsi is already below 50 and decreasing
The problem I am having is it seems that the strategy will only generate a signal if all of the criteria is met at the same time on the same bar. What I want it to do is generate a signal if all of the criteria is met, but not necessarily at the same time on the same bar. More like if they happen one after another in random orders or once they have all occurred the signal is generated. For instance, right now, as a simplified example, if i get a 5 sma cross under 20 before the adx crosses 20 or begins increasing, it will not generate a signal. How do I write the code so that they dont have to all happen at the same time. For instance, as a simplified example, I want generate a signal if the 5 sma crossed under 20 and 2 bars later the adx crosses above 20 . Therefore it should be like a checklist that once it is all met, whether its at the same time or sequential, it generates a signal.
Here is what I have. Please be kind, I have 0 experience!
# EMA Cross Short Entry
# By Prospectus @ readtheprospectus.wordpress.com
#
# Input Declarations:
#
declare SHORT_ENTRY;
input opentime = 0930;
input closetime = 1600;
input SMA_F = 5;
input SMA_S = 20;
input dmilength = 14;
input adxcrossingType = {default above, below};
input adxthreshold = 20;
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input AverageType = {SMA, default EMA};
input crossingType = {default "Positive to Negative", "Negative to Positive"};
input rsilength = 14;
input rsicrossingType = {default above, below};
input rsithreshold = 50;
# enter during market hours only
def AP = getAggregationPeriod();
def daily = if AP >= aggregationPeriod.DAY then 1 else 0;
def isopen = if daily then 1 else if secondsFromTime(opentime) >= 0 and secondsTillTime(closetime) >= 0 then 1 else 0;
# SMA crossunder
def smashort = if simplemovingAvg(close, SMA_S)[1] < simplemovingavg(close, SMA_F)[1] and simplemovingavg(close, SMA_S) >= simplemovingavg(close, SMA_F) then 1 else 0;
# adx crossover 20 or adx above 20 and increasing
def adxincreasing = if DMI(dmilength).ADX[1] < DMI(dmilength).ADX and DMI(dmilength).ADX >= adxthreshold then 1 else 0;
def adxsignal = if Crossover(adxcrossingType == adxCrossingType.above, DMI(dmilength).ADX > adxthreshold) or adxincreasing then 1 else 0;
# macd positive to negative cross or macd negative and decreasing
def Diff = MACD(fastLength, slowLength, MACDLength, AverageType).Diff;
def macddecreasing = if macd(fastlength, slowlength, macdlength, averagetype).diff < 0 and macd(fastlength, slowlength, macdlength, averagetype).diff[1] > macd(fastlength, slowlength, macdlength, averagetype).diff then 1 else 0;
def macdsignal = if Crossover(crossingType == CrossingType."Negative to Positive", Diff > 0) or macddecreasing then 1 else 0;
#rsi cross below 50 or rsi below 50 and decreasing
def rsidecreasing = if rsiwilder(length = rsilength).RSI[1] > rsiwilder(length = rsilength).rsi and rsiwilder(length = rsilength).RSI < rsithreshold then 1 else 0;
def rsisignal = crossover(rsicrossingType == rsiCrossingType.above, RSIWilder(length=rsilength).RSI > rsithreshold) or rsidecreasing;
#
def trigger = if smashort and isopen and adxsignal and macdsignal and rsisignal then 1 else 0;
def orderprice = close;
#
# This code triggers the strategy on the chart.
# Arg. 1 adds order if true, nothing if false
# Arg. 2 is the entry price
#
addOrder(trigger, orderprice);
#
# Formatting:
#
SetColor(color.red);
THANK YOU FOR ANY HELP AHEAD OF TIME!
I am new to the forum and thinkscript altogether. I am trying to write a strategy for what I look for in a trade. However, I do not have experience coding so bare with me. Any help would be greatly appreciated!
What I am trying to do is write a strategy that gives a short signal when the following criteria is met:
1) 5 sma crosses under 20 sma
2) adx crosses above 20 OR adx is already above 20 and increasing
3) macd crosses from positive to negative OR macd is already negative and decreasing
4) rsi crosses below 50 OR rsi is already below 50 and decreasing
The problem I am having is it seems that the strategy will only generate a signal if all of the criteria is met at the same time on the same bar. What I want it to do is generate a signal if all of the criteria is met, but not necessarily at the same time on the same bar. More like if they happen one after another in random orders or once they have all occurred the signal is generated. For instance, right now, as a simplified example, if i get a 5 sma cross under 20 before the adx crosses 20 or begins increasing, it will not generate a signal. How do I write the code so that they dont have to all happen at the same time. For instance, as a simplified example, I want generate a signal if the 5 sma crossed under 20 and 2 bars later the adx crosses above 20 . Therefore it should be like a checklist that once it is all met, whether its at the same time or sequential, it generates a signal.
Here is what I have. Please be kind, I have 0 experience!
# EMA Cross Short Entry
# By Prospectus @ readtheprospectus.wordpress.com
#
# Input Declarations:
#
declare SHORT_ENTRY;
input opentime = 0930;
input closetime = 1600;
input SMA_F = 5;
input SMA_S = 20;
input dmilength = 14;
input adxcrossingType = {default above, below};
input adxthreshold = 20;
input fastLength = 12;
input slowLength = 26;
input MACDLength = 9;
input AverageType = {SMA, default EMA};
input crossingType = {default "Positive to Negative", "Negative to Positive"};
input rsilength = 14;
input rsicrossingType = {default above, below};
input rsithreshold = 50;
# enter during market hours only
def AP = getAggregationPeriod();
def daily = if AP >= aggregationPeriod.DAY then 1 else 0;
def isopen = if daily then 1 else if secondsFromTime(opentime) >= 0 and secondsTillTime(closetime) >= 0 then 1 else 0;
# SMA crossunder
def smashort = if simplemovingAvg(close, SMA_S)[1] < simplemovingavg(close, SMA_F)[1] and simplemovingavg(close, SMA_S) >= simplemovingavg(close, SMA_F) then 1 else 0;
# adx crossover 20 or adx above 20 and increasing
def adxincreasing = if DMI(dmilength).ADX[1] < DMI(dmilength).ADX and DMI(dmilength).ADX >= adxthreshold then 1 else 0;
def adxsignal = if Crossover(adxcrossingType == adxCrossingType.above, DMI(dmilength).ADX > adxthreshold) or adxincreasing then 1 else 0;
# macd positive to negative cross or macd negative and decreasing
def Diff = MACD(fastLength, slowLength, MACDLength, AverageType).Diff;
def macddecreasing = if macd(fastlength, slowlength, macdlength, averagetype).diff < 0 and macd(fastlength, slowlength, macdlength, averagetype).diff[1] > macd(fastlength, slowlength, macdlength, averagetype).diff then 1 else 0;
def macdsignal = if Crossover(crossingType == CrossingType."Negative to Positive", Diff > 0) or macddecreasing then 1 else 0;
#rsi cross below 50 or rsi below 50 and decreasing
def rsidecreasing = if rsiwilder(length = rsilength).RSI[1] > rsiwilder(length = rsilength).rsi and rsiwilder(length = rsilength).RSI < rsithreshold then 1 else 0;
def rsisignal = crossover(rsicrossingType == rsiCrossingType.above, RSIWilder(length=rsilength).RSI > rsithreshold) or rsidecreasing;
#
def trigger = if smashort and isopen and adxsignal and macdsignal and rsisignal then 1 else 0;
def orderprice = close;
#
# This code triggers the strategy on the chart.
# Arg. 1 adds order if true, nothing if false
# Arg. 2 is the entry price
#
addOrder(trigger, orderprice);
#
# Formatting:
#
SetColor(color.red);
THANK YOU FOR ANY HELP AHEAD OF TIME!