How To Make An EA Which Is Able To Set SL&TP At A Certain Condition?

 

Hello Forex World,

How are you everybody? I am new in this world. I am learning about Forex everyday. During learning period, I have found an IDEA.

It is very simple. The IDEA is, in a single candlestick I want to open a trade if this candlestick goes up or down 10 pips from it's open position. After that, I want to set SL 5 pips up or down and also TP 5 pips up or down.

For example,

EURUSD current price = 1.2550

Think that, this market goes up and now price = 1.2560

I want to buy at this price. I will set take profit at 1.2565 and set stop loss at 1.2555

Again,

EURUSD current price = 1.3350

Think that, market goes down and current price = 1.3340

I want to sell at this price. I will set take profit at 1.3335 and set stop loss at 1.3345


Could you help help me to make a source code of this IDEA? I want to learn coding based on this. I think this will help me to learn how to code EA. So please give me an some suggestions or provide me a source code of this idea.


Thank you so much in Advance. :)

 
If no one helps you for free in this forum alternately you can post this idea as a job in Freelance section to hire some one to code it, then study and learn from that. There's plenty of online literature to gain knowledge from and the only step you are from that is to use a simple search engine.
 
//+------------------------------------------------------------------+
//|                                                         Test.mq4 |
//|                                   Copyright 2019, Catalin Zachiu |
//|                      https://www.mql5.com/en/users/catalinzachiu |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, Catalin Zachiu"
#property link      "https://www.mql5.com/en/users/catalinzachiu"
#property version   "1.0"
#property strict
//--- Inputs
input double Lots          =0.1;
input int MAGIC            =12345;
int BarTime  =0;
//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
  {
   int buys=0,sells=0;
//---
   for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC)
        {
         if(OrderType()==OP_BUY)  buys++;
         if(OrderType()==OP_SELL) sells++;
        }
     }
//--- return orders volume
   if(buys>0) return(buys);
   else       return(-sells);
  }

void OnTick()
  {
   
   int    res;

//--- sell conditions
   if(Bid>=Open[0]+100*Point && CalculateCurrentOrders(Symbol())==0)
     { 
       if(BarTime!=Time[0]) {
      res=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+50*Point,Bid-50*Point,"",MAGIC,0,Red);
    
     }
     BarTime=Time[0];
    } 
//--- buy conditions
   if(Ask<=Open[0]-100*Point && CalculateCurrentOrders(Symbol())==0)
     {
       if(BarTime!=Time[0]) {
      res=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Bid-50*Point,Ask+50*Point,"",MAGIC,0,Blue);
    
     }
     BarTime=Time[0];
   }  
//---
  }

I've reversed your trading coditions , test on EUR/USD , timeframe H1 , set spread setting to 2 in the tester .

 
Kenneth Parling:
If no one helps you for free in this forum alternately you can post this idea as a job in Freelance section to hire some one to code it, then study and learn from that. There's plenty of online literature to gain knowledge from and the only step you are from that is to use a simple search engine.
Thank you for your kind information. I am trying to learn using search engine. That's how I have found MQL5. 
 
Catalin Zachiu:

I've reversed your trading coditions , test on EUR/USD , timeframe H1 , set spread setting to 2 in the tester .

Thank you so much for your kind. I will learn this code to understand it. I will give you my learning feedback. Thank you again.
Reason: