SwingLow StopLoss

_Merch_Man_

Active member
Morning all! Need some help here. I am building a strategy for use on /ES 1000 tick chart. I want my stop loss to be the swinglow at the time of entry. What I am trying to get is "If I went long on this bar then stoploss = swinglow else stoploss = stoploss[1]" but I can't figure out the if condition. I cant use getquantity() to check if I have a position because getQuantity() doesn't work on tick charts. Any ideas? TIA!

Code:
#SWING HIGH LOW CODE
input LookbackPeriod = 3;

def _lowInPeriod1 = Lowest(low, LookbackPeriod);
def marketLow1 = if _lowInPeriod1 < _lowInPeriod1[-LookbackPeriod] then _lowInPeriod1 else _lowInPeriod1[-LookbackPeriod];
def _markedLow1 = low == marketLow1;
rec _lastMarkedLow1 = CompoundValue(1, if IsNaN(_markedLow1) then _lastMarkedLow1[1] else if _markedLow1 then low else _lastMarkedLow1[1], low);
#END SWING HIGH LOW CODE

def swingLow = _lastMarkedLow1 ;

def buyTrigger = close >= close[2] + 1;
def stopLoss = if [WHAT CONDITION GOES HERE?] then swingLow else swingLow[1];

addOrder(OrderType.BUY_TO_OPEN, buyTrigger, open[-1], 1, name = "open");
addOrder(OrderType.SELL_TO_CLOSE, close <= stopLoss, close, 1, name = "close");
 
Well I tried it and it doesn't work. I'm not sure why. You'll see in the screenshot of the strategy report that the swinglow used at the close is different to the swinglow at the open. Idea? Thanks for the assist.

lyCHNP4.png


Code:
#SWING HIGH LOW CODE
input LookbackPeriod = 2;
def _lowInPeriod1 = Lowest(low, LookbackPeriod);
def marketLow1 = if _lowInPeriod1 < _lowInPeriod1[-LookbackPeriod] then _lowInPeriod1 else _lowInPeriod1[-LookbackPeriod];
def _markedLow1 = low == marketLow1;
rec _lastMarkedLow1 = CompoundValue(1, if IsNaN(_markedLow1) then _lastMarkedLow1[1] else if _markedLow1 then low else _lastMarkedLow1[1], low);
#END SWING HIGH LOW CODE

def swingLow = _lastMarkedLow1 ;

def buyTrigger = close >= close[2] +2;
def stopLoss = if buyTrigger then swingLow else swingLow[1];
def sellTrigger = close <= stopLoss;

AddOrder(OrderType.BUY_TO_OPEN, buyTrigger, open[-1], 1, name = "open @ swinglow = " + swingLow);
AddOrder(OrderType.SELL_TO_CLOSE, sellTrigger, close, 1, name = "close @ swinglow = " + swingLow);
 
@rad14733 Thanks for the idea but that didn't work either. The image is with the same stop loss code as above. You can see that even though it opens two orders, both stop out at the active swing low, and not the swing log from the order entry point.

9Bqyzom.png
 
@_Merch_Man_ I'm fairly certain that opening position orders are the only orders that can be incremented over time but exits are triggered for all open positions... Does that answer your question...???
 
I managed to solve this using entryPrice. If entryPrice is not NaN then I know I am in a trade and need to use the same stoploss as the last bar. Obviously only works if you are using max of 1 entry order and in 1 direction only.

Code:
def stopLoss = if buyTrigger then swingLow else if !IsNaN(EntryPrice()) then stopLoss[1] else 0;
 
Last edited:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
218 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top