starting with a balance of €1,000 ending with a balance of close to 27,000 short trades one above 90% long trades one above 90% back test results and no running draw down because of a TI stop loss performance up and up if you have a computer internet connection and 1 hour of time then by the end of this video you will have a scalping robot which has more than 90% win rate with a very tighty stop loss of 20 Pips even if you know nothing about coding mql language or metat Trader as trading platform we go
step by step and these are the topics that we cover in the previous video we already covered the first three and we started coding the year today we complete the coding execute the EA and apply it to life charts without any further nonsense let's start continuing coding from the last video one thing which we forgot in the last video which we need to collect is also the time at which to start the trading and at which to stop the trading and the way that we ask that input from the user is is by creating another
enum because it's not available so you write enum I'm going to call it a start hour and then within the curly brackets we can give our user all of the options that they can select it from so that's going to appear in the drop- down menu you will see it in a few minutes so first one I'm going to write is inactive and I'm going to give it a zero separated by 0 100 hours and that's equals to one now to show you guys quickly how it works I'm going to create create another input variable
using the variable type that we have defined here as start our so start our and then I'm calling the variable itself as sh input so start our input and the default is zero which basically means inactive and within the hash after the dashes I'm saying start hour so that's giving the description so if I compile it now I get two errors which is there's a colon missing so I need to write a colon here and now no errors if I go back to metat Trader now what I will see here is now there is a
start hour drop-down created and within the dropdown you have the options of inactive which is the default value and then 0 100 hour that's what we have written here so we've created a variable type start hour where the options are inactive and we've given it the value zero 0 100 which is 1 I'll type in the rest so now we have all of the hours within this enum that we have created now this variable type start hour now has inactive as zero and going all the way up to 2300 and the value that we're assigning
to 2300 is 23 23 now if I compile it and go back to metat Trader now our drop- down menu is going to have all the hours from 0100 all the way down to 2300 and that's what we wanted now if I go back and create the same for the end hour which is creating another enum calling it the variable type name as end hour and then giving it exactly the same values from inactive zero all the way to 2300 hours which is 23 and then we can create a variable and we can call it
end hour input equals z and we assign it the variable type and hour that we have created in the previous line so now if I compile it and I go back to metat Trader I will see now there is the start time and then there is the end time and now the user can select for example if they want to start at 800 hours and they want to end the trading at 2100 hours now after creating these enms and these input variables we're going to write in the code some variables where we're going to create
the variable but we're not going to ask for an input from the user these are going to be hardcoded so the first two I'm creating are s choice and Es Choice which are start our choice and end our choice which is going to come from here from this user input selection and because we are assigning the we had assigned the values as 1 2 3 4 5 to these our numbers so I'm creating it as integers so int variable type as a choice and end our choice then three other variables that I'm going to require
the first one is going to be an integer and there we're going to store the number of bars and we're going to use this in when we we're going to create the function for higher highs and higher lows and you will see how it's going to be used and the expiration bars and Order distant Point these two we are going to use when we're going to be sending the buy orders and sell orders and this is basically going to say it's like okay once we have placed the order if if there are 100 bars that
have expired since then the order expires and then the order distance point is how far below the order that we're sending the price must be for the order to be sent you will learn about it later so this includes all the input variables and the variables there we're not requiring the input from the users now let's go ahead and create a function we're going to need a lot of functions besides the one that came with the template itself the first function that I'm going to create is finding the highs and we come down to the
bottom and we write a double find high and once you start the curly bracket this now creates a function that we can use to find the highs visually all know how to find the high so for example we know this is a high this is a high this is a high this is a low this is a low this is a low but how do we tell the code that it is a high one way to look at it is basically for a high what we go is if there are five bars to the left
all of them are below this point and five bars to the right are also below this candle then this is a high now you can say it's six bars to the left and six bars to the right 10 bars to the left 10 bars to the right I normally find it adequate to have five bars on the left and five bars on the right now how do we tell the code what we will do is we'll look at okay on every bar so we're going to start from the last bar that we currently have here
so this is our current bar and we'll continue backwards until we find a bar where we have five bars on the right and five bars on the left both below the middle candle so we're going to be looking at 11 candles at any point in time so we will start here and then we go look back 11 candles and then see if candle number six because that's going to be right in the middle is the highest bar if it is that means that five bars on the right and five bars on the left are below
that middle candle so we're going to loop from this bar to this bar to this bar to this bar on every bar checking whether in the last 11 bar the highest bar is the Sixth bar and from here if we look back 11 can candles then the candle number six is right in the middle of those 11 candles it has five bars on the right and five bars on the left as lower so that is a valid high for us now how do we write it in a code first we create a loop and this
is how you create a loop in mql so you start with for an integer equals I is starting position at zero an ending position as whenever I hits 200 so as long as it's below 200 continue to run this Loop and every time increase the value of I by 1 by saying I ++ so for the first time it's going to be I is going to be zero then the CES here are going to be done then the Cordes are going to finish the the loop will continue but with a value of one then with
a value of two then with a value of three then with a value of four and so on and so forth and then using the I highest function we look for those 11 bars starting at whatever is the current bar it start five bars to the right of it and then you find uh 11 bars to the left of that and then find the high of that highest and find the highest of that 11 bars range and if that turns out to be I then we know that bar is the highest bar in the last
11 bars so now we can create a variable before this and we can store the value of the high of our IE into that V able and let's call it high so if high if we find the highest bar at in right in the middle is starting five bars to the right of I look back 11 bars and then if the Sixth bar is which is the I bar itself is the highest bar then we can use this high value as the high and we have found our high but just as a precaution let's create
a value double and let's call it highest high and let's start the value at zero so that when we start the the the EA the highest high value is zero so that we don't start with a random value and now if the high is higher than the highest high meaning that it is a positive number and it is right in the middle of 11 bars with five bars to the left five bars to the right then we can return that high back to our normal coding that because we have now found the high and in
case if the if on the current bar I if this condition was not true then basically just set the highest high as the whatever is the more or the math Max between high and highest high so whatever High you have found because you have found the high here for that eye and the highest high just put the value to of highest high to that so that it has a value now and in case the loop finishes and we get nothing then we just need to return a minus one and I have cleaned up the spaces
so that it we we don't have too many spaces in between now one of the things which can cause a problem problem in this code is because what we're asking the the EA to look at is starting at i0 but then we're asking it to start this I highest five bars to the right by going minus five so that will mean that I on the first iteration will become i - 5 or I would say i- bars n so then we have to say here that if I is greater than baren because otherwise if I
is not greater than Bar N then I minus Bar's n will become a negative number so I'm starting with I is greater than Bar N if it is then you apply this formula and then if you find the high if if the condition is true then you return the high otherwise just put the value between high and highest high whichever is higher back into the highest high value and if none of this happens then just return a minus one this is probably the most complicated function in this entire EA so if you find it very
difficult to understand then just leave a comment behind and if there are enough people who did not understand this part then I will make a separate video to explain how to find higher highs in a much more detail with showing the explanations then finding the low is exactly the same function but in reverse the only difference the big difference is with highest high we started with a value of zero but with lowest low we cannot start with a value of zero otherwise any value that the year is going to find is going to not going
to be lower than zero so it will never be updated so we start with a double Max which is the max value within mql that you can have it's the opposite of zero in a way and then the same thing we start with an i and here again I will change it to bars n because in case if we ask the user to provide the bars value to look for the right and left the only difference are now instead of mode High we're looking for mode low and we're looking for I lowest that the remaining
is just simply the same function but the opposite way one other function that we're going to use is the is new bar because we only want the EA to check for conditions or or run only when there is a new bar so we don't want it to run on every tick now I have another video which explains the entire new bar function you can go check that out as well but in short what we do here is we start with the static date time of zero so previous time is zero then we look for the
current time by using the I time function for the current period and then we go you can actually have it for time frame as well so let's go time frame so that if the user has defined it as time frame then we look for the new bar on that time frame using the I time function starting at bar zero now if the previous time is is not equal to current time because previous time was Zero if it's not equal to current time so the new time that we getting on the opening of the bar is
different that means it's a new candle and if it's a new candle then we do two things one we put back the value of the current time into the previous time so the next time we come and check the previous time is already updated with the current time and then we return a true back so that it's a new bar but then the next time the year comes the this bar unless the new bar comes in the current time will still be the same as previous time and in which case it's going to return false
I have a full video in my channel which explains more in details if you want to go check it out next function we're going to need is when we will be sending the buy orders once we find the high then we will need to send the buy order on that high so we're going to create the bu order and I'm creating a separate function not to put too many Cordes in one function on tick so I'm going to create a separate function when we're going to call the send by order function we're going to send
a value of the entry price point to it first we're going to check what the current ask price is by creating a variable ask by using the symbol input double function which is available in mql by sending the value of the current chart symbol and asking for the simple ask it will return the value of the ask price as it is now then as a pre precaution if let's say the highest high is this point or this is where we sending the buy order if the price is very very close to this I don't want
to send the order I want at least a 10 pip 15 pip difference between the the point where I'm sending the order so if the price is currently below this by 105 points that's when I want to send the order if it is very close to the price I don't want to send the and this is what we created that variable order distance point for remember here we had an order distance point of 10 Pips or 1000 points order that we're sending needs to be above at least by 10 Pips of the current price otherwise
don't send the order and this is why I'm saying if they ask is greater than entry minus order distance Point times points then just simply return don't run any code below just return from here exit this don't send any buy order again here if the price was here and this was the order send point because it's a high if the price was here I don't want to send the order then we create a TP point it's a double variable because it's going to store price point I'm calling it TP so TP will be whatever entry
point we sending plus whatever TP points the user has defined here in our case we are starting with 200 points because we're sending TP points as points as 200 but we cannot add 200 to the price Euro USD is normally around 1.14 so adding 200 to it will become 2011 so we first multiply it by point to convert it into the right Decimal System USD JPY for example has three decimals after it GBP USD has five so this 200 needs to be converted into that Decimal System by multiplying it by underscore point we do the
same for stop-loss so we want the stop loss to be entry minus whatever the user has defined as SL points here but then again multiplying it by points to convert it properly for the way that the prices are denominated in in mql well for that particular chart we Define the variable as a small P so let's change it lot size but for that we need to create another function which is the calculation of the lot size based on the risk percentage that we have defined here now I have a full video of how to calculate
lot size based on risk in my channel so do check it out if you want to understand this details but what we look look for is what's the risk so the risk that we want to have is basically risk percentage that we are asking the user to Define here convert it into a value by using the account balance and multiplying it by thisk percentage divide by 100 then we collect the value of tick size tick value lot steps and everything else and then we divide the stop-loss points by tick size tick value and lot steps
that we have collected here to come at how much money per lot step do we have and then we can calculate the Lots by using the math floor function so dividing the risk by what we have calculated here and multiplying It by L step that we have the value from here now full details of this is available if you want to go check out that video you go check out that video otherwise you can just simply write these codes here then we come back to our buy order and then we say create a variable lots
and we're giving it a default value of 0.01 so in case if the user forgot to provide a value here or put a zero here so at least there is a default value of 0.01 to start with but if the risk percentage is greater than zero then we want to calculate the loss size by sending the value of Entry minus a stop- loss entry is here and then stop loss we calculate here so we send the difference between the entry point and the stop loss to this function and then this function calculates the lot size
for that percentage of risk and then it returns the Lots back which then is stored in this Lots don't want our order that we're sending to remain there on the server forever so we need to set up an expiration time so we create a variable type dat date time and a variable expiration and we send it the value uh expiration bars numbers that we have coded here as 100 so 100 bars from now you should expire this so we're sending a Time 100 bars in the future by saying get the time using the I time
function of the current symbol on the time frame that the user has Define of bar zero so the current bar get the time of that and then add to it 100 expiration bars that we have put here 100 expiration bars put 100 expiration bars multiply by whatever is the time frame so if it's 5 minutes 10 minutes 15 minutes convert that into period seconds so if it's a 1 minute chart then convert it into 60 seconds and then multiply it by the number of bars that we want and then add it to the time which
is the current time and that is our expiration time have everything we send the order so we say trade. bu stop because we're sending a buy stop and we give it the Lots value which is coming from here entry value which is coming from here on the current symbol so whichever chart you've attached it to with a stop-loss value value of whatever we have calculated here TP value of calculated here then expiration on order time is specified so that we're going to specify the time on which the order is going to be cancelled and then
giving it a value of expiration now in order to have these trading operations done what we need to do is come back to the beginning and we need to reference to a library which is included here it comes with mql in the folder include trade and it's called trade. mq and we reference to it by writing hash include and giving it the path trade trade. mq by referencing to this now we have access to all of these codes that mql has pre-written for us so that we don't have to write them and we can just
simply reference to it and we're going to use three of them the first one is C trade which is for trading operations the second one that I'm going to use is see position info which is getting if there are positions open getting that position info and see order info so if there are orders placed on the server getting the information on those orders and I'm storing these into variables called trade POS and O So now after referencing to all of this and creating these three variables now if I go back down to where we were
writing the course and by order and just as an example if I write here again trade which is the way variable that we have created for trading operations and hit dot it will open up all of the operations that you can do and this is the one bu stop that we selected so bu stop and then it's going to ask for volume which is how many lot size and that we give as Lots that's the variable that we have created here then if you hit comma then it asks you for what's the price and the
price we want to enter is here entry then what's the symbol and there we say symbol which is the current chart then what's the stop-loss and we calculated the stoploss here then what's the DP and we calculated the DP here then it asks for what type of order are you sending when it's going to expire and we say it is good until the time that we say it should expire and then we provide the expiration time and then we end with a colon so this is how this code is written I'm going to delete this
code and here we have have our send by order function that we can use to send orders now let's create the send sell order as well which is the same as order send buy order the only difference is now this time around we're going to be looking for bid because sell orders are done on bids so we're going to ask for bid and then we're going to say if the bid is lower than the entry plus order distance point then return so if the price is too close so here for example here if the price
was here then we don't want to send the order because the price is way too close and then TP is this time around minus TP points because the TP for a sell order needs to be below the price and then the stop loss is entry Plus sell point because it needs to be above the order price the rest is the same except again calculate lot size now we're going to have to send stop- loss minus entry because a stop loss is bigger than the entry if this is the order point then the stop- loss is
going to be above so it's going to be a bigger number and TP is going to be below so it's going to be a smaller number and that's the adjustment that we making here that they have to start with stop- loss minus the entry to calculate the lot size then we use the same formula for expiration and now we use trade do sell a stop instead of buy a stop we provide the ls entry size stop loss and everything exactly the same way now let's compile the year to see if we have any errors so
far and we do have two errors the first one is the bars n is undefined variable yeah because it should be bars n and then the other one is SL point is undefined Undeclared yeah because point should have a smaller p in it so if we compile it now we do not have any errors so we can continue [Music] now let's go back up here where we're going to start using the template functions the first one on initialization we want to set the expert magic number of the EA so we have collected here in the
input from the user what the magic number should be for the EA and this is where we set it up and the way to do it is basically when you WR write trade and then hit set it will be set expert magic number and then you just provide in the brackets what your variable for that is we don't need to do anything in on the initialization so we're going to leave it blank and I'm going to remove these spaces and then on to the ontick function so every time there is a new price data arriving
into mql that's when this portion of the CES are going to be triggered and this is where we're going to perform our operations so on every take we're going to run the course here so the first thing we're going to do is we're going to check if it's a new candle and this we do by checking if it's not a new bar then return so if it's not a new bar which is basically we're calling for this function that we have written here is new bar it comes down here and it checks whether it's a
new bar if it's a new bar then it Returns the value true if it's not a new bar it Returns the value false and if the value is false and this sign is not sign so not is new bar so if the value comes back as false which means it's not a new bar then just simply return meaning exit the function don't run anything below and this is how we create a section here which is going to be run only when there is a new bar then we get the current time and we do that
by creating a variable by the name time and the type of the variable is mql data time and then we say current time time to structure it's a way of breaking down the total time into hours and days and minutes and seconds so we say time to structure and then we say time current which is a function available within mql to get the current time of the current candle and then we store that value into this variable that we have created and now if we say int and I'm creating a variable our now so creating
a variable int our now and now using that variable that we have created here time and if we hit dot then we can get the the day of the week the day of the year the hour and the one that we are interested in is finding the hour and this we do so that we can compare the hour now with our trading hours that we have asked the user to Define so if the user has defined the hour start hour and end hour as 800 hours in the morning until 2100 hours in the evening then
we say that if our now is less than the asset choice so start our choice that we have created here then basically close all the orders which are currently on the server and return and then if our now is greater than or equal to whatever the end our choices and the end of choice is not zero so it's not inactive because it can also be inactive in which case it doesn't matter then close all the orders and exit this function which then reminds me we need to create this function close all orders as well so
let's go down to the bottom and here we create another function by and call it close all orders so whenever we will call this function the mql is going to come here and it's going to go through again a loop of all the order total so orders total is how many orders are available on the server currently minus one because I starts with zero so if the value of the orders total is one then we want to to start at zero and then as long as I is greater than or equal to zero then continue
running and decrease the I every time by one until it hits zero now order select by index of I and then check the ticket number by assigning the order ticket number ticket and this ORD is the variable that we have created here the c order info and order info basically brings back all of the order information which is available on the server so for example if I again write here or dot then all of the information available pertaining to that order is available here what we are interested in after selecting it by index I is
looking at its ticket number and is storing the ticket number into a uong and then we go if the order symbol so the symbol of the current order equals to the symbol of our chart and the magic number of the order equals to our magic number that means that this order has been placed by this EA because it has that magic number then use the trade and delete that order by using this ticket number once we have created that we can come back here so if the hour currently is before the start hour then close
all the orders if there are any orders and then return and if the hour is bigger than what bigger than or equal to whatever is the end our choice and the end our our choice is not inactive then in that case also close all the orders and exit the function now the next thing we're going to do here is because we did not assign a value to start our choice and end our choice so what we can do here is before checking this we say s choice is equals to sh input which is the variable
here where the the user selects which hour and then the end hour Choice equals in our input the input that the user selects here going back to metat Trader here and here okay coming back to our ontick function if the hour now is outside of the trading period defined by the user then exit so if mql comes down here to this line that basically means that we are in the trading period so we can continue now let's check if there are any open positions already because if there are already open positions then I don't want
to send another order so let's start by creating a buy total variable and a sell total variable and I'm starting with a position of zero then we check if there are any buy or sell open positions by looping through total number of positions selecting them by index and then checking whether it's a buy position on the current symbol and using the magic number because if it is another year that has placed um a trade on the server we're not interested we are only interested if it is on the same symbol and on our magic number
then if it is then increase the the value of BU total by 1 by saying buy total Plus+ Plus+ basically means plus one and then if it is a cell type position then increase the cell total by one now I have a full video on how to check for open positions and open orders in my channel so if you want to have more details you can go check out that with as well then we check if there are already any orders placed by this by our EA for the current symbol because if there is already
a buy order then we don't want to send another buy order and if there's already a sell order then we don't want to send another sell order so we say if there is a buy type order already placed or a cell type order already placed for the current symbol and using our magic number then increase the buy total and set holder by by one as well so whether or not there is a position open or whether or not there is a order placed increase the buy total from 0 to 1 or two so it's not
zero anymore it can only be zero if there is no open position and there is no order placed so if there is no buy position open and there is no buy order placed on the server and that we know because the buy total is either zero or less than zero then go ahead and find the high by using the function that we have created and assign it to a value high and if the high is greater than zero meaning it's not an empty variable then send the by order and send the value of price as
or entry price as high so now the the code is going to go to this function we have created here and here it's going to use the value high as the entry value value to basically send a Buy stop for the lot size that we calculate for the entry that we have just found high for the stop loss and take take profit that we have calculated within this function the same is true for the cell that if by going through these Loops we find that the total number of either cell positions currently or cell orders
on the server is zero by saying less than or equal to zero then go ahead and find the the Low by using this function and put the value of that low in a variable by the name low and if low is greater than zero so it's not an empty variable then go ahead and execute the send cell order function that we have created here by using the value of the low and sending a cell stop with the lots that we calculate here with the entry that comes out of here on the current symbol and using
our own magic number now let's go ahead and compile and now let's go back to metat Trader and see if what codes we have written so far they work so if I go back to metat Trader and we have our scalloping robot in the strategy tester if I go to input all of the input variables that we have defined for the user to select are there we're starting at 800 hours and we're ending at 2100 hours let's start and it looks horrible with the grids so I'll go back to meta editor and on the initialization
of the EA I will remove the grid by saying chart integer grid as false I hate these grid lines so I'll compile it again and now we go back to metatrader and if we start the test again now we don't see the GDs so now at this point the EA has started but because it's 4:50 in the morning the EA is not doing anything so we wait until it's 800 hours and around at 800s hours it will start to place the buy orders and the sell orders now what we see is it has placed the
buy order and the sell order has been placed at this value and the buy order has been placed at that value so the first time it could find looking backwards it could find this point as a point where there were five bars to the left and five bars to the right and at this point it found five bars to the left and five bars to the right so it has placed the by orders now the only thing that the EA still does not have is once the the buy order is triggered or the sell order
is triggered that and we go into profit then we want to Trail the stop- loss very very aggressively so now let's start building the trailing stop as well so I go back to metatrader and now we need to write a function for trailing the stop loss so let's come down to the bottom and let's create a function and let's call it Trail stop and we stop with curly bracket so all the codes that we're going to right here between these two curly brackets are going to be run whenever we're going to call this function Trail
stop we start by creating two variables stoploss and TP and these are both doubles because they're going to store decimal values and I'm starting them with a value of zero then let's get the value of the ask and the bid prices by creating two variables ask and bid and by using the function available to us in mql as symbol info double and then providing the symbol value and asking for the ask price and asking for the bid price now we're going to create a loop and what we're creating a loop for is we're going to
go through all of the positions which are currently open by looking at the function position total so by position total we get the value of all of the positions which are currently open and we assign it to the variable that we create as I for the loop function and we say minus one because I needs to start at0 so if there's one position open then we need to have I as zero so I start with position total minus one so it's zero and as long as I is greater than zero continue to run this Loop
and let's say if the position total was three so there were three positions open so the I will start at the value of two and then I minus minus meaning decrease it by 1 so first time it's 2 then it comes back then I equals 1 then it runs the code and then comes back and I equals 0 as long as it's greater than and equal to Z it will continue run on it now we will use the variable that we created at the beginning as POS which is the position information is stored in this
variable we select by index I and then for that position we get the value of the ticket number so here we get the total positions and We're looping through those positions so I is whatever the position number is so we select it and then for that selected position we get the ticket number for that position and we close the bracket here so I'm going to give it a bit more space so what we have done so far is we've gotten the stop loss we started the stop loss at zero TP as zero then we got
the current ask and bid price Now We're looping through all the positions and we're selecting the positions bying index and when we select the position by index we get its ticket number now what we can start to do is check we check first if the position's magic number is our magic number so the position is ours and that it is on the correct symbol by checking the position symbol and comparing it versus the chart symbol now if the position type is BU position type Buy then we check that if the bid minus position price open
meaning whatever price the position was open if the bid minus that is greater than what we had defined as our triggering of the stop loss remember at the beginning we had the trigger stop-loss points at 15 how much the price needs to be in profit before we start to trigger the trailing stop and how much do we Trail the stop-loss by so this is the the first one we say if the current price is minus the opening price is bigger than 15 points that means now we are in profit more than 15 points then we
take the TP as the positions take profit so getting the current takeprofit of the position but we change the stop loss by saying that the stoploss needs to be what the bid is what the price is currently minus the trailing stop-loss points by how much to Trail it by and then we say if the stop loss is greater than the current stop loss of the position because we don't want to go down once we have moved the stop loss up we don't want to Trail it back down and that by no stupid calculation that the
stop loss is zero so we check that the stop- loss is not zero this sign is not so it's not equal to zero then if both of these conditions are true then we change the positions stop loss by saying trade do position modify and what are we modifying we send the ticket number the same ticket number we send the new stop- loss and we send the old TP so the thing that we're changing is the stop loss and then we say if it's not position type buy then else if it is position type cell then
we're basically checking if the ask plus trigger point because now we need to check that the price has come below by more than 1 and 1/2 pip than the than the positions open price so now we're checking that if ask plus the trigger point the 15 points if the ask and I add 15 points to it is is still below the opening price of the position then we were basically 1 and 1/2 Pips in profit and then we change the stop- loss again and the way to change the stop- loss for a sell position is
that we go ask plus the trailing stop-loss points and then if the new stop- loss is smaller than the previous stop loss because we do not want the stop- loss to go up we only want it to Trail downwards and that the stop loss is not zero then we modify the position exactly the same way as we modified it here by sending the ticket value stop-loss value and DP value now guys the brackets open and closing is crucial here so I'm going to show it one more time because there are a lot of brackets opening
and closing so you need to have all of this trailing and then if I compile it let's hope that I got all the brackets closing correctly yeah it seems like I've got all the brackets closed down correctly once again I'll leave it on the screen the entire trailing stop code and let me make it smaller so that it can all be in one place so you can see all of the brackets and not miss some opening or closing of the bracket yeah so now the entire Trail stoploss function is in one place so you can
type it correctly with the correct parenthesis closing and opening so once we have written that then we go back to the on tick function because we have now a trailing stop-loss function but we need to apply it on every tick now we said if it's new bar then return so everything below here is only going to be run on a new bar so we need to write this above that trail stop-loss because this one we want to check that on every tick not on every new candle but we want to check it on every tick
so we call this function Trail stop that we have written on every tick before the new bar is checked so now if I compile it I have no errors now when we will run the test it will go very quick so one of the things that I do want to check is that my trailing stop- loss is working correctly so I'm going to put a Mark here and a Mark here and a Mark here and a Mark here because I want the test I want the test to stop whenever the code comes here so I'll
hit the play button and now the test will start but the test will stop whenever it will get to that code so the testing has started and we've got our bu stop here which has been triggered and this is why now if we go back here what we see here there is the plus button which basically means that there is the price has moved in our Direction by more than 1 and a/2 pip and this is why the code has come here because before this the code will never come here it will only come here
if this condition is met so if I hit play one more time then it goes to the modify the trade and then if I go back then I can see here that here our our trade was executed and because the price moved in our Direction by more than 1 and 1/2 pip the stop now has started to Trail and it's trailing by one pip now if I go back to our code and hit plus again then it just continues which basically means that here our stop loss was hit and then the trading just continued now
let's check it for one time for this for the sell stop as well and here I think our sell has been triggered and if we go back here to meta editor we see that now the code has stopped on this line which basically means that there is an open position of sell and it has moved in our Direction by more than 1 and 1/2 pip and that is why this code is triggered so if I hit plus and I hit again the same button to modify the stop- loss and then go back to our test
what I will see is that here our trade was triggered it went back a little bit and then once the price moved in our favor by more than 1 and 1/2 pip now the stop loss is is starting to Trail if I go back to man Trader and hit plus again and then again and come back here now the price has moved more in our favor so the stop- loss is moving along with the price so the trailing stop is working so I'll end the test and I'll remove these blocks so now what we can
do is we can simply go back to the meta editor here is our trading robot and then if I go to inputs I can Define here here what percentage do I want let's say I want 2% I want my take profit to be 30 Pips I want my stop loss to be 25 Pips I want my stop losses to start trailing when I in profit by two Pips I want the stop- loss to trail by say 10 Pips I want to run it on 5 minutes chart I want the comments to be my amazing scalping
robot I want the trading to start at 800 hours that is server time not your computer time that is the server time so you need to be careful about that and then if I start the test we should now see the back test as you have seen in the beginning of this video and if I pull this up we already start to see the result here because it's a 2% risk so we had a loss so we had 20 uh EUR loss I'll speed up the test a little bit to make it a little bit
go faster and then we start to have our profit trades our loss trades currently we are in €23 of loss I'll run it faster we're starting to get into profits €29 I'll speed up the test test has finished and we are ending on up in total around €4,000 of profit more or less similar to the test that I had run at the beginning we were running the test on gpp USD 5 minutes from the beginning of 2023 until July the 10th now when you will run your test because you will be using different inputs you
might get a slightly different results but one thing which you will see is that your short trades win would be above 90% your long trades won would be above 90% you might end up with a different numbers here and maybe in draw down depending on what risk percentages you choose what time frame do you use what take profit and stop-loss settings you use what time frame do you run it on and what trading period do you run it on normally my advice would be gpp USD and Euro USD to start somewhere around 7:00 UK time
and finish around 2100 hours UK time for USD JPY because the Japanese Market work during the night as well so I would say something like 2:00 in the morning UK time until 2100 hours UK time in the night my default positions are these these are the ones that I normally use except for that I use risk as 2% not three these are the default positions that I had used in my testing that you had seen before so go ahead you can run these test on gbb USD USD JPY and your USD now the last part
is applying the EA to a chart which is once you have connected to your account a real account or a demo account whichever one you want you need to open up a chart gbpusd for example you need to open up algo trading and then you need to bring your expert to the Chart you need to allow elgo trading here as well then you go to the inputs Tab and in the inputs tab you have all of the user inputs that you can provide say you want to start at 800 hours on gpp USD and you
want to end at 2100 hours on USD gpp USD you want the EA number to be this you want the time frame to be 5 minutes and then again your take profit is stop- loss how many points do you want to Trail the stop- loss and how much in profit do you want to be before you start trailing the stop- loss and then you click okay and then your EA is applied to the chart and it will start to trade during the trading hours that you wanted to trade I'm going to remove it because I'm
already in in a live environment and I don't want to apply it in that live environment so this is it guys I hope you enjoyed it uh I'll combine both the parts into one part and release it again so it can be in in one place please leave me a comment if there's anything which you did not understand if there is any part of the code which was complex if there is any more explanation and I'll make separate video to then explain that I will come back with more variations of the EA again depending on
how how much you like it it can also be applied to bitcoin oil and stock indices with certain tweaks that we will need to make in the code so I will come back with more video videos on what tweaks to make in order to apply it on those charts there are some coding changes but the base will remain the same so until next time guys stay safe trade safe [Music]