NexusFi: Find Your Edge


Home Menu

 





THINKorSWIM Scripting


Discussion in ThinkOrSwim

Updated
      Top Posters
    1. looks_one StockT8er with 19 posts (0 thanks)
    2. looks_two kjhosken with 6 posts (3 thanks)
    3. looks_3 Skidboot with 1 posts (1 thanks)
    4. looks_4 masterelf1 with 1 posts (0 thanks)
    1. trending_up 6,459 views
    2. thumb_up 4 thanks given
    3. group 5 followers
    1. forum 24 posts
    2. attach_file 4 attachments




 
Search this Thread

THINKorSWIM Scripting

  #1 (permalink)
 StockT8er 
ROY, Utah
 
Experience: Intermediate
Platform: TradeStation
Posts: 81 since Apr 2020
Thanks Given: 11
Thanks Received: 6

I am trying to code a strategy in ThinkorSwim to Buy when the MACD Down Bar turns from Red to DarkRed and the RSI < 45, Then Sell when the Up Bar turn from Green to Dark Green and RSI > 65
This is my coding so far

input fastLength = 12;
input slowLength = 26;
input macdLength = 9;
input averageType = AverageType.EXPONENTIAL;
input price = close;
input length = 14;
input over_bought = 70;
input over_sold = 30;
input rsiAverageType = AverageType.WILDERS;

def rsi = reference RSI(price = price, length = length, averageType = rsiAverageType);

def diff = reference MACD(fastLength, slowLength, macdLength, averageType).Diff;

input RSIBuy = 45;
input RSISell = 65;

UpSignal.SetHiding(!showBreakoutSignals);
DownSignal.SetHiding(!showBreakoutSignals);

Value.SetDefaultColor(GetColor(1));
Avg.SetDefaultColor(GetColor(8));
Diff.SetDefaultColor(GetColor(5));
Diff.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Diff.SetLineWeight(3);
Diff.DefineColor("Positive and Up", Color.GREEN);
Diff.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff.DefineColor("Negative and Down", Color.RED);
Diff.DefineColor("Negative and Up", Color.DARK_RED);
Diff.AssignValueColor(if Diff >= 0 then if Diff > Diff[1] then Diff.color("Positive and Up") else Diff.color("Positive and Down") else if Diff < Diff[1] then Diff.color("Negative and Down") else Diff.color("Negative and Up"));
ZeroLine.SetDefaultColor(GetColor(0));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);

def buy = diff < diff[1] and rsi > RSIBuy

AddOrder(OrderType.SELL_TO_CLOSE, buy, name = "BUY_LE");

def sell = diff > diff[1] and rsi > RSISell;

AddOrder(OrderType.SELL_TO_CLOSE, sell, name = "SELL_SE");

It's not liking the def lines how do I fix it
See Attached for a diagram of what I want to do

Attached Files
Elite Membership required to download: ThinkorSwim Strategy.pdf
Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
My NT8 Volume Profile Split by Asian/Euro/Open
NinjaTrader
Build trailing stop for micro index(s)
Psychology and Money Management
ZombieSqueeze
Platforms and Indicators
The space time continuum and the dynamics of a financial …
Emini and Emicro Index
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
 
  #2 (permalink)
 kjhosken 
Seattle, WA/USA
 
Experience: Intermediate
Platform: TOS, TS
Trading: Forex, crude
Posts: 96 since Sep 2016
Thanks Given: 7
Thanks Received: 35

There were a couple issues, one in the order syntax and there was a missing ';' and since there are no plots called all the plotting information irritated the compiler. I reworked the code a little bit because TOS hates it when you reference several other studies in a single script and tossed in my usual positioning code snipet. Enjoy
 
Code
input fastLength = 12;
input slowLength = 26;
input macdLength = 9;
input averageType = AverageType.EXPONENTIAL;
input price = close;
input length = 14;
input over_Bought = 70;
input over_Sold = 30;
input rsiaverageType = AverageType.WILDERS;
input showBreakoutSignals = no;

def NetChgAvg = MovingAverage(rsiaverageType, price - price[1], length);
def TotChgAvg = MovingAverage(rsiaverageType, AbsValue(price - price[1]), length);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;

def RSI = 50 * (ChgRatio + 1);

input RSIBuy = 45;
input RSISell = 65;

def Value = MovingAverage(averageType, close, fastLength) - MovingAverage(averageType, close, slowLength);
def Avg = MovingAverage(averageType, Value, MACDLength);

def Diff = Value - Avg;
def buy = diff > diff[1] and RSI > RSIBuy and diff < 0;

def sell = diff < diff[1] and RSI > RSISell and diff > 0;

### begin positioning###
input mode = {default reverse, normal, other};

def bullish;
def bearish;
def entryPriceS;
def entrypriceL;

switch (mode) { 
case reverse: 
    bearish = buy;
    bullish = sell;
    entryPriceS = open[-1]-ticksize();
    entrypriceL = open[-1]+ticksize();
case normal:
    bullish = buy;
    bearish =sell ;
    entryPriceS = close-ticksize();
    entrypriceL = close+ticksize();
case other:
    bearish = 4;
    bullish = 4;
    entryPriceS = 2;
    entrypriceL = 0;
}
;

def signalUP = bullish and !bullish[1];
def signalDN =  bearish and !bearish[1];
def LX = signalDN;
def SX = signalUP;
input usetimefilter = yes;

#positioning
input rthopen = 0400;
input rthclose = 2000;
def RTH = if usetimefilter == yes then if SecondsFromTime(rthopen) >= 0 and
               SecondstillTime(rthclose) >= 0
            then 1
            else 0 else 1;

AddOrder(OrderType.buy_AUTO,rth == 1 and signalup, entrypriceL, tickcolor = GetColor(6), arrowcolor = GetColor(4), name = "LE");
AddOrder(OrderType.SELL_AUTO,rth == 1 and signaldn, entrypriceS, tickcolor = GetColor(6), arrowcolor = GetColor(5), name = "SE");

AddOrder(OrderType.BUY_TO_CLOSE, rth == 0, tickcolor = GetColor(4), arrowcolor = GetColor(3), name = "EODExit");
AddOrder(OrderType.SELL_TO_CLOSE, rth == 0, tickcolor = GetColor(4), arrowcolor = GetColor(3), name = "EODExit");

AddOrder(OrderType.BUY_TO_CLOSE, rth == 1 and SX, tickcolor = GetColor(4), arrowcolor = GetColor(3), name = "SX");
AddOrder(OrderType.SELL_TO_CLOSE, rth == 1 and LX, tickcolor = GetColor(4), arrowcolor = GetColor(3), name = "LX");
#end of code

Follow me on Twitter Reply With Quote
Thanked by:
  #3 (permalink)
 StockT8er 
ROY, Utah
 
Experience: Intermediate
Platform: TradeStation
Posts: 81 since Apr 2020
Thanks Given: 11
Thanks Received: 6


Is there anyway to get it to trigger at very time of condition, or does it always go to next bar

Started this thread Reply With Quote
  #4 (permalink)
 kjhosken 
Seattle, WA/USA
 
Experience: Intermediate
Platform: TOS, TS
Trading: Forex, crude
Posts: 96 since Sep 2016
Thanks Given: 7
Thanks Received: 35

Change 'open[-1]' to 'close'. The reason I do this is because you don't know that it will be a signal until the bar actually closes, therefore the absolute earliest you can get in is the open of the next bar (open[-1]).


StockT8er View Post
Is there anyway to get it to trigger at very time of condition, or does it always go to next bar


Follow me on Twitter Reply With Quote
Thanked by:
  #5 (permalink)
 StockT8er 
ROY, Utah
 
Experience: Intermediate
Platform: TradeStation
Posts: 81 since Apr 2020
Thanks Given: 11
Thanks Received: 6

The attached doc's explain what I am trying to accomplish on the Custom TOS MACD
After thinking about it be nice to trigger on bar sooner on the last GREEN OR RED

(pasted screenshot)

Attached Files
Elite Membership required to download: Custom TOS MACD.pdf
Started this thread Reply With Quote
  #6 (permalink)
 StockT8er 
ROY, Utah
 
Experience: Intermediate
Platform: TradeStation
Posts: 81 since Apr 2020
Thanks Given: 11
Thanks Received: 6

How would I correct this to work

def colorB = if isLastBar = (Color.GREEN) and color.CURRENT = (Color.DARK_GREEN) then 1 else 0;
def colorS = if isLastBar = (Color.RED) and color.CURRENT = (Color.DARK_RED) then 1 else 0;

Started this thread Reply With Quote
  #7 (permalink)
 kjhosken 
Seattle, WA/USA
 
Experience: Intermediate
Platform: TOS, TS
Trading: Forex, crude
Posts: 96 since Sep 2016
Thanks Given: 7
Thanks Received: 35


StockT8er View Post
How would I correct this to work

def colorB = if isLastBar = (Color.GREEN) and color.CURRENT = (Color.DARK_GREEN) then 1 else 0;
def colorS = if isLastBar = (Color.RED) and color.CURRENT = (Color.DARK_RED) then 1 else 0;

without knowing what you are trying to do a '==' would be used to find where a condition is true/false

Follow me on Twitter Reply With Quote
  #8 (permalink)
 StockT8er 
ROY, Utah
 
Experience: Intermediate
Platform: TradeStation
Posts: 81 since Apr 2020
Thanks Given: 11
Thanks Received: 6

I am trying to work out the above scenario

Started this thread Reply With Quote
  #9 (permalink)
 kjhosken 
Seattle, WA/USA
 
Experience: Intermediate
Platform: TOS, TS
Trading: Forex, crude
Posts: 96 since Sep 2016
Thanks Given: 7
Thanks Received: 35

Look at the line that assigns the color values. Diff > 0 and Diff > diff[1] is bright green; Diff > 0 and Diff< diff[1] is dark green. So
 
Code
def condition1 = if (Diff > 0 and Diff< diff[1]) then 1 else 0;
The inverse would be your other condition.

Follow me on Twitter Reply With Quote
  #10 (permalink)
 StockT8er 
ROY, Utah
 
Experience: Intermediate
Platform: TradeStation
Posts: 81 since Apr 2020
Thanks Given: 11
Thanks Received: 6


Question Can we get a strategy to put a label on the chart the P/L of a strategy, so we know if a stock is profitable without opening the report box?

Started this thread Reply With Quote




Last Updated on August 7, 2020


© 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