Code: Select all
#include <Trade\Trade.mqh>
CTrade trade;
//inputs
input double tradelot = 0.01; // Lot Size
static input long MagicNumber = 989658; // Magic Number
input ENUM_TIMEFRAMES IndicTime = PERIOD_M30; // chart Time Frame
input int IndicPeriod = 23; // Indicator Period
input int formula = 1; // formula 1 or 2
int indicline ,barsTotal,indiclow;
double indicBuffer[],lowBuffer[];
//+------------------------------------------------------------------+
int OnInit()
{
//---
trade.SetExpertMagicNumber(MagicNumber);
indicline = iMA(_Symbol,IndicTime,IndicPeriod,0,MODE_EMA,PRICE_HIGH);
indiclow = iMA(_Symbol,IndicTime,IndicPeriod,0,MODE_EMA,PRICE_LOW);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
if(indicline != INVALID_HANDLE){IndicatorRelease(indicline);}
if(indiclow != INVALID_HANDLE){IndicatorRelease(indiclow);}
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
double close1 =iClose(NULL,IndicTime,1);
//------HandleTrade -------------------
double high1 = iHigh(NULL,IndicTime,1);
double low1 = iLow(NULL,IndicTime,1);
double high = iHigh(NULL,IndicTime,0);
double low = iLow(NULL,IndicTime,0);
if(formula ==1) {
int values = CopyBuffer(indicline,0,1,1,indicBuffer);
Print(indicBuffer[0]);
int values2 = CopyBuffer(indiclow,0,1,1,lowBuffer);
if( indicBuffer[0] <= close1) //BuyCall
{ if(buytotal()==0)trade.Buy(tradelot);
CloseSellCall();}
if(lowBuffer[0]>= close1) // CLose Buy
{ CloseBuyCall();
if(selltotal()==0) trade.Sell(tradelot); }
}
if(formula ==2) {
int values = CopyBuffer(indicline,0,1,1,indicBuffer);
Print(indicBuffer[0]);
int values2 = CopyBuffer(indiclow,0,1,1,lowBuffer);
if( lowBuffer[0] <= close1) //BuyCall
{ if(buytotal()==0)trade.Buy(tradelot);
}
if(lowBuffer[0]>= close1) CloseBuyCall();
if(indicBuffer[0]>= close1) // CLose Buy
{
if(selltotal()==0) trade.Sell(tradelot); }
}
if(indicBuffer[0]<= close1)CloseBuyCall();
}
//+------------------------------------------------------------------+
int buytotal()
{ int buy = 0;
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){
buy++;
}}} return(buy) ;
}
int selltotal()
{ int sell = 0;
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){
sell++;
}}} return(sell) ;
}
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){
trade.PositionClose(positionTicket);
}}}}
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){
trade.PositionClose(positionTicket);
}}}}