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
//======indicator============
input double sarstep = 0.02 ; //SAR step
input double sarmax = 0.20 ; //SAR Max
input ENUM_TIMEFRAMES sartime = PERIOD_H4; // SAR Time Period
//==========================
int sarline, barsTotal;
double sarBuffer[];
//=======================
static double eaprofit = 1;
static double eanegative =0;
//+------------------------------------------------------------------+
int OnInit()
{
//---
//Set Magic number to trade objects
trade.SetExpertMagicNumber(MagicNumber);
barsTotal = iBars(_Symbol,handle);
//==========indicator handle line =====================
// int iSAR(
// string symbol, // symbol name
// ENUM_TIMEFRAMES period, // period
// double step, // price increment step-acceleration factor
// double maximum // maximum value of step
// );
//===================================================
// Indicator Line
sarline = iSAR(_Symbol,sartime,sarstep,sarmax);
if(sarline == INVALID_HANDLE) {
Alert("Failed to create SAR line");
return (INIT_FAILED);
}
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
if(sarline != INVALID_HANDLE){IndicatorRelease(sarline);}
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
int bars = iBars(_Symbol,handle);
//------HandleTrade -------------------
double high1 = iHigh(NULL,handle,1);
double low1 = iLow(NULL,handle,1);
double high = iHigh(NULL,handle,0);
double low = iLow(NULL,handle,0);
//=========indicator value copy buffer =================
//int CopyBuffer(
// int indicator_handle, // indicator handle
// int buffer_num, // indicator buffer number
// int start_pos, // start position
// int count, // amount to copy
// double buffer[] // target array to copy
// );
//=======================================================
//Get Indicator buffer values
int values = CopyBuffer(sarline,0,1,3,sarBuffer);
if(values != 3) {
Print("Not enough data for trend Check");
return;
}
//order open
double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
if(high1 < high){
if(sarBuffer[1] < ask && sarBuffer[2] < ask) {
if(bars != barsTotal){ barsTotal = bars;
trade.Buy(tradelot);
Print(high1, "value 1 =", high);}}}
if(low1 > low ){
if(sarBuffer[1] > ask && sarBuffer[2] > ask ) {
if(bars != barsTotal){ barsTotal = bars;
trade.Sell(tradelot);
Print(low1,"low =",low);}}}
CloseSellCall(); //Close calls
CloseBuyCall();
//closeprofit
roundclose();
}
//+------------------------------------------------------------------+
//---------onTick-Close-------------------------------------------
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); }
}}}
}
//==============
//================== 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
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);}
}}}
//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;
}
//===============over==================