Example:
Forum on trading, automated trading systems and testing trading strategies
Learning to code. Need help with this.
Vladimir Karputov, 2020.08.21 17:50
One deal per bar
//+------------------------------------------------------------------+ //| One deal per bar.mq5 | //| Copyright 2019, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2019, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" //--- #include <Trade\PositionInfo.mqh> #include <Trade\Trade.mqh> #include <Trade\SymbolInfo.mqh> //--- CPositionInfo m_position; // object of CPositionInfo class CTrade m_trade; // object of CTrade class CSymbolInfo m_symbol; // object of CSymbolInfo class //--- input parameters input ulong InpDeviation = 10; // Deviation, in points (1.00045-1.00055=10 points) input bool InpPrintLog = false; // Print log input ulong InpMagic = 300; // Magic number //--- datetime m_last_deal_time; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- if(!m_symbol.Name(Symbol())) // sets symbol name { Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name"); return(INIT_FAILED); } RefreshRates(); //--- m_trade.SetExpertMagicNumber(InpMagic); m_trade.SetMarginMode(); m_trade.SetTypeFillingBySymbol(m_symbol.Name()); m_trade.SetDeviationInPoints(InpDeviation); //--- iTime(m_symbol.Name(),Period(),0); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- datetime time=iTime(m_symbol.Name(),Period(),0); if(time!=m_last_deal_time) m_trade.Buy(m_symbol.LotsMin(),m_symbol.Name()); } //+------------------------------------------------------------------+ //| TradeTransaction function | //+------------------------------------------------------------------+ void OnTradeTransaction(const MqlTradeTransaction &trans, const MqlTradeRequest &request, const MqlTradeResult &result) { //--- get transaction type as enumeration value ENUM_TRADE_TRANSACTION_TYPE type=trans.type; //--- if transaction is result of addition of the transaction in history if(type==TRADE_TRANSACTION_DEAL_ADD) { long deal_ticket =0; long deal_order =0; long deal_time =0; long deal_time_msc =0; long deal_type =-1; long deal_entry =-1; long deal_magic =0; long deal_reason =-1; long deal_position_id =0; double deal_volume =0.0; double deal_price =0.0; double deal_commission =0.0; double deal_swap =0.0; double deal_profit =0.0; string deal_symbol =""; string deal_comment =""; string deal_external_id =""; if(HistoryDealSelect(trans.deal)) { deal_ticket =HistoryDealGetInteger(trans.deal,DEAL_TICKET); deal_order =HistoryDealGetInteger(trans.deal,DEAL_ORDER); deal_time =HistoryDealGetInteger(trans.deal,DEAL_TIME); deal_time_msc =HistoryDealGetInteger(trans.deal,DEAL_TIME_MSC); deal_type =HistoryDealGetInteger(trans.deal,DEAL_TYPE); deal_entry =HistoryDealGetInteger(trans.deal,DEAL_ENTRY); deal_magic =HistoryDealGetInteger(trans.deal,DEAL_MAGIC); deal_reason =HistoryDealGetInteger(trans.deal,DEAL_REASON); deal_position_id =HistoryDealGetInteger(trans.deal,DEAL_POSITION_ID); deal_volume =HistoryDealGetDouble(trans.deal,DEAL_VOLUME); deal_price =HistoryDealGetDouble(trans.deal,DEAL_PRICE); deal_commission =HistoryDealGetDouble(trans.deal,DEAL_COMMISSION); deal_swap =HistoryDealGetDouble(trans.deal,DEAL_SWAP); deal_profit =HistoryDealGetDouble(trans.deal,DEAL_PROFIT); deal_symbol =HistoryDealGetString(trans.deal,DEAL_SYMBOL); deal_comment =HistoryDealGetString(trans.deal,DEAL_COMMENT); deal_external_id =HistoryDealGetString(trans.deal,DEAL_EXTERNAL_ID); } else return; ENUM_DEAL_ENTRY enum_deal_entry=(ENUM_DEAL_ENTRY)deal_entry; if(deal_symbol==m_symbol.Name() && deal_magic==InpMagic) if(deal_type==DEAL_TYPE_BUY || deal_type==DEAL_TYPE_SELL) if(deal_entry==DEAL_ENTRY_IN) m_last_deal_time=iTime(m_symbol.Name(),Period(),0); } } //+------------------------------------------------------------------+ //| Refreshes the symbol quotes data | //+------------------------------------------------------------------+ bool RefreshRates() { //--- refresh rates if(!m_symbol.RefreshRates()) { if(InpPrintLog) Print(__FILE__," ",__FUNCTION__,", ERROR: ","RefreshRates error"); return(false); } //--- protection against the return value of "zero" if(m_symbol.Ask()==0 || m_symbol.Bid()==0) { if(InpPrintLog) Print(__FILE__," ",__FUNCTION__,", ERROR: ","Ask == 0.0 OR Bid == 0.0"); return(false); } //--- return(true); } //+------------------------------------------------------------------+
and
Forum on trading, automated trading systems and testing trading strategies
Vladimir Karputov, 2020.04.09 08:44
Open position every new candle.
First we define a new bar.
If this is a new bar - open a BUY position with a minimum volume.
//+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- we work only at the time of the birth of new bar datetime time_0=iTime(m_symbol.Name(),Period(),0); if(time_0==m_prev_bars) return; m_prev_bars=time_0; //--- m_trade.Buy(m_symbol.LotsMin()); }
Full code:
//+------------------------------------------------------------------+ //| Open position every new candle.mq5 | //| Copyright © 2020, Vladimir Karputov | //+------------------------------------------------------------------+ #property copyright "Copyright © 2020, Vladimir Karputov" #property version "1.000" /* barabashkakvn Trading engine 3.116 */ #include <Trade\Trade.mqh> #include <Trade\SymbolInfo.mqh> //--- CTrade m_trade; // object of CTrade class CSymbolInfo m_symbol; // object of CSymbolInfo class //--- input parameters input ulong InpDeviation = 10; // Deviation, in points (1.00045-1.00055=10 points) input ulong InpMagic = 201; // Magic number //--- datetime m_prev_bars = 0; // "0" -> D'1970.01.01 00:00'; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- if(!m_symbol.Name(Symbol())) // sets symbol name { Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name"); return(INIT_FAILED); } //--- m_trade.SetExpertMagicNumber(InpMagic); m_trade.SetMarginMode(); m_trade.SetTypeFillingBySymbol(m_symbol.Name()); m_trade.SetDeviationInPoints(InpDeviation); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- we work only at the time of the birth of new bar datetime time_0=iTime(m_symbol.Name(),Period(),0); if(time_0==m_prev_bars) return; m_prev_bars=time_0; //--- m_trade.Buy(m_symbol.LotsMin()); } //+------------------------------------------------------------------+
Example:
and
Still doesn't work, i have posted the code i used above
/*Here is the solution:
***ONLY COPY AND PASTE FROM VOID TICK ALL THE WAY DOWN***
*/
//+------------------------------------------------------------------+
void OnTick()
{}
//+------------------------------------------------------------------+
//| Returns true if a new bar has appeared for a symbol/period pair |
//+------------------------------------------------------------------+
bool isNewBar()
{
//--- memorize the time of opening of the last bar in the static variable
static datetime last_time=0;
//--- current time
datetime lastbar_time=SeriesInfoInteger(Symbol(),Period(),SERIES_LASTBAR_DATE);
//--- if it is the first call of the function >> equal << to "==""
if(last_time==0)
{
//--- set the time and exit
last_time=lastbar_time;
return(false);
}
//--- if the time differs >>not equal<< to "!=""
if(last_time!=lastbar_time)
{
//--- memorize the time and return true
last_time=lastbar_time;
return(true);
}
//--- if we passed to this line, then the bar is not new; return false
return(false);
}
the make a call to use it simple as checking for the statement to be true:
if (signal =="sell" && isNewBar()==true && PositionsTotal()==0)
trade.Sell(0.01,NULL,Bid,(Bid+425 * _Point),(Bid-800 * _Point),NULL);
if (signal =="buy" && isNewBar()==true && PositionsTotal()==0)
trade.Buy(0.01,NULL,Ask,(Ask-425 * _Point),(Ask+800 * _Point),NULL);

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use