Problem with flags

Jompatan

New member
Hello, I am new to all this, I have the following problem:
I need that from a certain condition fulfilled (A) I can do an operation only once (in this case add 1 point to the counter), and then do another different operation (in this case add 10 points to the counter).
The problem is that I cannot modify a variable that is used in the IF condition.

def A=1;
def counter;
if A==1 {
counter= 1+counter[1];
A=0;
}else{
counter= 10+counter[1];
}
plot prueba= A;

Already assigned: a at 6:1
Value never assigned to a at 6:1
Already assigned: a at 6:1
Value never assigned to a at 6:1

def counter = if A == 1 then 1 + counter[1] else if a==0 then 10 + counter[1] else 0;
plot prueba = counter;

not a lot of info to go on but try this

Thanks for answering, but actually I need to do many more operations within the true and false condition.

def A = 1;

if A == 1 {
MANY OPERATIONS
A = 0;----------------------->>>> Here I want to change the value of A so that after doing the operations on the true side, it will never enter again.
} else {
MANY OPERATIONS
}

left me confused so think you need to be more specific

It's a secret.

Ok, I want to try a strategy with backtesting and as an example I am going to assume the following exercise (remember that it is an example):
I have a starting capital of \$ 1000 and I am going to trade long on all the bars (just to understand it). The first operation will be (1000 / shares purchased) * profit.
From the second my capital will no longer be 1000, and that's where my problem begins to know the new profits according to the new capital.

declare lower;
input investment = 1000;
def profit = close-open;
def A = 1 + A [1];
def total;

if A == 1 {
total = round((investment/ open), 0) * profit;
} else {
total = round((total[1] / open), 0) * profit;
}
def totals = total + total[1];

plot data = totals;

This example doesn't throw any error in the code, but it doesn't show results, so there is something wrong.

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
311 Online

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.