NexusFi: Find Your Edge


Home Menu

 





Thinkscript - Code/Logic Question...


Discussion in ThinkOrSwim

Updated
    1. trending_up 3,258 views
    2. thumb_up 2 thanks given
    3. group 1 followers
    1. forum 4 posts
    2. attach_file 0 attachments




 
Search this Thread

Thinkscript - Code/Logic Question...

  #1 (permalink)
netarchitech
NY, NY
 
Posts: 68 since Dec 2011
Thanks Given: 27
Thanks Received: 19

I've been trying to work with the following scan code. Unfortunately, there appears to be a 3 bar/candle delay built into the logic (see "hlc3[-3]" references)...

Given my limited thinkscripting abilities, I am able to grasp and understand certain basic aspects of the code. However, I am currently unable to make the necessary changes in order to draw the signal on the current or just completed bar/candle. Is it possible?

 
Code
input signalOffsetFactor = 0.20;

def signalOffset = Average(TrueRange(high, close, low), 9) * signalOffsetFactor;

def triggerSell = If(If(close[-1] < high, 1, 0) and (hlc3[-2] < close[-1] or hlc3[-3] < close[-1]), 1, 0);

def triggerBuy = If(If(close[-1] > low, 1, 0) and (hlc3[-2] > close[-1] or hlc3[-3] > close[-1]), 1, 0);

rec buySellSwitch = If(triggerSell, 1, If(triggerBuy, 0, buySellSwitch[1]));

def thirdBarClosed = If(IsNaN(hlc3[-3]), 0, 1);

plot BS_Long = If(triggerBuy and thirdBarClosed and buySellSwitch[1], low - signalOffset, Double.NaN);
     BS_Long.SetStyle(Curve.FIRM);
     BS_Long.SetPaintingStrategy(PaintingStrategy.POINTS);
     BS_Long.SetLineWeight(5);
     BS_Long.AssignValueColor(CreateColor(153, 255, 153));

Thanks in advance for any assistance/guidance in this endeavor. It is certainly appreciated

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
New Micros: Ultra 10-Year & Ultra T-Bond -- Live Now
Treasury Notes and Bonds
Deepmoney LLM
Elite Quantitative GenAI/LLM
Exit Strategy
NinjaTrader
Are there any eval firms that allow you to sink to your …
Traders Hideout
ZombieSqueeze
Platforms and Indicators
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Get funded firms 2023/2024 - Any recommendations or word …
61 thanks
Funded Trader platforms
39 thanks
Battlestations: Show us your trading desks!
26 thanks
NexusFi site changelog and issues/problem reporting
26 thanks
The Program
18 thanks
  #2 (permalink)
 
rmejia's Avatar
 rmejia 
Puerto Rico
 
Experience: Intermediate
Platform: thinkorswim
Broker: TD Ameritrade
Trading: Options
Posts: 379 since Oct 2010
Thanks Given: 3,614
Thanks Received: 441

The 3 candle plot delay comes from the text with the strikethrough, you can delete it or put a pound sign in front if you want to convert it to text to save it.
 
Code
input signalOffsetFactor = 0.20;

def signalOffset = Average(TrueRange(high, close, low), 9) * signalOffsetFactor;

def triggerSell = If(If(close[-1] < high, 1, 0) and (hlc3[-2] < close[-1] or hlc3[-3] < close[-1]), 1, 0);

def triggerBuy = If(If(close[-1] > low, 1, 0) and (hlc3[-2] > close[-1] or hlc3[-3] > close[-1]), 1, 0);

rec buySellSwitch = If(triggerSell, 1, If(triggerBuy, 0, buySellSwitch[1]));

def thirdBarClosed = If(IsNaN(hlc3[-3]), 0, 1);

plot BS_Long = If(triggerBuy and thirdBarClosed and buySellSwitch[1], low - signalOffset, Double.NaN);
     BS_Long.SetStyle(Curve.FIRM);
     BS_Long.SetPaintingStrategy(PaintingStrategy.POINTS);
     BS_Long.SetLineWeight(5);
     BS_Long.AssignValueColor(CreateColor(153, 255, 153));
 
Code
input signalOffsetFactor = 0.20;

def signalOffset = Average(TrueRange(high, close, low), 9) * signalOffsetFactor;

def triggerSell = If(If(close[-1] < high, 1, 0) and (hlc3[-2] < close[-1] or hlc3[-3] < close[-1]), 1, 0);

def triggerBuy = If(If(close[-1] > low, 1, 0) and (hlc3[-2] > close[-1] or hlc3[-3] > close[-1]), 1, 0);

rec buySellSwitch = If(triggerSell, 1, If(triggerBuy, 0, buySellSwitch[1]));

#def thirdBarClosed = If(IsNaN(hlc3[-3]), 0, 1);

#plot BS_Long = If(triggerBuy and thirdBarClosed and buySellSwitch[1], low - signalOffset, Double.NaN);
plot BS_Long = If(triggerBuy and buySellSwitch[1], low - signalOffset, Double.NaN);
     BS_Long.SetStyle(Curve.FIRM);
     BS_Long.SetPaintingStrategy(PaintingStrategy.POINTS);
     BS_Long.SetLineWeight(5);
     BS_Long.AssignValueColor(CreateColor(153, 255, 153));

Reply With Quote
Thanked by:
  #3 (permalink)
netarchitech
NY, NY
 
Posts: 68 since Dec 2011
Thanks Given: 27
Thanks Received: 19


Unfortunately, the commenting out/removing of the "thirdBarClosed" variable and associated references did not affect the 3 bar/candle delay. I think the remaining bold/underlined hlc3[-3] code is also affecting the delay, not to mention the bold/underlined hlc3[-2] code that I think would create a 2 bar/candle delay if the hlc3[-3] code was removed...
 
Code
def triggerSell = If(If(close[-1] < high, 1, 0) and (hlc3[-2] < close[-1] or hlc3[-3] < close[-1]), 1, 0);

def triggerBuy = If(If(close[-1] > low, 1, 0) and (hlc3[-2] > close[-1] or hlc3[-3] > close[-1]), 1, 0);

Now that I'm looking more closely, it looks like the script is written to yield attractive historical results. Worthwhile signals 3 or 2 bars/candles ago are not very helpful now in a scan

Thank you for the swift response, rmejia Your assistance is certainly appreciated...

Reply With Quote
  #4 (permalink)
 
rmejia's Avatar
 rmejia 
Puerto Rico
 
Experience: Intermediate
Platform: thinkorswim
Broker: TD Ameritrade
Trading: Options
Posts: 379 since Oct 2010
Thanks Given: 3,614
Thanks Received: 441

I tried the study as a regular study and it plots a dot (point) when triggered. With the "thirdbarClosed" it waits 2 candles after the signal is activated to draw the dot, without that it draws it after the signal is activated and the candle closes.

The trigger part of the script compares the previous candle [-1] to the 2 previous ones before it [-2] & [-3] and if it is higher or lower than either of those it triggers the signal.

Some studies like the TTM Scalper Alert plot signals with a couple of candle delays; don't know how to make use of that for real time trading.

Reply With Quote
Thanked by:
  #5 (permalink)
netarchitech
NY, NY
 
Posts: 68 since Dec 2011
Thanks Given: 27
Thanks Received: 19

Thanks for the follow-up and additional debugging, as well as providing a clear and concise explanation of the internal mechanics of the script, rmejia I really appreciate it...

I've gone back and tried the script as a regular study, with the "thirdBarClosed" variable and references removed, in order to visualize what you relayed in your last post. Unfortunately, I'm not seeing any signals being placed any earlier than the 3 bar/candle delay

I find it ironic that a script named "TTM Scalper Alert" incorporates delays. I would have thought a "Scalper" tool would be much more fast acting...

Reply With Quote




Last Updated on August 31, 2015


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
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.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts