Forex
Google
New signals service!

Go Back   Forex Trading > Programming > Metatrader Programming


Register in Forex TSD!
Trading Systems Leaders in this forum (automated trading systems) are winning more than 3000 pips in a month (30000$ investing one lot every time).
Click here to register and get more information

Reply
 
LinkBack (1) Thread Tools Display Modes
  #1341 (permalink)  
Old 10-13-2008, 11:54 PM
Junior Member
 
Join Date: Aug 2008
Posts: 2
jani is on a distinguished road
Closing Multiple Position By Magic Number - Help

hello all.

I'm new in mt4 programming and below are my very first EA. The purpose of this EA is to close all position regardless any currency pair based on the same magic number.

Let say under magic # 8675310, i got 2 position EURUSD & USDCFh. It shall be able to close all these pair when it reaches certain profit target.

The problem with this EA is,
Open Transaction # 1 EURUSD (able to close)
Open Transaction # 2 USDCFh (won't be able to close)

IT WON'T BE ABLE TO CLOSE TRANSACTION # 2.

Any Ideas ?

Thanks

//+------------------------------------------------------------------+
//| Closing_Trade_By_Magic_No_v1 .mq4 |
//| Mine |
//| Forex Trading Software: Forex Trading Platform MetaTrader 4 |
//+------------------------------------------------------------------+
#property copyright "Mine"
#property link "http://www.metaquotes.net"

//---- input parameters
extern int MagicNumber1=8675310;
extern int Profit1=10;
extern int MagicNumber2=8675311;
extern int Profit2=15;
extern int MagicNumber3=8675312;
extern int Profit3=15;
extern int MagicNumber4=8675313;
extern int Profit4=15;
extern int MagicNumber5=8675314;
extern int Profit5=15;
extern int MagicNumber6=0;
extern int Profit6=10;


//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
onScreenComment(98,"Tengak Initialize..");
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
string myMessage="";
myMessage = myMessage + ProfitLossMonitor(1,MagicNumber1,Pr ofit1,myMessage);
myMessage = myMessage + ProfitLossMonitor(2,MagicNumber2,Pr ofit2,myMessage);
myMessage = myMessage + ProfitLossMonitor(3,MagicNumber3,Pr ofit3,myMessage);
myMessage = myMessage + ProfitLossMonitor(4,MagicNumber4,Pr ofit4,myMessage);
myMessage = myMessage + ProfitLossMonitor(5,MagicNumber5,Pr ofit5,myMessage);
myMessage = myMessage + ProfitLossMonitor(6,MagicNumber6,Pr ofit6,myMessage);

onScreenComment(98,myMessage);
//----

//----
return(0);
}

string ProfitLossMonitor(int myGroupNumber,int myMagicNumber, int myProfit,string myMessage )
{
int total = OrdersTotal();
double MyCurrentProfit=0;
string MyOrderNo="";
for (int cnt = 0 ; cnt < total ; cnt++)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_ TRADES);
if (OrderMagicNumber() == myMagicNumber)
{
MyCurrentProfit += OrderProfit();
MyOrderNo= MyOrderNo + "," + OrderTicket();
}
}

if(MyCurrentProfit>=myProfit)
CloseAll(myMagicNumber);

myMessage="Group Position #" + myGroupNumber + " " + myMagicNumber + "=" + myProfit + "(" + DoubleToStr(MyCurrentProfit,2) + " " + MyOrderNo + ")" + "\n" ;

return (myMessage);
}

void CloseAll(int myMagicNumber)
{
int total = OrdersTotal();
for (int cnt = 0 ; cnt < total ; cnt++)
{
OrderSelect(cnt,SELECT_BY_POS,MODE_ TRADES);
if (OrderMagicNumber() == myMagicNumber)
if(OrderType()==OP_BUY)
OrderClose(OrderTicket(),OrderLots( ),Bid,5,Violet);
if(OrderType()==OP_SELL)
OrderClose(OrderTicket(),OrderLots( ),Ask,5,Violet);
}
}

void onScreenComment(int myEvent, string myComment)
{
switch (myEvent)
{
case 98: Comment(myComment); break;
}
}


//+------------------------------------------------------------------+
Edit/Delete Message
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #1342 (permalink)  
Old 10-14-2008, 12:47 AM
cockeyedcowboy's Avatar
Senior Member
 
Join Date: Nov 2005
Posts: 347
cockeyedcowboy is on a distinguished road
Quote:
The problem with this EA is,
Open Transaction # 1 EURUSD (able to close)
Open Transaction # 2 USDCFh (won't be able to close)

IT WON'T BE ABLE TO CLOSE TRANSACTION # 2.

Any Ideas ?
I assume that you are running the ea on the EURUSD chart at the time you try to close orders. as those positions are closing ok. When your code trys to close the USDCHF (a little dislexies?) positions its using the wrong bid and or ask, the closing price it uses is from the currency pair that the ea is attached too not the price of the possitions symbol. If your trying to close another currency pair from a chart that doesnot match that currency, you must first retrive the correct price before trying to close it.

keit

edit: Also the currency, USDCHF in this case must be displayed in your market watch window at the time your trying to retreive the correct quotes to close the orders. If the USDCHF is not listed in the market watch window you will not be able to get any quotes for that pair.

Last edited by cockeyedcowboy; 10-14-2008 at 01:10 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #1343 (permalink)  
Old 10-14-2008, 02:43 AM
Junior Member
 
Join Date: Aug 2008
Posts: 2
jani is on a distinguished road
Thanks cockeyedcowboy !.

"you must first retrive the correct price before trying to close it".

Could your share the syntax or function that can do the above needs;
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #1344 (permalink)  
Old 10-14-2008, 07:00 PM
Junior Member
 
Join Date: Oct 2008
Posts: 2
askahlon is on a distinguished road
how to not trade till the next bar

I've tried programming for the first time, but have one issue.
- if I close a trade in a bar, I can only open a new trade when the new bar opens.
I have tried the following;

datetime time0=0;
int start()
if (time0 == Time[0]) return;
{
"program code"
}
time0 = Time[0];
return(0);


If I take this bit out I get multiple entries in various bar. With it in, I get far fewer entries (from 70 in a backtest. to 4), but with missing entries.
Anyone know what I'm doing wrong.
Thanks in advance.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #1345 (permalink)  
Old 10-15-2008, 03:14 AM
Senior Member
 
Join Date: Nov 2006
Posts: 210
luxinterior is on a distinguished road
You can use the NewBar() function below..

Like if(NewBar(){ ........programming here

Code:
bool NewBar() {

	static datetime LastTime = 0;

	if (Time[0] != LastTime) {
		LastTime = Time[0];		
		return (true);
	} else
		return (false);
}
You might also want to test for open orders if you only want one order at a time.

Hope that helps.

Lux
__________________
Build An Expert Advisor. FREE E-course As Seen On TV
ForexArea.com
Users of Gap Trader from 'Forex-Assistant' MUST Read This
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #1346 (permalink)  
Old 10-15-2008, 12:02 PM
Junior Member
 
Join Date: Oct 2008
Posts: 2
askahlon is on a distinguished road
Lux
Thanks for your help with this.
I think I've figured out what was going on on the program.
I was previously checking for a new bar at the start, so the program just checked the first tick of the new bar to see if it agreed with my signals, without checking the other ticks. This is why do few trade signals came through the backtest.
I have now placed the code correctly, (ie. where my ordersend instuction is) it seems to be working ok.
Am I correct in my thinking?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #1347 (permalink)  
Old 10-15-2008, 12:06 PM
Junior Member
 
Join Date: Apr 2007
Posts: 14
haan is on a distinguished road
Please help to update the code of expert.

There is a simple expert. Code is clear and simple too, but i have a big problem - expert got only ONE try to Open/Close order.
Can somebody help to fix it?
Expert must try 5-10 times to open/close order.
Maybe n=number of try, and please without "while".
Thanks a lot.
Attached Files
File Type: mq4 test.mq4 (8.4 KB, 6 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #1348 (permalink)  
Old 10-15-2008, 03:58 PM
Junior Member
 
Join Date: Jun 2008
Posts: 1
tommy0418 is on a distinguished road
My code for 'adjust lots after loss',need help..thanks

My code below for 'adjust lots after loss', but it did an error massage "incorrect start position 0 for ArraySort function" during Testing. Anyone can help me to fix it? need help..

PHP Code:
double AdjtLotsByWinRateint magicnumber,double NormLots)
{
  
int i,counter;
  
int ProfitAndTime[][2];
  
double Profits[];
//----
  
ArrayResize(ProfitAndTime,OrdersHistoryTotal());

  for (
i=0;i<OrdersHistoryTotal();i++)
  {
   if (
OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
   {
    if (
OrderType()<=OP_SELL && OrderMagicNumber()==magicnumber )  // 0  OP_BUY    1  OP_SELL  2 OP_BUYLIMIT  3 OP_SELLLIMIT 4 OP_BUYSTOP  5 OP_SELLSTOP      
    
{
    
ProfitAndTime[counter][0]=OrderCloseTime();
    
ProfitAndTime[counter][1]=OrderProfit();
    
counter++;
    }
   }
  }
  
ArrayResize(ProfitAndTime,counter);
  
ArrayResize(Profits,counter);
  
ArraySort(ProfitAndTime);
  
  for (
i=0;i<counter;i++)
  {
   
Profits[i]=ProfitAndTime[i][1];
  }
  
//Print(Profits);
  
int err=GetLastError();

  
int WinRate_N=0,WinRate_A=5;
  
double WinRate;
  for (
i=counter;i<counter-WinRate_A+1;i--)
  {
   if (
Profits[i]>0){ 
     
WinRate_N=WinRate_N+1;
     }
     else if (
Profits[i]<0)
     {
     
WinRate_N=WinRate_N-1;
     }
     else
     {
     
WinRate_N=WinRate_N+0;
     }
   }
   
   
   
WinRate=WinRate_N/WinRate_A;
   
double NewLots;
   if (
WinRate>=0.7)
   {
     
NewLots=NormLots*1.5;
   }
   else if (
WinRate>=0.5 && WinRate<0.7)
   {
     
NewLots=NormLots*1;
   }
   else if (
WinRate>=0.3 && WinRate<0.5)
   {
     
NewLots=NormLots*0.5;
   }
   else       
//if (WinRate<0.3)
   
{
     
NewLots=NormLots*0.1;
   }
   
   return(
NewLots);


Last edited by tommy0418; 10-16-2008 at 12:45 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #1349 (permalink)  
Old 10-16-2008, 10:07 AM
Junior Member
 
Join Date: Aug 2008
Posts: 29
fercan is on a distinguished road
how do you use Fractal?

lets say i only want the fractal up? i am confuse.. is fractal up mode upper?

and also about the shift?

below is the code i place to check the newest fractal up
iFractals (Symbol (), PERIOD_H1, MODE_UPPER, 1);

is this correct? or should i put 0 in the shift?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #1350 (permalink)  
Old 10-16-2008, 10:30 AM
Senior Member
 
Join Date: Nov 2006
Posts: 210
luxinterior is on a distinguished road
0 shift is the current bar that is still forming, 1 is the previous bar that has already closed. It depends which one you want.

Good luck

Lux
__________________
Build An Expert Advisor. FREE E-course As Seen On TV
ForexArea.com
Users of Gap Trader from 'Forex-Assistant' MUST Read This
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks

Tags
candle time, CHinGsMAroonCLK, coders guru, expert advisor, forex, how to code, I_XO_A_H, mechanical trading, trading


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

LinkBacks (?)
LinkBack to this Thread: http://www.forex-tsd.com/metatrader-programming/554-how-code.html
Posted By For Type Date
Need an experienced programmer? - Page 2 Post #0 Refback 09-24-2008 07:24 AM

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to code this? iscuba11 Metatrader 4 mql 4 - Development course 1 08-03-2007 05:22 PM


All times are GMT. The time now is 06:24 AM.



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