Little help please for an EA

 

Hello, there, I have an EA, which profits are based on TrailingStopLoss. It has constant StopLoss, but doesn't have constant TakeProfit.

I'll ask any coder here to edit this EA to have TakeProfit, that can be defined by the user, and to remove the TrailingStopLoss.

Here is it:

#include

//---- Trades limits

extern double TakeProfit = 150;

extern double TrailingStop = 50;

extern double StopLoss = 50;

extern double Lots = 0.1;

extern int Slippage = 100;

//--- Indicators settings

extern int MA_Period=120;

extern int MA_Shift=60;

extern int Price=10;

extern int Mode=8;

//--- Global variables

int CurrentBar = 1;

int MagicNumber = 122334;

string ExpertComment = "EA";

int NumberOfTries = 1;

//+------------------------------------------------------------------

int init()

{

return(0);

}

int deinit()

{

return(0);

}

//+------------------------------------------------------------------

bool isNewSymbol(string current_symbol)

{

//loop through all the opened order and compare the symbols

int total = OrdersTotal();

for(int cnt = 0 ; cnt < total ; cnt++)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

string selected_symbol = OrderSymbol();

if (current_symbol == selected_symbol && OrderMagicNumber()==MagicNumber)

return (False);

}

return (True);

}

//+------------------------------------------------------------------+

int start()

{

int cnt, ticket, total;

double mSL; //Broker stop loss minimum value

double OsUSD_P = 0 , OsUSD_C = 0 ;

if(Bars < 100) {Print("bars less than 100"); return(0);}

OsUSD_C = USDINDEX(CurrentBar,MA_Period,MA_Shift,Price,Mode);

OsUSD_P = USDINDEX(CurrentBar+1,MA_Period,MA_Shift,Price,Mode);

//--- Trading conditions

bool BuyCondition = false , SellCondition = false , CloseBuyCondition = false , CloseSellCondition = false ;

int base = true;

if (Symbol() == "EURUSD" || Symbol() == "GBPUSD" || Symbol() == "AUDUSD") base = 1;

if (Symbol() == "USDJPY" || Symbol() == "USDCHF") base = 2;

if (base == 1)

{

if (OsUSD_C > 0 && OsUSD_P < 0 )

SellCondition = true;

if (OsUSD_C 0 )

BuyCondition = true;

}

if (base == 2)

{

if (OsUSD_C > 0 && OsUSD_P < 0)

BuyCondition = true;

if (OsUSD_C 0)

SellCondition = true;

}

total = OrdersTotal();

if(total < 1 || isNewSymbol(Symbol()))

{

if(BuyCondition) //<-- BUY condition

{

ticket = OpenOrder(OP_BUY); //<-- Open BUY order

}

if(SellCondition) //<-- SELL condition

{

ticket = OpenOrder(OP_SELL); //<-- Open SELL order

}

}

for(cnt=0;cnt<total;cnt++)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())

{

if(OrderType()==OP_BUY) //<-- Long position is opened

{

if(CloseBuyCondition) //<-- Close the order and exit!

{

CloseOrder(OrderType());

}

TrailOrder(OrderType());

}

if(OrderType()==OP_SELL) //<-- Go to short position

{

if(CloseSellCondition) //<-- Close the order and exit!

{

CloseOrder(OP_SELL);

}

TrailOrder(OrderType());

}

}

}

return(0);

}

//+------------------------------------------------------------------+

void TrailOrder(int type)

{

if(TrailingStop>0)

{

if(OrderMagicNumber() == MagicNumber)

{

if(type==OP_BUY)

{

if(Bid-OrderOpenPrice()>Point*TrailingStop)

{

if(OrderStopLoss()<Bid-Point*TrailingStop)

{

OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);

}

}

}

if(type==OP_SELL)

{

if((OrderOpenPrice()-Ask)>(Point*TrailingStop))

{

if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))

{

OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);

}

}

}

}

}

}

int OpenOrder(int type)

{

int ticket=0;

int err=0;

int c = 0;

if(type==OP_BUY)

{

for(c = 0 ; c < NumberOfTries ; c++)

{

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,Ask-StopLoss*Point,0 ,ExpertComment,MagicNumber,0,Green);

err=GetLastError();

if(err==0)

{

break;

}

else

{

if(err==4 || err==137 ||err==146 || err==136) //Busy errors

{

Sleep(5000);

continue;

}

else //normal error

{

Print("Error opening BUY order : ", ErrorDescription(err));

break;

}

}

}

}

if(type==OP_SELL)

{

for(c = 0 ; c < NumberOfTries ; c++)

{

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,Bid+StopLoss*Point,0,ExpertComment,MagicNumber,0,Red);

err=GetLastError();

if(err==0)

{

break;

}

else

{

if(err==4 || err==137 ||err==146 || err==136) //Busy errors

{

Sleep(5000);

continue;

}

else //normal error

{

Print("Error opening SELL order : ", ErrorDescription(err));

break;

}

}

}

}

return(ticket);

}

bool CloseOrder(int type)

{

Print("Asked to close!");

if(OrderMagicNumber() == MagicNumber)

{

if(type==OP_BUY)

return (OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Violet));

if(type==OP_SELL)

return (OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Violet));

}

}

double USDINDEX(int bar,int MA_Period,int MA_Shift,int Price,int Mode)

{

double USD_Index=

(iMA("EURUSD",0,MA_Period,0,Mode,Price,bar+MA_Shift)-

iMA("EURUSD",0,MA_Period,0,Mode,Price,bar))*10000

+

(iMA("GBPUSD",0,MA_Period,0,Mode,Price,bar+MA_Shift)-

iMA("GBPUSD",0,MA_Period,0,Mode,Price,bar))*10000

+

(iMA("USDCHF",0,MA_Period,0,Mode,Price,bar+MA_Shift)-

iMA("USDCHF",0,MA_Period,0,Mode,Price,bar))*10000

+

(iMA("USDJPY",0,MA_Period,0,Mode,Price,bar+MA_Shift)-

iMA("USDJPY",0,MA_Period,0,Mode,Price,bar))*100

;

return (USD_Index);

}

 

if U will set TrailingStop to 0 , traling stop will not be used.

As for order takeproft change this part:

OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage ,Ask-StopLoss*Point,0 ,ExpertComment,MagicNumber,0,Green)

to this

OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage ,Ask-StopLoss*Point,Ask+Traling*Point ,ExpertComment,MagicNumber,0,Green)

And opposite for short trade.

Regards

Kale

 

You type "Triling" , I understand it as "TrailingStop" and put it, but now the EA doesn't even open an order. Help.

 
evgeni1980:
You type "Triling" , I understand it as "TrailingStop" and put it, but now the EA doesn't even open an order. Help.

Try this:

#include

//---- Trades limits

extern double TakeProfit = 150;

extern double TrailingStop = 50;

extern double StopLoss = 50;

extern double Lots = 0.1;

extern int Slippage = 100;

//--- Indicators settings

extern int MA_Period=120;

extern int MA_Shift=60;

extern int Price=10;

extern int Mode=8;

//--- Global variables

int CurrentBar = 1;

int MagicNumber = 122334;

string ExpertComment = "EA";

int NumberOfTries = 1;

//+------------------------------------------------------------------

int init()

{

return(0);

}

int deinit()

{

return(0);

}

//+------------------------------------------------------------------

bool isNewSymbol(string current_symbol)

{

//loop through all the opened order and compare the symbols

int total = OrdersTotal();

for(int cnt = 0 ; cnt < total ; cnt++)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

string selected_symbol = OrderSymbol();

if (current_symbol == selected_symbol && OrderMagicNumber()==MagicNumber)

return (False);

}

return (True);

}

//+------------------------------------------------------------------+

int start()

{

int cnt, ticket, total;

double mSL; //Broker stop loss minimum value

double OsUSD_P = 0 , OsUSD_C = 0 ;

if(Bars < 100) {Print("bars less than 100"); return(0);}

OsUSD_C = USDINDEX(CurrentBar,MA_Period,MA_Shift,Price,Mode) ;

OsUSD_P = USDINDEX(CurrentBar+1,MA_Period,MA_Shift,Price,Mod e);

//--- Trading conditions

bool BuyCondition = false , SellCondition = false , CloseBuyCondition = false , CloseSellCondition = false ;

int base = true;

if (Symbol() == "EURUSD" || Symbol() == "GBPUSD" || Symbol() == "AUDUSD") base = 1;

if (Symbol() == "USDJPY" || Symbol() == "USDCHF") base = 2;

if (base == 1)

{

if (OsUSD_C > 0 && OsUSD_P < 0 )

SellCondition = true;

if (OsUSD_C 0 )

BuyCondition = true;

}

if (base == 2)

{

if (OsUSD_C > 0 && OsUSD_P < 0)

BuyCondition = true;

if (OsUSD_C 0)

SellCondition = true;

}

total = OrdersTotal();

if(total < 1 || isNewSymbol(Symbol()))

{

if(BuyCondition) //<-- BUY condition

{

ticket = OpenOrder(OP_BUY); //<-- Open BUY order

}

if(SellCondition) //<-- SELL condition

{

ticket = OpenOrder(OP_SELL); //<-- Open SELL order

}

}

for(cnt=0;cnt<total;cnt++)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())

{

if(OrderType()==OP_BUY) //<-- Long position is opened

{

if(CloseBuyCondition) //<-- Close the order and exit!

{

CloseOrder(OrderType());

}

TrailOrder(OrderType());

}

if(OrderType()==OP_SELL) //<-- Go to short position

{

if(CloseSellCondition) //<-- Close the order and exit!

{

CloseOrder(OP_SELL);

}

TrailOrder(OrderType());

}

}

}

return(0);

}

//+------------------------------------------------------------------+

void TrailOrder(int type)

{

if(TrailingStop>0)

{

if(OrderMagicNumber() == MagicNumber)

{

if(type==OP_BUY)

{

if(Bid-OrderOpenPrice()>Point*TrailingStop)

{

if(OrderStopLoss()<Bid-Point*TrailingStop)

{

OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);

}

}

}

if(type==OP_SELL)

{

if((OrderOpenPrice()-Ask)>(Point*TrailingStop))

{

if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))

{

OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);

}

}

}

}

}

}

int OpenOrder(int type)

{

int ticket=0;

int err=0;

int c = 0;

if(type==OP_BUY)

{

for(c = 0 ; c < NumberOfTries ; c++)

{

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage ,Ask-StopLoss*Point,Ask+TakeProfit*Point ,ExpertComment,MagicNumber,0,Green);

err=GetLastError();

if(err==0)

{

break;

}

else

{

if(err==4 || err==137 ||err==146 || err==136) //Busy errors

{

Sleep(5000);

continue;

}

else //normal error

{

Print("Error opening BUY order : ", ErrorDescription(err));

break;

}

}

}

}

if(type==OP_SELL)

{

for(c = 0 ; c < NumberOfTries ; c++)

{

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippag e,Bid+StopLoss*Point,Bid-TakeProfit*Point,ExpertComment,MagicNumber,0 ,Red);

err=GetLastError();

if(err==0)

{

break;

}

else

{

if(err==4 || err==137 ||err==146 || err==136) //Busy errors

{

Sleep(5000);

continue;

}

else //normal error

{

Print("Error opening SELL order : ", ErrorDescription(err));

break;

}

}

}

}

return(ticket);

}

bool CloseOrder(int type)

{

Print("Asked to close!");

if(OrderMagicNumber() == MagicNumber)

{

if(type==OP_BUY)

return (OrderClose(OrderTicket(),OrderLots(),Bid,Slippage ,Violet));

if(type==OP_SELL)

return (OrderClose(OrderTicket(),OrderLots(),Ask,Slippage ,Violet));

}

}

double USDINDEX(int bar,int MA_Period,int MA_Shift,int Price,int Mode)

{

double USD_Index=

(iMA("EURUSD",0,MA_Period,0,Mode,Price,bar+MA_Shif t)-

iMA("EURUSD",0,MA_Period,0,Mode,Price,bar))*10000

+

(iMA("GBPUSD",0,MA_Period,0,Mode,Price,bar+MA_Shif t)-

iMA("GBPUSD",0,MA_Period,0,Mode,Price,bar))*10000

+

(iMA("USDCHF",0,MA_Period,0,Mode,Price,bar+MA_Shif t)-

iMA("USDCHF",0,MA_Period,0,Mode,Price,bar))*10000

+

(iMA("USDJPY",0,MA_Period,0,Mode,Price,bar+MA_Shif t)-

iMA("USDJPY",0,MA_Period,0,Mode,Price,bar))*100

;

return (USD_Index);

}

 

Code that was changed:

OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage ,Ask-StopLoss*Point,0,ExpertComment,MagicNumber,0,Green);[/CODE]

to:

OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage ,Ask-StopLoss*Point,Ask+TakeProfit*Point ,ExpertComment,MagicNumber,0,Green);[/CODE]

and

OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippag e,Bid+StopLoss*Point,0,ExpertComment,MagicNumber,0 ,Red);[/CODE]

to

[CODE]OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippag e,Bid+StopLoss*Point,Bid-TakeProfit*Point,ExpertComment,MagicNumber,0 ,Red);

And I fixed traling:

[CODE]OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Po int*TrailingStop,OrderTakeProfit(),0,Red);

to

[CODE]OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
 

God bless you, Kalenzo, good job, thanks

The earliest date from that I can test the EA is 25.09.2006, how can I have earlier hystory, because I want to test from earlier date than 25.09?

 
evgeni1980:
God bless you, Kalenzo, good job, thanks The earliest date from that I can test the EA is 25.09.2006, how can I have earlier hystory, because I want to test from earlier date than 25.09?

Here is the post about backtesting

https://www.mql5.com/en/forum/general

U can also read about how to make backtest with the best modeling quality at codersguru web page www.metatrader.info