How to write over a thinkscript variable?

tommytx

New member
VIP
Can anyone help me out with this one simple piece of thinkscript code..

Code:
input positionsize = 5000;
plot sma = SimpleMovingAvg(length = 200);
def cnt = 1;
def buy = (close > sma and RSI() < 30 and cnt);
def sell = RSI() > 80;
AddOrder(OrderType.BUY_TO_OPEN, buy, close);
AddOrder(OrderType.SELL_TO_CLOSE, sell, close);

cnt = 0;

The problem is this last line of code.. it is not allowing me to write over this variable since i have defined it in the 3rd line above..
What i am trying to do is prevent more than one buy... In other words once i buy the first time it will not buy again until the cnt variable is changed.. To allow a new buy...

But the cnt = 0 is not passing the smell test and its telling me that once i have defined a variable i am stuck with it... I have seen something like cnt += 0; or maybe cnt := 0; and that allowed a write over but i have searched near and far and cannot find anything that works....

The first time thur i set the cnt to 1 to allow a buy but once the buy is done i can find no way to shut off buy till i am ready to order the next buy... Any assist will be greatly appreciated.. Thanks
 
Last edited by a moderator:
Instead of manually managing a cnt variable to block repeated buys, the best and most reliable way to control single entries in ThinkOrSwim is to use its built-in Global Strategy Settings.

Here’s how to do it:
  1. Open the Edit Studies and Strategies window
  2. Click the “Global Strategy Settings” button in the lower-left corner
  3. In the Position limits section:
    • Set “Allow up to [ 1 ] entry order(s) in the same direction”
    • Select the option: “Regardless of the entry that generated the order”
This ensures your strategy will only place one active position at a time in any in any given direction. Once that position is closed, it will allow a new one. Hope this helps


.
 
@tommytx You're close... The easiest way to detect whether your Strategy has entered a trade or not is by utilizing the EntryPrice() function... You use a logic structure like the following... If EntryPrice() is true then 1 else Double.NaN is the logic... When cnt = 1 then the Strategy is in a trade...

Ruby:
def cnt = if EntryPrice() then 1 else Double.NaN;
 
Instead of manually managing a cnt variable to block repeated buys, the best and most reliable way to control single entries in ThinkOrSwim is to use its built-in Global Strategy Settings.

Here’s how to do it:
  1. Open the Edit Studies and Strategies window
  2. Click the “Global Strategy Settings” button in the lower-left corner
  3. In the Position limits section:
    • Set “Allow up to [ 1 ] entry order(s) in the same direction”
    • Select the option: “Regardless of the entry that generated the order”
This ensures your strategy will only place one active position at a time in any in any given direction. Once that position is closed, it will allow a new one. Hope this helps


.
Thank you so much for your advice as i have learned a lot of stuff that i did not know and will help me in the future.. However it won't work for this problem... and here is why..
First I am aware of the Global setting that prevents more that one buy in the same direction and it was already set to 1... But i must close the buy immediately and i will explain more..
First this is just a dry run for the larger project I am working on.. as the big final plan is to to the following...
1. Allow no buying or selling until a crossover at the RSI 30 occurs then i plan to buy one share on each and every 1 dollar drop below the 30 so first buy is 30 second buy is 31 then 31 etc.. until it crosses above the 30 on its way up.. then all buy and sell is shut down until it reaches a point like oversold area then all the accumulate 1 shares will be sold at one shot...but it will buy and not sell all the way down till it turns around and crosses back over the 30.. then just sits on them till it eventually reaches oversold... and dumps.. So i must let it close to get the single shares as it goes down.. ideally i can use the RSI to turn the buy off and on for each dollar dopr. I will be monitoring the stock price to buy 1 share at a time 1 dollar at a time.. so 30,31,32 all the way down then same on the way back up till it crosses the RSI thirty on the way back up... then it will just sit there doing nothing till oversold day. Make sense.? I loved your idea of using the purchase to tell me a trade has been made but i must close to get ready for the next sale which will be 1 dollar drop away. I tried everything I could think of to get your idea to work but just would not allow any buys at all...I tried you one line entry before and after the buy but it never allowed any buys at all. So it's obvious you are way further along than I am and if you can help me work this out i would appreciate it...
So what I have to do is monitor the stock price or the rsi and make a single 1 share buy for each dollar the price drops below the RSI 30 buying on the way down and back up and shutting down the buying with NO selling the entire time.. until it finally reaches as high as it can go.. maybe the oversold on rsi and then dump all the accumulated purchases...at once.. And i may even set an alert to let me know when its on the way back up and i may decide to do a manual sell at what ever point i think is best .. But still want it automated to dump at oversold if I am not available to personally monitor the huge package sale.. and i can intervene if I like to see if it might go even higher than oversold..
Bless you for all your help.. as I have a ton of ideas that i am playing with but just need more knowledge and you have been super helpful there. Just let me know if you are interested in helping with some of the other projects I am working on... The 1 share is just a beginner.. it could be upscaled to big time if the idea pans out.. Also i may only buy on the way down and not on the way back up to the 30.. but that can be changed based on what the results are... It would absolutely dip the very bottom for sure.. and that is always good to find the absolute bottom and this would do it bigtime..
So bottom line is i must control shutting down the buy not the close... hopefully that makes sense..
Thanks again for your help.
 
Last edited:
@tommytx

You are aware that Strategies are just for testing strategy concepts and don't make real trades, right...???

I just want to make sure before going further... Also, Strategies don't perform like real time live trading... Strategies only trigger after a candle/bar closes and this will radically skew those results from live trades...

Using the PaperTrading feature, not OnDemand, to practice would be a better option...

Now, if you are contemplating using Conditional Orders that's another path to go down...
 
@tommytx You're close... The easiest way to detect whether your Strategy has entered a trade or not is by utilizing the EntryPrice() function... You use a logic structure like the following... If EntryPrice() is true then 1 else Double.NaN is the logic... When cnt = 1 then the Strategy is in a trade...

Ruby:
def cnt = if EntryPrice() then 1 else Double.NaN;
I placed your code in many locations and none would allow even 1 sale...since the cnt must be set to 1 to get a buy to even log an entry price...
So i even tried this to replace my initial def cnt = 1;
def cnt = if EntryPrice() then 1 else Double.Nan since cnt is not yet 1 so it cannot buy i changed to this.
def cnt = If ! EntryPrice() then 1 else Double.Nan no help.. Still won't buy.

Oh by the way my code can be run in the Strategy tester just as it is.. and it will then show a ton of sales
so you can test it to understand what i mean..About your code not allowing even 1 sale..
Then i tried using def cnt = 1; before the buy just as it was and placed this after the buy
cnt = if Entryprice() then 0 else Double.Nan this was an attempt to change cnt to 0 so that on the
next loop it would not buy .. but nothing even allowed a single buy...
Does thinkscript understand what a doubl.nan is.. i see you are using Ruby?
 
@tommytx

You are aware that Strategies are just for testing strategy concepts and don't make real trades, right...???

I just want to make sure before going further... Also, Strategies don't perform like real time live trading... Strategies only trigger after a candle/bar closes and this will radically skew those results from live trades...

Using the PaperTrading feature, not OnDemand, to practice would be a better option...

Now, if you are contemplating using Conditional Orders that's another path to go down...
Yes thanks.. i AM FULLY aware that Tos will not make a realtrade but have worked with contitional TOS a lot and am hoping to make most of the idea work conditionally.. Especially if I use Visual Basic Script to build
conditional code automatically... so pretty sure i can fully automate it thru Conditional calls if I can simulate it and everything goes on paper trade for months before live..
But thanks for the reminder.. most folks even thing Pinescript can do live trades but not without 3rd party software.. and most folks they they can do real automated trading on TOS.. even today
I have done a lot of almost fully automated and am getting better but finding that visual basic or python or Autoit can pick up and enter the trades for me automatically by maneuvering TOS conditional script..
As i can program a conditional script to be totally built and installed by python.. so not worried about that part if i can finally get a better handle on thinkscript. I work it 24/7 min 20 hours per week..
A lot of folks got thier bubble busted to find out TOS is not fully automated...

I am pretty sure that I can make some major headway if I can find a simple way to turn the buy off and on easily like with the cnt variable changing it to Zero for buy off and to on to make just one simple buy...... Can't be that hard to set it up to turn the buy on for one share each time the
stock price drops by one dollar... and it must be totally controlled or it will buy up the whole county in just a few seconds. LOL.. Thanks again for any help you offer.
 
Last edited:

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
411 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