Plot distance from moving average in thinkscript?

scott69

scott69

Member
@ProfessorAR15 I use the relationship between two moving averages to do three things. 1) I have the MAs plotted on a chart to show the visual relationship. You can get a lot of info from this just by the way the MAs cross (quickly or gradually) the steepness of the MAs and the relationship of price to the MAs (does price ride along the MAs or criss-cross them). 2) I have up/down arrows plotted where the crosses occur for a visual alert. 3) I also have a very simple script which plots the relationship of the two MAs. This acts as a visual rendering of the relationship of the two MAs. My system uses crosses and price pattern on the one minute chart for entry trigger. The codes provided by the other knowledgeable traders above is a great beginning and should be either what you are looking for or close. Both of the codes above create labels, or my codes can be modified to give sound, labels or bubbles. Just a quick question. If you are using 3 & 9 MAs, are you using Dennis Anderson's E-mini system by any chance? He has written several books and has a blog (I read it every Tuesday - google it) and is VERY successful. He claims to have made $543,000 for the year up to October, and he is not fibbing. I've been following him for years and he swings a big stick and makes some serious money.
 
P

ProfessorAR15

Member
@scott69 Thank you so much for this! I am very familiar with the things you mentioned above. I use the 3/9 cross to determine when short term trends begin/end, but don’t have a system for it. I do have several studies related to the EMA cross though that are very helpful. I will definitely Google him when I get home from work. Thank you again!
 
scott69

scott69

Member
@ProfessorAR15 Please note that Dr. Anderson has multiple accounts and probably has a line of several million and trades multiple contracts in each one of those accounts so results may vary. Set your initial goals for a few hundred a day until you know what you are doing. $200 a day is $50K a year. Not too shabby with a $20,000 or so account (margin on one e-mini contract is $18,000? now) . Trading with a bigger account doesn't mean you are going to make more money, it usually means you are going to lose more money. :) I would also advise starting out with e-micros if you insist on trading index futures ( you will have to qualify to trade futures at TDA). Good luck !!
 
P

ProfessorAR15

Member
So it isn't that you can't learn, you just don't want to... Maybe someone else will write your code for you... It doesn't sound like you've even tried...

As I have stated here previously, I have a problem with members of forums who just take and never give and never try to help themselves... I see far too many members taking advantage of some members here, especially @BenTen... Once @BenTen helps someone, selflessly, people just keep asking him to do more and more... That isn't the way things should work... I used to run a software forum years ago and the rule there was that we only helped those willing to help themselves... Start some code and I'm willing to help but I don't write free code on request/demand... Yet I will share code I write for myself... I'll tell you right now that I'm not the one with a skewed view of things here...
You don’t know me man. Part of me wishes this convo was face to face. Then you wouldn’t be coming at me like you are. I’m no keyboard warrior, I rarely get all worked up via forum posts, but this one struck me the wrong way.

You got me, I definitely am only looking for freebies here, but not Bc that’s who I am, I’m just that new to coding. If I had the weekend off, I would definitely take a stab at coding this, knowing it would be wrong, knowing I’ll need to ask for help. I know it’s gotta be just a few lines. But no, haven’t started.

if you u look at my posts, you won’t see any contributions from me, just asking for help. I don’t have anything i think is of value. I’m all ears if you think I have something I can contribute that would add value.

This is going to floor you I’m sure, but I am an analyst in a few discord’s, where I post plays for members at no charge. My way of giving back. I don’t want anyone’s money. I believe in karma.

and I’m sorry you have a problem with @BenTen and his willingness to help. He is a selfless person, and he sure doesn’t come off as someone who thinks he is being taken advantage of. He is a fantastic person and I owe him a lot even though he has no idea who I am.

Are you some kind of moderator here? It’s odd to have you of all people respond to me and try to put me in my place......

last thing I’ll say is.....you obviously have an opinion from when you used to run a forum, but I don’t think you have any legs to stand on, Bc this isn’t a place you run. To each his own.....
 
BenTen

BenTen

Administrative
Staff
VIP
Let me chime in before this gets out of hand. @ProfessorAR15 originally posted his request as a separate thread, but I moved his post here because of its similarity to the one that @dj45 asked for. @horserider and @markos already posted the code to find the distance between the current price and a moving average. I was hoping their work would help @ProfessorAR15 as he attempts to work on the script himself. I understand the frustration from @rad14733, but I also understand that not everyone has time to code or to learn how to code.

@ProfessorAR15 Here is the code that you need to look at. It was posted by @horserider

Code:
declare lower;

input price = close;
input length = 200;
input AverageType = {default Simple, Exponential};
input pdl = 3;
input ndl = -3;
def MA = MovingAverage(AverageType, ( price[0]), length);
plot DEV = (price / MA - 1 ) * 1000;

plot pdll = pdl;
plot ndll = ndl;
plot zero = 0;

DEV.SetDefaultColor(GetColor(1));

AddLabel(1, "Percentage from " + length + " MA: " + AsPercent(DEV * .01), if DEV > 0 then Color.GREEN else Color.PINK);

A simple moving average has already been added. Now your job is to add another moving average into it. Take a stab at it. We'll be here to help.
 
P

ProfessorAR15

Member
@BenTen Sorry for any trouble I may have caused. I will drop it and move forward.
 
rad14733

rad14733

Well-known member
VIP
@ProfessorAR15 I'm definitely not a "keyboard warrior" by any means... To be honest, I'm not communicating any differently with you than I do with my customers - some of which I have had for over 30 years... And I know some people are more sensitive to it than others... At any rate you got what you wanted from @BenTen... It's all good... I hold no animosities whatsoever... Everyone holds their own world view on things... Let's make some money...!!! Take care...

Edited to add: Posted the same time as @ProfessorAR15
 
G

generic

Member
@ProfessorAR15 Got a little tense in here but here's the code if you're still interested. It'll work for both chart and watchlist.

Code:
input price = close;
input length_1 = 3;
input length_2 = 9;
input AverageType = AverageType.SIMPLE;

def MA_1 = MovingAverage(AverageType, price, length_1);
def MA_2 = MovingAverage(AverageType, price, length_2);

def MA_delta = Round(MA_1 - MA_2, 2);

AddLabel(yes, MA_delta, if MA_delta > 0 then Color.GREEN else Color.RED);
 
P

ProfessorAR15

Member
@generic Yeah I’m sorry for that. Thank you so much for this. I will try to complete what I am doing and share it with the group to try and give back for all the work people have done to help me out! This week would be good for that after all!
 
M

Mordecai

New member
Good day folks,

I have been trying to create a scanner in TOS which highlights current candlesticks that are being squeezed between a 21 & 55 Simple Moving Average

The conditions I seek are:

1. For the 21 SMA to be within 2.5% - 5% above the close price of the candle (green candle) and
2. For the 55 SMA to be within 2.5% - 5% below the open of the candle (green candle).

Ideally these SMAs could be interchanged with a 13 & 50 Exponential Moving Aveage with the same conditions and could be used on multiple time frames but primarily on 30 min time frame option and stock charts.


Below you will find an image of what I'm looking for.
The candle in question is the one circled in red. The yellow line above the candle is the 21 SMA and the pink line below is the 55 SMA



I have tried searching on the Stock Hacker tab under the Scan tab in TOS and also have been searching through various posts here but can't seem to find anything remotely close to what I seek. Being that I am also new to scripting, creating a custom script poses a challenge currently.

I appreciate and look forward to any assistance I may receive with this script and am also open to anyone pointing me in the right direction to something similar that has already been created.


Kind regards,
Mordecai
 
G

generic

Member
@Mordecai 2.5 - 5% of what? The body of the candle? The range of the candle? 2.5-5% of close price is really high and same for below open.
 
M

Mordecai

New member
@Mordecai 2.5 - 5% of what? The body of the candle? The range of the candle? 2.5-5% of close price is really high and same for below open.
I make reference to the options contract .ABBV201127C104, 30 min timeframe (3:30 pm candle) as an example:

The candle has Close = 0.48 and the 21 SMA at that point is approximately = 0.49. By my calculations, the value of the 21 SMA is about 2% above the candles close.
Same with the Open = 0.4 and the value of the 55 SMA at that point is approximately = 0.39. That make the 55 SMA about 2.5% below the open of that candle.
Based on this example, and what I seek is for the SMAs on either side of the candle to be very close, effectively creating a 'squeeze'.

I tried applying @horserider's code above to suit my needs, however, I seem to be encountering an error when I get to the 'if statement'.

The code below I was tinkering with ( with my limited coding abilities) is attempting to, when run as a scan, find stocks/options with current candle (green candle) having a closed price about 2% below the respective 21 SMA value at that point. I would then try to do the same with the 55 SMA but have that below the open of the candle (green candle) then combine the two conditions in a scan.

Thank you for taking the time to respond.

Code:
declare lower;

input price = close;
input length = 21;
input AverageType = {default Simple, Exponential};
input pdl = 6;
input ndl = -6;
def MA = MovingAverage(AverageType, ( price[0]), length);
def PercentDiff = ((MA/price) - 1)* 100;

if PercentDiff <= 5{
plot pdll = pdl;
plot ndll = ndl;
plot zero = 0;
plot PercentDiff;
}
else {double.NaN;}

The candle I made reference to from the contract is on 11/25/20 @3:30 pm. (30 min TF)
 
G

generic

Member
@Mordecai Ah, my mistake I thought you were using this for individual tickers and not for the contracts itself. I'll take a stab at it tmr if nobody else posts.
 
M

Mordecai

New member
@Mordecai Ah, my mistake I thought you were using this for individual tickers and not for the contracts itself. I'll take a stab at it tmr if nobody else posts.
Ideally, I would like to use this on both individual tickers as well as options contracts.

I'm just now seeing the point you made earlier - that based on the underlying price, 2% could end up being quite a distance away from the open & close of the candle especially for tickers with significant prices. In that regard, I might have to rethink my approach in achieving the desired result so it fits right across the board.

Thank you for your consideration and looking forward to hearing from you tomorrow.
 
G

generic

Member
@Mordecai It works for stock scanner but not option scanner, not sure if you can use studies with option hacker since I tested some built-in studies.

Code:
def price = close;
input length1 = 21;
input length2 = 55;
input AverageType1 = AverageType.SIMPLE;
input AverageType2 = AverageType.SIMPLE;
input top_multi = 2.5;
input bot_multi = 2.5;

def o = open;
def c = close;
def MA1 = Round(MovingAverage(AverageType1, price, length1), 2);
def MA2 = Round(MovingAverage(AverageType2, price, length2), 2);
def green = c > o;
def top = Round(c * (1 + top_multi/100), 2);
def bot = Round(o * (1 - bot_multi/100), 2);
def topcondition = MA1 > c and MA1 < top;
def botcondition = MA2 < o and MA2 > bot;

#FOR SCANNER
plot squeeze = green and topcondition and botcondition;

#FOR CHART
#def squeeze = green and topcondition and botcondition;
#AssignPriceColor(if squeeze then color.WHITE else color.CURRENT);
 

Similar threads

Top