Plot futures contract with 6 and 12 mths expiry


Category:
0
0

I have this code so far:

declare lower;

input symbol1 = “/GC”;

plot smb1 = close(symbol1);

This plots the standard gold futures contract. I want to plot specific gold contracts with different expirations, namely 6 months and 12 months out until expiration. Instead of writing out each contract manually, i.e. “/GCU22”, is there a way to automate the process by using a code like this:

declare lower;

def yearv = GetYear() – 2000 + 1

def monthproxy = GetMonth();

#montharray = {F,G,H…. , Z};

if monthproxy == 1 then monthv == F;

if monthproxy == 2 then monthv == G;

etc.#

input symbol1 = “/GC” + monthv + yearv;

plot smb1 = close(symbol1″;

 

The part inside ## is not thinkscript conform as I’m not quite sure how to create that motharray that let’s me pick out the correct month letter for the futures contract.

I basically want to go from this: “/GC” to “/GCM22” automatically by using the getyear and getmonth functions.

So far, I just can’t concat the different variables and the “/GC” string

Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on March 21, 2021 7:49 pm
31 views
1
Private answer

Sorry but the language for Thinkorswim does not provide the tools needed to make something like this work. We cannot create custom sized arrays, and we cannot create variables with data type of string. There are no string parsing methods except for Concat().

The only way to streamline any of this would be to include user inputs for each expiration month and expiration year. Then use those string input values to assemble the various full ticker symbols for each contract month.

Marked as spam
Posted by (Questions: 37, Answers: 4087)
Answered on March 21, 2021 8:03 pm
0
Thanks for this information, Pete!
( at March 22, 2021 12:04 am)