fernandesj

New member
VIP
Hello All, Newbie here. I am trying to use the Floating PL study to see how this works (for /ES) and I would like to add the AddOrder functionality to the ParabolicSAR study which I have pasted below. I know I have no idea what I am doing with thinkscript

but what I would like to do is

1. when the state goes from short to long or from init to long I would like to buy one futures contract.
2. when the state goes from long to short I would like to sell (to close) one futures contract
3. when the initial state is short (no action at this time)

I hope someone can help straighten me out.

### Code Below ###
Ruby:
``````input accelerationFactor = 0.02;
input accelerationLimit = 0.2;
input quantity = 1;

assert(accelerationFactor > 0, "'acceleration factor' must be positive: " + accelerationFactor);
assert(accelerationLimit >= accelerationFactor, "'acceleration limit' (" + accelerationLimit + ") must be greater than or equal to 'acceleration factor' (" + accelerationFactor + ")");

def state = {default init, long, short};
def extreme;
def SAR;
def acc;

switch (state[1]) {
case init:
state = state.long;
acc = accelerationFactor;
extreme = high;
SAR = low;
case short:
if (SAR[1] < high)
then {
state = state.long;
acc = accelerationFactor;
extreme = high;
SAR = extreme[1];

} else {
state = state.short;
if (low < extreme[1])
then {
acc = min(acc[1] + accelerationFactor, accelerationLimit);
extreme = low;
} else {
acc = acc[1];
extreme = extreme[1];
}
SAR = max(max(high, high[1]), SAR[1] + acc * (extreme - SAR[1]));
}

case long:
if (SAR[1] > low)
then {
state = state.short;
acc = accelerationFactor;
extreme = low;
SAR = extreme[1];
} else {
state = state.long;
if (high > extreme[1])
then {
acc = min(acc[1] + accelerationFactor, accelerationLimit);
extreme = high;
} else {
acc = acc[1];
extreme = extreme[1];
}
SAR = min(min(low, low[1]), SAR[1] + acc * (extreme - SAR[1]));
}
}

plot parSAR = SAR;
parSAR.SetPaintingStrategy(PaintingStrategy.POINTS);
parSAR.SetDefaultColor(GetColor(5));

Last edited by a moderator:
Solution
Hello All, Newbie here. I am trying to use the Floating PL study to see how this works (for /ES) and I would like to add the AddOrder functionality to the ParabolicSAR study which I have pasted below. I know I have no idea what I am doing with thinkscript

but what I would like to do is

1. when the state goes from short to long or from init to long I would like to buy one futures contract.
2. when the state goes from long to short I would like to sell (to close) one futures contract
3. when the initial state is short (no action at this time)

I hope someone can help straighten me out.

See if this helps. This has to be copied and pasted in the Strategy Tab.

Ruby:
``````#
# TD Ameritrade IP Company, Inc. (c) 2008-2021
#...``````
Hello All, Newbie here. I am trying to use the Floating PL study to see how this works (for /ES) and I would like to add the AddOrder functionality to the ParabolicSAR study which I have pasted below. I know I have no idea what I am doing with thinkscript

but what I would like to do is

1. when the state goes from short to long or from init to long I would like to buy one futures contract.
2. when the state goes from long to short I would like to sell (to close) one futures contract
3. when the initial state is short (no action at this time)

I hope someone can help straighten me out.

See if this helps. This has to be copied and pasted in the Strategy Tab.

Ruby:
``````#
# TD Ameritrade IP Company, Inc. (c) 2008-2021
#

input accelerationFactor = 0.02;
input accelerationLimit = 0.2;

Assert(accelerationFactor > 0, "'acceleration factor' must be positive: " + accelerationFactor);
Assert(accelerationLimit >= accelerationFactor, "'acceleration limit' (" + accelerationLimit + ") must be greater than or equal to 'acceleration factor' (" + accelerationFactor + ")");

def state = {default init, long, short};
def extreme;
def SAR;
def acc;

switch (state[1]) {
case init:
state = state.long;
acc = accelerationFactor;
extreme = high;
SAR = low;
case short:
if (SAR[1] < high)
then {
state = state.long;
acc = accelerationFactor;
extreme = high;
SAR = extreme[1];
} else {
state = state.short;
if (low < extreme[1])
then {
acc = Min(acc[1] + accelerationFactor, accelerationLimit);
extreme = low;
} else {
acc = acc[1];
extreme = extreme[1];
}
SAR = Max(Max(high, high[1]), SAR[1] + acc * (extreme - SAR[1]));
}
case long:
if (SAR[1] > low)
then {
state = state.short;
acc = accelerationFactor;
extreme = low;
SAR = extreme[1];
} else {
state = state.long;
if (high > extreme[1])
then {
acc = Min(acc[1] + accelerationFactor, accelerationLimit);
extreme = high;
} else {
acc = acc[1];
extreme = extreme[1];
}
SAR = Min(Min(low, low[1]), SAR[1] + acc * (extreme - SAR[1]));
}
}

plot parSAR = SAR;
parSAR.SetPaintingStrategy(PaintingStrategy.POINTS);
parSAR.SetDefaultColor(GetColor(5));

#1. when the state goes from short to long or from init to long I would like to buy one futures contract.
def buy = if state[1] == state.short and state == state.long or state[1] == state.init and state == state.long then 1 else 0;

#2. when the state goes from long to short I would like to sell (to close) one futures contract
def sell = if state[1] == state.long and state == state.short then 1 else 0;
AddOrder(type = OrderType.SELL_TO_CLOSE, condition = sell, name = "SAR Sell Strategy", arrowColor = Color.YELLOW);

#3. when the initial state is short (no action at this time)``````

Not the exact question you're looking for?

87k+ Posts
409 Online

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?