int posMagic = (int)PositionGetInteger(POSITION_MAGIC);
int posMagic = (int)PositionGetInteger(POSITION_MAGIC);
You need to select a position before using this command - it should be in the loop.
Quick question if I have the same EA applied to multiple different charts, each short and long trade of each chart having it's own unique magic number, would applying the magic number in the loop still close each trade according to it's own unique magic number?
Quick question if I have the same EA applied to multiple different charts, each short and long trade of each chart having it's own unique magic number, would applying the magic number in the loop still close each trade according to it's own unique magic number?
As long as each EA has a unique number for its long/short trades, it should work ok.
But I have a question - why do you need a magic number for long/short when you could read it from the order type? Then you could have a single magic number for each EA.
As long as each EA has a unique number for its long/short trades, it should work ok.
But I have a question - why do you need a magic number for long/short when you could read it from the order type? Then you could have a single magic number for each EA.
Hi! I have seen that only one magic number is needed per EA and thus I have fixed it. Thank you so much for your kindness and help. It really is appreciated. I attached the improved version to this comment.
//| ProjectName | //| Copyright 2020, CompanyName | //| http://www.companyname.net | //+------------------------------------------------------------------+ #property copyright "Copyright 2024, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" #include <trade/trade.mqh> #include <Trade/SymbolInfo.mqh> int GetTotalResultOne; int GetTotalResultTwo; int GetTotalResultThree; int GetTotalResultFour; int GetTotalResultFive; int GetTotalResultSix; int GetTotalResultSeven; int GetTotalResultEight; int GetTotalResultNine; int GetTotalResultTen; int GetTotalResultEleven; int GetTotalResultTwelve; int GetTotalResultThirteen; int GetTotalResultFourteen; int GetTotalResultFifteen; int GetTotalResultSixteen; int GetTotalResultSeventeen; int GetTotalResultEighteen; int GetTotalResultNineteen; int GetTotalResultTwenty; int GetTotalResultTwentyOne; int GetTotalResultTwentyTwo; int MACD_handle; int Stochastic_handle; int ADX_handle; int IchimokuCloud_handle; int WPR; int AO; int Bull; int Bear; int RSI; int CCI; int Momentum; int EMAOneDay10; int EMAOneDay20; int EMAOneDay30; int EMAOneDay50; int EMAOneDay100; int EMAOneDay200; int SMAOneDay10; int SMAOneDay20; int SMAOneDay30; int SMAOneDay50; int SMAOneDay100; int SMAOneDay200; bool RSIisRising; bool RSIisFalling; bool CCIisRising; bool CCIisFalling; bool WilliamsRPercentageisRising; bool WilliamsRPercentageisFalling; bool BullPowerisFalling; bool BearPowerisRising; bool ADXIsRising; double OneDayCalculationMA; double OneDayCalculationOS; int input MagicNumber = 1; CTrade trade;
if(bNewBarEventOneMinute) { // stop loss MqlDateTime structTime; TimeLocal(structTime); structTime.hour = 23; Print("Hour: ",structTime.hour); structTime.min = 55; Print("Minute: ",structTime.min); structTime.sec = 00; Print("Second: ",structTime.sec); datetime timeClose = StructToTime(structTime); Print("time Close: ", timeClose); double AccountEquity = AccountInfoDouble(ACCOUNT_EQUITY); double OpenPrice = iOpen(_Symbol,PERIOD_D1,0); Print("Open Price: ", OpenPrice); double ClosePrice = iClose(_Symbol,PERIOD_D1,1); double HighPrice = iHigh(_Symbol,PERIOD_D1,1); double LowPrice = iLow(_Symbol,PERIOD_D1,1); double PivotPointValueCalculationOneDay = ClosePrice + HighPrice + LowPrice; double PivotPointValue = NormalizeDouble(PivotPointValueCalculationOneDay/3,3); Print("Pivot Point: ", PivotPointValue); double BuyPrice = SymbolInfoDouble(_Symbol,SYMBOL_ASK); Print("Buy Price: ", BuyPrice); double SellPrice = SymbolInfoDouble(_Symbol,SYMBOL_BID); Print("Sell Price: ", SellPrice); //CTrade trade; for(int i = PositionsTotal()-1; i>=0; i--) { ulong posTicket = PositionGetTicket(i); int posMagic = (int)PositionGetInteger(POSITION_MAGIC); Print("Magic number: ", posMagic); if(posMagic == MagicNumber) { if(OpenPrice > PivotPointValue && SellPrice > OpenPrice) { if(PositionSelectByTicket(posTicket)) { if(trade.PositionClose(posTicket)) { Print(__FUNCTION__," > Position number: ",posTicket,"with magic number(",posMagic, ")"," was closed! (Head)"); } } } else if(OpenPrice < PivotPointValue && BuyPrice < OpenPrice) { if(PositionSelectByTicket(posTicket)) { if(trade.PositionClose(posTicket)) { Print(__FUNCTION__," > Position number: ",posTicket,"with magic number(",posMagic, ")"," was closed! (Shoulders)"); } } } else if(TimeLocal() > timeClose) { if(PositionSelectByTicket(posTicket)) { if(trade.PositionClose(posTicket)) { Print(__FUNCTION__," > Position number: ",posTicket,"with magic number(",posMagic, ")"," was closed! (knees and toes)"); } } } } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ Print("Pivot Point Value: ", PivotPointValue); Print("Open Price: ", OpenPrice); Print("Daily MA value: ", OneDayCalculationMA); Print("Daily OS value: ", OneDayCalculationOS); // This is to ensure it only submits between Monday and Friday if(structTime.day_of_week > 0 && structTime.day_of_week < 6) { if(TimeLocal() < timeClose) { if(OneDayCalculationMA > 0.9 && OneDayCalculationOS > 0.15 && PivotPointValue < OpenPrice && BuyPrice < PivotPointValue && AccountEquity > 1) // 15/22 or higher Daily time frame { static datetime prevTime=0; datetime lastTime[1]; if(CopyTime(_Symbol,PERIOD_D1,0,1,lastTime)==1 && prevTime!=lastTime[0]) { prevTime=lastTime[0]; trade.SetExpertMagicNumber(MagicNumber); double EnterLong = SymbolInfoDouble(_Symbol,SYMBOL_ASK); EnterLong = NormalizeDouble(EnterLong,_Digits); trade.Buy(0.01,_Symbol,EnterLong,NULL,NULL,"Enter Long"); } } // Check conditions to enter a short position if(OneDayCalculationMA < -0.9 && OneDayCalculationOS < -0.15 && PivotPointValue > OpenPrice && SellPrice > PivotPointValue && AccountEquity > 1) // 13/22 or higher Daily Time Frame { static datetime prevTime=0; datetime lastTime[1]; if(CopyTime(_Symbol,PERIOD_D1,0,1,lastTime)==1 && prevTime!=lastTime[0]) { prevTime=lastTime[0]; trade.SetExpertMagicNumber(MagicNumber); double EnterShort = SymbolInfoDouble(_Symbol,SYMBOL_BID); EnterShort = NormalizeDouble(EnterShort,_Digits); trade.Sell(0.01,_Symbol,EnterShort,NULL,NULL,"Enter Short"); } } } } } }

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi! I have tried to have my EA close my position according to the magic number (1 is for long trades and 2 is for short trades), whenever I try to print the value of the current magic number the value is void and thus my trades can only exit based on time. Does someone know where I might have gone wrong?
This is defined in the global space.
The next bar is in the OnTick() part of the script.