Forex



Go Back   Forex Trading > Programming > MetaTrader
Forex Forum Register More recent Blogs 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
  #1851 (permalink)  
Old 06-19-2009, 04:45 PM
codersguru's Avatar
Senior Member
 
Join Date: Oct 2005
Posts: 994
codersguru has a spectacular aura aboutcodersguru has a spectacular aura aboutcodersguru has a spectacular aura about
Quote:
Originally Posted by Beno View Post
hello

Does anyone have a script to close positions on the open of a new bar.

cheers

Beno
You need to take a note of the time the order opened at with a code like this:

PHP Code:
OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,StopLoss,TakeProfit,ExpertComment,MagicNumber);
OpenTradeTime Time[0]; 
And then use this code to close the trade after 1 bar:

PHP Code:
OpenTradeBar iBarShift(NULL,0,OpenTradeTime,true);
  
   
   for(
int cnt=0;cnt<OrdersTotal();cnt++) 
   {
      
OrderSelect(cntSELECT_BY_POSMODE_TRADES);
      
      if(
OrderType()==OP_BUY && OrderMagicNumber() == MagicNumber && OpenTradeBar>=ExitBars && ExitBars>0
      {
         
OrderClose(OrderTicket(),OrderLots(),Bid,5,Yellow);
       }
      if(
OrderType()==OP_SELL && OrderMagicNumber() == MagicNumber && OpenTradeBar>=ExitBars && ExitBars>0
      {
         
OrderClose(OrderTicket(),OrderLots(),Ask,5,Yellow);  
      }
   } 
__________________
Hope it helps !
Coders' Guru
Senior MQL programmer:
www.xpworx.com/custom.htm
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
  #1852 (permalink)  
Old 06-19-2009, 05:14 PM
Beno's Avatar
Senior Member
 
Join Date: Aug 2006
Location: London
Posts: 516
Beno is on a distinguished road
Thanks Guru that works very well.

Could a similar script be used for the opening of a position.

Regards

Beno
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
  #1853 (permalink)  
Old 06-22-2009, 01:38 PM
Junior Member
 
Join Date: Sep 2008
Posts: 5
yetidarko is on a distinguished road
Remote indicator control

Hi. I'm trying to use my EA to control an indicator I've written. I have exposed an extern double in the indicator which it uses on every tick to set its horizontal level. If it gets no new info in continues on its present course. I want to use the indicator as a stop out so I need the EA to be able to synchronize buy in and sell outs.

I assumed using
double somedisposedofvalue = iCustom(null, 0, "AntariesRising", [value], 0, 0);

would restart the constructor or at least update the variable, but this doesn't appear to be the case. I'm not sure if its creating an off screen instance of the indicator or its using the one I've instantiated, or if theres just another function I can call custom extern variables from.

Thanks
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
  #1854 (permalink)  
Old 06-22-2009, 02:04 PM
kecoaorgasme's Avatar
Junior Member
 
Join Date: Mar 2006
Location: Surakarta-Indonesia
Posts: 14
kecoaorgasme is on a distinguished road
It's seem so nice to learn this thing....
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
  #1855 (permalink)  
Old 06-22-2009, 03:19 PM
Junior Member
 
Join Date: Sep 2008
Posts: 5
yetidarko is on a distinguished road
Okay, on the original question:

Code:
#property  indicator_chart_window
#property  indicator_buffers 1
#property indicator_color1 Magenta      

extern int       MAPeriod=200;
extern double       iController = 1.386;
//---- buffers
//double Memory[200];
double ExtMapBuffer1[];
int limit = 200;

//---- variables

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
  
   IndicatorBuffers(1);
   
//---- drawing settings
   SetIndexBuffer(0,ExtMapBuffer1);
   
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
   IndicatorShortName( "Antaria_Rising");
//---- initialization done
   return(0);
  }

int start()

  {
   int limit;
   
   //ExtMapBuffer1[1] = ExtMapBuffer1[0];
   //ExtMapBuffer1[0] = iController;
   for(int i=(0); i<=(limit-1); i++)
   {
    ExtMapBuffer1[i+1] = ExtMapBuffer1[i];
    }
    ExtMapBuffer1[0] = iController;  
 

      return(0);
  }
Above is the code for the indicator. Simply, it draws a line that should be updatable via the EA using the intiially mentioned code:

Code:
temp = iCustom(NULL, 0, "AntariaRising", 200, 1.2, 0, 0); //temp not used. Function returns a double
I guess I want to replace the final piece of code that actually allows me to update into the incicator object the variable that its tracking on, so that I can later use it as a stop-out line. The remaining code is irrelevant however. When I run the above function i expect antaria to move position from 1.39 or whatever it starts on, to 1.2. I can't remember if I use the 200 variable. I think I've chopped out my header and footer comments, but the entire slave indicator is up there, which is AntariaRising meantioned in immediately above code

this was the only function I could find that talked to indicator objects, and I use it to read off values in other areas.

Last edited by yetidarko; 06-22-2009 at 03:24 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
  #1856 (permalink)  
Old 06-22-2009, 04:20 PM
Senior Member
 
Join Date: Oct 2008
Location: Vancouver, BC
Posts: 158
Roger09 is on a distinguished road
About your indicator code. It's wrong.
Var. limit equal zero ever.
Take off the string:
int limit;
from the start().
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
  #1857 (permalink)  
Old 06-22-2009, 04:52 PM
Member
 
Join Date: Dec 2008
Posts: 30
johnray is on a distinguished road
Help me with this code pls

i want someone to help me with this code it open trade when i want it but close it automatically so i want it to trade with stop loss not closing when not crossing again this is the code

//+------------------------------------------------------------------+
//| JR300.mq4 |
//| onelove |
//| Forex Forum | Forex Tsd | Metatrader Forum |
//+------------------------------------------------------------------+
#property copyright "oneLove"
#property link "http://www.forex-tsd.com"
//---- input parameters
extern double TakeProfit=250.0;
extern double Lots=0.1;
extern double TrailingStop=35.0;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
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);
}
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
int cnt, ticket, total;
double shortEma, longEma;
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}
shortEma = iMA(NULL,0,1,0,MODE_EMA,PRICE_CLOSE,0);
longEma = iMA(NULL,0,200,0,MODE_SMA,PRICE_CLOSE,0);
int isCrossed = Crossed (shortEma,longEma);
total = OrdersTotal();
if(total < 1)
{
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("Error opening BUY order : ",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("Error opening SELL order : ",GetLastError());
return(0);
}
return(0);
}
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
{
// should it be closed?
if(isCrossed == 2)
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet) ;
// close position
return(0); // exit
}
// check for trailing stop
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-
Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}
else // go to short position
{
// should it be closed?
if(isCrossed == 1)
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet) ;
// close position
return(0); // exit
}
// check for trailing stop
if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
if((OrderStopLoss()>(Ask+Point*TrailingStop)) ||
(OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Poi nt*TrailingStop,
OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
}
}
return(0);
}
//+------------------------------------------------------------------+
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
  #1858 (permalink)  
Old 06-22-2009, 07:41 PM
Senior Member
 
Join Date: Oct 2008
Location: Vancouver, BC
Posts: 158
Roger09 is on a distinguished road
Try this:
Code:
//+------------------------------------------------------------------+
//| JR300.mq4 |
//| onelove |
//| Forex Forum | Forex Tsd | Metatrader Forum |
//+------------------------------------------------------------------+
#property copyright "oneLove"
#property link "http://www.forex-tsd.com"
//---- input parameters
extern double TakeProfit=250.0;
extern double StopLoss=100.0;
extern double Lots=0.1;
extern double TrailingStop=35.0;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
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);
}
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
int cnt, ticket, total;
double shortEma, longEma;
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}
shortEma = iMA(NULL,0,1,0,MODE_EMA,PRICE_CLOSE,0);
longEma = iMA(NULL,0,200,0,MODE_SMA,PRICE_CLOSE,0);
int isCrossed = Crossed (shortEma,longEma);
total = OrdersTotal();
if(total < 1)
{
if(isCrossed == 1)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,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("Error opening BUY order : ",GetLastError());
return(0);
}
if(isCrossed == 2)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,
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("Error opening SELL order : ",GetLastError());
return(0);
}
return(0);
}
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
{
// should it be closed?
//if(isCrossed == 2)
//{
//OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet) ;
// close position
//return(0); // exit
//}
// check for trailing stop
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-
Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}
else // go to short position
{
// should it be closed?
//if(isCrossed == 1)
//{
//OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet) ;
// close position
//return(0); // exit
//}
// check for trailing stop
if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
if((OrderStopLoss()>(Ask+Point*TrailingStop)) ||
(OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,
OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
}
}
return(0);
}
//+------------------------------------------------------------------+
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
  #1859 (permalink)  
Old 06-22-2009, 09:38 PM
Member
 
Join Date: Dec 2008
Posts: 30
johnray is on a distinguished road
Thanks

Quote:
Originally Posted by Roger09 View Post
Try this:
Code:
//+------------------------------------------------------------------+
//| JR300.mq4 |
//| onelove |
//| Forex Forum | Forex Tsd | Metatrader Forum |
//+------------------------------------------------------------------+
#property copyright "oneLove"
#property link "http://www.forex-tsd.com"
//---- input parameters
extern double TakeProfit=250.0;
extern double StopLoss=100.0;
extern double Lots=0.1;
extern double TrailingStop=35.0;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
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);
}
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
int cnt, ticket, total;
double shortEma, longEma;
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}
shortEma = iMA(NULL,0,1,0,MODE_EMA,PRICE_CLOSE,0);
longEma = iMA(NULL,0,200,0,MODE_SMA,PRICE_CLOSE,0);
int isCrossed = Crossed (shortEma,longEma);
total = OrdersTotal();
if(total < 1)
{
if(isCrossed == 1)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,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("Error opening BUY order : ",GetLastError());
return(0);
}
if(isCrossed == 2)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,
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("Error opening SELL order : ",GetLastError());
return(0);
}
return(0);
}
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
{
// should it be closed?
//if(isCrossed == 2)
//{
//OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet) ;
// close position
//return(0); // exit
//}
// check for trailing stop
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-
Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}
else // go to short position
{
// should it be closed?
//if(isCrossed == 1)
//{
//OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet) ;
// close position
//return(0); // exit
//}
// check for trailing stop
if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
if((OrderStopLoss()>(Ask+Point*TrailingStop)) ||
(OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,
OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
}
}
return(0);
}
//+------------------------------------------------------------------+


thanks roger09 God Bless you
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
  #1860 (permalink)  
Old 06-23-2009, 02:51 AM
Junior Member
 
Join Date: Sep 2008
Posts: 5
yetidarko is on a distinguished road
Thanks Roger, I've removed that second limit declaration from my start function. I'm surprised the compiler was allowing that one, but it hadn't impacted my program noticably.

Still I am unsure how to alter the running instance of that indcator from my EA. A method would be much appreciated, or at least confirmation that the API does not currently allow me to pass information to running indicators.

Thanks.

[UPDATE: The code was holding me up too much so I've opted for GlobalVariableSet/Get. Its not optimal for obvious reasons, but it gets the job done]

Last edited by yetidarko; 06-23-2009 at 03:24 AM.
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

Tags
#include, candle time, CHinGsMAroonCLK, code, coders guru, conditionally, dll, eli hayun, Eur_harvester.ex4, expert adviser, expert advisor, forex, higher high, how to code, indicator, I_XO_A_H, kehedge, mechanical trading, metatrader command line, mt4, MT4-LevelStop-Reverse, OrderReliable.mqh, programming, rectangle tool, trading, volty channel stop


Currently Active Users Viewing This Thread: 4 (0 members and 4 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

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 04:17 AM.



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