Forex
Google

Go Back   Forex Trading > Discussion Areas > Metatrader 4
Forex Forum FAQ Members List Calendar Mark Forums Read


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

 
 
LinkBack (1) Thread Tools
  1 links from elsewhere to this Post. Click to view.
Old 07-10-2007, 07:21 PM
trevman trevman is offline
Senior Member
 
Join Date: Dec 2005
Posts: 142
trevman is on a distinguished road
Recall result of last trade

is it possible to recall if the last trade hit TP or SL and make a trade according to that? thanks.
__________________
There is no candle.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
 
Old 07-10-2007, 11:58 PM
Michel Michel is online now
Senior Member
 
Join Date: Feb 2006
Posts: 499
Michel is on a distinguished road
Quote:
Originally Posted by trevman View Post
is it possible to recall if the last trade hit TP or SL and make a trade according to that? thanks.
You can try something like this (maybe it doesn't work with slippage > 0):
PHP Code:
int CheckLastClosed()
{
   
int iResHstTotal=OrdersHistoryTotal();
   
datetime LastClose 0;
   for(
HstTotal-1>= 0--)
   {
      if(!
OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
      if(
OrderSymbol() != Symbol()) continue;
      if(
OrderMagicNumber() != Magic) continue;
      if(
OrderCloseTime() <= LastClose) continue;
      
LastClose OrderCloseTime();
      
Res 0;
      if(
OrderClosePrice() == OrderStopLoss()) Res 1;
      if(
OrderClosePrice() == OrderTakeProfit()) Res 2;
   }
   return(
Res);

Another way is to look for "SL" or "TP" in OrderComment(); but this doesn't seem more reliable.

Last edited by Michel : 07-19-2007 at 06:06 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
 
Old 07-11-2007, 12:45 AM
ralph.ronnquist's Avatar
ralph.ronnquist ralph.ronnquist is offline
Senior Member
 
Join Date: Oct 2006
Posts: 280
ralph.ronnquist is on a distinguished road
Also, you may make use of the practical but undocumented "fact" (well, my belief, really) that the orders history list is sorted in close-time order. That is, scan from high index towards low (0), and break the loop as soon as discovery is made. This makes some difference to performance when there are many closed trades.

As an aside, I think it's good habit to always scan the orders lists from high index to low, since it's typically in search of a most recent something, or within a recent history, or that some trades should be closed (which then avoids changing the remaining list, in a high-to-low scan sequence).
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
 
Old 07-11-2007, 07:44 AM
Michel Michel is online now
Senior Member
 
Join Date: Feb 2006
Posts: 499
Michel is on a distinguished road
Quote:
Originally Posted by ralph.ronnquist View Post
Also, you may make use of the practical but undocumented "fact" (well, my belief, really) that the orders history list is sorted in close-time order. That is, scan from high index towards low (0), and break the loop as soon as discovery is made. This makes some difference to performance when there are many closed trades.
This is true on BT, but in live, one may sort the list by any column of the History tab, so the last index doesn't index anymore the last closed order.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
 
Old 07-19-2007, 05:38 PM
trevman trevman is offline
Senior Member
 
Join Date: Dec 2005
Posts: 142
trevman is on a distinguished road
ive changed the code according to the other thread i made, and all is well so thank you for that - btw very clean code i like it ive compiled and no problems, however, no trades are made either heres my code

Code:
//+----------------------------START---------------------------------+

extern int     TakeProfit = 90;
extern int     StopLoss = 30; 

int start()
{
   double  ma1, stop, profit, slippage;
   ma1 = iMA(NULL,0,21,0,MODE_EMA,PRICE_CLOSE,0);
   slippage = 1;
   stop = StopLoss*Point;
   profit = TakeProfit*Point;

      
      if(OrdersTotal()<1)        //max number of orders at a time
      {
/*BUY*/ if(Close[1]<bbL && Open[1]<ma1 && Close[1]<ma1)
         OrderSend(Symbol(),OP_BUY,lots(),Ask,slippage,Ask-stop,Ask+profit,0);
/*SELL*/if(Close[1]>bbU && Open[1]>ma1 && Close[1]>ma1)
         OrderSend(Symbol(),OP_SELL,lots(),Bid,slippage,Bid+stop,Bid-profit,0);
      }     
}
  
int lots()
{
   int i, lots,HstTotal=OrdersHistoryTotal();
   datetime LastClose = 0;
   int Magic = 2005;
   for(i = HstTotal-1; i >= 0; i --)
   {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
      if(OrderSymbol() != Symbol()) continue;
      if(OrderMagicNumber() != Magic) continue;
      if(OrderCloseTime() <= LastClose) continue;
      LastClose = OrderCloseTime();
      lots = 0.1;
      if(OrderClosePrice() == OrderStopLoss()) lots = 0.2;
      if(OrderClosePrice() == OrderTakeProfit()) lots = 0.1;
   }
   return(lots);
}  

//+-----------------------------END----------------------------------+
__________________
There is no candle.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
 
Old 07-19-2007, 06:25 PM
Michel Michel is online now
Senior Member
 
Join Date: Feb 2006
Posts: 499
Michel is on a distinguished road
Quote:
Originally Posted by trevman View Post
ive changed the code according to the other thread i made, and all is well so thank you for that - btw very clean code i like it ive compiled and no problems, however, no trades are made either heres my code

Code:
//+----------------------------START---------------------------------+

extern int     TakeProfit = 90;
extern int     StopLoss = 30; 

int start()
{
   double  ma1, stop, profit, slippage;
   ma1 = iMA(NULL,0,21,0,MODE_EMA,PRICE_CLOSE,0);
   slippage = 1;
   stop = StopLoss*Point;
   profit = TakeProfit*Point;

      
      if(OrdersTotal()<1)        //max number of orders at a time
      {
/*BUY*/ if(Close[1]<bbL && Open[1]<ma1 && Close[1]<ma1)
         OrderSend(Symbol(),OP_BUY,lots(),Ask,slippage,Ask-stop,Ask+profit,0);
/*SELL*/if(Close[1]>bbU && Open[1]>ma1 && Close[1]>ma1)
         OrderSend(Symbol(),OP_SELL,lots(),Bid,slippage,Bid+stop,Bid-profit,0);
      }     
}
  
int lots()
{
   int i, lots,HstTotal=OrdersHistoryTotal();
   datetime LastClose = 0;
   int Magic = 2005;
   for(i = HstTotal-1; i >= 0; i --)
   {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
      if(OrderSymbol() != Symbol()) continue;
      if(OrderMagicNumber() != Magic) continue;
      if(OrderCloseTime() <= LastClose) continue;
      LastClose = OrderCloseTime();
      lots = 0.1;
      if(OrderClosePrice() == OrderStopLoss()) lots = 0.2;
      if(OrderClosePrice() == OrderTakeProfit()) lots = 0.1;
   }
   return(lots);
}  

//+-----------------------------END----------------------------------+
Problems I see :
- if there is no one order in the History, the func Lots() returns 0 as order size.
- OrderSend() has more arguments, how did you compile that ? more precisely, the magic number must be set as a parameter, ortherwhize the Lots() func cannot recognize those orders.
- Where are you computing bbL and bbU ? if this is done in the init() func, it's the wrong place because init() is called only once, not at every new bar.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
 
Old 07-19-2007, 06:38 PM
trevman trevman is offline
Senior Member
 
Join Date: Dec 2005
Posts: 142
trevman is on a distinguished road
my mistake bbL and bbU shouldn't be there, should be
Code:
/*BUY*/ if(Open[1]<ma1 && Close[1]<ma1)
         OrderSend(Symbol(),OP_BUY,lots(),Ask,slippage,Ask-stop,Ask+profit,0);
/*SELL*/if(Open[1]>ma1 && Close[1]>ma1)
         OrderSend(Symbol(),OP_SELL,lots(),Bid,slippage,Bid+stop,Bid-profit,0);
ive never dealt much with magic number, should lots be set at start of the lots() function? so it will be 0.1 until a order is made and then it will be based on the result of that trade
__________________
There is no candle.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
 
Old 07-19-2007, 06:52 PM
Michel Michel is online now
Senior Member
 
Join Date: Feb 2006
Posts: 499
Michel is on a distinguished road
Quote:
Originally Posted by trevman View Post
my mistake bbL and bbU shouldn't be there, should be
Code:
/*BUY*/ if(Open[1]<ma1 && Close[1]<ma1)
         OrderSend(Symbol(),OP_BUY,lots(),Ask,slippage,Ask-stop,Ask+profit,0);
/*SELL*/if(Open[1]>ma1 && Close[1]>ma1)
         OrderSend(Symbol(),OP_SELL,lots(),Bid,slippage,Bid+stop,Bid-profit,0);
ive never dealt much with magic number, should lots be set at start of the lots() function? so it will be 0.1 until a order is made and then it will be based on the result of that trade
The magic number is just a way that the EA can use to sign its own orders : so you can mix multiple EAs on the same pair, and each one works only with its own orders.
The use of OrderSend() I know is :
Quote:
int OrderSend( string symbol, int cmd, double volume, double price, int slippage, double stoploss, double takeprofit, string comment=NULL, int magic=0, datetime expiration=0, color arrow_color=CLR_NONE)
So, put "int Magic = 2005;" at the global level of the EA(not at the Lots() level()) and use OrderSend() like this :
PHP Code:
OrderSend(Symbol(),OP_BUY,lots(),Ask,slippage,Ask-stop,Ask+profit,"My EA has open this order",Magic,0,Blue); 
About the lots size, just initialize Lots = 0.1 at the beginning of the Lots() func, not inside the loop, because if no a single loop is executed, Lots will not be initialized.

Last edited by Michel : 07-19-2007 at 11:13 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
 
Old 07-19-2007, 09:38 PM
trevman trevman is offline
Senior Member
 
Join Date: Dec 2005
Posts: 142
trevman is on a distinguished road
ok ive made them changes but i dont get any trades still, though no errors either which is good i guess, heres my new code

Code:
//+----------------------------START---------------------------------+

extern int     TakeProfit = 90;
extern int     StopLoss = 30; 

int start()
{
   double  ma1, stop, profit, slippage;
   ma1 = iMA(NULL,0,21,0,MODE_EMA,PRICE_CLOSE,0);
   slippage = 1;
   stop = StopLoss*Point;
   profit = TakeProfit*Point;

      
      if(OrdersTotal()<1)        //max number of orders at a time
      {
/*BUY*/ if(Close[1]<bbL && Open[1]<ma1 && Close[1]<ma1)
         OrderSend(Symbol(),OP_BUY,lots(),Ask,slippage,Ask-stop,Ask+profit,"My EA has open this order",0,Blue); 
/*SELL*/if(Close[1]>bbU && Open[1]>ma1 && Close[1]>ma1)
         OrderSend(Symbol(),OP_SELL,lots(),Bid,slippage,Bid+stop,Bid-profit,"My EA has open this order",0,Blue); 
      }     
}
  
int lots()
{
   int i, HstTotal=OrdersHistoryTotal();
   int lots = 0.1;
   datetime LastClose = 0;
   int Magic = 2005;

   for(i = HstTotal-1; i >= 0; i --)
   {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
      if(OrderSymbol() != Symbol()) continue;
      if(OrderMagicNumber() != Magic) continue;
      if(OrderCloseTime() <= LastClose) continue;
      LastClose = OrderCloseTime();
      
      if(OrderClosePrice() == OrderStopLoss()) lots = 0.2;
      if(OrderClosePrice() == OrderTakeProfit()) lots = 0.1;
   }
   return(lots);
}
//+-----------------------------END----------------------------------+
__________________
There is no candle.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
 
Old 07-19-2007, 11:08 PM
Michel Michel is online now
Senior Member
 
Join Date: Feb 2006
Posts: 499
Michel is on a distinguished road
Quote:
Originally Posted by trevman View Post
ok ive made them changes but i dont get any trades still, though no errors either which is good i guess, heres my new code

Code:
//+----------------------------START---------------------------------+

extern int     TakeProfit = 90;
extern int     StopLoss = 30; 

int start()
{
   double  ma1, stop, profit, slippage;
   ma1 = iMA(NULL,0,21,0,MODE_EMA,PRICE_CLOSE,0);
   slippage = 1;
   stop = StopLoss*Point;
   profit = TakeProfit*Point;

      
      if(OrdersTotal()<1)        //max number of orders at a time
      {
/*BUY*/ if(Close[1]<bbL && Open[1]<ma1 && Close[1]<ma1)
         OrderSend(Symbol(),OP_BUY,lots(),Ask,slippage,Ask-stop,Ask+profit,"My EA has open this order",0,Blue); 
/*SELL*/if(Close[1]>bbU && Open[1]>ma1 && Close[1]>ma1)
         OrderSend(Symbol(),OP_SELL,lots(),Bid,slippage,Bid+stop,Bid-profit,"My EA has open this order",0,Blue); 
      }     
}
  
int lots()
{
   int i, HstTotal=OrdersHistoryTotal();
   int lots = 0.1;
   datetime LastClose = 0;
   int Magic = 2005;

   for(i = HstTotal-1; i >= 0; i --)
   {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
      if(OrderSymbol() != Symbol()) continue;
      if(OrderMagicNumber() != Magic) continue;
      if(OrderCloseTime() <= LastClose) continue;
      LastClose = OrderCloseTime();
      
      if(OrderClosePrice() == OrderStopLoss()) lots = 0.2;
      if(OrderClosePrice() == OrderTakeProfit()) lots = 0.1;
   }
   return(lots);
}
//+-----------------------------END----------------------------------+
Please, look carefully at my previous answer : you have forget to put the Magic in the OrderSend() func, AND you have to put the statement "int Magic = 2005;" at the global scope because it's used by the start() func AND the Lots() func. Global scope means outside of any function, so the value remains accessible by any function.
Another mistake is to declare the return value of the lots() func as int.
First, OrderSend() needs a double value, and second, if you want to return 0.1, it's clearly a double value...
Now, "My EA has open this order" as comment was a sample, if you do not use it, just put "" as comment argument for the OrderSend().

Last edited by Michel : 07-19-2007 at 11:11 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
 

Thread Tools

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

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

LinkBacks (?)
LinkBack to this Thread: http://www.forex-tsd.com/metatrader-4/8542-recall-result-last-trade.html
Posted By For Type Date
forex trading related resources This thread Refback 07-10-2007 08:41 PM

Similar Threads
Thread Thread Starter Forum Replies Last Post
various ea forward testing result arui Post and compare Trades 13 05-06-2007 03:03 PM
Different backtesting result of same EA. takechance Metatrader 4 2 10-10-2006 04:00 AM
Profitable EA result in Backtest trohoang Expert Advisors - Metatrader 4 6 08-28-2006 09:10 AM
Use another Indicator Result NoName Questions 0 04-09-2006 02:35 AM


All times are GMT. The time now is 03:29 PM.