Forex Automatic trading robot for MT5 demo account
Posted: Fri Sep 22, 2023 4:04 pm
trading ea coding tutorial program details posted in youtube channel video link
Code: Select all
#include <Trade\Trade.mqh>
CTrade trade;
//inputs
static input long MagicNumber = 989658; // Magic Number
input int takeprofit = 5; // Take profit dollar
input int roundprofit = 100 ; // round profit dollar
input double tradelot = 0.01;// starter Lot Size
input ENUM_TIMEFRAMES handle = PERIOD_H4; // chart handle period
static datetime time = 0;
static double tradelots = tradelot;
static double eaprofit = 1;
static double eanegative =0;
//+------------------------------------------------------------------+
int OnInit()
{
//---
//Set Magic number to trade objects
trade.SetExpertMagicNumber(MagicNumber);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//HandleTrade
//----------------------------------------
double open1 = iOpen(NULL,handle,1);
double close1 = iClose(NULL,handle,1);
double high1 = iHigh(NULL,handle,1);
double low1 = iLow(NULL,handle,1);
double high = iHigh(NULL,handle,0);
double low = iLow(NULL,handle,0);
// order open
if(time==0 || (time != iTime(NULL,handle,0)) ){
if(SellTotal()>=0 && low1 > low )
{
time = iTime(NULL,handle,0);
trade.Sell(tradelot);
}}
if (SellTotal() > 0 ) CloseSellCall();
//buy orders
if(time==0 || (time != iTime(NULL,handle,0)) ){
if(BuyTotal()>=0 && high1 < high )
{
time = iTime(NULL,handle,0);
trade.Buy(tradelot);
}}
if(BuyTotal() > 0 ) CloseBuyCall();
//==========================
//closeprofit
roundclose();
Print(eaprofit,"=1 profit");
}
//+------------------------------------------------------------------+
int SellTotal()
{
int total = PositionsTotal();
int count = 0;
for(int i=total-1; i>=0; i--) {
ulong positionTicket = PositionGetTicket(i);
if(positionTicket<=0){Print("Failed to get ticket"); return false;}
if(!PositionSelectByTicket(positionTicket))
{Print("Failed to select position"); return false;}
long magic;
if(!PositionGetInteger(POSITION_MAGIC,magic))
{Print("Failed to get magic"); return false;}
if(magic==MagicNumber) {
long orderType;
if(!PositionGetInteger(POSITION_TYPE, orderType))
{ Print("Failed to get order type"); return false;}
if(orderType==POSITION_TYPE_SELL){count++;}
}
}
return count;
}
void CloseSellCall()
{
int total = PositionsTotal();
for(int i=total-1; i>=0; i--) {
ulong positionTicket = PositionGetTicket(i);
if(positionTicket<=0){Print("Failed to get Ticket"); }
if(!PositionSelectByTicket(positionTicket))
{ Print("Failed to select position"); }
long magic;
if(!PositionGetInteger(POSITION_MAGIC,magic))
{Print("Failed to get magic");}
if(magic==MagicNumber){
long orderType;
if(!PositionGetInteger(POSITION_TYPE,orderType))
{Print("failed to get order type"); }
if(orderType==POSITION_TYPE_SELL){
if(PositionGetDouble(POSITION_PROFIT) > takeprofit ){
eaprofit=eaprofit+PositionGetDouble(POSITION_PROFIT);
trade.PositionClose(positionTicket); }
}}}
}
//============== buy total
int BuyTotal()
{
int total = PositionsTotal();
int count = 0;
for(int i=total-1; i>=0; i--) {
ulong positionTicket = PositionGetTicket(i);
if(positionTicket<=0){Print("Failed to get ticket"); return false;}
if(!PositionSelectByTicket(positionTicket))
{Print("Failed to select position"); return false;}
long magic;
if(!PositionGetInteger(POSITION_MAGIC,magic))
{Print("Failed to get magic"); return false;}
if(magic==MagicNumber) {
long orderType;
if(!PositionGetInteger(POSITION_TYPE, orderType))
{ Print("Failed to get order type"); return false;}
if(orderType==POSITION_TYPE_BUY){count++;}
}
}
return count;
}
//================== close buy call
void CloseBuyCall()
{
int total = PositionsTotal();
for(int i=total-1; i>=0; i--) {
ulong positionTicket = PositionGetTicket(i);
if(positionTicket<=0){Print("Failed to get Ticket"); }
if(!PositionSelectByTicket(positionTicket))
{ Print("Failed to select position"); }
long magic;
if(!PositionGetInteger(POSITION_MAGIC,magic))
{Print("Failed to get magic");}
if(magic==MagicNumber){
long orderType;
if(!PositionGetInteger(POSITION_TYPE,orderType))
{Print("failed to get order type"); }
if(orderType==POSITION_TYPE_BUY){
if(PositionGetDouble(POSITION_PROFIT) > takeprofit ) {
eaprofit=eaprofit+PositionGetDouble(POSITION_PROFIT);
trade.PositionClose(positionTicket);}
}}}}
void roundclose() {
//roundclose
Print(eanegative, "=2 ea negative");
int total = PositionsTotal();
for(int i=total-1; i>=0; i--) {
ulong positionTicket = PositionGetTicket(i);
if(positionTicket<=0){Print("Failed to get Ticket"); }
if(!PositionSelectByTicket(positionTicket))
{ Print("Failed to select position"); }
long magic;
if(!PositionGetInteger(POSITION_MAGIC,magic))
{Print("Failed to get magic");}
if(magic==MagicNumber){
long orderType;
if(!PositionGetInteger(POSITION_TYPE,orderType))
{Print("failed to get order type"); }
if(orderType==POSITION_TYPE_SELL){
if(PositionGetDouble(POSITION_PROFIT) <0 ) {
eanegative = eanegative +PositionGetDouble(POSITION_PROFIT);
}}
else if(orderType==POSITION_TYPE_BUY){
if(PositionGetDouble(POSITION_PROFIT) <0 ) {
eanegative = eanegative +PositionGetDouble(POSITION_PROFIT);}
}}}
Print(( eanegative),"...3nd", eaprofit , "--4th profit");
//closecall
if((eaprofit + eanegative) > roundprofit){
int total2 = PositionsTotal();
for(int i=total2-1; i>=0; i--) {
ulong positionTicket = PositionGetTicket(i);
if(positionTicket<=0){Print("Failed to get Ticket"); }
if(!PositionSelectByTicket(positionTicket))
{ Print("Failed to select position"); }
long magic;
if(!PositionGetInteger(POSITION_MAGIC,magic))
{Print("Failed to get magic");}
if(magic==MagicNumber){
long orderType;
if(!PositionGetInteger(POSITION_TYPE,orderType))
{Print("failed to get order type"); }
if(orderType==POSITION_TYPE_SELL){
trade.PositionClose(positionTicket); }
else if(orderType==POSITION_TYPE_BUY){
trade.PositionClose(positionTicket); }
eaprofit =0;
}}
} eanegative=0;
}