Level 1 - Learn to code simply - PineScript
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Maddrix_club

//@version=4
// The goal of this script is honestly to help everyone learn about trading with bots and algos. 


// First of all, you should know that the algo is constantly running, to make it simple, consider that each time a new bar appears, the algo is ran, from top to bottom. 
// Every single line of code will be read by the computer, and it will do what you want it to do - it could be drawing something on the chart, it could be doing a calculation,
// it could be sending a signal, etc.

// if you wish to add comments, notes or reminders to your code, you simply add a double slash like so "//" at the beginning of the line. It will become gray and the computer won't read it.

// This specific script uses the version 4 of PineScript. This means the code is evoluting and improving. The latest version is Version 5, which I have not dove into yet.
// This is why I use version 4. You can see it on line 4. You should not change that. Same for lines 1 and 2. No need to touch anything.

study("Level 1 - Learn to code simply - PineScript", shorttitle="This one will appear on the setting dialog box", overlay=true)
// In version 4 you have 2 types of algo - the study and the strategy. The strategy allows you to use special functions that will simulate trading and backtest a specific strategy.
// I personally don't use strategies ever, because I can't see what is going on under the hood, I mean the calculation in detail.
// The other reason is that any strategy that shows great results, are usually only valid "on paper". Once you run it live on the exchange, there is slippage, fees, and all that 
// stuff, which invalidates your strategy. 
// on line 15 you can see that it says overlay=true, this means that the algo will be applied over the chart. If you wanted your indicator to be an oscillator for example, or 
// simply not applied to the chart, you set it to "false" and it will show as a separate drawing below your chart.

// My recommendation is for you to copy/past this code, and change the settings one by one to see what they do and learn from that. This is how I learned all I know.
// Then once you save it and apply to your chart, the bottom windown of your screen will show all the coding errors in red. Only focus on the very first line that is in red
// and try to fix it. It will usually show you at which line number your code is having a problem. Sometimes it is just an easy typing error, sometimes it is harder
// and you have to google it.

// In this Level 1, we will simply call different functions that are built-in tradingview's database, we will plot them, we will create a condition for a signal and then 
// we will create a signal. 



// ---------------  LET'S DO THIS ---------------------//
// --------- More classes on bot trading at ---------- //
//                www.maddrix.club                     //



// Let's calculate the simple moving average of the price for the last 14 bars
SMA14 = sma(close,14)
// ------------ SMA14 in white is a variable that I just create just by typing it. Try to change the name if you want to call it differently.
// ------------ you can also create more variable if you want other SMA's
// ------------ if you hover your mouse over the "sma" in blue, you will see what the function required to work - sma(source, length). The 2 elements are separated by the comma.
// ------------ Basically it will automatically calculate the average "close" for the last "14" bars

// Let's now plot this sma on the chart so we can visually understand what it is
plot(SMA14, color=color.red, style=plot.style_linebr, transp=0, linewidth = 2)
// ------------ SMA14 is what we want to plot, so this is the first element we put, then we give it a color, and a style. 
// ------------ We can also the transparency of the line, as well as its width


// Let's calculate the exponential moving average of the price for the last 14 bars
EMA14 = ema(close,14)
// ------------ Notice here, she blue "sma" became "ema"
// ------------ We are still calculating it based on the closing price "close" in red
// ------------ Notice also how I changed the name of the variable, from SMA14 to EMA14
// ------------ you cannot have 2 variables with the same name, or you cannot define/calculate two times the same variable

// Let's plot it as well, this time in green, and also much thicker
plot(EMA14, color=color.green, style=plot.style_linebr, transp=0, linewidth = 4)



// Alright so now we are going to create a condition which we will use to create ou alert
// Our first buy condition will be when the ema14 crosses over the sma14
first_condition = crossover(EMA14,SMA14)
// ------------ Here I create a new variable. Previously the 2 variables creates were going to be numbers with decimal, which means they are "Float" variables.
// ------------ This one though, is not a "float" variable, it will be a "boolean" variable, this means its value can only be TRUE or FALSE. It is like YES or NO, 0 or 1. Opened or Closed.
// ------------ I am using another built-in function which is "crossover". This functions requires 2 elements. In our case both Moving Averages.
// ------------ Herem the "first_condition" will be equal to "true" when EMA14 crosses over SMA14. Also note the underscore, because you can't have spaces within the name of a variable.



// Now that my condition is calculated, I want to show a little label on the chart, to see when that is true
plotshape( first_condition, title="Cross Over" , text="Long" , location=location.belowbar, style=shape.labelup  , size=size.tiny, color=color.green, textcolor=color.white, transp=0)
// ------------ Same as with the regular plot, I need to indicate what I want to plotshape. Here it is "first_condition"
// ------------ You can see also that the plotshape function has several options that you can add/remove in order to adjust. If you remove them, they should appear with default settings.
// ------------ If you would like to see other options, you can hover over you mouth on the red part, for example "size.tiny", then hold "Ctrl" and click on it.



// Now, we are going to compare these signal with other ones. Let's create a second condition.
second_condition = crossover(close,EMA14)
// ------------ This time, we want the condition to be true when the closing price "Close" crosses over the EMA14

//Let's plot it as well
plotshape( second_condition, title="Cross Over #2" , text="Long #2" , location=location.belowbar, style=shape.labelup  , size=size.tiny, color=color.lime, textcolor=color.red, transp=0)
// ------------ Notice how I changed the name as well as the text color and the color of the label


//Now that I have created my 2 signals, I would like to create 2 alerts for them
alertcondition(first_condition, title="buy #1", message="buy #1 signal!!!")
alertcondition(second_condition, title="buy #2", message="buy #2 signal!!!")
// ------------ Here I am using another function which is alertcondition. This will trigger an alert each time the condition is true
// ------------ This is the type of alert you can create and hook up to your 3commas bot platform for example



// In level 2, I will add more details, more options, more flexibility, how to add user input and show you other functions


// --------------------  END --------------------------//
// --------- More classes on bot trading at ---------- //
//                www.maddrix.club                     //

