Hi MT

it would be very helpful if you do this for me. This code is based on the following logic to sell GBPUSD or EURJPY after the specific time frame and provided that the price range between this time frame is no more than certain pips, then stop and profit are placed respectfully. But something wrong is here, it doesn't work and backtesting is not accurate. Can you take a look and tell me if I am on right path?

//+------------------------------------------------------------------+

//| simple EA.mq4 |

//| Copyright © 2011, luktom.biz |

//| www.luktom.biz/en |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2011, luktom.biz."

#property link "http://www.luktom.biz"

extern double stopLoss = 30;

extern double takeProfit = 50;

//extern string Begin="00:00";

//extern string End="08:15";

extern int StartHour = 0; // Start Hour of Trade Session

extern int StartMinute= 0; // Start Minute of Trade Session

extern int EndHour = 8; // End Hour of Trade Session

extern int EndMinute = 15; // End Hour of Trade Session

extern double distance = 60;

extern double pipBuffer = 10;

extern bool isECN = true;

extern double lots = 0.1;

extern bool tradesunday=false; // trade on sunday

bool cond = false;

bool cond2 = false;

int magic = 1232344524285020;

int slippage = 1;

string comment = "";

int ticket;

int myDigits = 1 ;

double RangeHigh , RangeLow ;

string theComment = "" , theComment2 = "" ;

//+------------------------------------------------------------------+

//| expert initialization function |

//+------------------------------------------------------------------+

int init()

{

//----

//----

if(Digits==5||Digits==3){

stopLoss=stopLoss*10;

takeProfit=takeProfit*10;

distance = distance*10;

pipBuffer = pipBuffer*10;

myDigits = myDigits*10 ;

}

return(0);

}

//+------------------------------------------------------------------+

//| expert deinitialization function |

//+------------------------------------------------------------------+

int deinit()

{

//----

//----

return(0);

}

//+------------------------------------------------------------------+

//| expert start function |

//+------------------------------------------------------------------+

int start()

{

//----

double myDistance = distance*Point ;

int myEndTimeShift = 0 ;

double dt1 = StrToTime(TimeToStr(TimeCurrent(),TIME_DATE) + " " + StartHour + ":" + StartMinute + ":00");

double dt2 = StrToTime(TimeToStr(TimeCurrent(),TIME_DATE) + " " + EndHour + ":" + EndMinute + ":00");

if (dt1 > dt2) dt1 = StrToTime(TimeToStr(TimeCurrent() - 86400, TIME_DATE) + " " + StartHour + ":" + StartMinute + ":00"); //If the StartTime is greater than the EndTime on the same day, the code assumes that the intended start time was 86400 seconds earlier.

if (dt1 > dt2) dt1 -= 86400;

int StartTimeShift = iBarShift(NULL,0,dt1, false);

int EndTimeShift = iBarShift(NULL,0,dt2, false);

if(StartTimeShift!=-1)

{

RangeHigh = iHigh(NULL, 0, iHighest(NULL, 0, MODE_HIGH, (StartTimeShift - EndTimeShift), EndTimeShift));

if(EndTimeShift==0)

{

RangeLow = iLow(NULL, 0, iLowest(NULL, 0, MODE_LOW, (StartTimeShift - EndTimeShift ), EndTimeShift ));

myEndTimeShift = EndTimeShift ;

}else

{

RangeLow = iLow(NULL, 0, iLowest(NULL, 0, MODE_LOW, (StartTimeShift - EndTimeShift +1 ), EndTimeShift + 1 ));

myEndTimeShift = EndTimeShift + 1 ;

}

}

/*Print(" high StartTimeShift " + StartTimeShift + " " + ( iHigh(NULL, 0, StartTimeShift ) ) ) ;

Print(" Low StartTimeShift " + StartTimeShift + " " + ( iLow(NULL, 0, StartTimeShift ) ) ) ;

Print(" high myEndTimeShift " + myEndTimeShift + " " + ( iHigh(NULL, 0, myEndTimeShift ) ) ) ;

Print(" Low myEndTimeShift " + myEndTimeShift + " " + ( iLow(NULL, 0, myEndTimeShift ) ) ) ;

*/

if(TimeCondition())

{

theComment = "Breakout Range - FROM: ["+TimeToStr(dt1)+"] -> TO: ["+TimeToStr(dt2)+"] = " + DoubleToStr( ( (RangeHigh - RangeLow)/ Point / myDigits ), 2 ) +" Pips" ;

theComment2 = "nNO TRADING TIME" ;

}else

{

theComment = "Breakout Range - FROM: ["+TimeToStr(dt1)+"] -> TO: ["+TimeToStr(dt2)+"] = " + DoubleToStr( ( (RangeHigh - RangeLow)/ Point / myDigits ), 2 ) +" Pips" ;

theComment2 = "nNOW IS TRADING TIME!! " ;

}

Comment(theComment+theComment2);

if( (RangeHigh - RangeLow) < myDistance )

{

cond2 = true;

}else

{

cond2 = false;

}

Print(" range " + DoubleToStr( ( (RangeHigh - RangeLow)/ Point / myDigits ), 2 ));

if ( ! TimeCondition() )

{

cond = false;

}

if(!TimeCondition() && cond2 && ( CheckValidOrders() == false) && (DayOfWeek()!=0) )

{

//Print(" range " + DoubleToStr( ( (RangeHigh - RangeLow)/ Point / myDigits ), 2 ));

if(countOrders(magic,OP_SELL) == 0 )

{

if( ( Bid <= RangeLow - (pipBuffer*Point) ) ) // && short == 0

{

if(isECN == false)

{

OrderSend(Symbol(),OP_SELL,lots,Bid, slippage,sltpValue(Ask+ stopLoss*Point,stopLoss) ,sltpValue(Ask- takeProfit*Point,takeProfit),comment,magic);

}

if(isECN)

{

ticket=OrderSend(Symbol(),OP_SELL,lots,Bid, slippage,0,0,comment,magic);

OrderSelect(ticket,SELECT_BY_TICKET);

OrderModify(ticket,OrderOpenPrice(),sltpValue(Ask +stopLoss*Point, stopLoss),sltpValue(Bid- takeProfit*Point,takeProfit),0);

}

}

}

if(countOrders(magic,OP_BUY) == 0)

{

if( ( Ask>= RangeHigh + (pipBuffer*Point) ) )

{

if(isECN==false)

{

OrderSend(Symbol(),OP_BUY,lots,Ask, slippage,sltpValue(Ask - stopLoss*Point, stopLoss),sltpValue(Bid+ takeProfit*Point,takeProfit),comment,magic);

}

if(isECN)

{

ticket=OrderSend(Symbol(),OP_BUY,lots,Ask, slippage,0,0,comment,magic);

OrderSelect(ticket,SELECT_BY_TICKET);

OrderModify(ticket,OrderOpenPrice(),sltpValue(Bid - stopLoss*Point, stopLoss),sltpValue(Ask+ takeProfit*Point,takeProfit),0);

}

}

}

}

//----

return(0);

}

//+------------------------------------------------------------------+

bool TimeCondition()

{

bool result = false;

datetime SessionStart = StrToTime(StartHour+":"+StartMinute);

datetime SessionEnd = StrToTime(EndHour+":"+EndMinute);

if (StartHour < EndHour)

result = TimeCurrent() >= SessionStart && TimeCurrent() < SessionEnd;

else

if (StartHour > EndHour)

result = (TimeCurrent() >= SessionStart && TimeHour(TimeCurrent()) < 24)

||(TimeHour(TimeCurrent()) >= 0 && TimeCurrent() < SessionEnd);

return(result);

}

int countOrders(int oMagic,int oType) {

int count=0;

for(int i=0;i<OrdersTotal();i++) {

if(OrderSelect(i,SELECT_BY_POS)) {

if(OrderMagicNumber()==oMagic) {

if(OrderSymbol()==Symbol()) {

if(OrderType()==oType || oType<0) {

count++;

}

}

}

}

}

return(count);

}

double sltpValue(double w1, int w2)

{

if(w2 == 0)

return (0);

return (NormalizeDouble(w1, Digits));

}

int TotalHistoryOrders() {

int cnt = 0;

int TotalOpenOrders = 0;

cnt=OrdersHistoryTotal();

if(cnt==0) {

return(0);

}

else {

for(;cnt>=0;cnt--) {

RefreshRates();

OrderSelect(cnt,SELECT_BY_POS,MODE_HISTORY);

if(OrderSymbol()==Symbol()) {

TotalOpenOrders++;

}

}

}

return(TotalOpenOrders);

}

bool CheckValidOrders(){

if(TotalHistoryOrders() == 0)

return(false);

int last_order_idx = (TotalHistoryOrders() - 1);

OrderSelect(last_order_idx,SELECT_BY_POS,MODE_HISTORY);

if(TimeDayOfYear(OrderOpenTime()) == TimeDayOfYear(TimeCurrent()) && OrderSymbol() == Symbol()){

return (true);

}

if( TimeDayOfYear(OrderCloseTime()) == TimeDayOfYear(TimeCurrent())

&& TimeDayOfYear(OrderOpenTime()) != TimeDayOfYear(OrderCloseTime()) && OrderSymbol() == Symbol() ){

return (true);

}

return(false);

}

it would be very helpful if you do this for me. This code is based on the following logic to sell GBPUSD or EURJPY after the specific time frame and provided that the price range between this time frame is no more than certain pips, then stop and profit are placed respectfully. But something wrong is here, it doesn't work and backtesting is not accurate. Can you take a look and tell me if I am on right path?

//+------------------------------------------------------------------+

//| simple EA.mq4 |

//| Copyright © 2011, luktom.biz |

//| www.luktom.biz/en |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2011, luktom.biz."

#property link "http://www.luktom.biz"

extern double stopLoss = 30;

extern double takeProfit = 50;

//extern string Begin="00:00";

//extern string End="08:15";

extern int StartHour = 0; // Start Hour of Trade Session

extern int StartMinute= 0; // Start Minute of Trade Session

extern int EndHour = 8; // End Hour of Trade Session

extern int EndMinute = 15; // End Hour of Trade Session

extern double distance = 60;

extern double pipBuffer = 10;

extern bool isECN = true;

extern double lots = 0.1;

extern bool tradesunday=false; // trade on sunday

bool cond = false;

bool cond2 = false;

int magic = 1232344524285020;

int slippage = 1;

string comment = "";

int ticket;

int myDigits = 1 ;

double RangeHigh , RangeLow ;

string theComment = "" , theComment2 = "" ;

//+------------------------------------------------------------------+

//| expert initialization function |

//+------------------------------------------------------------------+

int init()

{

//----

//----

if(Digits==5||Digits==3){

stopLoss=stopLoss*10;

takeProfit=takeProfit*10;

distance = distance*10;

pipBuffer = pipBuffer*10;

myDigits = myDigits*10 ;

}

return(0);

}

//+------------------------------------------------------------------+

//| expert deinitialization function |

//+------------------------------------------------------------------+

int deinit()

{

//----

//----

return(0);

}

//+------------------------------------------------------------------+

//| expert start function |

//+------------------------------------------------------------------+

int start()

{

//----

double myDistance = distance*Point ;

int myEndTimeShift = 0 ;

double dt1 = StrToTime(TimeToStr(TimeCurrent(),TIME_DATE) + " " + StartHour + ":" + StartMinute + ":00");

double dt2 = StrToTime(TimeToStr(TimeCurrent(),TIME_DATE) + " " + EndHour + ":" + EndMinute + ":00");

if (dt1 > dt2) dt1 = StrToTime(TimeToStr(TimeCurrent() - 86400, TIME_DATE) + " " + StartHour + ":" + StartMinute + ":00"); //If the StartTime is greater than the EndTime on the same day, the code assumes that the intended start time was 86400 seconds earlier.

if (dt1 > dt2) dt1 -= 86400;

int StartTimeShift = iBarShift(NULL,0,dt1, false);

int EndTimeShift = iBarShift(NULL,0,dt2, false);

if(StartTimeShift!=-1)

{

RangeHigh = iHigh(NULL, 0, iHighest(NULL, 0, MODE_HIGH, (StartTimeShift - EndTimeShift), EndTimeShift));

if(EndTimeShift==0)

{

RangeLow = iLow(NULL, 0, iLowest(NULL, 0, MODE_LOW, (StartTimeShift - EndTimeShift ), EndTimeShift ));

myEndTimeShift = EndTimeShift ;

}else

{

RangeLow = iLow(NULL, 0, iLowest(NULL, 0, MODE_LOW, (StartTimeShift - EndTimeShift +1 ), EndTimeShift + 1 ));

myEndTimeShift = EndTimeShift + 1 ;

}

}

/*Print(" high StartTimeShift " + StartTimeShift + " " + ( iHigh(NULL, 0, StartTimeShift ) ) ) ;

Print(" Low StartTimeShift " + StartTimeShift + " " + ( iLow(NULL, 0, StartTimeShift ) ) ) ;

Print(" high myEndTimeShift " + myEndTimeShift + " " + ( iHigh(NULL, 0, myEndTimeShift ) ) ) ;

Print(" Low myEndTimeShift " + myEndTimeShift + " " + ( iLow(NULL, 0, myEndTimeShift ) ) ) ;

*/

if(TimeCondition())

{

theComment = "Breakout Range - FROM: ["+TimeToStr(dt1)+"] -> TO: ["+TimeToStr(dt2)+"] = " + DoubleToStr( ( (RangeHigh - RangeLow)/ Point / myDigits ), 2 ) +" Pips" ;

theComment2 = "nNO TRADING TIME" ;

}else

{

theComment = "Breakout Range - FROM: ["+TimeToStr(dt1)+"] -> TO: ["+TimeToStr(dt2)+"] = " + DoubleToStr( ( (RangeHigh - RangeLow)/ Point / myDigits ), 2 ) +" Pips" ;

theComment2 = "nNOW IS TRADING TIME!! " ;

}

Comment(theComment+theComment2);

if( (RangeHigh - RangeLow) < myDistance )

{

cond2 = true;

}else

{

cond2 = false;

}

Print(" range " + DoubleToStr( ( (RangeHigh - RangeLow)/ Point / myDigits ), 2 ));

if ( ! TimeCondition() )

{

cond = false;

}

if(!TimeCondition() && cond2 && ( CheckValidOrders() == false) && (DayOfWeek()!=0) )

{

//Print(" range " + DoubleToStr( ( (RangeHigh - RangeLow)/ Point / myDigits ), 2 ));

if(countOrders(magic,OP_SELL) == 0 )

{

if( ( Bid <= RangeLow - (pipBuffer*Point) ) ) // && short == 0

{

if(isECN == false)

{

OrderSend(Symbol(),OP_SELL,lots,Bid, slippage,sltpValue(Ask+ stopLoss*Point,stopLoss) ,sltpValue(Ask- takeProfit*Point,takeProfit),comment,magic);

}

if(isECN)

{

ticket=OrderSend(Symbol(),OP_SELL,lots,Bid, slippage,0,0,comment,magic);

OrderSelect(ticket,SELECT_BY_TICKET);

OrderModify(ticket,OrderOpenPrice(),sltpValue(Ask +stopLoss*Point, stopLoss),sltpValue(Bid- takeProfit*Point,takeProfit),0);

}

}

}

if(countOrders(magic,OP_BUY) == 0)

{

if( ( Ask>= RangeHigh + (pipBuffer*Point) ) )

{

if(isECN==false)

{

OrderSend(Symbol(),OP_BUY,lots,Ask, slippage,sltpValue(Ask - stopLoss*Point, stopLoss),sltpValue(Bid+ takeProfit*Point,takeProfit),comment,magic);

}

if(isECN)

{

ticket=OrderSend(Symbol(),OP_BUY,lots,Ask, slippage,0,0,comment,magic);

OrderSelect(ticket,SELECT_BY_TICKET);

OrderModify(ticket,OrderOpenPrice(),sltpValue(Bid - stopLoss*Point, stopLoss),sltpValue(Ask+ takeProfit*Point,takeProfit),0);

}

}

}

}

//----

return(0);

}

//+------------------------------------------------------------------+

bool TimeCondition()

{

bool result = false;

datetime SessionStart = StrToTime(StartHour+":"+StartMinute);

datetime SessionEnd = StrToTime(EndHour+":"+EndMinute);

if (StartHour < EndHour)

result = TimeCurrent() >= SessionStart && TimeCurrent() < SessionEnd;

else

if (StartHour > EndHour)

result = (TimeCurrent() >= SessionStart && TimeHour(TimeCurrent()) < 24)

||(TimeHour(TimeCurrent()) >= 0 && TimeCurrent() < SessionEnd);

return(result);

}

int countOrders(int oMagic,int oType) {

int count=0;

for(int i=0;i<OrdersTotal();i++) {

if(OrderSelect(i,SELECT_BY_POS)) {

if(OrderMagicNumber()==oMagic) {

if(OrderSymbol()==Symbol()) {

if(OrderType()==oType || oType<0) {

count++;

}

}

}

}

}

return(count);

}

double sltpValue(double w1, int w2)

{

if(w2 == 0)

return (0);

return (NormalizeDouble(w1, Digits));

}

int TotalHistoryOrders() {

int cnt = 0;

int TotalOpenOrders = 0;

cnt=OrdersHistoryTotal();

if(cnt==0) {

return(0);

}

else {

for(;cnt>=0;cnt--) {

RefreshRates();

OrderSelect(cnt,SELECT_BY_POS,MODE_HISTORY);

if(OrderSymbol()==Symbol()) {

TotalOpenOrders++;

}

}

}

return(TotalOpenOrders);

}

bool CheckValidOrders(){

if(TotalHistoryOrders() == 0)

return(false);

int last_order_idx = (TotalHistoryOrders() - 1);

OrderSelect(last_order_idx,SELECT_BY_POS,MODE_HISTORY);

if(TimeDayOfYear(OrderOpenTime()) == TimeDayOfYear(TimeCurrent()) && OrderSymbol() == Symbol()){

return (true);

}

if( TimeDayOfYear(OrderCloseTime()) == TimeDayOfYear(TimeCurrent())

&& TimeDayOfYear(OrderOpenTime()) != TimeDayOfYear(OrderCloseTime()) && OrderSymbol() == Symbol() ){

return (true);

}

return(false);

}

kiss the trend