need a coding wizard's help

If you can help me, it would GREATLY appreciated.

On a 2m chart with a SMA of 200. I am looking to write thinskcript code for when the price crosses and closes above the 200MA and another "confirmation" candlestick closes higher than that original crossing-over candlestick. The tricky part is that the "confirmation" candlestick can be a few candlesticks later. This is the part I can't figure out with code. o_O

After that is figured out, I want a boolean value to oscilate between ABOVE 200SMA and BELOW200SMA (which i have in the code) based on whether that confirmation above is true.

HERE IS MY CODE
#MOVING AVERAGES
def Short_MA = 200;
plot Short = SimpleMovingAvg(length = Short_MA); #pink line


def CrossUp = open < short and high > short and close > short; #CROSS AND CLOSE ABOVE
def CrossDn = open > short and low < short and close < short;

def CrossUpConfirmed = close > high[1]; #I want this to be true if the close is greater than the original crossover bar, even if it's 5 bars later
def CrossDnConfirmed = close < low[1];

def CrossingUp = CrossUp[1] and CrossUpConfirmed;
def CrossingDown = CrossDn[1] and CrossDnConfirmed;

plot MA_Buy = CrossingUp;
plot MA_Sell = CrossingDown;

MA_BUY.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);
MA_Sell.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_DOWN);

#OSCILATE
def MA;
if MA_Sell is true {
MA = yes;
} else if MA_Buy is true {
MA = no;
} else {
MA = MA[1];
}
plot Buy = !MA;
plot Sell = MA;

BUY.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);
Sell.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_DOWN);
 
Solution
@mrmitch
Code:
#MOVING AVERAGES
def Short_MA = 200;
plot Short = SimpleMovingAvg(length = Short_MA); #pink line


def CrossUp;
def CrossDn;
If crosses(close[1],Short[1],crossingdirection.ABOVE) {
CrossUp = high[1];
CrossDN = Double.NaN;
} else If crosses(close[1],Short[1],crossingdirection.BELOW) {
CrossUp = Double.NaN;
CrossDN = low[1];
} else {
CrossUP = CrossUp[1];
CrossDn = CrossDn[1];}

def CrossUpConfirmed = close[1] > CrossUp;
def CrossDnConfirmed = close[1] < CrossDn;

def CrossingUp = CrossUpConfirmed;
def CrossingDown = CrossDnConfirmed;

plot MA_Buy = CrossingUp and !CrossingUp[1];
plot MA_Sell = CrossingDown and !CrossingDown[1];

MA_BUY.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP)...
@mrmitch
Code:
#MOVING AVERAGES
def Short_MA = 200;
plot Short = SimpleMovingAvg(length = Short_MA); #pink line


def CrossUp;
def CrossDn;
If crosses(close[1],Short[1],crossingdirection.ABOVE) {
CrossUp = high[1];
CrossDN = Double.NaN;
} else If crosses(close[1],Short[1],crossingdirection.BELOW) {
CrossUp = Double.NaN;
CrossDN = low[1];
} else {
CrossUP = CrossUp[1];
CrossDn = CrossDn[1];}

def CrossUpConfirmed = close[1] > CrossUp;
def CrossDnConfirmed = close[1] < CrossDn;

def CrossingUp = CrossUpConfirmed;
def CrossingDown = CrossDnConfirmed;

plot MA_Buy = CrossingUp and !CrossingUp[1];
plot MA_Sell = CrossingDown and !CrossingDown[1];

MA_BUY.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);
MA_Buy.AssignValueColor(Color.GREEN);
MA_Sell.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_DOWN);
MA_Sell.AssignValueColor(Color.RED);
 
#OSCILATE
def MA;
if MA_Sell is true {
MA = yes;
} else if MA_Buy is true {
MA = no;
} else {
MA = MA[1];
}
plot Buy = !MA;
plot Sell = MA;

Buy.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);
Buy.AssignValueColor(Color.GREEN);
Sell.setpaintingStrategy(paintingStrategy.BOOLEAN_ARROW_DOWN);
Sell.AssignValueColor(Color.RED);

You really don't need the second buy/sell definition. You could just reference the previous condition until condition change.
 
Solution

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

@mrmitch
If you change plot on confirmation bar, it will give false signals.
Close of the confirmation bar could move above previous high, giving a buy signal and then drop below previous high before the end of the bar.
Same for the sell signal.
You should always wait until after bar close for confirmation of signals.
 
Last edited:
Thanks again. I definitely agree to wait until after the bar closes for the signal, but is it not possible to get that confirmation arrow when the second bar closes lower?

In the picture for example, is there a way to get the switch from up to down to happen one bar earlier. That second bar that closes lower is the confirmation, and I want the down arrow to appear as soon as it closes to trigger a sell.

Maybe I'm still not fully understanding...

Screen-Shot-2022-02-08-at-5.55.47-PM.png
 
@mrmitch
The literal second the confirmation bar closes, the next bar (with the red arrow) opens.
The open of that bar is equal to the close of the confirmation bar.

So it is plotting the red arrow the second the confirmation bar closes.
 
Ok i understand. My current strategy (that has many other thinkscript indicators) populates arrows on the previous bar if the close makes it true and addOrder function places on close of previous bar, so I was hoping to add this indicator to that.
 
Ok i understand. My current strategy (that has many other thinkscript indicators) populates arrows on the previous bar if the close makes it true and addOrder function places on close of previous bar, so I was hoping to add this indicator to that.
Any strategy that is plotting on the close of the previous bar is not written correctly and will display meaningless results. While it would be wonderful to go back in history to place trades after we know the results, this can't happen. Strategies must be written as open[-1] this way the order price reflects the next moment in time -- the open of the following bar., which in the real world is the earliest that a trade can be placed.
 
Last edited:
Well this is quite unfortunate considering my whole strategy is written based on the close of the previous bar 😂
Moving averages are a lagging indicator, they will only give you an exit signal after some loss.
To mitigate how much that loss will be, trade in stocks that have a long-term uptrend, high liquidity and not extreme volatility. This will help control the "falling sword" situation, which is where the moving average exit signal occurs on a large downward bar.
Strong financials will mitigate sudden downward spirals based on news.
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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