Trouble porting MQL4 for loop to Pinescript
Trouble porting MQL4 for loop to Pinescript
Loading saved threads...
RRS · External communityPost link
External question — Stack Overflow Stack Exchange
Author: RRS
Original post: https://stackoverflow.com/questions/73071987
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
Well, I taught myself MQL4/C++ (enough to get around in it at least) and I'm trying to transfer an indicator I built over to TradingView. I'm having trouble wrapping my head around the syntax differences though.
Here's the MQL4 loop
for(int i=limit; i>=1; i--)
{
if(KBuffer[i]>80 || KBuffer[i]<20) CrossoverValue=KBuffer[i];
else if(KBuffer[i]>CrossoverValue) BuyBuffer[i]=100;
else if(KBuffer[i]<CrossoverValue) SellBuffer[i]=100;
if(KBuffer[i]>CrossoverValue) EntryCondition[i]=1; //1=Buy
if(KBuffer[i]<CrossoverValue) EntryCondition[i]=2; //2=Sell
}
and here's what I've got so far in Pine
for i = 1 to 100
if k[i] > 80 or k[i] < 20
crossoverValue = k[i]
else if k[i] > crossoverValue
buyBuffer[i] = 100
else k[i] < crossoverValue
sellBuffer[i] = 100
if k > crossoverValue
entryCondition := 1
if k< crossoverValue
entryCondition := -1
I know I'm not getting the Pine syntax right, but after doing a bunch of googling and reading the Pine manual I'm still struggling.
Currently I'm getting a
Mismatched input 'if' expecting 'end of line without line continuation'.
error.
Any pointers as to where I'm going wrong would be really appreciated!
For reference, here's a screenshot of the MT4 version. It's delivering a buy signal when the Stochastic K line has crossed above 20 and is below 80, and vice versa.
Quote
Report
Dave · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: Dave
Original post: https://stackoverflow.com/a/73075772
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
You're almost there
Documentation:
https://www.tradingview.com/pine-script-reference/v5/#op_for
for i = limit to 0 by -1
if k[i] > 80 or k[i] < 20
crossoverValue = k[i]
else if k[i] > crossoverValue
buyBuffer[i] = 100
else k[i] < crossoverValue
sellBuffer[i] = 100
if k > crossoverValue
entryCondition := 1
if k< crossoverValue
entryCondition := -1
"For reference, here's a screenshot of the MT4 version. It's delivering a buy signal when the Stochastic K line has crossed above 20 and is below 80, and vice versa."
Maybe use the standard stochastic indicator then?
//@version=5
indicator(title="Stochastic", shorttitle="Stoch", format=format.price, precision=2, timeframe="", timeframe_gaps=true)
periodK = input.int(14, title="%K Length", minval=1)
smoothK = input.int(1, title="%K Smoothing", minval=1)
periodD = input.int(3, title="%D Smoothing", minval=1)
k = ta.sma(ta.stoch(close, high, low, periodK), smoothK)
d = ta.sma(k, periodD)
buy = ta.crossover(k, 20)
sell = ta.crossunder(k, 80)
plot(k, title="%K", color=#2962FF)
plot(d, title="%D", color=#FF6D00)
plotshape(buy, title = "Buy", shape = shape.triangleup, location = location.belowbar, size = size.normal, color = color.green)
plotshape(sell, title = "Sell", shape = shape.triangledown, location = location.abovebar, size = size.normal, color = color.red)
h0 = hline(80, "Upper Band", color=#787B86)
hline(50, "Middle Band", color=color.new(#787B86, 50))
h1 = hline(20, "Lower Band", color=#787B86)
fill(h0, h1, color=color.rgb(33, 150, 243, 90), title="Background")
Quote
Report
RRS · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: RRS
Original post: https://stackoverflow.com/a/73085603
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
Here's what I ended up with – the 'or' operator in the first if statement wasn't working for some reason, so I had to split it up, and a number of other syntactical things.
var float crossoverValue = 0
for i = 0 to 0
if k[i] > 80
crossoverValue := k[i]
else if k[i] < 20
crossoverValue := k[i]
entryCondition = if k > crossoverValue
color.green
else if k < crossoverValue
color.red
else
#9598a1
Note: the code editor is massacring my line breaks when I try to paste it into the basic 'curly brackets' type. I have to use the JS/HTML/CSS block for some reason.
Quote
Report
RRS · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: RRS
Original post: https://stackoverflow.com/a/73085921
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
Oh. Turns out I don't need a for loop at all! 🤣
I'm so used to using them in MT4.
Here's the actual final result.
var float crossoverValue = 0
if k > 80
crossoverValue := k
else if k < 20
crossoverValue := k
entryCondition = if k > crossoverValue
color.green
else if k < crossoverValue
color.red
else
#9598a1
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External question — Stack Overflow Stack Exchange Author: RRS Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/questions/73071987 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. Well, I taught myself MQL4/C++ (enough to get around in it at least) and I'm trying to transfer an indicator I built over to TradingView. I'm having trouble wrapping my head around the syntax differences though. Here's the MQL4 loop for(int i=limit; i>=1; i--) { if(KBuffer[i]>80 || KBuffer[i]<20) CrossoverValue=KBuffer[i]; else if(KBuffer[i]>CrossoverValue) BuyBuffer[i]=100; else if(KBuffer[i]<CrossoverValue) SellBuffer[i]=100; if(KBuffer[i]>CrossoverValue) EntryCondition[i]=1; //1=Buy if(KBuffer[i]<CrossoverValue) EntryCondition[i]=2; //2=Sell } and here's what I've got so far in Pine for i = 1 to 100 if k[i] > 80 or k[i] < 20 crossoverValue = k[i] else if k[i] > crossoverValue buyBuffer[i] = 100 else k[i] < crossoverValue sellBuffer[i] = 100 if k > crossoverValue entryCondition := 1 if k< crossoverValue entryCondition := -1 I know I'm not getting the Pine syntax right, but after doing a bunch of googling and reading the Pine manual I'm still struggling. Currently I'm getting a Mismatched input 'if' expecting 'end of line without line continuation'. error. Any pointers as to where I'm going wrong would be really appreciated! For reference, here's a screenshot of the MT4 version. It's delivering a buy signal when the Stochastic K line has crossed above 20 and is below 80, and vice versa.
Checking account access…