- How to connect to the web platform - MetaTrader 5
- MetaTrader 5 Virtual Hosting for Forex and stocks
- Flexible MetaTrader 5 trading system with all order types
I need it to also reset the Take Profit at the same time it resets the Stop Loss'
Basic Example with round numbers to make it easier to explain:
Buy AUDUSD at 1.0500 with TP 1.0600 SL 1.04700
Once market gets to 1.0590 ( just below my TP) it resets my TP to 1.0700 and resets SL to 1.0550 allowing price to climb even further than my original TP and if it starts to drop I still end up with profit even if it hits new SL
Lets say it continues to climb, once it gets to 1.0690 (just below the new TP) it resets my TP to 1.0800 and resets SL to 1.0650, once again allowing it to climb even further or increasing profit levels even if it starts to drop back to my new SL.
Currently trying similar EA's on MT4, but not exactly working as I would like. (Hidden SL and TP was not what I was after but thank you anyway for suggestion "song song")
Try this:
#property copyright "meisme" #property version "1.00" #include <Trade\SymbolInfo.mqh> #include <Trade\Trade.mqh> #include <Trade\PositionInfo.mqh> //User input input double THRESHOLD_POINT_DIFFERENCE = 100; class TPShifter{ public: TPShifter(){ m_pPosition = new CPositionInfo; m_pSymbol = new CSymbolInfo; m_pTrade = new CTrade; m_pSymbol.Name(Symbol()); //Computes difference (pip or pipete) m_dbPointDifference = THRESHOLD_POINT_DIFFERENCE / (pow(10, m_pSymbol.Digits())); } ~TPShifter(){ delete m_pPosition; delete m_pSymbol; delete m_pTrade; } void Run(){ //Check if any open position? if (m_pPosition.Select(Symbol()) == true){ //Yes there is an open position, get current TP and SL double dbPrice_TP = m_pPosition.TakeProfit(); double dbPrice_SL = m_pPosition.StopLoss(); //Update symbol rate m_pSymbol.Refresh(); m_pSymbol.RefreshRates(); //Check current order type if (m_pPosition.PositionType() == POSITION_TYPE_BUY){ //Position BUY, use BID price //Check if the current bid price already meet the threshold. If yes, move TP and SL if (m_pSymbol.Bid() > (dbPrice_TP - m_dbPointDifference)){ //Move TP and SL double dbNewPriceTP = NormalizeDouble(dbPrice_TP + m_dbPointDifference, m_pSymbol.Digits()); double dbNewPriceSL = NormalizeDouble(dbPrice_SL + m_dbPointDifference, m_pSymbol.Digits()); if (m_pTrade.PositionModify(Symbol(), dbNewPriceSL, dbNewPriceTP ) == true) Print(Symbol(), "-(POSITION_TYPE_BUY) SUCCESS to modify TP to: ", dbNewPriceTP, ", SL to: ", dbNewPriceSL); else Print(Symbol(), "-(POSITION_TYPE_BUY) FAILED to modify TP to: ", dbNewPriceTP, ", SL to: ", dbNewPriceSL); } }else{ //Position SELL, use ASK price //Check if the current bid price already meet the threshold. If yes, move TP and SL if (m_pSymbol.Ask() < (dbPrice_TP + m_dbPointDifference)){ //Move TP and SL double dbNewPriceTP = NormalizeDouble(dbPrice_TP - m_dbPointDifference, m_pSymbol.Digits()); double dbNewPriceSL = NormalizeDouble(dbPrice_SL - m_dbPointDifference, m_pSymbol.Digits()); if (m_pTrade.PositionModify(Symbol(), dbNewPriceSL, dbNewPriceTP) == true) Print(Symbol(), "-(POSITION_TYPE_SELL) SUCCESS to modify TP to: ", dbNewPriceTP, ", SL to: ", dbNewPriceSL); else Print(Symbol(), "-(POSITION_TYPE_SELL) FAILED to modify TP to: ", dbNewPriceTP, ", SL to: ", dbNewPriceSL); } } } } private: CPositionInfo *m_pPosition; CSymbolInfo *m_pSymbol; CTrade *m_pTrade; double m_dbPointDifference; }; TPShifter *pTPShifter; int OnInit(){pTPShifter = new TPShifter;return(0);} void OnDeinit(const int reason){delete pTPShifter;} void OnTick(){pTPShifter.Run();}
Don't forget to test and verify it before u use it in your real account. Cheers!
- www.mql5.com
- www.metatrader5.com
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use