The code below is borrowed off one of the fabulous contributors on this site.
What I am trying to achieve is working in different time frames. I have used the code below to create an example of this. However if I kick off the 'Strategy Tester' on a minute time frame (M1), none of the ADX charts or the MA appear on any of the charts. The price charts for all the time frames appear.
Questions:-
is using different time frames for the indicators allowed?
Can we get the indicators in the different time frames to appear
Is there a really good example of how to work in more than one time frame, out there please? I just want to work in the 2minute and 15 minute time frames.
Hope someone can help :-)
Thanks in advance.
TC
//+------------------------------------------------------------------+//| My_First_EA.mq5 |//| Copyright 2010, MetaQuotes Software Corp. |//| http://www.mql5.com |//+------------------------------------------------------------------+#property copyright "Copyright 2010, MetaQuotes Software Corp."#property link "http://www.mql5.com"#property version "1.00"//--- input parametersinputint StopLoss=30; // Stop Lossinputint TakeProfit=100; // Take Profitinputint ADX_Period=8; // ADX Periodinputint MA_Period=8; // Moving Average Periodinputint EA_Magic=12345; // EA Magic Numberinputdouble Adx_Min=22.0; // Minimum ADX Valueinputdouble Lot=0.1; // Lots to Trade//--- Other parametersint adxHandle1; // handle for our ADX indicatorint adxHandle2; // handle for our ADX indicatorint maHandle; // handle for our Moving Average indicatordouble plsDI[],minDI[],adxVal[]; // Dynamic arrays to hold the values of +DI, -DI and ADX values for each barsdouble plsDI2[],minDI2[],adxVal2[]; // Dynamic arrays to hold the values of +DI, -DI and ADX values for each barsdouble maVal[]; // Dynamic array to hold the values of Moving Average for each barsdouble p_close; // Variable to store the close value of a barint STP, TKP; // To be used for Stop Loss & Take Profit values//+------------------------------------------------------------------+//| Expert initialization function |//+------------------------------------------------------------------+intOnInit()
{
//--- Get handle for ADX indicator
adxHandle1=iADX(NULL,PERIOD_M3,ADX_Period);
adxHandle2=iADX(NULL,PERIOD_M15,ADX_Period);
//--- Get the handle for Moving Average indicator
maHandle=iMA(_Symbol,PERIOD_M30,MA_Period,0,MODE_EMA,PRICE_CLOSE);
//--- What if handle returns Invalid Handleif(adxHandle1<0 || adxHandle2 <0 || maHandle<0)
{
Alert("Error Creating Handles for indicators - error: ",GetLastError(),"!!");
return(-1);
}
//--- Let us handle currency pairs with 5 or 3 digit prices instead of 4
STP = StopLoss;
TKP = TakeProfit;
if(_Digits==5 || _Digits==3)
{
STP = STP*10;
TKP = TKP*10;
}
return(0);
}
//+------------------------------------------------------------------+//| Expert deinitialization function |//+------------------------------------------------------------------+voidOnDeinit(constint reason)
{
//--- Release our indicator handlesIndicatorRelease(adxHandle1);
IndicatorRelease(adxHandle2);
IndicatorRelease(maHandle);
}
//+------------------------------------------------------------------+//| Expert tick function |//+------------------------------------------------------------------+voidOnTick()
{
//--- Do we have enough bars to work withif(Bars(_Symbol,_Period)<60) // if total bars is less than 60 bars
{
Alert("We have less than 60 bars, EA will now exit!!");
return;
}
// We will use the static Old_Time variable to serve the bar time.// At each OnTick execution we will check the current bar time with the saved one.// If the bar time isn't equal to the saved time, it indicates that we have a new tick.staticdatetime 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 tradeMqlTick latest_price; // To be used for getting recent/latest price quotesMqlTradeRequest mrequest; // To be used for sending our trade requestsMqlTradeResult mresult; // To be used to get our trade resultsMqlRates mrate[]; // To be used to store the prices, volumes and spread of each bar/*
Let's make sure our arrays values for the Rates, ADX Values and MA values
is store serially similar to the timeseries array
*/// the rates arraysArraySetAsSeries(mrate,true);
// the ADX DI+values arrayArraySetAsSeries(plsDI,true);
// the ADX DI-values arrayArraySetAsSeries(minDI,true);
// the ADX values arraysArraySetAsSeries(adxVal,true);
// the MA-8 values arraysArraySetAsSeries(maVal,true);
//--- Get the last price quote using the MQL5 MqlTick Structureif(!SymbolInfoTick(_Symbol,latest_price))
{
Alert("Error getting the latest price quote - error:",GetLastError(),"!!");
return;
}
//--- Get the details of the latest 3 barsif(CopyRates(_Symbol,_Period,0,3,mrate)<0)
{
Alert("Error copying rates/history data - error:",GetLastError(),"!!");
ResetLastError();
return;
}
//--- Copy the new values of our indicators to buffers (arrays) using the handleif(CopyBuffer(adxHandle1,0,0,3,adxVal)<0 || CopyBuffer(adxHandle1,1,0,3,plsDI)<0
|| CopyBuffer(adxHandle1,2,0,3,minDI)<0)
{
Alert("Error copying ADX indicator Buffers - error:",GetLastError(),"!!");
ResetLastError();
return;
}
if(CopyBuffer(adxHandle2,0,0,3,adxVal2)<0 || CopyBuffer(adxHandle2,1,0,3,plsDI2)<0
|| CopyBuffer(adxHandle2,2,0,3,minDI2)<0)
{
Alert("Error copying ADX indicator 2 Buffers - error:",GetLastError(),"!!");
ResetLastError();
return;
}
if(CopyBuffer(maHandle,0,0,3,maVal)<0)
{
Alert("Error copying Moving Average indicator buffer - error:",GetLastError());
ResetLastError();
return;
}
//--- we have no errors, so continue//--- Do we have positions opened already?bool Buy_opened=false; // variable to hold the result of Buy opened positionbool Sell_opened=false; // variables to hold the result of Sell opened positionif(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; // It is a Sell
}
}
// Copy the bar close price for the previous bar prior to the current bar, that is Bar 1
p_close=mrate[1].close; // bar 1 close price/*
1. Check for a long/Buy Setup : MA-8 increasing upwards,
previous price close above it, ADX > 22, +DI > -DI
*///--- Declare bool type variables to hold our Buy Conditionsbool Buy_Condition_1=(maVal[0]>maVal[1]) && (maVal[1]>maVal[2]); // MA-8 Increasing upwardsbool Buy_Condition_2 = (p_close > maVal[1]); // previuos price closed above MA-8bool Buy_Condition_3 = (adxVal[0]>Adx_Min); // Current ADX value greater than minimum value (22)bool Buy_Condition_4 = (plsDI[0]>minDI[0]); // +DI greater than -DI//--- Putting all together if(Buy_Condition_1 && Buy_Condition_2)
{
if(Buy_Condition_3 && Buy_Condition_4)
{
// any opened Buy position?if(Buy_opened)
{
Alert("We already have a Buy Position!!!");
return; // Don't open a new Buy Position
}
mrequest.action = TRADE_ACTION_DEAL; // immediate order execution
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_AON; // 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;
}
}
}
/*
2. Check for a Short/Sell Setup : MA-8 decreasing downwards,
previous price close below it, ADX > 22, -DI > +DI
*///--- Declare bool type variables to hold our Sell Conditionsbool Sell_Condition_1 = (maVal[0]<maVal[1]) && (maVal[1]<maVal[2]); // MA-8 decreasing downwardsbool Sell_Condition_2 = (p_close <maVal[1]); // Previous price closed below MA-8bool Sell_Condition_3 = (adxVal[0]>Adx_Min); // Current ADX value greater than minimum (22)bool Sell_Condition_4 = (plsDI[0]<minDI[0]); // -DI greater than +DI//--- Putting all togetherif(Sell_Condition_1 && Sell_Condition_2)
{
if(Sell_Condition_3 && Sell_Condition_4)
{
// any opened Sell position?if(Sell_opened)
{
Alert("We already have a Sell position!!!");
return; // Don't open a new Sell Position
}
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_AON; // 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;
}
}
}
return;
}
//+------------------------------------------------------------------+
Hi,
Really could do with some help here.
The code below is borrowed off one of the fabulous contributors on this site.
What I am trying to achieve is working in different time frames. I have used the code below to create an example of this. However if I kick off the 'Strategy Tester' on a minute time frame (M1), none of the ADX charts or the MA appear on any of the charts. The price charts for all the time frames appear.
Questions:-