fbpx ...

Before we get technical, we need to get a grasp of the basic functions and tasks we will be doing to get from point A to point B. 

Starting with Point A, where you may not have any experience in either major subjects of finance and/or coding. This lesson will take you through the very basics of building a robot. Do NOT try and understand everything all at once. This course guides you through everything you need and the learning of each part will come later.

Like with any objective or action, there is a point to it, and that is to get to Point B, where we know how a robot works and can implement it into a trading platform to actually place orders, without our intervention.

Let’s get into it..

General Workflow

Code The Robot  ||  Test it  ||  Analyse  ||  Run Live Test  ||  Run Live

Part 1: Building It

Well... Copy and Paste it.

We build code in the MetaEditor software downloaded previously. Code can be copied and pasted from this lesson (everything in the green section below). To create a new MetaEditor file, you go to File, then New Project. Then click next, and give your algo file a title in the name section. Then simply click next all the way through and then Finish. 


//|                                                  DemoLesson1.mq5 |

//|                        Copyright 2019, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |


#property copyright “Copyright 2019, MetaQuotes Software Corp.”

#property link      “https://www.mql5.com”

#property version   “1.00”


CTrade trade;

int TSMM = 0;

int TSD = 0;

int TSH = 0;

int WeeklyBollinger;

double Ask = 0.0000;

double Bid = 0.0000;

int MagicNumber = 1114;

double WBol [];

double StopLoss = 0.00;

double Target = 0.00;

double WBolo [];

string Symbols;

double SwapLong = 0.00;

double SwapShort = 0.00;

double LotSize = 1.00; //Change lotsize calc once done testing for topstep

double LotCalc = 0.00;

input double Width = 2.8;

int Highest = 0;

int Lowest = 0;

double HP = 0.00;

double LP = 0.00;

double WB = 0.00;

double WBo = 0.00;

double MinVol = 0.00;

double MaxVol = 0.00;


double Balance = 0.00;

double BalancePercent = 0.10;

double Profit = 0.00;

double StopPercent = 0.01;

double TargetPercent = 0.0003;

void OnTick()


  // Minute 

  MqlDateTime TripleSwapMin;


  TSMM =  TripleSwapMin.min;



  MqlDateTime TripleSwapHour;


  TSH =  TripleSwapHour.hour;


  //Day of week  

  MqlDateTime TripleSwapDay;


{   TSD =  TripleSwapDay.day_of_week;

  Balance = AccountInfoDouble(ACCOUNT_BALANCE);

  Profit = (AccountInfoDouble(ACCOUNT_EQUITY)-AccountInfoDouble(ACCOUNT_BALANCE));



      // Data building

      Ask = NormalizeDouble(SymbolInfoDouble(Symbol(), SYMBOL_ASK), _Digits);  

      Bid = NormalizeDouble(SymbolInfoDouble(Symbol(), SYMBOL_BID), _Digits);  

      ArraySetAsSeries(WBol, true);

      ArraySetAsSeries(WBolo, true);

      WeeklyBollinger = iBands(Symbol(), TimeFrame, 20, 0, Width, PRICE_CLOSE);

      CopyBuffer(WeeklyBollinger, 1, 0, 3, WBol);   //upper band

      CopyBuffer(WeeklyBollinger, 2, 0, 3, WBolo);   //lower band

      WB = WBol[0]; //High Band of current candle

      WBo = WBolo[0];  // lower band

      Lowest = iLowest(Symbol(), TimeFrame, MODE_LOW, 100, 1);

      Highest = iHighest(Symbol(), TimeFrame, MODE_HIGH, 100, 1);

      HP = iHigh(Symbol(), TimeFrame, Highest);

      LP = iLow(Symbol(), TimeFrame, Lowest); 

      StopLoss = (Bid*StopPercent);

      Target = (Bid*TargetPercent);

      MinVol = SymbolInfoDouble(Symbol(), SYMBOL_VOLUME_MIN);

      MaxVol = SymbolInfoDouble(Symbol(), SYMBOL_VOLUME_MAX);

      LotCalc = ((AccountInfoDouble(ACCOUNT_BALANCE))/20000);

      LotSize = NormalizeDouble(MathMin((MathMax(LotCalc, MinVol)), (MathMin(LotCalc ,MaxVol))), 2);


   // Action set   

   if((TSD<4)&&(WB>0.01) && (LP>0.01) && (Ask>WB)&& (CheckForPositionShort()<1) && (Bid>LP))



     trade.Sell(LotSize, Symbol(), 0, (Bid+StopLoss), (Bid-Target), “Short”);



    if((TSD<4)&&(WBo>0.01) && (LP>0.01) && (Ask<WBo) && (CheckForPositionLong()<1) && (Bid<HP))



    trade.Buy(LotSize, Symbol(), 0, (Bid-StopLoss), (Bid+Target), “Long”);





 int CheckForPositionShort()


int PositionCount =0;

for(int i =PositionsTotal()-1; i>=0; i–)


     ulong PositionTicket = PositionGetTicket(i);

      if (PositionSelectByTicket(PositionTicket) && ((PositionGetInteger(POSITION_TYPE)) == POSITION_TYPE_SELL)&&

      (PositionGetString(POSITION_SYMBOL)==Symbol()) &&

      (PositionGetInteger(POSITION_MAGIC) == MagicNumber))


      (PositionCount += 1) ;





int CheckForPositionLong()


int PositionCount =0;

for(int i =PositionsTotal()-1; i>=0; i–)


     ulong PositionTicket = PositionGetTicket(i);

      if (PositionSelectByTicket(PositionTicket) && ((PositionGetInteger(POSITION_TYPE)) == POSITION_TYPE_BUY)&&

      (PositionGetString(POSITION_SYMBOL)==Symbol()) &&

      (PositionGetInteger(POSITION_MAGIC) == MagicNumber))


      (PositionCount += 1) ;





Delete all the content and then copy and paste the code below into the file. 

Now click Compile or press F7. 

Head over to your MetaTrader Platform, and your file will be in your navigator (you may need to refresh your platform or close and open it again).

Part 2: Test It

Now it is time to test it. In your MetaTrader platform, open the Strategy Tester (View, then Strategy Tester, you can also toggle CTRL R on your keyboard).

In the settings tab down the bottom of the Strategy tester, Select the expert (the file you made in MetaEditor). Select the Symbol to run the test on, I like to use AUDUSD but major FX pair should work ok.  

The timeframe doesn’t matter as the time frame has been preset in the code. Set date as ‘Last Year’. Modelling needs to be Every Tick, and Optimization set to ‘Disabled’. If you can’t see optimization, try moving the top border higher to allow the strategy tester area to expand. Press start, and it will load up the data and run the test.

You will see the graph showing results, and once it is done testing the timeframe selected, you can see more results in the Backtest tab.

Part 3: Analyse It

Check out the results and try seeing any major faults or issues. If all looks ok, its time to dive a little deeper. This deeper analysis will be covered throughout future lessons and come from experience as well. 

An easy chart to look at is the ‘Profits and losses by weekdays’ chart. If you see a particular day that is not performing, you can stop trading on that day. If you see a day that is making all the profit, you might choose to only trade on that day of the week. 

Sometimes it can be easier to determine improvements by looking at the visuals. Before running your next test, under the settings tab, tick the box next to ‘visual mode with…’ 

This option will show you trades being placed on a chart so you can visually see how the trades run. You can then make changes based on what you see if you think the algo could be better.

Part 4: Live test on Demo

This part is the fun part. Drag and drop your file (whatever you named it) from the Navigator (under Expert Advisors) onto the chart. Any chart will do really, this is just a test to see how things work.

The live test on a demo will show you a realistic example of how your algo will run on a live account. There will be a few minor differences such as difference in spread, more slippage etc. that you may notice but it will be more or less the same.

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.