alert reloadedtype: alert creation
required time: 5 minutes
level: easy
Getting alerts for indicators and strats is good to easy the eyes of the charts for a while :)
After my first script itroducing how to do alerts here there is an easier, more clear way to create an alert. It's still good to do it the "old way" to understand/debug an alert, but after that you'll want this extra tool for sure.
I assume you read my previous post, or understand how alerts are set up on a chart.
So, there is this function called alercondition() that does (almost)the whole job for us :)
The function does NOT create the alert automatically, what is does is introducing a new option in the alerts popup. While in the v1.0 of my script the alert was triggered checking for the plot value against 0.9 or -0.9, in this example the alertcondition() function just fires the alert by itself.
Get the script for yourself, in favourites, copy/paste, whatever. Add it to any chart. Then, open the popup to create an alert(the little clock at the top). There will be a drop menu with the options and "Alert reloaded". This is the title provided in the study() parameter. Choose this one.
Now, the second drop menu has the "going UP" and "going DOWN" options. These are the titles from the alertcondition() function. Select one. Make all the aditional choices for the alert you want. Save it.
And that's it, the alert will fire when the conditions become true. You can still keep the plot() for graphical reference, but it's now an extra, removing it will not affect the alert, so more space in the chart. Yey!! :)
Noob
Back to zero: Understanding seriestype: pine series basic example
time required: 10 minutes
level: medium (need to know the "array" data variable as a generic programming concept, basic Pine syntax)
tl;dr how variables and series work in Pine
Pine is an array/vector language. That's something that twists how it behaves, and how we have to think about it. A lot of misunderstandings come from forgetting this fact. This example tries to clear that concept.
First, you need to know what an array is, and how it works in a programmig language. Also, having javascript under your belt helps too. If you don't, google "javascript array basic tutorial" is your friend :)
So, in pine arrays are called "series". Every variable is an array with values for each candle in the chart. if we do:
myVar = true
this is not a constant. It is a series of values for each candle, { true, true,....., true }
In practice, the result is the same, but we can access each of the values in the series, like myVar{0}, myVar{7}, myVar{anyNumber}....
Again, it is not a constant, since you can access/modify the each value individually
so, lets show it:
plot (myVar, clolor = gray)
this plots an horizontal line of value 1 ( 1 is equal to true ) so it's all good.
On to a more usual series:
tipicalSeries = close > open ? true : false
plot(tipicalSeries, color= blue)
This gives the expected result, a tipical up and down line with values at 1 or 0. Naturally, "tipicalSeries" is an array, the "ups" and "downs" are all stored under the same variable, indexed by the candles.
In Pine, the ZERO position in the array is the last one, which corresponds to the last candle on the right. Say you have a chart with 12 candles. The close would be the closing value of what we intuitively think as first candle, the one on the left. then close ... and so on.... until close , the value of the "last" candle, the one on the right. It actually helps to start thinking of the positions backwards, counting down to zero, rocket launch style :)
And back to our series. The myVar will also be the same size, from myVar to myVar .
When we do some operation with them, something simple like
if ( myVar == tipicalSeries)
what is really happening is that internally, Pine is checking each of the indexes, as in myVar == tipicalSeries , myVar == tipicalSeries .... myVar == tipicalSeries
And we can store that stuff to check it. simply:
result = (myVar == tipicalSeries) ? true : false //yes, this is the same as tipicalSeries, but we're not in a boolean logic tut ;)
plot (result)
The reason we can plot the result is that it is an array, not a single value. The example indicator i provide shows a plot where the values are obtained from different places in the array, this line here:
mySeries3 = mySeries2 and mySeries1
this creates a series that is the result of the PREVIOUS values stored (the zero index is the one most at the right, or the "current" one), which here just causes a shift in the plotted line by one candle.
Go ahead, grab a copy of my code, try to change the indexes and see the results. Understanding this stuff is critical to go deeper into Pine :)
Understanding order sizestype: properties manipulation, no programming needed
time required: 15minutes, at least
level: medium (need to know contracts, trading pairs)
A strategy can "appear" to work or be broken depending on the pile of cash that is working on. This amount is defined in the strat properties, under "order size".
For noobs (like me) this is very confusing at first :)
A strat opens/closes positions using units, a generic measure for the chart being operated on. Thes "units" can be a fixed amount of cash, a fixed amount of contracts, or a floating amount based on the last profits made. I recommend checking my previous strat to figure the case of contracts .
So, any trading price is the amount of "things" you get for some "cash". The things are the first unit, the "cash" is the second. Some examples:
XAU/USD - 1 xau oz is worth x dollars
BTC/USD - 1 bitcoin is worth x dollars
GBP/EUR - 1 pound is worth x euros
To add to confusion, a lot of markets the "unit size" is different from what the strat thinks it is. An options contract is 100 shares(the unit), 1 xau contract is 10 oz(units), 1 eur/usd contract is 100k euros and so on... so, after figuring out how the sizes work in a strat, then the sizes must be adapted for the specific market in question.
The choice os using the ETHUSD pair is because:
1 - you can buy 1eth, unlike a gold contract for example, so 1 "unit" = 1 eth, easier to get
2 - ETH is around 12 bucks, wich gives round numbers on the math, easier to wrap the brains around :)
3- is an unusual pair, so the regular contract sizes don't apply, and the brain is not conditioned to work inside the box ;)
You will have to access the script properties, to change the values. As these values are changed you will see exactly the differences in the values of the strat.
Text is too long, check the comments for all the cases
Understanding contract sizes in a strategyThis simple strat fires up on green bars, down on red bars. cannot get any simpler. So, it's a good example to check how returns are calculated.
First, the internal firing mechanism for the strategy.entry function is something hardcore. As result, the entry points can be confusing, and seem to appear in a wrong bar (as the 2nd and 3rd signals are good examples), but i'll put that aside to keep it simple. And, because i don't yet get it myself ;)
The example is simple, so that numbers can be followed easy. Chart in BTC/USD, so USD is the "base" currency used by strat to calculate. A contract/unit is the value of 1 unit in base currency. 1 Apple share is 600$, 1 bitcoin is 600$, 1 oz gold is 1330 bucks. So, here in each bar, the value of 1 contract is the value of the BTC in USD. simple as that.
The strat properties, can be passed as input fields (line 2) or accessed/changed in the right click->properties pop-up. To make it easier, initial capital is 1000 bucks, and "order size" is 1 contract. This means that the strat will open a position of 1 BTC when it fires. Value "Initial capital" makes no difference at all, at least with these choices. It's just for show. Try to put 1$ and 1 contract, the strat will still trade anyway. It manages to trade 1 contract(or BTC) values at ~600$, with a single dollar. nice ;)
Check the chart. see the little blue "BarUp +1" ? that's it, strat goes long 1 BTC. there's a little blue triangle on the bar, points to the value of entry.
Then later, on second move, the "BarDn -2", the strat goes short 2BTC. 1BTC to close the long +1 more to open a short.
The profit here is the difference between the value of the long opening and the long closing. The extra BTC (shorted) is part of the next position. Since this dumb strat just reverses the direction, there are always +2, -2 , +2.... 1 to close previous position, 1 to open another. At the strategy tester tab, the option "list of trades" shows in details each of the moves
Checking each move and comparing what we see with the chart itself helps to achieve ilumination :)
Bonus feature: as soon as you get it, try to increase the option "pyramiding" and see how the strat adds more contracts, and how it reverses the positions. sometimes it even makes sense!!!! :)
Hello World scriptmy first script!!!
took me a couple of hours to put it together, the documentation could be much, much better