Forex
Google

Go Back   Forex Trading > Programming > Metatrader Programming
Forex Forum Register FAQ Members List Calendar Search Today's Posts 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

Reply
 
LinkBack (1) Thread Tools Display Modes
  #11 (permalink)  
Old 06-17-2007, 05:54 PM
RickD's Avatar
RickD RickD is offline
Member
 
Join Date: Jan 2006
Location: Eastern Europe
Posts: 52
RickD is on a distinguished road
Quote:
Originally Posted by yocy1
i would like to write a code for a hedge strategy in one currency and stop open when, for example, buy OP are more then sell OP, so i need to write something like, "if totalorder of OP_BUY > totalorders of OP_SELL" any idea?
Code:
  int BuyCnt = 0;
  int SellCnt = 0;
  int BuyStopCnt = 0;
  int SellStopCnt = 0;
  int BuyLimitCnt = 0;
  int SellLimitCnt = 0;
  
  int cnt = OrdersTotal();
  for (int i=0; i < cnt; i++) 
  {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
    //if (OrderSymbol() != Symbol()) continue;
    //if (OrderMagicNumber() != Magic) continue;
    
    int type = OrderType();
    if (type == OP_BUY) BuyCnt++;
    if (type == OP_SELL) SellCnt++;
    if (type == OP_BUYSTOP) BuyStopCnt++;
    if (type == OP_SELLSTOP) SellStopCnt++;
    if (type == OP_BUYLIMIT) BuyLimitCnt++;
    if (type == OP_SELLLIMIT) SellLimitCnt++;
  }
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12 (permalink)  
Old 06-17-2007, 05:56 PM
RickD's Avatar
RickD RickD is offline
Member
 
Join Date: Jan 2006
Location: Eastern Europe
Posts: 52
RickD is on a distinguished road
Quote:
Originally Posted by hopokuk
I would like to program this

IF NUMBER OF OPEN TRADES = 8 THEN CLOSE THE OLDEST OPEN TRADE
I want to close the oldest market order if there are >=8 market orders, but to ignore all the pending orders

thanks
Code:
 extern int Magic = ...
 
  //-----
 
  int BuyCnt = 0;
  int SellCnt = 0;
  
  int cnt = OrdersTotal();
  for (int i=0; i < cnt; i++) 
  {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
    if (OrderSymbol() != Symbol()) continue;
    if (OrderMagicNumber() != Magic) continue;
    
    int type = OrderType();
    if (type == OP_BUY) BuyCnt++;
    if (type == OP_SELL) SellCnt++;
  }
  
  if (BuyCnt+SellCnt < 8) return;
 
  //-----
 
  datetime tm = 0;
  int ticket = -1;
 
  cnt = OrdersTotal();
  for (i=0; i < cnt; i++) 
  {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue;
    if (OrderSymbol() != Symbol()) continue;
    if (OrderMagicNumber() != Magic) continue;
    
    type = OrderType();
    if (type == OP_BUY || type == OP_SELL)
    {
      if (OrderOpenTime() > tm)
      {
        tm = OrderOpenTime();
        ticket = OrderTicket();
      }
    }
  }
 
  //-----
 
  if (ticket != -1) OrderClose(ticket, ...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #13 (permalink)  
Old 06-17-2007, 06:11 PM
RickD's Avatar
RickD RickD is offline
Member
 
Join Date: Jan 2006
Location: Eastern Europe
Posts: 52
RickD is on a distinguished road
Quote:
Originally Posted by DooMGuarD
hi all

How i obtain the initial deposit in account for statistical questions?

regards
Code:
#define OP_BALANCE          6

int cnt = OrdersHistoryTotal();
for (int i=0; i<cnt; i++) 
{
  if (!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) continue;

  if (OrderType() == OP_BALANCE)
  {
    Alert(OrderProfit());
    break;
  }
}
And do not forget to set Show All History option.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #14 (permalink)  
Old 06-23-2007, 08:04 AM
Terry French Terry French is offline
Member
 
Join Date: Apr 2006
Posts: 49
Terry French is on a distinguished road
exiting a trade

Lets say I am in a trade based on a stochastic setting. I want to exit based on another stochastic cross totally different from my entry. No problem yet. But lets say at my exit the market continues to go up. I want to change the exit based on 95% of the current stochastic signal line to the next level to keep me in the trade. So once I have my current stochastic setting at 95% and it gets hit, my new exit is 95% of the new setting and once this gets hit my new exit is 95% of that setting. This setup can keep you in a trade until the market peaks and turns down crossing the signal and the trade is exited on the cross. You know when it seems overbought or oversold. By changing the stochastic settings, as the price increases you are able to stay in the market till the trend breaks. Once trade is closed, you start next order with 1st level of settings. Currently I use a main signal on one setting crossing a signal signal on another setting. I only use 1 of the lines each stoch and black the other one out. I want to be able to move at least 4 levels up if the market continues to trend. Of course the opposite would be for a sell.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #15 (permalink)  
Old 01-24-2008, 08:18 PM
RickD's Avatar
RickD RickD is offline
Member
 
Join Date: Jan 2006
Location: Eastern Europe
Posts: 52
RickD is on a distinguished road
Quote:
Originally Posted by golden188
Could someone please help me with some language to make my Expert Advisor trade a percentage of my balance? For example, I would like each trade, instead of being x number of lots, to be 10% of my current balance. Does anyone know how to do this? The more explicit the details the more helpful it would be.

thanks.
I suggest to use the following function:
Code:
extern double Lots = 0.1;
extern double MinLot = 0.1;
extern double MaxLot = 5.0;
extern int LotPrec = 1;
extern bool DynamicLot = false;
extern double LotStep = 0.1;
extern double BalanceStep = 500;
 
double GetLots() 
{
  double lots = Lots;
 
  if (DynamicLot)
  {
    lots = NormalizeDouble(LotStep*AccountBalance()/BalanceStep, LotPrec);  
  }
 
  lots = MathMax(lots, MinLot);
  lots = MathMin(lots, MaxLot);
  return (lots);
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #16 (permalink)  
Old 02-03-2008, 03:32 AM
KuPretMan KuPretMan is offline
Junior Member
 
Join Date: Jul 2007
Posts: 18
KuPretMan is on a distinguished road
help EA

hello RickD

i try make simpel EA.... can u help me rick...

sell:
when price have 70pip range
exampel : price open day : 208.55
low price(low price day) :208.30
and price going up (high price day)to 209.00
OP sell 209.00.....

buy: when price have 70pip range
exampel : price open day : 208.55
high price(high price day) :208.70
and price going down (low price day)to 207.00
OP buy 207.00.....

thks RickD
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #17 (permalink)  
Old 03-24-2008, 07:20 PM
RickD's Avatar
RickD RickD is offline
Member
 
Join Date: Jan 2006
Location: Eastern Europe
Posts: 52
RickD is on a distinguished road
Quote:
Originally Posted by KuPretMan View Post
hello RickD

i try make simpel EA.... can u help me rick...

sell:
when price have 70pip range
exampel : price open day : 208.55
low price(low price day) :208.30
and price going up (high price day)to 209.00
OP sell 209.00.....

buy: when price have 70pip range
exampel : price open day : 208.55
high price(high price day) :208.70
and price going down (low price day)to 207.00
OP buy 207.00.....

thks RickD
Hi,

Do you mean that I should write the EA for you?
Please send a PM me.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #18 (permalink)  
Old 03-24-2008, 07:21 PM
RickD's Avatar
RickD RickD is offline
Member
 
Join Date: Jan 2006
Location: Eastern Europe
Posts: 52
RickD is on a distinguished road
Quote:
Originally Posted by wonderkey
Hi RickD,

many thanks for being so helpful here. You couldnīt be so kind to introduce the solution of the following problem to me?

My EA sometimes gets stopped out in a winning trade and a few seconds later it sets up the same trade once again, which is not only senseless but very often results in losses. How can I tell the EA to wait with a new order 1 hour, or any other time?

Thanks in advance!

-wonderkey
Hi,

Select the last order and don't trade some time.

Code:
int MinTimeSec = 60; //no trade 60 seconds after last close
int Magic = ...

void start()
{
  ...

  if (LastOrderSelect(MODE_HISTORY, OP_BUY, OP_SELL))
  {
    if (TimeCurrent() - OrderCloseTime() < MinTimeSec) return;
  }

  ...
}


bool LastOrderSelect(int pool, int type1, int type2 = -1)
{
  datetime tm = -1;
  int ticket = -1;

  if (pool == MODE_TRADES)
  {
    int cnt = OrdersTotal();
    for (int i=0; i < cnt; i++) 
    {
      if (!OrderSelect(i, SELECT_BY_POS, pool)) continue;
      if (OrderSymbol() != Symbol()) continue;
      if (OrderMagicNumber() != Magic) continue;
        
      int type = OrderType();
      if (type == type1 || type == type2)
      {
        if (OrderOpenTime() > tm)
        {
          tm = OrderOpenTime();
          ticket = OrderTicket();
        }
      }
    }
  
    return (OrderSelect(ticket, SELECT_BY_TICKET));
  }

  if (pool == MODE_HISTORY)
  {
    cnt = OrdersHistoryTotal();
    for (i=0; i < cnt; i++) 
    {
      if (!OrderSelect(i, SELECT_BY_POS, pool)) continue;
      if (OrderSymbol() != Symbol()) continue;
      if (OrderMagicNumber() != Magic) continue;
        
      type = OrderType();
      if (type == type1 || type == type2)
      {
        if (OrderCloseTime() > tm)
        {
          tm = OrderCloseTime();
          ticket = OrderTicket();
        }
      }
    }
  
    return (OrderSelect(ticket, SELECT_BY_TICKET));
  }
    
  return (false);
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #19 (permalink)  
Old 04-16-2008, 12:40 PM
Pucio Pucio is offline
Senior Member
 
Join Date: Sep 2007
Location: Poland
Posts: 187
Pucio is on a distinguished road
Alert question

Hi

I wrote Alert but it only works when I open window with my indicator. It works only also when I jump beetwen charts windows, in other words when I jump between charts time levels. It seems that indicator can not count when whorks. How can I solve my problem ? Need help. Is it conected with Indicatorcounted ? Should I do something in parameter of Alert. How to do it ?

Should I use something like:

double

? = ObjectGetValueByShift(string name, int shift));

if (???????????)

Alert("Pattern on " + Symbol() + " " + Period());




Pucio
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #20 (permalink)  
Old 04-17-2008, 01:20 AM
bdht bdht is offline
Junior Member
 
Join Date: Jan 2008
Location: Brooklyn
Posts: 21
bdht is on a distinguished road
Crossing

Hey Rick, I'm glad you're doing this.
What's the code for crossing of two 2-line indicators? I'm sure it isn't really difficult. Say, the 1st indicator's lines cross each other 1 bar before [MAIN > SIGNAL] = BUY order, 2nd indicator's lines cross one another [MAIN < SIGNAL] = SELL order.
What I came up with is this, but it just doesn't cut it.

Code:
if (Time[0] == prevtime)  
      return(0);
   prevtime = Time[0];

//IND1 = iIND1 (NULL, 0, 0, MODE_MAIN, 0); mentioned later
//IND1S = iIND1 (NULL, 0, 0, MODE_SIGNAL, 0); mentioned later
IND2 = iIND2 (NULL, 0, 0, MODE_MAIN, 0); // how to go about these two?
IND2S = iIND2 (NULL, 0, 0, MODE_SIGNAL, 0); // how to go about these two?
Taking into consideration Guru's approach, [with some minor changes and fixed direction b/c it's misspelled all over the lesson ]

Code:
int Crossed (double line1 , double line2)
   {
      static int last_direction = 0;
      static int current_direction = 0;
      
      if(line1>line2)current_direction = 1; //up
      if(line1<line2)current_direction = 2; //down

      if(current_direction != last_direction) //changed 
      {
            last_direction = current_direction;
            return (last_direction);
      }
      else
      {
            return (0);
      }
   } 


IND1 = iIND1 (NULL, 0, 0, MODE_MAIN, 0);
IND1S = iIND1 (NULL, 0, 0, MODE_SIGNAL, 0);
   
   int isCrossed  = Crossed (IND1 ,IND2);
   
   if(isCrossed == 1)
         {
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"My EA",12345,0,Green);
            if(ticket>0)
              {
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
              }
            else Print("Yikes! You're screwed! Get back to the manual!!! Ha! ",GetLastError()); 
            return(0);
         }
         if(isCrossed == 2)
         {
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"My EA",12345,0,Red);
            if(ticket>0)
              {
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
              }
            else Print(""Yikes! You're screwed! Get back to the manual!!! Ha! "",GetLastError()); 
            return(0);
         }
         return(0);
     }
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


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

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-programming/7813-mql4-guide.html
Posted By For Type Date
E2E-Fx This thread Refback 08-17-2007 11:18 PM

Similar Threads
Thread Thread Starter Forum Replies Last Post
Welcome to the MQL4 course codersguru Metatrader 4 mql 4 - Development course 81 03-22-2008 10:32 AM
Help for convert from VT to MQL4 M-E-C Expert Advisors - Metatrader 4 11 07-27-2007 06:53 PM
Arrays in MQL4 clippertm Questions 4 04-12-2007 09:35 PM
www.mql4.com DeSt Metatrader 4 18 02-01-2006 11:59 PM


All times are GMT. The time now is 09:47 PM.