I have a code example like this: Traffic
get the iMA value on bar #0, we pass the received value to the Trailing procedure
if(InpTrailingFrequency<10) { double level; if(FreezeStopsLevels(level)) { double ma[]; if(iGetArray(handle_iMA_Trailing,0,0,1,ma)) Trailing(level,ma[0]); } }
Trailing procedure:
//+------------------------------------------------------------------+ //| Trailing | //| InpTrailingStop: min distance from price to Stop Loss | //+------------------------------------------------------------------+ void Trailing(const double stop_level,const double ima_price) { /* Buying is done at the Ask price | Selling is done at the Bid price ------------------------------------------------|---------------------------------- TakeProfit >= Bid | TakeProfit <= Ask StopLoss <= Bid | StopLoss >= Ask TakeProfit - Bid >= SYMBOL_TRADE_STOPS_LEVEL | Ask - TakeProfit >= SYMBOL_TRADE_STOPS_LEVEL Bid - StopLoss >= SYMBOL_TRADE_STOPS_LEVEL | StopLoss - Ask >= SYMBOL_TRADE_STOPS_LEVEL */ if(InpTrailingStop==0) return; for(int i=PositionsTotal()-1;i>=0;i--) // returns the number of open positions if(m_position.SelectByIndex(i)) if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==InpMagic) { if(m_position.PositionType()==POSITION_TYPE_BUY) { if(m_position.PriceCurrent()-ima_price>ExtTrailingStop+ExtTrailingStep) if(m_position.StopLoss()<ima_price && !CompareDoubles(m_position.StopLoss(),ima_price,m_symbol.Digits())) if(m_position.PriceCurrent()-ima_price>=stop_level) { if(!m_trade.PositionModify(m_position.Ticket(), m_symbol.NormalizePrice(ima_price), m_position.TakeProfit())) Print("Modify ",m_position.Ticket(), " Position -> false. Result Retcode: ",m_trade.ResultRetcode(), ", description of result: ",m_trade.ResultRetcodeDescription()); RefreshRates(); m_position.SelectByIndex(i); PrintResultModify(m_trade,m_symbol,m_position); continue; } } else { if(ima_price-m_position.PriceCurrent()>ExtTrailingStop+ExtTrailingStep) if((m_position.StopLoss()>ima_price && !CompareDoubles(m_position.StopLoss(),ima_price,m_symbol.Digits())) || (m_position.StopLoss()==0)) if(ima_price-m_position.PriceCurrent()>=stop_level) { if(!m_trade.PositionModify(m_position.Ticket(), m_symbol.NormalizePrice(ima_price), m_position.TakeProfit())) Print("Modify ",m_position.Ticket(), " Position -> false. Result Retcode: ",m_trade.ResultRetcode(), ", description of result: ",m_trade.ResultRetcodeDescription()); RefreshRates(); m_position.SelectByIndex(i); PrintResultModify(m_trade,m_symbol,m_position); } } } }
Traffic
- www.mql5.com
При открытии у позиции Стоп лосс устанавливается в ноль. Затем производится трейлинг по индикатору iMA (Moving Average, MA). Торговые настройки В советнике можно как включать так и выключать уровни Take Profit и Trailing Stop. Для отключения выбранного параметра достаточно выбранный параметр установить в "0.0". Трейлинг можно задавать двумя...
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hello my dear friends
actually I'm trying to code an EA but I can't code the take profit part...
I would like to take profit if the price hit the MA5 at that point so I wrote something like this :
and I thought each new candle my take profit amount will be update to new mahi5Val[0] if it dose't triggered before, but I'm wrong....
if you can help I'll appreciate you