Manipulate input variable to account for different expirations on the same futures contract

Lumberjack88

New member
Futures contracts are denoted by the regular futures name like /GC and followed by the month letter, for example M for June, and then followed by the year, for example 22.

input symobl1 = "/GC"

will give me just the regular gold futures contract. If the letters and numbers after /GC constantly change, what can I do to account for that?

I'd like to have something like this: input symbol1 = "/GC" + monthvariable + yearvariable and this should be the same as "/GCM22"

if I define the year as: def yearvariable = GetYear() - 2000 + 1;

and def monthvariable = M;

how can I concat these yearvariable and monthvariable expressions into the symbol1 input so that I don't have to change it manually each time i want to use that plot?
 
@Lumberjack88 It would help if we knew where you want to use this code... Is it for a plot on a chart, in a lower indicator, scan, custom watchlist column, or other...??? If on a chart, are you attempting to plot a different symbol than the chart is displaying...??? Yep, more questions than answers at this point... Regardless, we can use concatenation in certain instances but we need to know how this will be used...
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

I want to plot calendar spreads of gold and silver futures contracts inside the lower section and the chart window will be disabled. For example i'd like to display /GCM22 - /GCU22 and i want to do this with different months and years, too.

Should I further elaborate?
@Lumberjack88 It would help if we knew where you want to use this code... Is it for a plot on a chart, in a lower indicator, scan, custom watchlist column, or other...??? If on a chart, are you attempting to plot a different symbol than the chart is displaying...??? Yep, more questions than answers at this point... Regardless, we can use concatenation in certain instances but we need to know how this will be used...
 
If you want to plot a future/future, eg, aug v dec, you have to plot first delivery date for both to see the number of days and implied interest rate

Are you trying to see if you can borrow dollars in one market and lend into another?
 
If you want to plot a future/future, eg, aug v dec, you have to plot first delivery date for both to see the number of days and implied interest rate

Are you trying to see if you can borrow dollars in one market and lend into another?

I am trying to visualize the relationship between a front and back contract of gold future spreads in a continuous manner, e.g. the inputs should all increase by one month automatically as near term contracts start to expire.

So far; I just need to know how to construct a string like "/GCM22" with M being the monthly variable and 22 being the yearly variable. I have taken care of the plot functions themselves, I just need that symbol1 input to change gradually in string format.

So far I don't think input allows us to concat these 3 parts together and, as far as I know, it is the only way to create a string inside thinkscript..
 
All I need at this point is this:

Code:
def yearv = some number;

def monthv = some letter;

input symbol1 = "/GC" + monthv + yearv;

the input should then be accepted as /GCM22 .

The rest isn't even important. How would I go about constructing that input if I just want to plot /GCM22 on its own, because if that works, the rest will work, too.

I need to construct that input string. That is all. Is it really impossible to do with thinkscript?
 
@Lumberjack88 Perhaps you don't think it's important for us to know how this will be implemented but it might be in reality as far as coding... However, in answer to your question, convert your yearv and monthv def's to input's... Beyond that we'd need more details to be of further help...
 
@Lumberjack88 Perhaps you don't think it's important for us to know how this will be implemented but it might be in reality as far as coding... However, in answer to your question, convert your yearv and monthv def's to input's... Beyond that we'd need more details to be of further help...
I am convinced that the rest of the code will work if I deliver the correct input.

I just need the code to display futures contracts with specific monthly and yearly declarations. If I can get that, I'm good.

So you are saying that if i use inputs instead of defs i can just piece together these strings by a plus sign?

input yearv = GetYear() - 2000 +1;
input monthproxy = GetMonth();

I will use an array to choose between monthly contract letters:

{G,H,J,K.....,Z}

and then by case, if i get 1 month for example, i choose the first entry inside that array. Then I get the letter, but I guess i need to have a variable for this? I shouldn't do it with def?

I will then save the letter to the monthv variable.

and then i just use:

input symbol1 = "/GC" + monthv + yearv;

?
 
@Lumberjack88 I'm not familiar with Futures, just Thinkscript... How you code your symbols is up to your needs... I provided what you asked for so see what you can come up with... If you get errors we can help from there but, as mentioned earlier, you'll need to post your code...
 
@Lumberjack88 I'm not familiar with Futures, just Thinkscript... How you code your symbols is up to your needs... I provided what you asked for so see what you can come up with... If you get errors we can help from there but, as mentioned earlier, you'll need to post your code...

The question is not really about futures, the application just uses futures in the end, but the topic at hand is only about changing inputs by adding variables to them.

Let's just break it down to these few lines of code:
Code:
declare lower;

input symbol1 = "/GC";

plot smb1 = close(symbol1);

These simple lines of codes can plot the /GC price. If I want to have a specific contract plotted, e.g. /GCM22, I can substitute this line:
Code:
input symbol1 = "/GCM22";

This is fine. But I want to automate this process.
Code:
def monthproxy = GetMonth();

def yearv = GetYear()-2000+1;
About the month letters part I'm still not sure: I have these letters that represent January to December contracts:

{F,G,H,J,K,M,N,Q,U,V,X,Z}

If, for example, monthproxy is equal to 3, it should pick the third letter, i.e. H.

Then I would like to add that letter H and the year 22 to the end of "/GC" so that it becomes "/GCM22".

So the end result should look like this: input symbol1 = "/GC" + monthv + yearv;

and if that above function transforms symbol1 into "/GCM22", the problem's already solved. the plot will then display that specific contract based on the GetMonth and GetYear functions, without me manually changing these letters and numbers.

I tried your def to input conversion, i.e.

input monthproxy = GetMonth();

input yearv = GetYear()-2000+1;

Both those inputs are shown as "invalid statements", probably because inputs do not expect functions inside of them. Is there another way around this issue?
 
@Lumberjack88 You're finding, as I have, that using the date related functions seem lacking in the TOS platform... I rarely bother with them because I really don't need them... Perhaps @XeoNoX can assist further as seems to be the resident counter and date-related coding guru... I have too much else on my plate to help further with Date/Time functionality... If I had the time I'd gladly look deeper...
 
@Lumberjack88 You're finding, as I have, that using the date related functions seem lacking in the TOS platform... I rarely bother with them because I really don't need them... Perhaps @XeoNoX can assist further as seems to be the resident counter and date-related coding guru... I have too much else on my plate to help further with Date/Time functionality... If I had the time I'd gladly look deeper...
Thanks for your input, so far!

But let's assume that I have a letter and numbers in the def format that I want to add to an input string. That shouldn't have anything to do with dates and times.

It's basically just going from this "/GC" to this "/GCM22"...
 
@Lumberjack88 If I'm understanding what you're looking for it would be something like the following...

Ruby:
input monthv = {A, B, C, D, E, F, G, default H, I, J, K, L, M};
input yearv = {default 21, 22, 23, 24};
 
@Lumberjack88 If I'm understanding what you're looking for it would be something like the following...

Ruby:
input monthv = {A, B, C, D, E, F, G, default H, I, J, K, L, M};
input yearv = {default 21, 22, 23, 24};

I tried your code:

Code:
declare lower;

input monthv = {F,G,H,J,default K,M,N,Q,U,V,X,Z };

input yearv = {default "21", "22", "23", "24","25"};

input symbol1 = "/GC" + monthv + yearv;

plot smb1 = close(user1);

I had to use "" quotes around the yearv values, as I assume input only accepts strings and not numbers...

I couldn't piece them together in the end, with the simple + sign. I tried the Concat function but that didn't work either.

Apart from that, how would I make sure that I can pick out, let's say the letter N, if GetMonth gets me a 7 and N represents the 7th element in that list?
 
In tos' thinkscript room, I think alphainvestor, or another regular, posted something similar they did for vix, near month versus maybe 3 month out or so. You may want to look there

You haven't said what you're doing this for, but in a deliverable versus non-deliverable futures contract, there are some nuances. In a deliverable, there are price anomalies around first notice day until a few days after first delivery date as large specs roll out their positions. These anomalies are mostly interest rate arbs and large banks that can take/make physical delivery flatten these curves when the price differentials exceed their funding costs, by some preset amount because these transactions affect their balance sheet.
 
In tos' thinkscript room, I think alphainvestor, or another regular, posted something similar they did for vix, near month versus maybe 3 month out or so. You may want to look there

You haven't said what you're doing this for, but in a deliverable versus non-deliverable futures contract, there are some nuances. In a deliverable, there are price anomalies around first notice day until a few days after first delivery date as large specs roll out their positions. These anomalies are mostly interest rate arbs and large banks that can take/make physical delivery flatten these curves when the price differentials exceed their funding costs, by some preset amount because these transactions affect their balance sheet.

I looked at the script and he does not manipulate the input parameters. I want to display the difference between the same futures commodity with different expirations. But that's beside the point actually. What I need currently is just a piece of code that allows me to do this here:

"/GC" into "/GCM22"

That's it. If I get that, I'm happy. So far, thinkscript won't allow me to piece together the input variable of a futures contract and I do not want to adjust it by hand every time, which is why I want it automated.

Again, the real purpose behind it is of far lesser importance than many assume. If I'm able to transform that "/GC" line into "/GCM22" I'm done.

M needs to be calculated via GetMonth() and 22 can be obtained by GetYear and that's about it. It's just so onerous to deal with the input strings as I can't piece them together correctly...
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
372 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top