I have been working on an EA that (based on presses of chart object buttons);
- Opens trades at market price caluculating lot size based my risk % using a SL line that I place on the chart.
- Sets pending trades (and calculates right lot size based on risk %) using a Entry and SL line that I place on the chart.
- Various types of closing functions (all open, all pendings, all O&P, all profit, all loss etc...)
- Moves trades to BE + enough to cover comm. and swaps
- I have been working on a trailing SL function which works fine and does what it is suppsoed to do but only the first time. I press the button, it figures out where price is and moves the SL accordingly. Again this determines how much to move based on initial risk (as SL moves I use the open price and a fixed say 5:1 take profit for calculating the how much to move as these are set factors that won't move and mess up the calcs).
What I am trying to do is to set a loop of sorts where I press the button once on the chart and it moves the SL. Then if price hits a new level it moves the SL again without having to hit the trailing SL buttion again. I realize that if I change TF etc... and we ReInit that I have to re hit the button. Can this code be put into a loop that keep checking and moving the SL after the first hit of the button? I have tried some things like using "while" but it just messes things up and the SL bounces back and forth. If I put continue; after each if then it stops after the first line...
I hope someone can help. I am sure it is a stupid error on my part or something simple I am not seeing. I am still quite new to coding.
Inserted Code
if(sparam=="TSLButton") // Trail SL button has been pressed
{
ObjectSetInteger(0,"TSLButton",OBJPROP_STATE,false);
bool TSL = false;
double TickValue = MarketInfo(Symbol(),MODE_TICKVALUE);
double GGG = OrderCommission()+OrderSwap();
double GGG1= NormalizeDouble(OrderCommission()+OrderSwap(),Digits)*Point;
double XXX = GGG1/TickValue;
double XXY = (OrderOpenPrice()-OrderTakeProfit())/TPFactor;
int orders_cnt = 0;
int total = OrdersTotal();
for(int i=0; i<total; i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)continue;
if(OrderSymbol()==Symbol()) orders_cnt++;
ObjectSetInteger(0,"TSLButton",OBJPROP_COLOR,ButtonTxtCLR);
ObjectSetInteger(0,"TSLButton",OBJPROP_BGCOLOR,Red);
ObjectSetInteger(0,"TSLButton",OBJPROP_BORDER_COLOR,Red);
RefreshRates();
if(OrderType()==OP_BUY)
{
if(Bid>(OrderOpenPrice()+(XXY*2)))
{TSL = OrderModify(OrderTicket(),OrderOpenPrice(),(OrderOpenPrice()-(XXX)),OrderTakeProfit(),0,clrNONE);}
if(Bid>(OrderOpenPrice()+(XXY*3)))
{TSL = OrderModify(OrderTicket(),OrderOpenPrice(),((OrderOpenPrice()-(XXX))+(XXY)),OrderTakeProfit(),0,clrNONE);}
if(Bid>(OrderOpenPrice()+(XXY*4)))
{TSL = OrderModify(OrderTicket(),OrderOpenPrice(),((OrderOpenPrice()-(XXX))+(XXY*3)),OrderTakeProfit(),0,clrNONE);}
}
if(OrderType()==OP_SELL)
{
if(Ask<(OrderOpenPrice()-(XXY*2)))
{TSL=OrderModify(OrderTicket(),OrderOpenPrice(),(OrderOpenPrice()+(XXX)),OrderTakeProfit(),0,clrNONE);}
if(Ask<(OrderOpenPrice()-(XXY*3)))
{TSL=OrderModify(OrderTicket(),OrderOpenPrice(),((OrderOpenPrice()+(XXX))-(XXY)),OrderTakeProfit(),0,clrNONE);}
if(Ask<(OrderOpenPrice()-(XXY*4)))
{TSL=OrderModify(OrderTicket(),OrderOpenPrice(),((OrderOpenPrice()+(XXX))-(XXY*3)),OrderTakeProfit(),0,clrNONE);}
}
}
} Today's zone = Tomorrow's opportunity!