stochastic code

 

hi please can somebody help me with my first bit of coding ever. its based on the step by step guide for beginners www.mql5.com/en/articies/100 I've taken out the ma and adx indicators and I think i've but in a stochastic. The ea will run in he tester but doesn't open any trades

#property copyright "Copyright 2010, MetaQuotes Software Corp."
#property link "http://www.mql5.com"
#property version "1.00"
input int StopLoss=0; // Stop Loss
input int TakeProfit=0; // Take Profit
input int KPeriod=5; //stochastic k line jcode
input int DPeriod=3;//stochastic d line jcode
input int Slowing=10;//stochastic jcode
input ENUM_MA_METHOD StochMethod = MODE_SMA;//stochastic jcode
input ENUM_STO_PRICE StochPrice = STO_LOWHIGH;//stochastic jcode
input int EA_Magic=12345; // EA Magic Number
input double Lot=0.1; // Lots to Trade

int stocHandle;// handle for our stochastic indicator jcode
////xxxx
double main[], signal[];

double p_close; // Variable to store the close value of a bar
int STP, TKP;

//+------------------------------------------------------------------+
//| Expert initialization function
//+------------------------------------------------------------------+
int OnInit()
{

//---- handle stochastic jcode
stocHandle = iStochastic(_Symbol, 0, KPeriod, DPeriod,Slowing,StochMethod, StochPrice);//j

if(stocHandle<0)
{
Alert("Error Creating Handles for indicators - error: ",
GetLastError(),"!!");
return(-1);
}

//---  handle currency pairs with 5 or 3 digit prices instead
STP = StopLoss;
TKP = TakeProfit;
if(_Digits==5 || _Digits==3)
{
STP = STP*10;
TKP = TKP*10;
}
return(0);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{

IndicatorRelease(stocHandle);//jcode
}
//+------------------------------------------------------------------+
//| Expert tick function
//+------------------------------------------------------------------+
void OnTick()
{
// enough bars to work with?
if(Bars(_Symbol,_Period)<60)
// if total bars is less than 60 bars
{
Alert("We have less than 60 bars, EA will now exit!!");
return;
}
// use the static Old_Time variable for the bar time.
// At each OnTick execution we will check the current bar time with
// If the bar time isn't equal to the saved time, it indicates that
static datetime Old_Time;
datetime New_Time[1];
bool IsNewBar=false;
// copying the last bar time to the element New_Time[0]
int copied=CopyTime(_Symbol,_Period,0,1,New_Time);

if(copied>0) // ok, the data has been copied successfully
{
if(Old_Time!=New_Time[0])
// if old time isn't equal to new bar time
{
IsNewBar=true;
// if it isn't a first call, the new bar has appeared
if(MQL5InfoInteger(MQL5_DEBUGGING)) Print(
"We have new bar here ",New_Time[0]," old time was ",Old_Time);
Old_Time=New_Time[0]; // saving bar time
}
}
else
{
Alert("Error in copying historical times data, error =",
GetLastError());
ResetLastError();
return;
}
//--- EA should only check for new trade if we have a new bar
if(IsNewBar==false)
{
return;
}
//--- Do we have enough bars to work with
int Mybars=Bars(_Symbol,_Period);
if(Mybars<60) // if total bars is less than 60 bars
{
Alert("We have less than 60 bars, EA will now exit!!");
return;
}
//--- Define some MQL5 Structures we will use for our trad
MqlTick latest_price;
//  get price quotes
MqlTradeRequest mrequest;

MqlTradeResult mresult;
// get trade results
MqlRates mrate[];
//  store the prices, volumes and spread of each bar
ZeroMemory(mrequest);


ArraySetAsSeries(main,true);
ArraySetAsSeries(signal,true);

//--- Get last price quote using  MqlTick
if(!SymbolInfoTick(_Symbol,latest_price))
{
Alert("Error getting the latest price quote - error:",
GetLastError(),"!!");
return;
}
//Get  details of the latest 3 bars
if(CopyRates(_Symbol,_Period,0,3,mrate)<0)
{
Alert("Error copying rates/history data - error:",
GetLastError(),"!!");
ResetLastError();
return;
}
 

if (CopyBuffer(stocHandle,0,0,3,main)<0 || CopyBuffer(stocHandle,1,0,3,signal)<0)//  jcode
{
Alert("Error copying stochastic indicator Buffers - error:",
GetLastError(),"!!");
ResetLastError();
return;
}


bool Buy_opened=false;
bool Sell_opened=false;

if(PositionSelect(_Symbol)==true)
// we have an opened position
{
if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)
{
Buy_opened=true; //It is a Buy
}
else if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)
{
Sell_opened=true;
}
}

p_close=mrate[1].close;


bool Buy_Condition_1 = (main[1]<30 && main[1]>signal[1] );//jcode

//main line is below 30 and has crosed signal

if(Buy_Condition_1 )

if(Buy_opened)
{
Alert("We already have a Buy Position!!!");
return;
}
ZeroMemory(mrequest);
mrequest.action = TRADE_ACTION_DEAL;
//
mrequest.price = NormalizeDouble(latest_price.ask,
_Digits); // latest ask price
mrequest.sl = NormalizeDouble(latest_price.ask - STP*
_Point,_Digits); // Stop Loss
mrequest.tp = NormalizeDouble(latest_price.ask + TKP*
_Point,_Digits); // Take Profit

mrequest.symbol = _Symbol;
// currency pair
mrequest.volume = Lot;
// number of lots to trade
mrequest.magic = EA_Magic;
// Order Magic Number
mrequest.type = ORDER_TYPE_BUY;
// Buy Order
mrequest.type_filling = ORDER_FILLING_FOK;
// Order execution type
mrequest.deviation=100;
// Deviation from current price
//--- send order
OrderSend(mrequest,mresult);
// get the result code
if(mresult.retcode==10009 || mresult.retcode==10008)
//Request is completed or order placed
{
Alert(
"A Buy order has been successfully placed with Ticket#:",
mresult.order,"!!");
}
else
{
Alert(
"The Buy order request could not be completed -error:",
GetLastError());
ResetLastError();
return;
}

bool Sell_Condition_1 =(main[1]>70)&&(main[1]<signal[1]);
//main line is above 70 and has crosed signal


if(Sell_Condition_1)


{
Alert("We already have a Sell position!!!");
return; // Don't open a new Sell Position
}
ZeroMemory(mrequest);
mrequest.action=TRADE_ACTION_DEAL;
// immediate order execution
mrequest.price = NormalizeDouble(latest_price.bid,
_Digits); // latest Bid price
mrequest.sl = NormalizeDouble(latest_price.bid + STP*
_Point,_Digits); // Stop Loss
mrequest.tp = NormalizeDouble(latest_price.bid - TKP*
_Point,_Digits); // Take Profit
mrequest.symbol = _Symbol;
// currency pair
mrequest.volume = Lot;
// number of lots to trade
mrequest.magic = EA_Magic;
// Order Magic Number
mrequest.type= ORDER_TYPE_SELL;
// Sell Order
mrequest.type_filling = ORDER_FILLING_FOK;
// Order execution type
mrequest.deviation=100;
// Deviation from current price
//--- send order
OrderSend(mrequest,mresult);
// get the result code
if(mresult.retcode==10009 || mresult.retcode==10008)
//Request is completed or order placed
{
Alert(
"A Sell order has been successfully placed with Ticket#:",
mresult.order,"!!");
}
else
{
Alert(
"The Sell order request could not be completed -error:",
GetLastError());
ResetLastError();
return;
}
}
 

Forum on trading, automated trading systems and testing trading strategies


Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.


Reason: