hello
I am trying to modify my orders so that when the position moves 5 pips (50 points) over the open price the SL is set to the open price. I have tried multiple ways and sometimes it works and sometimes it doesn't but it always moves the SL of Sell Orders immediately even if price hasn't moved 50 points . I have a 5 digit broker so to try to brute force things to work i set the 50 points as 0.00050
Below are 3 different codes i have tried. any help is appreciated. I have tried using the code by added the function Breakeven() to OnTick and OnTimer , not at the same time obviously.
I am trying to modify my orders so that when the position moves 5 pips (50 points) over the open price the SL is set to the open price. I have tried multiple ways and sometimes it works and sometimes it doesn't but it always moves the SL of Sell Orders immediately even if price hasn't moved 50 points . I have a 5 digit broker so to try to brute force things to work i set the 50 points as 0.00050
Below are 3 different codes i have tried. any help is appreciated. I have tried using the code by added the function Breakeven() to OnTick and OnTimer , not at the same time obviously.
Inserted Code
void Breakeven() { int total = OrdersTotal()-1; if (total >= 0) { for(int cnt=0;cnt<total;cnt++) { if(OrderSelect(cnt,SELECT_BY_POS)) { if (OrderType() == OP_BUY) { if (Bid > (OrderOpenPrice()+0.00050)) { OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Green); } } if (OrderType() == OP_SELL) { if (Ask < (OrderOpenPrice()-0.00050)) { OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Red); } } } } } }
Inserted Code
void Breakeven() { int cnt=OrdersTotal(); for(int i=cnt-1;i>=0;i--) {OrderSelect(i, SELECT_BY_POS, MODE_TRADES); if(OrderType()==OP_SELL) {if(Ask <= OrderOpenPrice()-.00050) {if (OrderStopLoss() != OrderOpenPrice()) OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), 0);}}} for(int j=cnt-1;j>=0;j--) {OrderSelect(j, SELECT_BY_POS, MODE_TRADES); if(OrderType()==OP_BUY) {if(Bid >= OrderOpenPrice()+.00050) {if (OrderStopLoss() != OrderOpenPrice()) OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(),OrderTakeProfit(), 0);}}} }
Inserted Code
void Breakeven() { int orders=0,TicOrder=0; orders= OrdersTotal(); for(int m=0;m<orders;m++) { if(OrderSelect(m,SELECT_BY_POS)==true && OrderType()==OP_BUY){ if (Bid > (OrderOpenPrice()+OrderOpenPrice()-OrderStopLoss())) { double new_stoploss= OrderOpenPrice(); double current_SL=OrderStopLoss(); if(new_stoploss>current_SL) { double SL=NormalizeDouble(new_stoploss,Digits); OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0); } } } } for(int n=0;n<orders;n++) { if(OrderSelect(n,SELECT_BY_POS)==true && OrderType()==OP_SELL){ if (Ask<(OrderOpenPrice()+OrderOpenPrice()-OrderStopLoss())) { double new_stoploss= OrderOpenPrice(); double current_SL=OrderStopLoss(); if(new_stoploss>current_SL) { double SL=NormalizeDouble(new_stoploss,Digits); OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0); } } } }}