Forex



Go Back   Forex Trading > Downloads > Expert Advisors - Metatrader 4
Forex Forum Register More recent Calendar Advertising Others Help






Register
Welcome to Forex-TSD!, one of the largest Forex forums worldwide, where you will be able to find the most complete and reliable Forex information imaginable.

From the list below, select the forum that you want to visit and register to post, as many times you want. It’s absolutely free. Click here for registering on Forex-TSD.

Exclusive Forum
The Exclusive Forum is the only paid section. Once you subscribe, you will get free access to real cutting-edge Trading Systems (automated and not), Indicators, Signals, Articles, etc., that will help and guide you, in ways that you could only imagine, with your Forex trading.
  • Elite Section
    Get access to private discussions, specialized support, indicators and trading systems reported every week.
  • Advanced Elite Section
    For professional traders, trading system developers and any other member who may need to use and/or convert, the most cutting-edge exclusive indicators and trading systems for MT4 and MT5.
See more

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 11-10-2006, 09:23 PM
Member
 
Join Date: Sep 2006
Posts: 36
evgeni1980 is on a distinguished road
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 <stdlib.mqh>

//---- 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 && OsUSD_P > 0 )
BuyCondition = true;
}

if (base == 2)
{
if (OsUSD_C > 0 && OsUSD_P < 0)
BuyCondition = true;

if (OsUSD_C < 0 && OsUSD_P > 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+Poi nt*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,Slippag e,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_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);

}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #2 (permalink)  
Old 11-10-2006, 10:07 PM
Kalenzo's Avatar
Senior Member
 
Join Date: Dec 2005
Location: Bydgoszcz - Poland
Posts: 724
Kalenzo is on a distinguished road
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 need proffesional mql coder? Contact me! I will help you!
........................................
http://www.fxservice.eu/
........................................
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #3 (permalink)  
Old 11-11-2006, 10:02 AM
Member
 
Join Date: Sep 2006
Posts: 36
evgeni1980 is on a distinguished road
You type "Triling" , I understand it as "TrailingStop" and put it, but now the EA doesn't even open an order. Help.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #4 (permalink)  
Old 11-11-2006, 10:18 AM
Kalenzo's Avatar
Senior Member
 
Join Date: Dec 2005
Location: Bydgoszcz - Poland
Posts: 724
Kalenzo is on a distinguished road
Quote:
Originally Posted by 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:
Code:
#include <stdlib.mqh>

//---- 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 && OsUSD_P > 0 )
BuyCondition = true;
}

if (base == 2)
{
if (OsUSD_C > 0 && OsUSD_P < 0)
BuyCondition = true;

if (OsUSD_C < 0 && OsUSD_P > 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);

}
__________________
You need proffesional mql coder? Contact me! I will help you!
........................................
http://www.fxservice.eu/
........................................
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #5 (permalink)  
Old 11-11-2006, 10:22 AM
Kalenzo's Avatar
Senior Member
 
Join Date: Dec 2005
Location: Bydgoszcz - Poland
Posts: 724
Kalenzo is on a distinguished road
Code that was changed:
Code:
OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage ,Ask-StopLoss*Point,0,ExpertComment,MagicNumber,0,Green);
to:
Code:
OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage ,Ask-StopLoss*Point,Ask+TakeProfit*Point ,ExpertComment,MagicNumber,0,Green);

and


Code:
OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippag e,Bid+StopLoss*Point,0,ExpertComment,MagicNumber,0 ,Red);
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);
__________________
You need proffesional mql coder? Contact me! I will help you!
........................................
http://www.fxservice.eu/
........................................
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #6 (permalink)  
Old 11-11-2006, 04:46 PM
Member
 
Join Date: Sep 2006
Posts: 36
evgeni1980 is on a distinguished road
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?

Last edited by evgeni1980; 11-11-2006 at 05:29 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
  #7 (permalink)  
Old 11-12-2006, 10:08 AM
Kalenzo's Avatar
Senior Member
 
Join Date: Dec 2005
Location: Bydgoszcz - Poland
Posts: 724
Kalenzo is on a distinguished road
Quote:
Originally Posted by 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
http://www.forex-tsd.com/62781-post2.html
U can also read about how to make backtest with the best modeling quality at codersguru web page www.metatrader.info
__________________
You need proffesional mql coder? Contact me! I will help you!
........................................
http://www.fxservice.eu/
........................................
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!StumbleUpon this Post!Reddit this Post!Facebook this Post!BlinkList this Post!Google Bookmarks this Post!Yahoo! My Web this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump


All times are GMT. The time now is 02:22 AM.



Search Engine Friendly URLs by vBSEO 3.2.0 ©2008, Crawlability, Inc.