Fixed r/r based on support and resistance "zones", not sure if it is possible.

Je$ter

New member
First of all, thank you for reading this question. Second, I don't even know if this is possible, so if not, just let me know and I'll be on my way. I am relatively new to Thinkscript, but I am slowly learning. Most of my learning is based on finding scripts that are close to what I'm looking for and then diving into the code to try and make them into something I want. It's working okay so far, but this one is way above my head (if it's even possible).

First, I found a support/resistance zones script in a random discord, and I've removed everything from it that I didn't want, along with modifying a few small things. That code is here:

Code:
declare upper;

input TimeFrame = AggregationPeriod.FIVE_MIN;
input LookbackPeriod = 10;
input HideTimeFrame = yes;
input HideTimeFrame_cloud = no;
##############################################
def H2 = HIGH(period =  TimeFrame);
DEF L2 = LOW(period =  TimeFrame);
def C2 = close (period =  TimeFrame);
def O2 = open (period =  TimeFrame);
#############################################

def _highInPeriod2 = Highest(h2, LookbackPeriod);
def _lowInPeriod2 = Lowest(l2, LookbackPeriod);
#--------------------------------------------------------------
def marketLow2 = if _lowInPeriod2 < _lowInPeriod2[-LookbackPeriod] then _lowInPeriod2 else _lowInPeriod2[-LookbackPeriod];
def _markedLow2 = l2 == marketLow2;

rec _lastMarkedLow2 = CompoundValue(1, if IsNaN(_markedLow2) then _lastMarkedLow2[1] else if _markedLow2 then l2 else _lastMarkedLow2[1], l2);
rec _lastMarkedLow22 = CompoundValue(1, if IsNaN(_markedLow2) then _lastMarkedLow22[1] else if _markedLow2 then Min(o2, c2) else _lastMarkedLow22[1], l2);
#--------------------------------------------------------------
def marketHigh2 = if _highInPeriod2 > _highInPeriod2[-LookbackPeriod] then _highInPeriod2 else _highInPeriod2[-LookbackPeriod];
def _markedHigh2 = h2 == marketHigh2;

rec _lastMarkedHigh2 = CompoundValue(1, if IsNaN(_markedHigh2) then _lastMarkedHigh2[1] else if _markedHigh2 then h2 else _lastMarkedHigh2[1], h2);
rec _lastMarkedHigh22 = CompoundValue(1, if IsNaN(_markedHigh2) then _lastMarkedHigh22[1] else if _markedHigh2 then Max(o2, c2) else _lastMarkedHigh22[1], h2);
#--------------------------------------------------------------
plot Resistance2 = _lastMarkedHigh2;
plot Resistance22 = _lastMarkedHigh22;
plot Support2 = _lastMarkedLow2;
plot Support22 = _lastMarkedLow22;
#--------------------------------------------------------------
Resistance2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Resistance2.SetDefaultColor(Color.PINK);
Resistance2.SetHiding(HideTimeFrame);
Resistance22.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Resistance22.SetDefaultColor(Color.PINK);
Resistance22.SetHiding(HideTimeFrame);
AddCloud(if !HideTimeFrame_cloud  then RESistance2 else Double.NaN, RESistance22 , Color.PINK, Color.PINK);
#--------------------------------------------------------------
Support2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Support2.SetDefaultColor(Color.LIGHT_GREEN);
Support2.SetHiding(HideTimeFrame);
Support22.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Support22.SetDefaultColor(Color.LIGHT_GREEN);
Support22.SetHiding(HideTimeFrame);
AddCloud(if !HideTimeFrame_cloud  then SUPPort2  else Double.NaN, SUPPort22  , Color.LIGHT_GREEN, Color.LIGHT_GREEN);

And that gives you this:
Now, what I am trying to do is combine that one with this one (another one that I found while aimlessly strolling around the internet, I have NOT changed this one at all):

Code:
declare upper;

input TradeAmount = 5000;
input riskAmount = 200;


def Nshares = Floor(tradeAmount / close);
AddLabel(yes, "Shares: " + Nshares, Color.LIME);

def dollarRisk =   riskAmount / (Nshares);

def lastBar = HighestAll(if IsNaN(close) then 0 else BarNumber());

plot RiskStop = if BarNumber() < lastBar then Double.NaN else GetValue(close, BarNumber() - lastBar) - GetValue(dollarRisk, BarNumber() - lastBar);


RiskStop.SetDefaultColor(Color.RED);
RiskStop.SetPaintingStrategy(PaintingStrategy.LINE);
RiskStop.SetLineWeight(1);

So, what that one does is it says "if I spend $5,000 at the current stock price and I'm willing to lose $200 where is my stop and how many shares can I buy" You can change your dollar amounts in the settings. It plots a red line at your stop (circled in the picture), and then says up in the upper left corner how many shares you can buy.

Picture here:
Now, the combination I'm trying to make happen....I want to set it up for "if I buy at the current price, and my stop is the bottom of the most recent support zone how many shares can I buy if my max loss is $200 and I'm willing to spend up to $5,000".... Is this possible?

Again, thanks to anyone and everyone who reads this and has some input and even if you don't have any input or if this isn't possible then hopefully you can at least take those scripts above and use them for your own trading.

Have an awesome Easter weekend everybody, and good luck in the markets. :)
 

Similar threads

Top