SL and TP system - page 3 12345 New comment Keith Watford 2021.03.08 00:10 #21 OrderClose(OrderTicket(), LotsToTrade, Bid, 0, clrAliceBlue); Where do you select the order? Tiberious 2021.03.08 00:20 #22 Keith Watford: Where do you select the order? So I would have to select the LongSetup as the order?, Because I dont select an order for this lippmaje 2021.03.08 03:07 #23 if (LongSetup == True && FastEMA < SlowEMA){ OrderClose(OrderTicket(), LotsToTrade, Bid, 0, clrAliceBlue); LongSetup = False; } Your order close logic is in the wrong place, check your if statements and balance them. And as mentioned before you need to have an order selected before you could close it. Tiberious 2021.03.08 03:18 #24 lippmaje: Your order close logic is in the wrong place, check your if statements and balance them. And as mentioned before you need to have an order selected before you could close it. Im not sure what you mean by saying balance my if statements Tiberious 2021.03.08 03:46 #25 I tried this not sure if it will work, or if this is what you meant but Im getting an error with the ending brackets //+------------------------------------------------------------------+ //| EMA Cross 2.mq4 | //| Tiberious | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Tiberious" #property link "https://www.mql5.com" #property version "1.00" #property strict double LotsToTrade = 0.1; //Lot Size int Magic = 500; int MaxTrades = 1; bool LongSetup = False; bool ShortSetup = False; int OnInit() { return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { double SlowEMA = iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_CLOSE, 0); double LastSlowEMA = iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_CLOSE, 1); double FastEMA = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_CLOSE, 0); double LastFastEMA = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_CLOSE, 1); if (GetTotalOpenTrades() < MaxTrades) { if ((LastFastEMA < LastSlowEMA) &&(FastEMA > SlowEMA)) { LongSetup = True; } if (LongSetup == True){ Comment ("Buy"); int OrderResultLong = OrderSend(Symbol(), OP_BUY, LotsToTrade, Ask, 0, 0, 0, "Buy Order", Magic, 0, clrGreen); } if (LongSetup == True && FastEMA < SlowEMA){ CloseAllTrades(); //OrderClose(OrderTicket(), LotsToTrade, Bid, 0, clrAliceBlue); //LongSetup = False; } } else if (GetTotalOpenTrades() < MaxTrades) { if ((LastFastEMA > LastSlowEMA) && (FastEMA < SlowEMA)) { Comment ("Sell"); int OrderResultShort = OrderSend(Symbol(), OP_SELL, LotsToTrade, Bid, 10, 0, 0, "Buy Order", Magic, 0, clrRed); } } } // Return total number of open trades int GetTotalOpenTrades() { int TotalTrades = 0; //Loop through open orders and add them to TotalTrades for (int t=OrdersTotal()-1; t>=0; t--) { if(OrderSelect(t, SELECT_BY_POS, MODE_TRADES)) { if(OrderSymbol() != Symbol()) continue; if(OrderMagicNumber() != Magic) continue; if(OrderCloseTime() !=0) continue; TotalTrades = (TotalTrades + 1); } } return TotalTrades; } double CloseAllTrades() { for(int i=OrdersTotal(); i>=0;i--) { if (OrderSelect(i,SELECT_BY_POS)== True) if (OrderSymbol() == Symbol()) { OrderClose( OrderTicket(), LotsToTrade, MarketInfo(OrderSymbol(), MODE_BID), 0, clrAliceBlue); } } } lippmaje 2021.03.08 03:51 #26 You may want to take some basic lessions in computer programming before going on, or have someone develop this EA for you. Tiberious 2021.03.08 04:02 #27 lippmaje: You may want to take some basic lessions in computer programming before going on, or have someone develop this EA for you. Id rather make it on my own Tiberious 2021.03.08 04:08 #28 Tiberious: Id rather make it on my own Im pretty close to finishing it aswell lippmaje 2021.03.08 04:46 #29 void CloseAllTrades() { for(int i=OrdersTotal()-1; i>=0;i--) { if (OrderSelect(i,SELECT_BY_POS) ) Tiberious 2021.03.08 05:08 #30 lippmaje: My bad for all the questions. Thanks for taking the time to help out. I switched out the code for yours but trades are still not closing. Any idea why? //+------------------------------------------------------------------+ //| EMA Cross 2.mq4 | //| Tiberious | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Tiberious" #property link "https://www.mql5.com" #property version "1.00" #property strict double LotsToTrade = 0.1; //Lot Size int Magic = 500; int MaxTrades = 1; bool LongSetup = False; bool ShortSetup = False; int OnInit() { return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { double SlowEMA = iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_CLOSE, 0); double LastSlowEMA = iMA(NULL, 0, 50, 0, MODE_SMA, PRICE_CLOSE, 1); double FastEMA = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_CLOSE, 0); double LastFastEMA = iMA(NULL, 0, 1, 0, MODE_SMA, PRICE_CLOSE, 1); if (GetTotalOpenTrades() < MaxTrades) { if ((LastFastEMA < LastSlowEMA) &&(FastEMA > SlowEMA)) { LongSetup = True; } if (LongSetup == True){ Comment ("Buy"); int OrderResultLong = OrderSend(Symbol(), OP_BUY, LotsToTrade, Ask, 0, 0, 0, "Buy Order", Magic, 0, clrGreen); } if (LongSetup == True && FastEMA < SlowEMA){ CloseAllTrades(); //OrderClose(OrderTicket(), LotsToTrade, Bid, 0, clrAliceBlue); //LongSetup = False; } } else if (GetTotalOpenTrades() < MaxTrades) { if ((LastFastEMA > LastSlowEMA) && (FastEMA < SlowEMA)) { Comment ("Sell"); int OrderResultShort = OrderSend(Symbol(), OP_SELL, LotsToTrade, Bid, 10, 0, 0, "Buy Order", Magic, 0, clrRed); } } } // Return total number of open trades int GetTotalOpenTrades() { int TotalTrades = 0; //Loop through open orders and add them to TotalTrades for (int t=OrdersTotal()-1; t>=0; t--) { if(OrderSelect(t, SELECT_BY_POS, MODE_TRADES)) { if(OrderSymbol() != Symbol()) continue; if(OrderMagicNumber() != Magic) continue; if(OrderCloseTime() !=0) continue; TotalTrades = (TotalTrades + 1); } } return TotalTrades; } void CloseAllTrades() { for (int i=OrdersTotal()-1; i>=0; i--) { if (OrderSelect(i, SELECT_BY_POS)) if (OrderSymbol() == Symbol()) { OrderClose( OrderTicket(), LotsToTrade, MarketInfo(OrderSymbol(), MODE_BID), 0, clrAliceBlue); } } } 12345 New comment
Where do you select the order?
Where do you select the order?
So I would have to select the LongSetup as the order?, Because I dont select an order for this
Your order close logic is in the wrong place, check your if statements and balance them.
And as mentioned before you need to have an order selected before you could close it.
Your order close logic is in the wrong place, check your if statements and balance them.
And as mentioned before you need to have an order selected before you could close it.
Im not sure what you mean by saying balance my if statements
You may want to take some basic lessions in computer programming before going on, or have someone develop this EA for you.
Id rather make it on my own
Id rather make it on my own
Im pretty close to finishing it aswell
My bad for all the questions. Thanks for taking the time to help out. I switched out the code for yours but trades are still not closing. Any idea why?