Discuss News Trader expert advisor here. News Trader is a simple EA to help traders automate their news trading opportunities. News Trader is available for MT4, MT5, and cTrader. You can ask any questions about this expert advisor here.
............. GetPositionStates(); // Adjust SL and TP of the current position if ((HaveLongPosition) || (HaveShortPosition)) ControlPosition(); else { int time = (int)TimeCurrent(); if ((news_time - time <= SecondsBefore) && (news_time > time)) { if (Direction == Buy) fBuy(); else if (Direction == Sell) fSell(); else if (Direction == Both) { fBuy(); fSell(); } else if (Direction == Random) { MathSrand((uint)TimeCurrent()); if (MathRand() % 2 == 1) fBuy(); else fSell(); } if (ECN_Mode) ControlPosition(); } } return(0); } //+------------------------------------------------------------------+ //| Check what positions are currently open. | //+------------------------------------------------------------------+ void GetPositionStates() { int total = OrdersTotal(); for (int cnt = 0; cnt < total; cnt++) { if (OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) == false) continue; if (OrderMagicNumber() != Magic) continue; if (OrderSymbol() != Symbol()) continue; if (OrderType() == OP_BUY) HaveLongPosition = true; else if (OrderType() == OP_SELL) HaveShortPosition = true; } } //+------------------------------------------------------------------+ //| Add SL/TP, adjust SL/TP, set breakeven, close trade. | //+------------------------------------------------------------------+ void ControlPosition() { int total = OrdersTotal(); for (int cnt = 0; cnt < total; cnt++) { if (OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) == false) continue; if (OrderMagicNumber() != Magic) continue; if (OrderSymbol() != Symbol()) continue; if ((OrderType() == OP_BUY) || (OrderType() == OP_SELL)) { RefreshRates(); int time = (int)TimeCurrent(); // Need to adjust or add SL/TP if (time < news_time) { double new_sl, new_tp; if (OrderType() == OP_BUY) { new_sl = NormalizeDouble(Ask - SL * Point, Digits); ...........
.......if (Direction == Both_Pending) ControlPending(); // Adjust SL and TP of the current position if (PositionSelect(Symbol())) ControlPosition(); else { int time = (int)TimeCurrent(); if ((news_time - time <= SecondsBefore) && (news_time > time)) { if (Direction == Buy) fBuy(); else if (Direction == Sell) fSell(); else if ((Direction == Both_Pending) && ((!HaveBuyPending) || (!HaveSellPending))) { fBuy_Pending(); fSell_Pending(); } else if (Direction == Random) { MathSrand((uint)TimeCurrent()); if (MathRand() % 2 == 1) fBuy(); else fSell(); } if (ECN_Mode) ControlPosition(); } } } //+------------------------------------------------------------------+ //| Add SL/TP, adjust SL/TP, set breakeven, close trade. | //+------------------------------------------------------------------+ void ControlPosition() { double Ask = SymbolInfoDouble(Symbol(), SYMBOL_ASK); double Bid = SymbolInfoDouble(Symbol(), SYMBOL_BID); int time = (int)TimeCurrent(); // Need to adjust or add SL/TP if (time < news_time) { double new_sl, new_tp; if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) { new_sl = NormalizeDouble(Ask - SL * Point(), Digits()); new_tp = NormalizeDouble(Ask + TP * Point(), Digits()); } else if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) { new_sl = NormalizeDouble(Bid + SL * Point(), Digits()); new_tp = NormalizeDouble(Bid - TP * Point(), Digits()); } if ((((new_sl != NormalizeDouble(PositionGetDouble(POSITION_SL), Digits())) || (new_tp != NormalizeDouble(PositionGetDouble(POSITION_TP), Digits()))) && (PreAdjustSLTP)) || (((PositionGetDouble(POSITION_SL) == 0) || (PositionGetDouble(POSITION_TP) == 0)) && (ECN_Mode))) { Print("Adjusting SL: ", new_sl, " and TP: ", new_tp, "."); for (int i = 0; i < 10; i++) { bool result = Trade.PositionModify(Symbol(), new_sl, new_tp); if (result) return; else Print("Error modifying position: ", GetLastError()); } } } // Check for breakeven or trade time out. else { if /*(*/(TrailingStop == Breakeven).......
....... /* if (Direction == Both_Pending) ControlPending(); // Adjust SL and TP of the current position if (PositionSelect(Symbol())) ControlPosition(); else { */ int time = (int)TimeCurrent(); if ((news_time - time <= SecondsBefore) && (news_time > time)) { if (Direction == Buy) fBuy(); else if (Direction == Sell) fSell(); else if ((Direction == Both_Pending) && ((!HaveBuyPending) || (!HaveSellPending))) { fSell(); fBuy(); } else if (Direction == Random) { MathSrand((uint)TimeCurrent()); if (MathRand() % 2 == 1) fBuy(); else fSell(); } if (ECN_Mode) ControlPosition(); } //} } //+------------------------------------------------------------------+ //| Add SL/TP, adjust SL/TP, set breakeven, close trade. | //+------------------------------------------------------------------+ void ControlPosition() { double Ask = SymbolInfoDouble(Symbol(), SYMBOL_ASK); double Bid = SymbolInfoDouble(Symbol(), SYMBOL_BID); int total = OrdersTotal(); for (int cnt = 0; cnt < total; cnt ++) { /* if (ORDER_MAGIC() != Magic) continue; if (ORDER_SYMBOL() != Symbol()) continue; */ if ((PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) || (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)) { int time = (int)TimeCurrent(); // Need to adjust or add SL/TP if (time < news_time) { double new_sl, new_tp; if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) { new_sl = NormalizeDouble(Ask - SL * Point(), Digits()); new_tp = NormalizeDouble(Ask + TP * Point(), Digits()); } else if (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL) { new_sl = NormalizeDouble(Bid + SL * Point(), Digits()); new_tp = NormalizeDouble(Bid - TP * Point(), Digits()); } if ((((new_sl != NormalizeDouble(PositionGetDouble(POSITION_SL), Digits())) || (new_tp != NormalizeDouble(PositionGetDouble(POSITION_TP), Digits()))) && (PreAdjustSLTP)) || (((PositionGetDouble(POSITION_SL) == 0) || (PositionGetDouble(POSITION_TP) == 0)) && (ECN_Mode))) { Print("Adjusting SL: ", new_sl, " and TP: ", new_tp, "."); for (int i = 0; i < 10; i++) { bool result = Trade.PositionModify(Symbol(), new_sl, new_tp); if (result) return; else Print("Error modifying position: ", GetLastError()); } } } // Check for breakeven or trade time out. else { if /*(*/(TrailingStop == Breakeven) ...........
..... int total = OrdersTotal(); for (int cnt = 0; cnt < total; cnt ++) { /* if (ORDER_MAGIC() != Magic) continue; if (ORDER_SYMBOL() != Symbol()) continue; */ if ((PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY) || (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)) ......
.... if (OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) == false) continue; ....
Thank you for reminding me about the fact that the MT5 version News Trader is incompatible with the hedging mode.
If you can wait 2-3 days, I will update the EA to work properly in MT5 in hedging mode.
Also please on the EA meant for the cTrader, it seems to open trades earlier than specified.
For instance if I had a news by 13:30gmt broker time (which is the time the algo uses) andI set the seconds before to like say 10secs meaning I want the trades to be opened at 13:29:50 gmt, and click on the play button for the algo, it ends up opening the trade about 30-20secs. It will open the trade at let's say 13:29:32 gmt or some time like that which is not 13:29:50 gmt
Is there any fix to this? Every other thing seems to work well and ok. Please look into it.
Edit: It seems to buy and sell as soon as I click the Play button...
if ((difference <= TimeSpan.FromSeconds(60)) && (difference > TimeSpan.FromMilliseconds(0)))