THINKorSWIM Scripting - futures io
futures io futures trading



THINKorSWIM Scripting


Discussion in ThinkOrSwim

Updated by kjhosken
    1. trending_up 292 views
    2. thumb_up 2 thanks given
    3. group 2 followers
    1. forum 8 replies
    2. attach_file 3 attachments




Welcome to futures io: the largest futures trading community on the planet, with well over 100,000 members
  • Genuine reviews from real traders, not fake reviews from stealth vendors
  • Quality education from leading professional traders
  • We are a friendly, helpful, and positive community
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts
  • We are here to help, just let us know what you need
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.

-- Big Mike, Site Administrator

(If you already have an account, login at the top of the page)

 
Thread Tools Search this Thread
 

THINKorSWIM Scripting

  #1 (permalink)
ROY, Utah
 
 
Posts: 33 since Apr 2020
Thanks: 3 given, 5 received

THINKorSWIM Scripting

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
Register to download File Type: pdf ThinkorSwim Strategy.pdf (240.1 KB, 18 views)
Reply With Quote

Can you help answer these questions
from other members on futures io?
Convert NinjaTrader NT7 indicator to Tradestation EasyLanguage and RADARSCREEN
Platforms and Indicators
Issue meeting order entry conditions
Elite Automated NinjaTrader Trading
Last 5 minutes Label
ThinkOrSwim
Moving average with Lag reduction
NinjaTrader
Linux?
ThinkOrSwim
 
 
  #2 (permalink)
Seattle, WA/USA
 
Experience: Intermediate
Platform: TOS, TS
Trading: Forex, crude
 
Posts: 76 since Sep 2016
Thanks: 7 given, 26 received

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
The following user says Thank You to kjhosken for this post:
 
  #3 (permalink)
ROY, Utah
 
 
Posts: 33 since Apr 2020
Thanks: 3 given, 5 received


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

Reply With Quote
 
  #4 (permalink)
Seattle, WA/USA
 
Experience: Intermediate
Platform: TOS, TS
Trading: Forex, crude
 
Posts: 76 since Sep 2016
Thanks: 7 given, 26 received

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
The following user says Thank You to kjhosken for this post:
 
  #5 (permalink)
ROY, Utah
 
 
Posts: 33 since Apr 2020
Thanks: 3 given, 5 received

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
Register to download File Type: pdf Custom TOS MACD.pdf (364.5 KB, 3 views)

Last edited by StockT8er; May 22nd, 2020 at 07:11 AM.
Reply With Quote
 
  #6 (permalink)
ROY, Utah
 
 
Posts: 33 since Apr 2020
Thanks: 3 given, 5 received

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;

Reply With Quote
 
  #7 (permalink)
Seattle, WA/USA
 
Experience: Intermediate
Platform: TOS, TS
Trading: Forex, crude
 
Posts: 76 since Sep 2016
Thanks: 7 given, 26 received


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)
ROY, Utah
 
 
Posts: 33 since Apr 2020
Thanks: 3 given, 5 received

I am trying to work out the above scenario

Reply With Quote
 
  #9 (permalink)
Seattle, WA/USA
 
Experience: Intermediate
Platform: TOS, TS
Trading: Forex, crude
 
Posts: 76 since Sep 2016
Thanks: 7 given, 26 received

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



futures io Trading Community Platforms and Indicators ThinkOrSwim > THINKorSWIM Scripting




Upcoming Webinars and Events
 

Understanding Order Flow Stop Runs & Icebergs w/Bookmap

May 28
 

Free BloodHound Licenses to everyone!

June
     



Copyright © 2020 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@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