You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
I really appreciate the help that you and everyone else is providing. I have tried everything over the last couple days to get that compiled and I apologize that I didn't ask. Most of you have suggested that I defer from using my own code and use someone else's code, but I'm not going to learn if I do not understand what I am doing wrong. To me, my code looks sound. I am asking it to count the orders by position and select only those that meet my criteria and then modify that specific trade. Thus far, after 4 pages nobody has pointed out which line of my code is broken. Unless I first see and understand what I am doing wrong, it will not help me to learn by using someone else's code. My goal now is not to have a Trailingstop EA that works, because there are plenty out there, but for me to learn how to code, so that I can move on. In fact, my code was almost identical to the code that Jimdandy posted in his Youtube Tutorial on Trailingstops. Since it is virtually identical to his, I copied the code almost verbatim (except that I want to trade manually), but his code only works sometimes as well.
I have printed off some stuff. This trade is almost 300 points beyond the point where I specified that the Trailingstop kicks in(which was 150 points):
2016.04.22 00:18:02.301 Trailing_v33 EURAUD,M15: OrderModify = false
2016.04.22 00:18:02.301 Trailing_v33 EURAUD,M15: Ask = 1.45926
2016.04.22 00:18:02.301 Trailing_v33 EURAUD,M15: Selectbypos = 0
2016.04.22 00:18:02.301 Trailing_v33 EURAUD,M15: Selectbyticket = 1
2016.04.22 00:18:02.301 Trailing_v33 EURAUD,M15: OrderTicket = 50381828
2016.04.22 00:18:02.301 Trailing_v33 EURAUD,M15: ticket = 0
2016.04.22 00:18:02.301 Trailing_v33 EURAUD,M15: OrderSelect = true
2016.04.22 00:18:02.301 Trailing_v33 EURAUD,M15: OrdersTotal = 16
2016.04.22 00:18:02.301 Trailing_v33 EURAUD,M15: OrderSymbol = EURUSD
2016.04.22 00:18:02.301 Trailing_v33 EURAUD,M15: OrderTicket = 50381828
2016.04.22 00:17:58.934 Trailing_v33 EURAUD,M15: OrderModify = false
This EA is only placed on the EURAUD. As you can see there are 16 Orders open on 9 other charts. Ticket number Selected for this pair is completely wrong and OrderSymbol is complete wrong. Both are for an open EURUSD order. Based on the above, my for loop selected Position 0 in this case, whereas the EURAUD it was supposed to select was in position 14 (15-1). Why? I told it to ignore this.
In the above mentioned video Jim actually get into this problem, and said he uses the following code to overcome my problem, however, this is a counter for properly sending orders and not a function to filter orders. I tried to use it as a filter, but did not succeed.
My code now looks like this. Will someone please critique my code so that I can learn from this and tell me why it doesn't work on multiple charts. I know it cannot be that bad because it actually works perfectly sometimes. Thank you
Thus far, after 4 pages nobody has pointed out which line of my code is broken.
Not true! We have described your errors and offered solutions for them in many flavours and alternatives. Yet you ignore them and continue to do it YOUR WAY. Not only that, you have now even made it worse by adding more mistakes.
When a painter learns to paint, he does so by first coping the techniques of others that have mastered it. Only once he is comfortable with these techniques does he start to create his OWN WAY of painting.
Here is a short list of "my critique":
There are more things that I could list, but fix these at least, once and for all!
Coding is just like any other language. Yes, you can just string words together, but that does not make you a poet. In coding you also have to have rhythm and structure, in order to make it readable and functional.
Here is a short list of "my critique":
Ok thank you very much for this. I actually had brackets for all my 'if' statements initially as per the code I posted here earlier on, but since that code didn't work I tried various other things, which included copying the code that I pasted in my previous post. As I mentioned this is actually not even my code, but it also doesn't work on all pairs. I will however replace the brackets again and follow the other advice. In the meantime I learned that OrderSelect will print the Symbol of the first order in the terminal and not the one that the EA is on, so that was actually not wrong.
EDIT: Thank you for suggestion #3!!!!!!! I think this was the problem, because the Order must first be selected by Symbol and THEN the value must be stored. So I moved the variable to below those functions as below, and it seems to work now (but more testing is required before I will know with certainty
Ok thank you very much for this. I actually had brackets for all my 'if' statements initially as per the code I posted here earlier on, but since that code didn't work I tried various other things, which included copying the code that I pasted in my previous post. As I mentioned this is actually not even my code, but it also doesn't work on all pairs. I will however replace the brackets again and follow the other advice. In the meantime I learned that OrderSelect will print the Symbol of the first order in the terminal and not the one that the EA is on, so that was actually not wrong.
EDIT: Thank you for suggestion #3!!!!!!! I think this was the problem, because the Order must first be selected by Symbol and THEN the value must be stored. So I moved the variable to below those functions as below, and it seems to work now (but more testing is required before I will know with certainty
for(int i=OrdersTotal()-1; i>=0; i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
if(OrderSymbol()!= Symbol()) continue;
double Pip=Point*10, TSTP=TrailingStart*Pip, Trail=TrailingStop*Pip, SL=StopLoss*Pip;
if(OrderType()==OP_BUY){
if(Bid-OrderOpenPrice()>TSTP){
if(OrderStopLoss()<Bid-Trail){
if(!OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Trail, OrderTakeProfit(), Orange))
Print("Error Buy TrailingStop: ",GetLastError());
}
}
for(int i=OrdersTotal()-1; i>=0; i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) if(OrderSymbol()!= Symbol()) continue;
// Everything below is executed if NO order is selected or a selected order is symbol
double Pip=Point*10, TSTP=TrailingStart*Pip, Trail=TrailingStop*Pip,SL=StopLoss*Pip;
if(OrderType()==OP_BUY)
&& Bid-OrderOpenPrice()>TSTP)
&& OrderStopLoss()<Bid-Trail){
if(!OrderModify(OrderTicket(), OrderOpenPrice(), Bid-Trail, OrderTakeProfit(), Orange))
Print("Error Buy TrailingStop: ",GetLastError());
}
:
Now perhaps you understand my "hard criticism" when they waste everyone's time. While I understand "simple things" they think it is acceptable to waste everyone's time, I don't.
Yes! Unfortunately, it seems you are correct!
Thank you for the replies.So, I changed the code back to an earlier version as per below. With it like this it seems to be working perfectly on all pairs where the stoplevel is 50 points, but not on pairs where the stoplevel is higher eg, EURAUD even if the external variables is higher than the stoplevel. So this is bizarre
I also found another EA that has very similar code as mine and compared the code. I highlighted the main differences.
1. Increments instead of decrements
2. Multiply everything with Point instead of Point*10
3. He adds the Trailingstop and Trailingstep together and then subtracts one and modify the order with the Trailingstep. If I understand this correctly, the Trailingstart will get activated after a move of 15 pips (150 points). In my EA, the price will trail behind by 5 pips and get get stopped out if the price falls again and hit this 5 pip level. In his EA, it will also get activated after 15 pips, but the amount of pips it trails behind is 19 (15+5-1), so the price have to fall by 19 pips to get stopped out. As an example: OrderOpenPrice is 1.50000. Price goes up by 150 points to 1.50150 which activates the Trailingstop. If the price then falls back down to 1.50100, it will be stopped out for a 5 pip profit. In his EA, the Trailingstop is also activated at 1.50150, but the trail is at 1.49960 which I think will cause error 130 since the stop is to close to the stoplevel. Or am I misunderstanding this?
Could my problem be with one or more of these?
I also found another EA that has very similar code as mine and compared the code.
Lets see if I get this logic of yours! You are willing to find another EA from someone else from who knows where and are willing to incorporate that code into yours, BUT YOU ARE NOT WILLING to take code that was specifically written to help you out and correct you issues!!!
WOW! How LOGICAL of you! Where is SPOCK when you need him!
SPOCK, may you Rest in Peace! We all miss you!
Lets see if I get this logic of yours! You are willing to find another EA from someone else from who knows where and are willing to incorporate that code into yours, BUT YOU ARE NOT WILLING to take code that was specifically written to help you out and correct you issues!!!
WOW! How LOGICAL of you! Where is SPOCK when you need him!
SPOCK, may you Rest in Peace! We all miss you!