Forex
Google

Go Back   Forex Trading > Metatrader Training > Metatrader 4 mql 4 - Development course
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 Thread Tools Display Modes
  #1 (permalink)  
Old 12-12-2006, 06:05 PM
mikep's Avatar
mikep mikep is offline
Member
 
Join Date: Jun 2006
Posts: 87
mikep is on a distinguished road
Need help with simple EA problem - newbie

Hello Everyone!

I'm trying to hobble together an EA from a template program and am running into a few problems with the code and was hoping that someone could help (it's probably riduculously easy but I'm still lost).

The theory is simple:
- enter and go short if the RSI crosses up over 50
- enter and go long if the RSI crosses down 50

The exists are strictly based on TP and SL.

With this strategy, multiple open orders can accumulate. The problem is that the EA seems to only open a new order if the previous one has closed.

Can anyone please help and show me how the code needs to be modified to allow multiple open positions at the same time each time a new entry signal is given and exit only if a TP or SL has been reached.

Thanks in advance!



...first learning to crawl...then one day run...
Attached Files
File Type: mq4 RSI_Cross Market Orders.mq4 (4.7 KB, 15 views)

Last edited by mikep : 12-12-2006 at 06:08 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 12-13-2006, 02:27 AM
Eaglehawk's Avatar
Eaglehawk Eaglehawk is offline
Senior Member
 
Join Date: Jun 2006
Posts: 141
Eaglehawk is on a distinguished road
PHP Code:
int numorder OrderSend(Symbol(), OP_BUYdLotsAsknSlippagedStopLossdTakeProfitsNameExpertMAGIC0colorOpenBuy); 
You're setting an OrderSend() function as an integer. Try changing to;

PHP Code:
   {
   
OrderSend(Symbol(), OP_BUYdLotsAsknSlippagedStopLossdTakeProfitsNameExpertMAGIC0colorOpenBuy); 
   } 
Should do the trick. It works on my computer.
__________________
"One's ability to accomplish his or her goals is limited only by the ingenuity of how one uses what he or she already knows."- Eaglehawk
REMEMBER,
"Genius is nothing but a greater aptitude for patience." –Benjamin Franklin

____________________________________

Have a simple ea you just can't figure out how to code??? Odds are you can make it here. Just remember to push complete when you're done, NOT SAVE!!!

http://sufx.core.t3-ism.net/ExpertAdvisorBuilder
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-13-2006, 05:28 PM
mikep's Avatar
mikep mikep is offline
Member
 
Join Date: Jun 2006
Posts: 87
mikep is on a distinguished road
thank you but still not working...

Eaglehawk,

Thanks for your input - it is appreciated!

I tried your recommendation but it didn't seem to fix it.
I've attached a 2 day sample backtest result as well as a screen shot to show what happened versus what should have happened. During the time frame it only executed 3 trades when it should have executed something like 30.

Do you or anyone else out there have any more ideas?

Attached Images
File Type: gif gbp rsi sample.gif (18.2 KB, 23 views)
Attached Files
File Type: htm StrategyTester.htm (5.4 KB, 13 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 12-13-2006, 10:03 PM
Eaglehawk's Avatar
Eaglehawk Eaglehawk is offline
Senior Member
 
Join Date: Jun 2006
Posts: 141
Eaglehawk is on a distinguished road
did you change the settings on both of the ordersend functions? (i found two).

on my tester it got much more than three orders.

hope this helps.
__________________
"One's ability to accomplish his or her goals is limited only by the ingenuity of how one uses what he or she already knows."- Eaglehawk
REMEMBER,
"Genius is nothing but a greater aptitude for patience." –Benjamin Franklin

____________________________________

Have a simple ea you just can't figure out how to code??? Odds are you can make it here. Just remember to push complete when you're done, NOT SAVE!!!

http://sufx.core.t3-ism.net/ExpertAdvisorBuilder
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 12-13-2006, 10:23 PM
Eaglehawk's Avatar
Eaglehawk Eaglehawk is offline
Senior Member
 
Join Date: Jun 2006
Posts: 141
Eaglehawk is on a distinguished road
here are the results i've had.

notepad file has a number on the right, of the take profits and stop losses.
Attached Images
File Type: gif Rsi Tester.gif (7.3 KB, 12 views)
Attached Files
File Type: htm Rsi Tester.htm (131.2 KB, 9 views)
File Type: txt Rsi Take Profits and Stop Losses.txt (37.6 KB, 7 views)
__________________
"One's ability to accomplish his or her goals is limited only by the ingenuity of how one uses what he or she already knows."- Eaglehawk
REMEMBER,
"Genius is nothing but a greater aptitude for patience." –Benjamin Franklin

____________________________________

Have a simple ea you just can't figure out how to code??? Odds are you can make it here. Just remember to push complete when you're done, NOT SAVE!!!

http://sufx.core.t3-ism.net/ExpertAdvisorBuilder
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 12-13-2006, 11:34 PM
mikep's Avatar
mikep mikep is offline
Member
 
Join Date: Jun 2006
Posts: 87
mikep is on a distinguished road
Yes I changed both order fuctions - one for the OP_Buy and the OP_Sell.

I took a quick snapshot of only two days on a 5min chart to show that with many crosses the only ones that are executed are those where previous open orders have closed.

I attached the "fixed" EA version based on your recommendations.

I took a look at your results and noticed that while you have a lot more orders it’s because of you the test dates. Also, the results still show that only one order is in play at one time.

….hmmmm…...this is bumming me out….
Attached Files
File Type: mq4 RSI_Cross Market Orders FIX.mq4 (4.7 KB, 8 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 12-15-2006, 02:08 PM
mikep's Avatar
mikep mikep is offline
Member
 
Join Date: Jun 2006
Posts: 87
mikep is on a distinguished road
Question almost there...hopefully...

Guys - I know there's quite a few senior and experienced programmers out there - please help - again, I believe this is probably a simple problem that would take 5 min for the "mistake" to be found by someone with the skillset which alot of you have.

I'll put the code here to make it easier for someone to review...

Thanks again to Eaglehawk for stepping up and trying to help.

PHP Code:

extern string sNameExpert 
"RSI Cross";
extern int nAccount =0;
extern double dBuyStopLossPoint 25;
extern double dSellStopLossPoint 25;
extern double dBuyTakeProfitPoint 50;
extern double dSellTakeProfitPoint 50;
extern double dBuyTrailingStopPoint 0;
extern double dSellTrailingStopPoint 0;
extern double dLots 1;
extern int nSlippage 3;
extern color colorOpenBuy Blue;
extern color colorCloseBuy Aqua;
extern color colorOpenSell Red;
extern color colorCloseSell Aqua;


void deinit() {
   
Comment("");
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start(){
   

double LongRSI=iRSI(Symbol(),Period(),14,PRICE_OPEN,2);
double ShortRSI=iRSI(Symbol(),Period(),14,PRICE_OPEN,1);



   if(
AccountFreeMargin() < (1000*dLots)){
      Print(
"We have no money. Free Margin = " AccountFreeMargin());
      return(
0);
   }
   
   
bool lFlagBuyOpen falselFlagSellOpen falselFlagBuyClose falselFlagSellClose false;
   
   
lFlagBuyOpen = (LongRSI>=50 && ShortRSI<50);
   
lFlagSellOpen = (LongRSI<=50 && ShortRSI>50);
   
lFlagBuyClose False;
   
lFlagSellClose False;
   
   if (!
ExistPositions()){

      if (
lFlagBuyOpen){
         
OpenBuy();
         return(
0);
      }

      if (
lFlagSellOpen){
         
OpenSell();
         return(
0);
      }
   }
   if (
ExistPositions()){
      if(
OrderType()==OP_BUY){
         if (
lFlagBuyClose){
            
bool flagCloseBuy OrderClose(OrderTicket(), OrderLots(), BidnSlippagecolorCloseBuy);  
            return(
0);
         }
      }
      if(
OrderType()==OP_SELL){
         if (
lFlagSellClose){
            
bool flagCloseSell OrderClose(OrderTicket(), OrderLots(), AsknSlippagecolorCloseSell);  
            return(
0);
         }
      }
   }
   
   if (
dBuyTrailingStopPoint || dSellTrailingStopPoint 0){
      
      for (
int i=0i<OrdersTotal(); i++) { 
         if (
OrderSelect(iSELECT_BY_POSMODE_TRADES)) { 
            
bool lMagic true;
            if (
MAGIC && OrderMagicNumber() != MAGIC)
               
lMagic false;
            
            if (
OrderSymbol()==Symbol() && lMagic) { 
               if (
OrderType()==OP_BUY && dBuyTrailingStopPoint 0) { 
                  if (
Bid-OrderOpenPrice() > dBuyTrailingStopPoint*Point) { 
                     if (
OrderStopLoss()<Bid-dBuyTrailingStopPoint*Point
                        
ModifyStopLoss(Bid-dBuyTrailingStopPoint*Point); 
                  } 
               } 
               if (
OrderType()==OP_SELL) { 
                  if (
OrderOpenPrice()-Ask>dSellTrailingStopPoint*Point) { 
                     if (
OrderStopLoss()>Ask+dSellTrailingStopPoint*Point || OrderStopLoss()==0)  
                        
ModifyStopLoss(Ask+dSellTrailingStopPoint*Point); 
                  } 
               } 
            } 
         } 
      } 
   }
   return (
0);
}

bool ExistPositions() {
    for (
int i=0i<OrdersTotal(); i++) {
        if (
OrderSelect(iSELECT_BY_POSMODE_TRADES)) {
         
bool lMagic true;
         
         if (
MAGIC && OrderMagicNumber() != MAGIC)
            
lMagic false;

            if (
OrderSymbol()==Symbol() && lMagic) {
                return(
True);
            }
        } 
    } 
    return(
false);
}

void ModifyStopLoss(double ldStopLoss) { 
   
bool lFlagModify OrderModify(OrderTicket(), OrderOpenPrice(), ldStopLossOrderTakeProfit(), 0CLR_NONE); 
 


void OpenBuy() { 
   
double dStopLoss 0dTakeProfit 0;

   if (
dBuyStopLossPoint 0)
      
dStopLoss Ask-dBuyStopLossPoint*Point;
   
   if (
dBuyTakeProfitPoint 0)
     
dTakeProfit Ask dBuyTakeProfitPoint Point
   
   
OrderSend(Symbol(), OP_BUYdLotsAsknSlippagedStopLossdTakeProfitsNameExpertMAGIC0colorOpenBuy); 
   



void OpenSell() { 
   
double dStopLoss 0dTakeProfit 0;
   
   if (
dSellStopLossPoint 0)
      
dStopLoss Bid+dSellStopLossPoint*Point;
   
   if (
dSellTakeProfitPoint 0)
      
dTakeProfit Bid-dSellTakeProfitPoint*Point;
   
   
OrderSend(Symbol(),OP_SELLdLotsBidnSlippagedStopLossdTakeProfitsNameExpertMAGIC0colorOpenSell); 
   
    

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 12-15-2006, 09:29 PM
Eaglehawk's Avatar
Eaglehawk Eaglehawk is offline
Senior Member
 
Join Date: Jun 2006
Posts: 141
Eaglehawk is on a distinguished road
Quote:
Originally Posted by mikep
Guys - I know there's quite a few senior and experienced programmers out there - please help - again, I believe this is probably a simple problem that would take 5 min for the "mistake" to be found by someone with the skillset which alot of you have.

I'll put the code here to make it easier for someone to review...

Thanks again to Eaglehawk for stepping up and trying to help.

PHP Code:

extern string sNameExpert 
"RSI Cross";
extern int nAccount =0;
extern double dBuyStopLossPoint 25;
extern double dSellStopLossPoint 25;
extern double dBuyTakeProfitPoint 50;
extern double dSellTakeProfitPoint 50;
extern double dBuyTrailingStopPoint 0;
extern double dSellTrailingStopPoint 0;
extern double dLots 1;
extern int nSlippage 3;
extern color colorOpenBuy Blue;
extern color colorCloseBuy Aqua;
extern color colorOpenSell Red;
extern color colorCloseSell Aqua;


void deinit() {
   
Comment("");
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start(){
   

double LongRSI=iRSI(Symbol(),Period(),14,PRICE_OPEN,2);
double ShortRSI=iRSI(Symbol(),Period(),14,PRICE_OPEN,1);



   if(
AccountFreeMargin() < (1000*dLots)){
      Print(
"We have no money. Free Margin = " AccountFreeMargin());
      return(
0);
   }
   
   
bool lFlagBuyOpen falselFlagSellOpen falselFlagBuyClose falselFlagSellClose false;
   
   
lFlagBuyOpen = (LongRSI>=50 && ShortRSI<50);
   
lFlagSellOpen = (LongRSI<=50 && ShortRSI>50);
   
lFlagBuyClose False;
   
lFlagSellClose False;
   
   if (!
ExistPositions()){

      if (
lFlagBuyOpen){
         
OpenBuy();
         return(
0);
      }

      if (
lFlagSellOpen){
         
OpenSell();
         return(
0);
      }
   }
   if (
ExistPositions()){
      if(
OrderType()==OP_BUY){
         if (
lFlagBuyClose){
            
bool flagCloseBuy OrderClose(OrderTicket(), OrderLots(), BidnSlippagecolorCloseBuy);  
            return(
0);
         }
      }
      if(
OrderType()==OP_SELL){
         if (
lFlagSellClose){
            
bool flagCloseSell OrderClose(OrderTicket(), OrderLots(), AsknSlippagecolorCloseSell);  
            return(
0);
         }
      }
   }
   
   if (
dBuyTrailingStopPoint || dSellTrailingStopPoint 0){
      
      for (
int i=0i<OrdersTotal(); i++) { 
         if (
OrderSelect(iSELECT_BY_POSMODE_TRADES)) { 
            
bool lMagic true;
            if (
MAGIC && OrderMagicNumber() != MAGIC)
               
lMagic false;
            
            if (
OrderSymbol()==Symbol() && lMagic) { 
               if (
OrderType()==OP_BUY && dBuyTrailingStopPoint 0) { 
                  if (
Bid-OrderOpenPrice() > dBuyTrailingStopPoint*Point) { 
                     if (
OrderStopLoss()<Bid-dBuyTrailingStopPoint*Point
                        
ModifyStopLoss(Bid-dBuyTrailingStopPoint*Point); 
                  } 
               } 
               if (
OrderType()==OP_SELL) { 
                  if (
OrderOpenPrice()-Ask>dSellTrailingStopPoint*Point) { 
                     if (
OrderStopLoss()>Ask+dSellTrailingStopPoint*Point || OrderStopLoss()==0)  
                        
ModifyStopLoss(Ask+dSellTrailingStopPoint*Point); 
                  } 
               } 
            } 
         } 
      } 
   }
   return (
0);
}

bool ExistPositions() {
    for (
int i=0i<OrdersTotal(); i++) {
        if (
OrderSelect(iSELECT_BY_POSMODE_TRADES)) {
         
bool lMagic true;
         
         if (
MAGIC && OrderMagicNumber() != MAGIC)
            
lMagic false;

            if (
OrderSymbol()==Symbol() && lMagic) {
                return(
True);
            }
        } 
    } 
    return(
false);
}

void ModifyStopLoss(double ldStopLoss) { 
   
bool lFlagModify OrderModify(OrderTicket(), OrderOpenPrice(), ldStopLossOrderTakeProfit(), 0CLR_NONE); 
 


void OpenBuy() { 
   
double dStopLoss 0dTakeProfit 0;

   if (
dBuyStopLossPoint 0)
      
dStopLoss Ask-dBuyStopLossPoint*Point;
   
   if (
dBuyTakeProfitPoint 0)
     
dTakeProfit Ask dBuyTakeProfitPoint Point
   
   
OrderSend(Symbol(), OP_BUYdLotsAsknSlippagedStopLossdTakeProfitsNameExpertMAGIC0colorOpenBuy); 
   



void OpenSell() { 
   
double dStopLoss 0dTakeProfit 0;
   
   if (
dSellStopLossPoint 0)
      
dStopLoss Bid+dSellStopLossPoint*Point;
   
   if (
dSellTakeProfitPoint 0)
      
dTakeProfit Bid-dSellTakeProfitPoint*Point;
   
   
OrderSend(Symbol(),OP_SELLdLotsBidnSlippagedStopLossdTakeProfitsNameExpertMAGIC0colorOpenSell); 
   
    

perhaps turning your slippage to "0" or "1" might help a little?
__________________
"One's ability to accomplish his or her goals is limited only by the ingenuity of how one uses what he or she already knows."- Eaglehawk
REMEMBER,
"Genius is nothing but a greater aptitude for patience." –Benjamin Franklin

____________________________________

Have a simple ea you just can't figure out how to code??? Odds are you can make it here. Just remember to push complete when you're done, NOT SAVE!!!

http://sufx.core.t3-ism.net/ExpertAdvisorBuilder
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati