Here's the function i'm working on to automatically close all trades if price hit the following prices that are input by user in the settings box.
What's wrong with it, and why isn't it working?
//=== Global variables extern bool ActivateAutoCloseBreakOutOfRange = True; extern double HowManyPipsToAutoClose = 10; double pips; extern double MustBeLower = 1.10; //Price must be lower than this value to sell extern double MustBeHigher = 1.06; //Price must be higher than this value to sell extern double StartingDeposit = 1000; extern double MoneyAboveBalance=10; //how much profit above the StartingDeposit before EA can close trades //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int OnInit() { double ticksize = MarketInfo(Symbol(), MODE_TICKSIZE); if (ticksize == 0.00001 || ticksize == 0.001) pips = ticksize*10; else pips =ticksize; //--- return(INIT_SUCCEEDED); } //======I want the function below to automatically close all trades, if price move outside of price range //====== also, before it close all trades, EA checks to see if current equity is higher than balance input in global setting. if(ActivateAutoCloseBreakOutOfRange == True) { if( (Bid > (MustBeHigher + (HowManyPipsToAutoClose*pips))) && (AccountEquity() > (StartingDeposit + MoneyAboveBalance) ) ) CloseAllOrders(); if( (Bid < (MustBeLower - (HowManyPipsToAutoClose*pips))) && (AccountEquity() > (StartingDeposit + MoneyAboveBalance) ) ) CloseAllOrders(); }; //+------------------------------------------------------------------+ void CloseAllOrders() { for (int trade = OrdersTotal() - 1; trade >= 0; trade--) { if(!OrderSelect(trade, SELECT_BY_POS, MODE_TRADES))continue; if (OrderSymbol() == Symbol()) { if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) { if ( (OrderType() == OP_BUY) || (OrderType()== OP_SELL) ) if(!OrderClose(OrderTicket(), OrderLots(), Bid, slip, Blue)) GetLastError(); } Sleep(1000); } } }
//=== Global variables extern bool ActivateAutoCloseBreakOutOfRange = True; extern double HowManyPipsToAutoClose = 10; double pips; extern double MustBeLower = 1.10; //Price must be lower than this value to sell extern double MustBeHigher = 1.06; //Price must be higher than this value to sell extern int MagicNumber=1378; extern int slip = 3; extern double StartingDeposit = 1000; extern double MoneyAboveBalance=10; //how much profit above the StartingDeposit before EA can close trades //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int OnInit() { double ticksize = MarketInfo(Symbol(), MODE_TICKSIZE); if (ticksize == 0.00001 || ticksize == 0.001) pips = ticksize*10; else pips =ticksize; //--- return(INIT_SUCCEEDED); } int start(){ //======I want the function below to automatically close all trades, if price move outside of price range //====== also, before it close all trades, EA checks to see if current equity is higher than balance input in global setting. if(ActivateAutoCloseBreakOutOfRange == True) { if( (Bid > (MustBeHigher + (HowManyPipsToAutoClose*pips))) && (AccountEquity() > (StartingDeposit + MoneyAboveBalance) ) ) CloseAllOrders(); if( (Bid < (MustBeLower - (HowManyPipsToAutoClose*pips))) && (AccountEquity() > (StartingDeposit + MoneyAboveBalance) ) ) CloseAllOrders(); }; return(0); } //+------------------------------------------------------------------+ void CloseAllOrders() { for (int trade = OrdersTotal() - 1; trade >= 0; trade--) { if(!OrderSelect(trade, SELECT_BY_POS, MODE_TRADES))continue; if (OrderSymbol() == Symbol()) { if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) { if ( (OrderType() == OP_BUY) || (OrderType()== OP_SELL) ) if(!OrderClose(OrderTicket(), OrderLots(), Bid, slip, Blue)) GetLastError(); } Sleep(1000); } } }
i think it is correct now you lost your start func.... and i repair it
Hello,
If you add a "property strict, you can correct some error :
extern int BarsToLookBack = 100; // This variable is for iHighest and iLowest values. // The sliipage is an int : extern int slip = 3; string CurrentBalance = DoubleToStr(AccountBalance(), 0); // (?) to be checked string CurrentEquity = DoubleToStr(AccountEquity(), 0);
And some other little error.
for the close all :
void CloseAllOrders() { for (int trade = OrdersTotal() - 1; trade >= 0; trade--) { if(!OrderSelect(trade, SELECT_BY_POS, MODE_TRADES))continue; if (OrderSymbol() == Symbol()) { if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber) { if ( (OrderType() == OP_BUY) || (OrderType()== OP_SELL) ) if(!OrderClose(OrderTicket(), OrderLots(),OrderClosePrice(), slip, Blue)) GetLastError(); } Sleep(1000); } } }
Your entry's condition are too restrictive, they are never fullfilled :
cci do not show up in strategy tester, because this condition is never fullfilled :
Bid < iMA(NULL,0,1,0,0,PRICE_MEDIAN,0)
try this
if( (MaxBuyTrades < MaxOrders) && Bid < iLowest(NULL,0,MODE_OPEN,BarsToLookBack,0) && iMACD(NULL,0,1,2,2,0,0,0) < 0 && iDeMarker(NULL,0,14,0) < 0.3 && Bid < iMA(NULL,0,1,0,0,PRICE_MEDIAN,0) && iCCI(NULL,0,4,0,0) <= -100 ) //buy rule
and then this :
if( (MaxBuyTrades < MaxOrders) && Bid < iLowest(NULL,0,MODE_OPEN,BarsToLookBack,0) && iMACD(NULL,0,1,2,2,0,0,0) < 0 && iDeMarker(NULL,0,14,0) < 0.3 // && Bid < iMA(NULL,0,1,0,0,PRICE_MEDIAN,0) && iCCI(NULL,0,4,0,0) <= -100 ) //buy rule
Bid < iMA(NULL,0,1,0,0,0,0)
I used that line of code above because I wanted the EA to limit the buy orders.
That condition will full fill, but not all the time. It happens rarely :)
Your expert has a counter trend strategy, it use filter to pin point the higher and the lower, it is easy to create an indicator with the five fllters, to see and adjust on the screen the buy and sell order zone.
I used that line of code above because I wanted the EA to limit the buy orders.
That condition will full fill, but not all the time. It happens rarely :)
a person I know gave me this indi to calculate range. maybe it could be added to the EA too and make it calculate the range as it goes? i ve put the EA on demo for a week and so far only profits....
do you think if in the near future i want to go live a 500 euro account is enough? or it risks to get blown easily?
a person I know gave me this indi to calculate range. maybe it could be added to the EA too and make it calculate the range as it goes? i ve put the EA on demo for a week and so far only profits....
do you think if in the near future i want to go live a 500 euro account is enough? or it risks to get blown easily?
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
The Forbidden Fruit EA
This EA is very profitable when the market is ranging. It can achieve more than 95% to 100% win rate (long & short). This is a semi automatic EA. The user must manually enter in two prices inside a range (highest price range, lowest price range) for EA to trade.
I just created an EA with a profit factor of 1387. It works in the 1 minute time frame.
Here's the MQL4 code to the EA, I've also included the EURUSD set file as well.
Settings:
The EA will automatically divide price inside the range by two.
Enjoy!
PS. I haven't created a function to prevent the EA from placing trades during the London session. If anyone know how to read my MQL4 codes, they can add that part in the EA for me. I'm trying to add a code to make the EA automatically close all trades if price move outside that PriceMustBeLower range or PriceMustBeHigher range by 15 pips. (Before all trades are closed. Profit must be higher than starting balance that the user entered in the input field). Can someone modify it for Free and reply to this thread with their code modification?
You will automatically get credit for it, if you reply to this thread with your modification.