Where is the complete code?
This is how i'm using it:
#property strict // Enums struct STradeSum { int count; double profit; double trailPrice; }; // inputs input bool inpStackMartingale = false; // stack martingale input int inpMagicNumber = 7777; // Magic number // Globals double martingale_vol = 0.1; int lossCount = 0; ulong last_ticket = 0; double martingale_mod = 1.5; datetime m_prev_bars = 0; // "0" = D'1970.01.01 00:00'; ENUM_ORDER_TYPE OrderType = ORDER_TYPE_BUY; int OnInit(){return(INIT_SUCCEEDED);} //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick(){ STradeSum sum; GetSum(sum); if (sum.count > 0 && !isNewBar()) return; if (sum.count < 1){ GetLastResults(); OpenTrade(); } } //+------------------------------------------------------------------+ //| Checks if it's a new bar | //+------------------------------------------------------------------+ bool isNewBar(){ datetime time_0=iTime(Symbol(),Period(),0); if(time_0 == m_prev_bars) return false; m_prev_bars=time_0; return true; } //+------------------------------------------------------------------+ //| Gets Bid price //+------------------------------------------------------------------+ double GetBid(){ return NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits); } //+------------------------------------------------------------------+ //| Gets Ask price //+------------------------------------------------------------------+ double GetAsk(){ return NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK),_Digits); } //+------------------------------------------------------------------+ //| Gets price depending if it's long or short //+------------------------------------------------------------------+ double GetPrice(){ return (OrderType == ORDER_TYPE_BUY ? GetAsk() : GetBid()); } //+------------------------------------------------------------------+ //| Open new trades //+------------------------------------------------------------------+ void OpenTrade(){ if(!Trade.PositionOpen(Symbol(), OrderType, martingale_vol, GetPrice(), 0, 0, "EATest")){ //--- failure message uint resCode = Trade.ResultRetcode(); string resDesc = Trade.ResultRetcodeDescription(); Print("PositionOpen() method failed. Return code=", resCode,". Code description: ", resDesc); } else { uint resCode = Trade.ResultRetcode(); string resDesc = Trade.ResultRetcodeDescription(); Print("PositionOpen() method executed successfully. Return code=", resCode," (", resDesc,")"); } } //+------------------------------------------------------------------+ //| Gets total profit and count of active trades //+------------------------------------------------------------------+ void GetSum(STradeSum &sum){ int count = PositionsTotal(); sum.count = 0; sum.profit = 0.0; sum.trailPrice = 0.0; for (int i = count-1; i>=0; i--){ ulong ticket = PositionGetTicket(i); if (ticket > 0){ if (PositionGetString(POSITION_SYMBOL) == Symbol() && PositionGetInteger(POSITION_TYPE) == OrderType){ sum.count++; sum.profit += PositionGetDouble(POSITION_PROFIT) + PositionGetDouble(POSITION_SWAP); if (sum.trailPrice == 0) sum.trailPrice = PositionGetDouble(POSITION_PRICE_OPEN); } } } return; } //+------------------------------------------------------------------+ //| Get last results | //+------------------------------------------------------------------+ void GetLastResults(){ //--- request trade history datetime end_date = TimeCurrent(); datetime start_date = end_date - (PeriodSeconds(PERIOD_D1)*2); HistorySelect(start_date, end_date); int counter=0; //--- for all deals for (uint i = HistoryDealsTotal()-1; i >= 0; i--){ ulong ticket = HistoryDealGetTicket(i); if (ticket > 0){ string pos_symbol = HistoryDealGetString(ticket, DEAL_SYMBOL); ENUM_ORDER_TYPE pos_ordertype = (ENUM_ORDER_TYPE)HistoryDealGetInteger(ticket, DEAL_TYPE); long pos_magicnumber = HistoryDealGetInteger(ticket, DEAL_MAGIC); if (pos_symbol == Symbol() && OrderType == pos_ordertype && pos_magicnumber == inpMagicNumber){ double deal_commission = HistoryDealGetDouble(ticket, DEAL_COMMISSION); double deal_swap = HistoryDealGetDouble(ticket, DEAL_SWAP); double deal_profit = HistoryDealGetDouble(ticket, DEAL_PROFIT); long deal_entry = HistoryDealGetInteger(ticket,DEAL_ENTRY); //--- if (deal_entry==DEAL_ENTRY_OUT){ counter++; if (deal_commission + deal_swap + deal_profit < 0){ if (last_ticket != ticket){ last_ticket = ticket; lossCount++; if (lossCount >= 3){ if (!inpStackMartingale) martingale_vol = NormalizeDouble(0.1* martingaleMod, 2); else martingale_vol = NormalizeDouble(martingale_vol * martingaleMod, 2); } } }else{ lossCount=0; martingale_vol = 0.1; } if(counter==1) break; } } } } }
I have no issues with the rest of the code, as long as i comment out GetLastResults() the tester works properly, as soon as i add GetLastResults(); into the OnTick the tester freezes permanently.
- Mauricio Ramirez #: I have no issues with the rest of the code, as long as i comment out GetLastResults() the tester works properly, as soon as i add GetLastResults(); into the OnTick the tester freezes permanently.
if(time_0 == m_prev_bars) return false; m_prev_bars=time_0;
Post code that will actually compile.
-
double GetPrice(){ return (OrderType == ORDER_TYPE_BUY ? GetAsk() : GetBid());
Post code that will actually compile.
-
return NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
Prices you get from the terminal are already normalized. Prices you generate must be normalized to tick size not point.
-
Post code that will actually compile.
-
Post code that will actually compile.
-
Prices you get from the terminal are already normalized. Prices you generate must be normalized to tick size not point.
aah, sorry, i was missing the global variable for the newbar, i can't just copy/paste everything because i have a lot of trash commented out and variables that are not used, functions not being used and so on. I tried to show the used, clean and working code. Just fixed the code and it should compile now.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I have an EA in which i need to get the result of the latest deal, to see if it closed in profit or loss which works perfectly in a graph, however, when running the strategy tester, as soon as it reaches the code to check the deal history the tester permanently freezes.
The code only runs once before opening a trade if the conditions to open a trade are met in the OnTick() function:
Is there any other way i should do this so the tester won't freeze? Thank you in advance!