Hi, attached is my simple function to breakeven stoploss after price reaches certain profits on that pair. It is working on all the forex pairs i have tested but, not working on other pairs like indices and cryptos. Im trying to figure out how do i amend my code for this but cant really find much at the moment, any help is appreciated :)
I tried printing statements to debug but seems like it is not entering into the "if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY && PositionGetString(POSITION_SYMBOL) == Symbol() && PositionGetInteger(POSITION_MAGIC) == Magic)" loop.
Its probably not entering in the BreakEvenStop.
The function is working fine with Forex pairs, but not with pairs like XAUUSD,US30 and BTCUSD. Im not sure why it is not getting into the loop since those conditions are valid? or did i miss something, which portion of code do u think it could be causing the issue, appreciate the input :)
if(TotalProfit(Magic) >= risk_money * InpBE && currentSL != newSL) { if(ttrade.PositionModify(posTicket, newSL, currentTP)) Print("Stoploss moved to breakeven for ", Symbol()," after ", InpBE, " ratio reached..."); else Print("Failed to modify stoploss for ", Symbol(), ". Error: ", GetLastError()); }
the function uses Magic profit to decide if do a BE or not!! This will work if you have just one open position.
better to check for that single position profit.
Also check what error code is in journal
User the debugger in MetaEditor.
Open the expert file in MetaEditor, toggle a breakpoint (F9) on that line:
void BreakevenStop() { for(int i = PositionsTotal()-1; i >= 0; i--) { ulong posTicket = PositionGetTicket(i); //... //... }
Then start the debugger on history data (CTRL+F5). You can step over (F10) or step into (F11) to debug your code. Also, the sub-window on the right bottom will display your variables.
https://www.metatrader5.com/en/metaeditor/help/development/debug- www.metatrader5.com
Hi, I tried troubleshooting and printing the errorcode as a practice already, it is just not entering this loop for non forex pairs only, the fx pairs are working fine and as intended by the code attached
you insist not to share your code :D
also I can not see Position selection in your code!
void BreakevenStop() { for(int i = PositionsTotal()-1; i >= 0; i--) { ulong posTicket = PositionGetTicket(i); if (!PositionSelectByTicket(posTicket)) continue; . . .
you insist not to share your code :D
also I can not see Position selection in your code!
//+------------------------------------------------------------------+ //| | double TotalProfit(int magic) { double pft=0; for(int i=PositionsTotal()-1;i>=0;i--) { ulong ticket=PositionGetTicket(i); if(ticket>0) { if(PositionGetInteger(POSITION_MAGIC)==Magic && PositionGetString(POSITION_SYMBOL)==Symbol()) { pft+=PositionGetDouble(POSITION_PROFIT); } } } return(pft); } //+------------------------------------------------------------------+
This is the code to get the profit for that position via magic number, please look at this too :)
Also if im not mistaken the position selection was done alr via positon get ticket and the other conditions specified?
This is the code to get the profit for that position via magic number, please look at this too :)
As I guess before, this code returns profit of all positions from chart symbol. you need to have single position profit to compare because as I see you do not want to do BE for a basket of positions.
this one can help you(for buy):
if(PositionGetDouble(POSITION_PRICE_CURRENT) - PositionGetDouble(POSITION_PRICE_OPEN) >= InpBE * Point() && currentSL<PositionGetDouble(POSITION_PRICE_OPEN)) { if(ttrade.PositionModify(posTicket, newSL, currentTP)) Print("Stoploss moved to breakeven for ", Symbol()," after ", InpBE, " ratio reached..."); else Print("Failed to modify stoploss for ", Symbol(), ". Error: ", GetLastError()); }
Also if im not mistaken the position selection was done alr via positon get ticket and the other conditions specified?
yes, based on documentation it has to select position when we call for ticket number, but yet it is mentioned that " To make sure that you receive valid position data, it is recommended to call PositionSelectByTicket() before you access the data."
As we cannot see more from your code, better to check this item too.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi, attached is my simple function to breakeven stoploss after price reaches certain profits on that pair. It is working on all the forex pairs i have tested but, not working on other pairs like indices and cryptos. Im trying to figure out how do i amend my code for this but cant really find much at the moment, any help is appreciated :)
I tried printing statements to debug but seems like it is not entering into the "if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY && PositionGetString(POSITION_SYMBOL) == Symbol() && PositionGetInteger(POSITION_MAGIC) == Magic)" loop.