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"inputint StopLoss=0; // Stop Lossinputint TakeProfit=0; // Take Profitinputint KPeriod=5; //stochastic k line jcodeinputint DPeriod=3;//stochastic d line jcodeinputint Slowing=10;//stochastic jcodeinputENUM_MA_METHOD StochMethod = MODE_SMA;//stochastic jcodeinputENUM_STO_PRICE StochPrice = STO_LOWHIGH;//stochastic jcodeinputint EA_Magic=12345; // EA Magic Numberinputdouble Lot=0.1; // Lots to Tradeint stocHandle;// handle for our stochastic indicator jcode////xxxxdouble main[], signal[];
double p_close; // Variable to store the close value of a barint STP, TKP;
//+------------------------------------------------------------------+//| Expert initialization function//+------------------------------------------------------------------+intOnInit()
{
//---- handle stochastic jcode
stocHandle = iStochastic(_Symbol, 0, KPeriod, DPeriod,Slowing,StochMethod, StochPrice);//jif(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//+------------------------------------------------------------------+voidOnDeinit(constint reason)
{
IndicatorRelease(stocHandle);//jcode
}
//+------------------------------------------------------------------+//| Expert tick function//+------------------------------------------------------------------+voidOnTick()
{
// 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 thatstaticdatetime 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 appearedif(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 barif(IsNewBar==false)
{
return;
}
//--- Do we have enough bars to work withint 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 tradMqlTick latest_price;
// get price quotesMqlTradeRequest mrequest;
MqlTradeResult mresult;
// get trade resultsMqlRates mrate[];
// store the prices, volumes and spread of each barZeroMemory(mrequest);
ArraySetAsSeries(main,true);
ArraySetAsSeries(signal,true);
//--- Get last price quote using MqlTickif(!SymbolInfoTick(_Symbol,latest_price))
{
Alert("Error getting the latest price quote - error:",
GetLastError(),"!!");
return;
}
//Get details of the latest 3 barsif(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
}
elseif(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 signalif(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 orderOrderSend(mrequest,mresult);
// get the result codeif(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 signalif(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 orderOrderSend(mrequest,mresult);
// get the result codeif(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;
}
}
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
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