Ask! - page 31

 
if (FirstRun==1) {

FirstRun=0;

prevtime=Time[0];

}

if ((prevtime == Time[0]) && (CurTime()-prevtime)>MaxTradeTime) {

NewBar=0;

}

else {

prevtime

I found it! Will do this in future - We learn with the help of others that have more knowledge and wisdom than we do in areas of our lack of understanding! Thank You Very Much!

Dave <<<
 

for (int cnt1=OrdersTotal()-1;cnt1>=0;cnt1--)

OrderSelect(cnt1, SELECT_BY_POS, MODE_TRADES);

if (OrderType()>=OP_SELL && OrderSymbol()==Symbol() && (OrderMagicNumber () == MagicNumber || MagicNumber==0))

{

if(OrderOpenPrice()-Ask>=TrailingStop*Point)

{

if(OrderStopLoss()>Ask+Point*15||OrderStopLoss()==0)

OrderModify(OrderTicket(),OrderOpenPrice(),Ask+(15 *Point),Bid-(20*Point),Cyan); //Modify stop loss and take profit

}

return(0);

}

if (OrderType()>=OP_BUY && OrderSymbol()==Symbol() && (OrderMagicNumber () == MagicNumber || MagicNumber==0))

{

if(Bid-OrderOpenPrice()>=TrailingStop*Point)

{

if(OrderStopLoss()<Bid-Point*15 ||OrderStopLoss()==0)

OrderModify(OrderTicket(),OrderOpenPrice(),Bid-(15*Point),Ask+(20*Point),Cyan); //Modify stop loss and take profit

}

return(0);

}

return(0);

}

}

With right format now, would one of you very generous coders help me resolve this SL and TP issue above. Back to my original request. My sleep program deactivates the EA at various times. The EA created open trades prior to the sleep mode with 40 - 50 pip Stop Loss, and Take Profit of 100. Just prior to the EA being put to sleep, I want the Stop Loss on each open ticket to change to 15 pips, and the take profit changed to 25 pips. The stoploss is not to be a trailing stop, but a hard stop below(buy) or above(sell) its present trading price (likewise the Take Profit). Lend me your expertise and make my day!!

 

For trailingstops, use the following function. I think it is written in a way that can be understood easily. Sometimes programming geeks write code, that may be efficient by a fraction of a millisecond, but makes it a darn difficult job to read and debug.

void TrailingAlls(int start,int stop)

{

int profit;

double stoptrade;

double stopcal;

if(stop==0)

return;

int trade;

for(trade=OrdersTotal()-1;trade>=0;trade--)

{

if(!OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))

continue;

if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)

continue;

if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)

{

if(OrderType()==OP_BUY)

{

profit=NormalizeDouble((Bid-OrderOpenPrice())/Point,0);

if(profit<start)

continue;

stoptrade=OrderStopLoss();

stopcal=Bid-(stop*Point);

if(stoptrade==0||(stoptrade!=0&&stopcal>stoptrade))

OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Blue);

}//Long

if(OrderType()==OP_SELL)

{

profit=NormalizeDouble((OrderOpenPrice()-Ask)/Point,0);

if(profit<start)

continue;

stoptrade=OrderStopLoss();

stopcal=Ask+(stop*Point);

if(stoptrade==0||(stoptrade!=0&&stopcal<stoptrade))

OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Red);

}//Shrt

}

}//for

}

Call it in your start using the following:

TrailingAlls(TrailStartPips,TrailStopPips);

 

Maji, in my code (which is wrong). I do not want trailing stops. I just want the open tickets to be modified so that each open buy and sell has a hard stop loss of 15 pips, and a hard take profit of 25 pips from the present currency price.

I need for it to search to see what is open that relates to the currency symbol, and then modify the stop loss and take profit on each open trade. That is all I need.

Last night we got into a tangent and got into trailing stops - Misunderstanding between me and Phoenix.

Please help me to accomplish this not too difficult function - For you that is!

Dave <<<
 

Dave,

I think the ingredients to make that change are there in the routine I gave you.

Otherwise, you will have to post your code or email to me and I can see what I can do. Sometimes, it is very difficult to read others code, so it maybe a time consuming task. In that case, I will throw my hands up.

Good luck.

 

Post #309 is my code as it stands, in PHP.

Just need to change open trades to hard stop loss of 15 and hard take profit of 25. Should not be too much for a pro like you to modify this coding.

Dave <<
 

Dave,

Like I said, it is very time consuming to make those kind of edits. I am playing around in my lunch hour and here is a try. If it does not work, you are on your own. It is not tested, so...

Call this in your main program just before you instruct the program to sleep.

Good luck.

void ChangeStop(int takeprofit,int stop)

{

double profit;

double stopcal;

if(stop==0)

return;

int trade;

for(trade=OrdersTotal()-1;trade>=0;trade--)

{

if(!OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))

continue;

if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)

continue;

if(OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicNumber)

{

if(OrderType()==OP_BUY)

{

profit=Ask+(takeprofit*point);

stopcal=Bid-(stop*Point);

OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,profit,0,Blue);

}//Long

if(OrderType()==OP_SELL)

{

profit=Bid-(takeprofit*point);

stopcal=Ask+(stop*Point);

OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,profit,0,Red);

}//Shrt

}

}//for

}

 

Where does the modification take place in this?? The original stop loss was 40, and the take profit was 100. As a part of the sleep mode, the stop loss is to change to 15 on any existing open orders and the take profit is to change to 25 on any existing open orders.

Open Order Tickets:Prior to sleep mode.

Before Sleep Mode Stop Loss - 40 Before Sleep Mode Take Profit-100 After Sleep Mode Stop Loss Modification -15 After Sleep Mode Take Profit Modification - 25

I am not initializing order settings, I am modify existing orders already placed and still active.

Thanks for you help anyhow! Why does something so simple seem so difficult?

Dave <<
 

Dave,

It is not difficult. You will have to study MQ a little more. Check the code I provided. The parameters, stop and takeprofit should be the numbers you want to use.

It is all there, you will have to figure out how to use it

 

I throw in the towel. I get errors. I will use what Phoenix has provided because it works to a point I will live with. If this language would have a school to learn from in the United States, it sure would make my life easier. Unfortunately, there is none.

Thanks for the input, both Maji and Phoenix!

Dave <<<
Reason: