Hello everyone,
i have one simple script which makes buy and sell, i wanna change to direction of order while last order completed with stop loss. how can i do this, what should i add as a code thank you for helping
string direction="buy"; input int TakeProfitPoints = 50 ; input int StoplossPoints = 50 ; void OnTick() { if((OrdersTotal()==0) && (direction == "buy")) { // int buyticket = OrderSend( Symbol(),OP_BUY, 0.1, Ask, 3,Ask- StoplossPoints*_Point,Ask+TakeProfitPoints*_Point,NULL); int buyticket = OrderSend (Symbol(),OP_SELL,0.1,Bid,3,Bid + 100*_Point,Bid - 100*_Point,NULL,0,0,Red); if(OrderSelect(buyticket, SELECT_BY_TICKET)==true) if (( OrderProfit())<0) { direction = "sell" ; } else direction = "buy" ; } if((OrdersTotal()==0) && (direction == "sell")) { // int sellticket = OrderSend (Symbol(),OP_SELL,0.1,Bid,3,Bid + 100*_Point,Bid - 100*_Point,NULL,0,0,Red); int sellticke = OrderSend( Symbol(),OP_BUY, 0.1, Ask, 3,Ask- StoplossPoints*_Point,Ask+TakeProfitPoints*_Point,NULL); if(OrderSelect(sellticket, SELECT_BY_TICKET)==true) if (( OrderProfit())>0) { direction = "sell" ; } else direction = "buy" ; } }
Really thank you for your effort
i also try to find the issue, but it is not change the direction as per the condition, there is a line like a just one sell one buy one sell
do you have any idea to fix
thank you so much
i have one simple script which makes buy and sell, i wanna change to direction of order while last order completed with stop loss. how can i do this, what should i add as a code thank you for helping
When you want help, you should make it as easy as possible for other people to help you.
To be honest with you, usually when I see code as messy as this
void OnTick() { if((OrdersTotal()==0) && (direction == "buy")) { int buyticket = OrderSend( Symbol(),OP_BUY, 0.1, Ask, 3,Ask- StoplossPoints*_Point,Ask+TakeProfitPoints*_Point,NULL); if(OrderSelect(buyticket, SELECT_BY_TICKET)==true) if (( OrderProfit())<0) { direction = "sell" ; } else direction = "buy" ; } if((OrdersTotal()==0) && (direction == "sell")) { int sellticket = OrderSend (Symbol(),OP_SELL,0.1,Bid,3,Bid + 100*_Point,Bid - 100*_Point,NULL,0,0,Red); if(OrderSelect(sellticket, SELECT_BY_TICKET)==true) if (( OrderProfit())>0) { direction = "sell" ; } else direction = "buy" ; } }
I will not even bother to read it, especially if the code is relatively long.
I am sure that I am not the only one.
It is very simple to format the code so that it is easier for others to read. It will also make it easier for you to understand the code when you come back to it at a later date.
I use the styler (Ctrl+comma) to format the code. I use the VTK styler version with 2 space indent lines.
You can also set the styler to delete empty lines. I don't use this option myself as empty lines can be useful, but unnecessary empty lines are annoying and can make the code more difficult to follow. (In my opinion).
This is your code with the styler applied and unnecessary empty lines removed.
void OnTick() { if((OrdersTotal()==0) && (direction == "buy")) { int buyticket = OrderSend( Symbol(),OP_BUY, 0.1, Ask, 3,Ask- StoplossPoints*_Point,Ask+TakeProfitPoints*_Point,NULL); if(OrderSelect(buyticket, SELECT_BY_TICKET)==true) if (( OrderProfit())<0) { direction = "sell" ; } else direction = "buy" ; } if((OrdersTotal()==0) && (direction == "sell")) { int sellticket = OrderSend (Symbol(),OP_SELL,0.1,Bid,3,Bid + 100*_Point,Bid - 100*_Point,NULL,0,0,Red); if(OrderSelect(sellticket, SELECT_BY_TICKET)==true) if (( OrderProfit())>0) { direction = "sell" ; } else direction = "buy" ; } | //+-- }
Now it is so much easier to follow your code and I can immediately see a problem that was not obvious from the original messy code.
So let's look at the first section as it is the same for both.
void OnTick() { if((OrdersTotal()==0) && (direction == "buy")) //This block of code will only be executed if there are no open trades { int buyticket = OrderSend( Symbol(),OP_BUY, 0.1, Ask, 3,Ask- StoplossPoints*_Point,Ask+TakeProfitPoints*_Point,NULL); if(OrderSelect(buyticket, SELECT_BY_TICKET)==true) //This block of code will only be executed once, immediately after the trade is opened if (( OrderProfit())<0) //As the trade has only just been opened, trade will be in loss because of the spread { direction = "sell" ; //direction will be changed to "sell" because the trade is showing a loss } else direction = "buy" ; //This line is not necessary as direction must = "buy" as it is in the condition (first if) } //+-- }
So you will need to re-think your code.
Clues.
Make buyticket and sellticket variables either static or globalscope.
Check whether the trade has closed and then check whether it closed with profit. Change the direction variable if necessary.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello everyone,
i have one simple script which makes buy and sell, i wanna change to direction of order while last order completed with stop loss. how can i do this, what should i add as a code thank you for helping