I'm planning to do a buy script for currency pairs which can set a SL and TP level FROM THE ENTRY PRICE which WORKS WITH 2,3,4,5 digits broker . Can someone help me to double check it, the SL and TP are not working .
The way I do it was execute a trade FIRST, then only it will modify to add in the SL and TP level FROM THE ENTRY PRICE . Not adding SL and TP at the instant the order is sent . The 2,3,4,5 digits broker however, not so sure, need an eye for that .
How do you get the OrderOpenPrice() before you have even sent the order?
if ( StopLoss == 0 ) StopLossSettings = NULL ; else StopLossSettings = OrderOpenPrice() - ( StopLoss * Multiplier ) ; if ( TakeProfit == 0 ) TakeProfitSettings = NULL ; else TakeProfitSettings = OrderOpenPrice() + ( TakeProfit * Multiplier ) ; TradeExists = CheckIfTradeAlreadyExists ( Symbol() ) ; if ( !TradeExists ) Ticket = OrderSend ( Symbol() , OP_BUY , FixedLotSize , MarketInfo ( Symbol() , MODE_ASK ) , Slippage , NULL , NULL , TradeComment , MagicNumber , 0 , CLR_NONE ) ; if ( Ticket > -1 ) OrderModify ( Ticket , OrderOpenPrice() , StopLossSettings , TakeProfitSettings , 0 , CLR_NONE ) ; if ( Ticket > -1 ) TradesSent++ ;
You need to send the order, then select Ticket, THEN do the calculations for SL and TP before modifying the order
I've tried correcting my code around but still not working ... Let me show you the latest one, and is anyone out there who is kind enough to help me figure it out ? I tried here and there but the TP and SL is not working still .... hmmmmmmmmmmmmmmmmmmph can someone help me to correct it ??
//+------------------------------------------------------------------+ //| Scalping - Buy.mq4 | //| This script belongs to the property of Icey Jr. | //| | //+------------------------------------------------------------------+ #property copyright "This script belongs to the property of Icey Jr." #property link "" //+------------------------------------------------------------------+ //| Script program's external variables | //+------------------------------------------------------------------+ //#property show_inputs extern double FixedLotSize = 0.01 ; extern int Slippage = 2 ; extern double StopLoss = 20.0 ; extern double TakeProfit = 40.0 ; extern string TradeComment = "" ; extern int MagicNumber = 131484 ; //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Script initialize program's function | //+------------------------------------------------------------------+ double Multiplier ; int init() { if ( Digits == 2 ) Multiplier = 0.01 ; if ( Digits == 3 ) Multiplier = 0.001 * 10 ; if ( Digits == 4 ) Multiplier = 0.0001 ; if ( Digits == 5 ) Multiplier = 0.00001 * 10 ; return ( 0 ) ; } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Script start program's function | //+------------------------------------------------------------------+ int start() { bool TradeExists ; int Ticket ; int TradesSent = 0 ; double StopLossSettings ; double TakeProfitSettings ; TradeExists = CheckIfTradeAlreadyExists ( Symbol() ) ; if ( !TradeExists ) Ticket = OrderSend ( Symbol() , OP_BUY , FixedLotSize , MarketInfo ( Symbol() , MODE_ASK ) , Slippage , NULL , NULL , TradeComment , MagicNumber , 0 , CLR_NONE ) ; if ( Ticket > -1 ) TradesSent++ ; if ( Ticket > -1 ) OrderModify ( Ticket , OrderOpenPrice() , StopLossSettings , TakeProfitSettings , 0 , CLR_NONE ) ; if ( OrderType() == OP_BUY ) { if ( StopLoss == 0 ) StopLossSettings = NULL ; else StopLossSettings = OrderOpenPrice() - ( StopLoss * Multiplier ) ; if ( TakeProfit == 0 ) TakeProfitSettings = NULL ; else TakeProfitSettings = OrderOpenPrice() + ( TakeProfit * Multiplier ) ; } if ( OrderType() == OP_SELL ) { if ( StopLoss == 0 ) StopLossSettings = NULL ; else StopLossSettings = OrderOpenPrice() + ( StopLoss * Multiplier ) ; if ( TakeProfit == 0 ) TakeProfitSettings = NULL ; else TakeProfitSettings = OrderOpenPrice() - ( TakeProfit * Multiplier ) ; } MessageBox ( TradesSent + " " + Symbol() + " trade had executed successfully :) " , " Information " ) ; return ( 0 ) ; } //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Script program's sub-function | //+------------------------------------------------------------------+ bool CheckIfTradeAlreadyExists ( string Emblem ) { if ( OrdersTotal() == 0 ) return ( False ) ; for ( int cc = 0 ; cc < OrdersTotal() ; cc++ ) { OrderSelect ( cc , SELECT_BY_POS ) ; if ( OrderSymbol() == Emblem && OrderMagicNumber() == MagicNumber ) return ( True ) ; } return ( False ) ; } //+------------------------------------------------------------------+
You need to check the return values from your trading functions . . . then report the error when they fail along with any relevant variables, please read this: What are Function return values ? How do I use them ?
Once again, you are trying to use a value before it has been calculated
if ( Ticket > -1 ) OrderModify ( Ticket , OrderOpenPrice() , StopLossSettings , TakeProfitSettings , 0 , CLR_NONE ) ;
StopLossSettings and TakeProfitSettings are both zero when you try to modify the trade.
But I do put a if function below there, stating the condition of stop loss 0 value ..... Shouldn't it be correct ??
RaptorUK I've read about the post you gave me, but that seems like doesn't give me a spark .... Perhaps there's some part I need to focus more ?
Hmmmph, oredersend to ordermodify .... should be correct though ...
Oh ya RaptorUK I also did this changes after reading the link you gave .... but the TP SL still not working .....
I add a bool Modify ;
and change the ordermodify to this .
if ( Ticket > -1 ) Modify = OrderModify ( Ticket , OrderOpenPrice() , StopLossSettings , TakeProfitSettings , 0 , CLR_NONE ) ;
Oh ya RaptorUK I also did this changes after reading the link you gave .... but the TP SL still not working .....
I add a bool Modify ;
and change the ordermodify to this .
I think you need to select an order first before calling OrderOpenPrice()
Hmmmph, good call, but I tried like this and it still doesn't work .....
//+------------------------------------------------------------------+ //| Script start program's function | //+------------------------------------------------------------------+ int start() { bool TradeExists ; bool Modify ; bool Select ; int Ticket ; int TradesSent = 0 ; double StopLossSettings ; double TakeProfitSettings ; TradeExists = CheckIfTradeAlreadyExists ( Symbol() ) ; if ( !TradeExists ) Ticket = OrderSend ( Symbol() , OP_BUY , FixedLotSize , MarketInfo ( Symbol() , MODE_ASK ) , Slippage , NULL , NULL , TradeComment , MagicNumber , 0 , CLR_NONE ) ; if ( Ticket > -1 ) TradesSent++ ; for ( int i = 0 ; i < OrdersTotal() ; i++ ) { if ( Ticket > -1 ) Select = OrderSelect ( i , SELECT_BY_POS , MODE_TRADES ) ; Modify = OrderModify ( Ticket , OrderOpenPrice() , StopLossSettings , TakeProfitSettings , 0 , CLR_NONE ) ; if ( OrderType() == OP_BUY ) { if ( StopLoss == 0 ) StopLossSettings = NULL ; else StopLossSettings = OrderOpenPrice() - ( StopLoss * Multiplier ) ; if ( TakeProfit == 0 ) TakeProfitSettings = NULL ; else TakeProfitSettings = OrderOpenPrice() + ( TakeProfit * Multiplier ) ; } if ( OrderType() == OP_SELL ) { if ( StopLoss == 0 ) StopLossSettings = NULL ; else StopLossSettings = OrderOpenPrice() + ( StopLoss * Multiplier ) ; if ( TakeProfit == 0 ) TakeProfitSettings = NULL ; else TakeProfitSettings = OrderOpenPrice() - ( TakeProfit * Multiplier ) ; } } MessageBox ( TradesSent + " " + Symbol() + " trade had executed successfully :) " , " Information " ) ; return ( 0 ) ; } //+------------------------------------------------------------------+

- 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'm planning to do a buy script for currency pairs which can set a SL and TP level FROM THE ENTRY PRICE which WORKS WITH 2,3,4,5 digits broker . Can someone help me to double check it, the SL and TP are not working .
The way I do it was execute a trade FIRST, then only it will modify to add in the SL and TP level FROM THE ENTRY PRICE . Not adding SL and TP at the instant the order is sent . The 2,3,4,5 digits broker however, not so sure, need an eye for that .