Im not understanding the barindex command. What am i doing wrong here?
Just want to learn to i figured i would buy if:
Close > moving average 10
+
Close > highest high past 20 candles
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
fastma = average[10]
// Conditions to enter long positions
if barindex = 0 then
buy1 = highest[20](close)
sell1 = lowest[20](close)
buysignal = buy1 > fastma
sellsignal = sell1 < fastma
if close > buysignal then
buy 1 contract at market
elsif close < sellsignal then
sellshort 1 contract at market
endif
endif
c1 = close[2] < close
if c1 then
sell at market
endif
What do you want to achieve with this “barindex if-loop”? Take it away otherwise you won’t get any trades.
Lines 11-12 assign boolean values (0-1) to variables, then at lines 13 and 15 you compare them with prices. That would yield really odd trades, if any (see what Despair said)!
Anyway BARINDEX is zero when you first launch your strategy, then it is incremented by 1 at each new bar/candlestick.
Roberto
What do you want to achieve with this “barindex if-loop”? Take it away otherwise you won’t get any trades.
Lol, i tried to look at what other systems where doing, i have no clue man!
If im asking for “high[7]” (or “highest[20]”?) will it check for the highest point in the last 7 candles, or only the high from the 7th candle?
Sorry for being such a noob
It will check for the highest value of the last 7 or 20 candles. Sounds like you should read the manual. 😉
Sounds like you should read the manual. 
yea that was my last chance, figured id ask here first though. Not everything in that manual is written for newbs like me 😀 (meaning i dont always understand what it says in the manual 😛 )
LeoParticipant
Veteran
if barindex = 0 then ?????
So your code is only working at the first candle… after that your code is not doing anything
I learn to code just by reading the Manuals since the firs page and practizing chapter by chapter… so Keep Calm and Keep reading!
If ther’s room for some more questions here:
if intradaybarindex=0 then
trading=1
What exactly does that mean? I dont understand the barindex command at all.
In general the “If barindex = 0 then…” command, what does it mean? i dont get why so many people use it. Would love to understand 🙂
how would you rewrite my code in post 1, to make it actually trade if the close[0] > highest high in past 20 candles?
BARINDEX is the number of bars elapsed since your strategy has been launched.
TRADEINDEX is the number of bars elapsed since your trade was opened.
INTRADAYBARINDEX is the number of intraday bars elapsed since the beginning of the day and is reset daily to zero.
To reset some variables at the beginning of each day you may want to write:
IF IntraDayBarIndex THEN
.
. //here you know a new day has just begun
.
ENDIF
To check how many bars have elapsed since your trade was opend you may write:
IF (BarIndex - TradeIndex) >= x THEN //close after x bars have elapsed
SELL AT MARKET
EXITSHORT AT MARKET
ENDIF
I also suggest that you use the search box to find all occurrences of keywords you don’t feel at ease with. You’ll find documentation and plenty of examples to study.
Sorry Vonasi, you are absolutely right! I wrote the wrong explanation using a correc example.
LeoParticipant
Veteran
I think the barindex=1 is the first data loaded since we use DEFPARAM preloadbars=1000
then the first BARINDEX when your System was activated is 1001.
Anyway…
I use a lot BARINDEX in my codes because I use it to Figur out symmetries, triangles, etc.
LeoParticipant
Veteran
if intradaybarindex=0 then trading=1 What exactly does that mean? I dont understand the barindex command at all.
BARINDEX and INTRADAYBARINDEX are different, in your example as
robertogozzi says is for reseting variables
but in your code you have use: totally different
// Conditions to enter long positions
if barindex = 0 then
buy1 = highest[20](close)
Sorry for my incorrect example, it should read:
IF IntraDayBarIndex = 0 THEN //reset to ZERO each day
.
. //here you know a new day has just begun
.
ENDIF
LeoParticipant
Veteran
i dont get why so many people use it
Then simple: do not use it until !!! you copy things to your code without knowing… that dangerous.
have look, test it and tell us your results:
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
DEFPARAM preloadbars=100
fastma = average[10]
// Conditions to enter long positions
buy1 = highest[20](close)
sell1 = lowest[20](close)
buysignal = buy1 > fastma
sellsignal = sell1 < fastma
if buysignal then
buy 1 contract at market
elsif sellsignal then
sellshort 1 contract at market
endif
c1 = close[2] < close
if c1 then
sell at market
endif