Exit at close for trailing stop strategy - ThinkOrSwim Programming | futures.io
futures.io futures trading

Go Back   futures.io

> Futures Trading, News, Charts and Platforms > Platforms and Indicators > ThinkOrSwim > ThinkOrSwim Programming

Exit at close for trailing stop strategy
Started:July 27th, 2013 (04:56 AM) by skorpyo Views / Replies:1,646 / 0
Last Reply:July 27th, 2013 (04:56 AM) Attachments:0

Welcome to futures.io.

Welcome, Guest!

This forum was established to help traders (especially futures traders) by openly sharing indicators, strategies, methods, trading journals and discussing the psychology of trading.

We are fundamentally different than most other trading forums:
  • We work extremely hard to keep things positive on our forums.
  • We do not tolerate rude behavior, trolling, or vendor advertising in posts.
  • We firmly believe in openness and encourage sharing. The holy grail is within you, it is not something tangible you can download.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.

You'll need to register in order to view the content of the threads and start contributing to our community. It's free and simple, and we will never resell your private information.

-- Big Mike

Thread Tools Search this Thread

Exit at close for trailing stop strategy

Old July 27th, 2013, 04:56 AM   #1 (permalink)
Trading Apprentice
Bucharest + Romania
Futures Experience: Advanced
Platform: Thinkorswim, NinjaTrader, Metatrader
Favorite Futures: Options, Forex
Posts: 2 since Jul 2013
Thanks: 0 given, 0 received

Exit at close for trailing stop strategy

Hi guys,

I'm having an issue with a strategy here. I'm using a volatility based trailing stop similar to the chandelier stop. While backtesting i saw that the strategy records entering and exiting at the calculated stop price instead of the closed candle price.

In other words although the strategy waits for the candle to close before exiting and reversing, it records doing so at the stop price and not the actual closed candle price.

Where am i going wrong with the code here?

Thanks guys.

# Input Declarations:
input mult = 3.0; #Multiple of Average True Range to use
input length = 10; #Average True Range period
# Input to select Close or High/Low for switching:
input Style = {default Close, HighLow};
def type;
switch (Style){
case Close:
type = 1;
case HighLow:
type = 0;
# Set parameters based on switch selection:
def uu = if type then close else high;
def ll = if type then close else low;
def na = Double.NaN;
# Calculate average range for volatility:
def atr = ExpAverage(high - low, length);
# Calculate initial short and long volatility stop values:
def svs = low + Ceil(mult * atr / TickSize()) * TickSize();
def lvs = high - Ceil(mult * atr / TickSize()) * TickSize();
# Set up the trailing stop logic:
rec shortvs = if IsNaN(shortvs[1]) then svs else if uu > shortvs[1] then svs else Min(svs, shortvs[1]);
rec longvs = if IsNaN(longvs[1]) then lvs else if ll < longvs[1] then lvs else Max(lvs, longvs[1]);
# Detect if we breached the trailing stop (close or high/low):
def longswitch = if uu >= shortvs[1] and uu[1] < shortvs[1] then 1 else 0;
def shortswitch = if ll <= longvs[1] and ll[1] > longvs[1] then 1 else 0;
# This logic remembers the direction and changes when needed:
rec direction = CompoundValue(1, if IsNaN(direction[1]) then 0 else
if direction[1] <= 0 and longswitch then 1
else if direction[1] >= 0 and shortswitch then -1
else direction[1], 0);

# Set up the two plots. "na" makes the current value disappear
# if we're trading in the opposite direction:
def short = if direction > 0 then na else shortvs;
def long = if direction < 0 then na else longvs;

def stopout = if direction[1] < 0 and direction > 0 then 1 else 0;
def stopprice = shortvs[1];
AddOrder(OrderType.BUY_TO_CLOSE, stopout, stopprice);

I also have another separate script for the opposite side running them both at the same time.

def stopout = if direction[1] > 0 and direction < 0 then 1 else 0;
def stopprice = longvs[1];
AddOrder(OrderType.SELL_TO_CLOSE, stopout, stopprice);

Reply With Quote


futures.io > Futures Trading, News, Charts and Platforms > Platforms and Indicators > ThinkOrSwim > ThinkOrSwim Programming > Exit at close for trailing stop strategy

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Upcoming Webinars and Events (4:30PM ET unless noted)

NinjaTrader 8: Programming Profitable Trading Edges w/Scott Hodson

Elite only

Anthony Drager: Executing on Intermarket Correlations & Order Flow, Part 2

Elite only

Adam Grimes: Five critically important keys to professional trading

Elite only

Machine Learning Concepts w/FIO member NJAMC

Elite only

MarketDelta Cloud Platform: Announcing new mobile features

Dec 1

NinjaTrader 8: Features and Enhancements

Dec 6

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using SuperTrend for trailing stop in a strategy - help needed DarrenH NinjaTrader Programming 13 June 25th, 2014 07:07 PM
Simple trailing stop strategy jeemba EasyLanguage Programming 2 August 2nd, 2013 07:05 AM
Auto Trading Strategy - Exit On Close Price error TheGaryGuy NinjaTrader 3 January 23rd, 2013 08:00 AM
Trailing stop indicator Buscador The Elite Circle 3 November 15th, 2012 12:57 PM
Strategy Disables When Trailing Stop Filled graygoat NinjaTrader Programming 2 September 27th, 2012 05:16 PM

All times are GMT -4. The time now is 07:09 PM.

Copyright © 2016 by futures.io. All information is for educational use only and is not investment advice.
There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
no new posts

Page generated 2016-10-27 in 0.06 seconds with 19 queries on phoenix via your IP