Hi. I forgot to mention that the test was made for a time frame of 15Min. I am trying to learn the code, wich is difficult without some examples, so some help would be very usefull. I would like to test with a trailing stop but first would have to be sure if this is working as it should be without the trailing stop.
Best regards.
#define MAGICMA 20050610 extern double Lots = 0.1; extern double TrailingStop = 0; extern double Kperiod=89; extern double Dperiod=55; extern double Slowing=5; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int start() { double StoCurrent,StoPrevious,SignalCurrent; double SignalPrevious; int cnt, ticket, total; // initial data checks if(Bars<100) { Print("bars less than 100"); return(0); } // -------- StoCurrent=iStochastic(NULL,0,Kperiod,Dperiod,Slowing,MODE_SMA,0,MODE_MAIN,0); StoPrevious=iStochastic(NULL,0,Kperiod,Dperiod,Slowing,MODE_SMA,0,MODE_MAIN,1); SignalCurrent=iStochastic(NULL,0,Dperiod,0,0,MODE_SMA,0,MODE_SIGNAL,0); SignalPrevious=iStochastic(NULL,0,Dperiod,0,0,MODE_SMA,0,MODE_SIGNAL,1); total=OrdersTotal(); if(total<1) { // no opened orders identified if(AccountFreeMargin()<(1000*Lots)) { Print("We have no money. Free Margin = ", AccountFreeMargin()); return(0); } // check for long position (BUY) possibility if((StoPrevious<20 && StoCurrent>20) || (StoCurrent>SignalCurrent && StoPrevious<SignalPrevious && SignalCurrent>20 && SignalCurrent<80) || (StoPrevious<80 && StoCurrent>80)) { ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,0,"Forex15min",MAGICMA,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); } // check for short position (SELL) possibility if((StoPrevious>80 && StoCurrent<80) || (StoCurrent<SignalCurrent && StoPrevious>SignalPrevious && SignalCurrent>20 && SignalCurrent<80) || (StoPrevious>20 && StoCurrent<20)) { ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,0,0,0,"Forex15min",MAGICMA,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 && // check for opened position OrderSymbol()==Symbol()) // check for symbol { if(OrderType()==OP_BUY) // long position is opened { // should it be closed? if((StoPrevious>80 && StoCurrent<80) || (StoCurrent<SignalCurrent && StoPrevious>SignalPrevious && SignalCurrent>20 && SignalCurrent<80) || (StoPrevious>20 && StoCurrent<20)) { OrderClose(OrderTicket(),OrderLots(),Bid,0,Violet); // close position return(0); // exit } // check for trailing stop if(TrailingStop>0 && Bid-OrderOpenPrice()>Point*TrailingStop) { OrderClose(OrderTicket(),OrderLots(),Bid,0,Violet); // close position return(0); // exit } } else // go to short position { // should it be closed? if((StoPrevious<20 && StoCurrent>20) || (StoCurrent>SignalCurrent && StoPrevious<SignalPrevious && SignalCurrent>20 && SignalCurrent<80) || (StoPrevious<80 && StoCurrent>80)) { OrderClose(OrderTicket(),OrderLots(),Ask,0,Violet); // close position return(0); // exit } // check for trailing stop if(TrailingStop>0 && (OrderOpenPrice()-Ask)>(Point*TrailingStop)) { OrderClose(OrderTicket(),OrderLots(),Ask,0,Violet); // close position return(0); // exit } } } } return(0); } // the end.

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
I would like to ask someone if could explain what is wrong in this code of an EA with StochasticOsc. This one, with the parameters I chose, gives more than 650 trades with 5237 bars tested since 1st August. I tested in Metastock and gives about 200 trades. Best regards.
#define MAGICMA 20050610
extern double Lots = 0.1;
extern double TrailingStop = 0;
extern double Kperiod=89;
extern double Dperiod=55;
extern double Slowing=5;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
double StoCurrent,StoPrevious,SignalCurrent;
double SignalPrevious;
int cnt, ticket, total;
// initial data checks
// it is important to make sure that the expert works with a normal
// chart and the user did not make any mistakes setting external
// variables (Lots, StopLoss, TakeProfit,
// TrailingStop) in our case, we check TakeProfit
// on a chart of less than 100 bars
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
// --------
StoCurrent=iStochastic(NULL,0,Kperiod,Dperiod,Slowing,MODE_SMA,0,MODE_MAIN,0);
StoPrevious=iStochastic(NULL,0,Kperiod,Dperiod,Slowing,MODE_SMA,0,MODE_MAIN,1);
SignalCurrent=iStochastic(NULL,0,Dperiod,0,0,MODE_SMA,0,MODE_SIGNAL,0);
SignalPrevious=iStochastic(NULL,0,Dperiod,0,0,MODE_SMA,0,MODE_SIGNAL,1);
total=OrdersTotal();
if(total<1)
{
// no opened orders identified
if(AccountFreeMargin()<(1000*Lots))
{
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
// check for long position (BUY) possibility
if((StoPrevious<20 && StoCurrent>20) || (StoCurrent>SignalCurrent && StoPrevious<SignalPrevious && SignalCurrent>20 &&
SignalCurrent<80) || (StoPrevious<80 && StoCurrent>80))
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,0,"Forex15min",MAGICMA,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);
}
// check for short position (SELL) possibility
if((StoPrevious>80 && StoCurrent<80) || (StoCurrent<SignalCurrent && StoPrevious>SignalPrevious && SignalCurrent>20 &&
SignalCurrent<80) || (StoPrevious>20 && StoCurrent<20))
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,0,0,0,"Forex15min",MAGICMA,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 && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
if(OrderType()==OP_BUY) // long position is opened
{
// should it be closed?
if((StoPrevious>80 && StoCurrent<80) || (StoCurrent<SignalCurrent && StoPrevious>SignalPrevious && SignalCurrent>20 &&
SignalCurrent<80) || (StoPrevious>20 && StoCurrent<20))
{
OrderClose(OrderTicket(),OrderLots(),Bid,0,Violet); // close position
return(0); // exit
}
// check for trailing stop
if(TrailingStop>0 && Bid-OrderOpenPrice()>Point*TrailingStop)
{
OrderClose(OrderTicket(),OrderLots(),Bid,0,Violet); // close position
return(0); // exit
}
}
else // go to short position
{
// should it be closed?
if((StoPrevious<20 && StoCurrent>20) || (StoCurrent>SignalCurrent && StoPrevious<SignalPrevious && SignalCurrent>20 &&
SignalCurrent<80) || (StoPrevious<80 && StoCurrent>80))
{
OrderClose(OrderTicket(),OrderLots(),Ask,0,Violet); // close position
return(0); // exit
}
// check for trailing stop
if(TrailingStop>0 && (OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
OrderClose(OrderTicket(),OrderLots(),Ask,0,Violet); // close position
return(0); // exit
}
}
}
}
return(0);
}
// the end.