Hi
Great code - I would love to backtest it - please give us your icustom Eline.
I modified your code extensively.
Code:
/*
* Created by SharpDevelop.
* User: CARDIO
* Date: 1/17/2006
* Time: 4:55 AM
*
*Todo: if there is an ope position - close it- then open in opposite direction.
*
*/
//+------------------------------------------------------------------+
//| T1.mq4
//|
//|
//+------------------------------------------------------------------+
#include <stdlib.mqh>
extern double Lots = 0.1;
extern int Timeframe = 240;
string strDirCurrent="none";
string strDirPrevious="none";
int cnt, magicEA;
bool isclosing = false;
double slippage = 3;
int init() {
return(0);
}
int deinit() {
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
magicEA = 16384;
// check for current direction
if(iCustom(NULL,Timeframe,"ELine",0,0)>iCustom(NULL,Timeframe,"ELine",1,0))
{
strDirCurrent="long";
}
if(iCustom(NULL,Timeframe,"ELine",0,0)<iCustom(NULL,Timeframe,"ELine",1,0))
{strDirCurrent="short";
}
// compare to previous direction and open a position if there was a change
if(strDirCurrent=="long" && strDirPrevious=="short")
{
//firs close open positions
isclosing = true;
isclosing1();
OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,"T1_EA_Buy_Order",16384,0,Green);
return(0);
}
if(strDirCurrent=="short" && strDirPrevious=="long")
{
isclosing = true;
isclosing1();
OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,"T1_EA_Sell_Order",16384,0,Red);
return(0);
}
strDirPrevious=strDirCurrent;
return(0);
}
// the end.
void isclosing1(){
//Close all open orders
//todo: get a requote on the prices if error 138 occurs, use refreshrates
//todo: check if the last 3 closes where losers - if so stop the ea
int totalOrders = OrdersTotal();
int numPos = 0;
for(cnt=0; cnt<totalOrders; cnt++) { // scan all orders and positions...
OrderSelect(cnt, SELECT_BY_POS); // the next line will check for ONLY market trades, not entry orders
if(OrderSymbol() == Symbol() && OrderType() <= OP_SELL && OrderMagicNumber() == magicEA) { // only look for this symbol, and only orders from this EA
numPos++;
if(OrderType() == OP_BUY) { // Check for close signal for bought trade
if(isclosing) {
if (OrderClose(OrderTicket(),OrderLots(),Bid,slippage,Violet)) { // Close bought trade
//writetofile("10","Closed buy", OrderTicket());
//prtAlert("Day Trading: Closing BUY order");
} else {
// writetofile("10b","Closed buy fail", ErrorDescription(GetLastError()));
}
}
} else { // Check sold trade for close signal
if(isclosing) {
OrderClose(OrderTicket(),OrderLots(),Ask,slippage,Violet);
//writetofile("10","Closed buy", OrderTicket());
// prtAlert("Day Trading: Closing SELL order");
}else {
//writetofile("10c","Closed sell fail", ErrorDescription(GetLastError()));
}
}
}
}
}