Download MetaTrader 5

problem with back test in older code

To add comments, please log in or register
Learn operation principles of Trading Signals for MetaTrader.
shir
29
shir 2014.02.23 07:55 


hello

i try to test this code in mt v 610 and i have a problem with open orders (this code from jimme)


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

//| moving Average v4 .mq4 |

//| Copyright 2014, MetaQuotes Software Corp. |

//| https://www.mql5.com |

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

#property copyright "Copyright 2014, MetaQuotes Software Corp."

#property link "https://www.mql5.com"

//#property version "1.00"

//#property strict


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



extern int PadAmount=0;

extern int CandlesBack=5;

extern int RiskPrecent=1;

extern int Reward_ratio=2;

extern int MaximumStopDistance=50;

extern int FastMA=21;

extern int SlowMA=89;

extern int precentK=5;

extern int precentD=5;

extern int MagicNumber=1234;

int FastMAShift=0;

int FastMaMethod=1;

int FastMaAppliedTo=0;

int SlowMaShift=0;

int SlowMaMethod=1;

int SlowMaAppliedTo=0;

double pips;

//---------------------------------


int init()

{


double ticksize=MarketInfo(Symbol(),MODE_TICKSIZE);

if(ticksize==0.00001 || ticksize == 0.001)

pips=ticksize*10;

else pips=ticksize;

}

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

int deinit()

{

//---

return(0);

}

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

int start()

{

//if(IsNewCandle())CheckForMaTrade();

if(IsNewCandle())CheckForStochasticTrade();

return(0);

}

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

int OpenOrdersThisPair(string pair)

{

int total=0;

for(int i=OrdersTotal()-1; i >=0; i--)

{

OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

if(OrderSymbol()==pair) total++;

}

return (total);

}

//---------------------------------------------------------------------

bool IsNewCandle()

{

static int BarsOnChart=0;

if(Bars==BarsOnChart);

return(false);

BarsOnChart=Bars;

return(true);

}

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

//| |

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

void CheckForMaTrade()

{

double PreviousFast= iMA(NULL,0,FastMA,FastMAShift,FastMaMethod,FastMaAppliedTo,2);

double CurrentFast = iMA(NULL,0,FastMA,FastMAShift,FastMaMethod,FastMaAppliedTo,1);

double PreviousSlow= iMA(NULL,0,SlowMA,SlowMaShift,SlowMaMethod,SlowMaAppliedTo,2);

double CurrentSlow = iMA(NULL,0,SlowMA,SlowMaShift,SlowMaMethod,SlowMaAppliedTo,1);

if(PreviousFast<PreviousSlow && CurrentFast>CurrentSlow)OrderEntry(0);

if(PreviousFast>PreviousSlow && CurrentFast<CurrentSlow)OrderEntry(1);

}

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

//| |

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

void OrderEntry(int direction)

{

double LotSize=0;

double Equity=AccountEquity();

double RiskAmount=Equity*RiskPrecent*0.01;

int bayStopCandle=iLowest(NULL,0,1,0,1);

int sellStopCandle=iHighest(NULL,0,2,0,1);

double buy_stop_price=Low[bayStopCandle]-PadAmount*pips;

double pips_to_bsl=Ask-buy_stop_price;

double buy_takeprofit_price=Ask+pips_to_bsl*Reward_ratio;

double sell_stop_price=High[bayStopCandle]+PadAmount*pips;

double pips_to_ssl=sell_stop_price-Bid;

double sell_takeprofit_price=Bid -pips_to_ssl*Reward_ratio;

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

if (direction==0 && pips_to_bsl / pips<MaximumStopDistance)

{

double bsl=buy_stop_price;

double btp=buy_takeprofit_price;

//LotSize=100/0.00500/0.00010)/10;

LotSize=(RiskAmount/(pips_to_bsl/pips))/10;

if(OpenOrdersThisPair(Symbol())==0)int buyTicket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,0,0,NULL,MagicNumber,0,Green);

if (buyTicket>0)

OrderModify(buyTicket,OrderOpenPrice(),bsl,btp,0,CLR_NONE);


}


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

if (direction==1 && pips_to_ssl / pips<MaximumStopDistance)

{

double ssl=sell_stop_price;

double stp=sell_takeprofit_price;

LotSize=(RiskAmount/(pips_to_ssl/pips))/10;

if(OpenOrdersThisPair(Symbol())==0)int sellTicket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,0,0,NULL,MagicNumber,0,Red);

if (sellTicket>0)

OrderModify(sellTicket,OrderOpenPrice(),ssl,stp,0,CLR_NONE);

}


//| |

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

}

void CheckForStochasticTrade()

{


double CurrentFast = iMA(NULL,0,FastMA,FastMAShift,FastMaMethod,FastMaAppliedTo,1);

double CurrentSlow = iMA(NULL,0,SlowMA,SlowMaShift,SlowMaMethod,SlowMaAppliedTo,1);

double K_Line =iStochastic(NULL,0,precentK,precentD,3,0,0,MODE_MAIN,1);

double D_Line =iStochastic(NULL,0,precentK,precentD,3,0,0,MODE_SIGNAL,1);

double Previous_K_Line = iStochastic(NULL,0,precentK,precentD,3,0,0,MODE_MAIN,2);

double Previous_D_Line = iStochastic(NULL,0,precentK,precentD,3,0,0,MODE_SIGNAL,2);

if(CurrentFast<CurrentSlow)

if(Previous_K_Line > 80)

if(Previous_K_Line>Previous_D_Line && K_Line < D_Line)

OrderEntry(1);

if(CurrentFast > CurrentSlow)

if(Previous_K_Line < 20)

if(Previous_K_Line < Previous_D_Line && K_Line > D_Line)

OrderEntry(0);

}


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


Keith Watford
Moderator
9650
Keith Watford 2014.02.23 08:31  

What do these result in?

int bayStopCandle=iLowest(NULL,0,1,0,1);

int sellStopCandle=iHighest(NULL,0,2,0,1);
shir
29
shir 2014.02.23 09:20  
look back the candele low and the high before order
Alain Verleyen
Moderator
30755
Alain Verleyen 2014.02.23 10:12  
shir266:
look back the candele low and the high before order
I suggest you to read the documentation again : iLowest
Keith Watford
Moderator
9650
Keith Watford 2014.02.23 10:40  
shir266:
look back the candele low and the high before order


Check what the last 2 parameters do

int bayStopCandle=iLowest(NULL,0,1,0,1);
whroeder1
14532
whroeder1 2014.02.23 13:02  

  1. Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. int bayStopCandle=iLowest(NULL,0,1,0,1);
                                     ^ what does that mean? Document your code.
    int bayStopCandle=iLowest(NULL,0,MODE_LOW,0,1);
    
    RTFM iLowest Check what the last 2 parameters do.
  3. OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
    buyTicket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,0,0,NULL,MagicNumber,0,Green);
    if (buyTicket>0)
       OrderModify(buyTicket,OrderOpenPrice(),bsl,btp,0,CLR_NONE);
    }
    
    What are Function return values ? How do I use them ? - MQL4 forum
  4. Adjust for 4/5 digit brokers, including slippage
To add comments, please log in or register