Zone Strategy

 

Hello guys.. last days I am really struggling with my strategy and I don't know how to work it out.. so I want to ask you for help. 

This strategy is that we have zones... let's say it starts at 0.100 and ends at 0.200 and so on and so on... At the lower boundary of each zone a buy trade is opened in that zone with a take profit at 0.200 and as soon as the price reaches the upper boundary of the zone the trade is closed, an opposite trade is opened with a take profit at the lower boundary of the zone and at the same time a buy trade is opened in the new zone above that. The same applies if it goes to the zones below that. For example, I still had a small gap between the zones so that the trades could manage to open. Anyway, I wrote this code and I've been modifying it for a long time and it still doesn't work to my liking.. would anyone know what to do? Thanks in advance!

edit: there is no stop loss and the trades stay open until they reach their take profit

Files:
 
  1. Of course, you have a gap, you buy at the Ask and sell at the Bid.
  2. Why close and then reopen? Just keep it open.
 
William Roeder #:
  1. Of course, you have a gap, you buy at the Ask and sell at the Bid.
  2. Why close and then reopen? Just keep it open.

Because it has to reach take profits regularly. I just dont understand why sometimes that take profit is for example 2 USD and sometimes 4USD. It doesnt recognize these zones right..


Hello guys, last few weeks I have been working on my new EA.

This is a strategy that uses zones with a fixed position. There are an infinite number of zones and if the price enters a new one, a trade will open in that zone. Let's say a zone starts at 0.200 and ends at 0.300, the next zone will be 0.300-0.400, 0.400-.500 and so on. If we turn on the strategy and the price at that time is at say 0.450 and then moves up and reaches the upper boundary of the zone (0.500) then a short trade is opened with a take profit at the lower boundary of the zone (0.400) and at the same time, because the zone is also above that, a long trade is opened with a take profit at 0. 600) and if the price touches the lower boundary, then if there is no open trade in the zone below that, a sell will open to the zone below that and at the same time, because a sell has closed in our zone, a reverse buy will open.. 

Attached you will find a picture of the strategy how it should be right without that space between zones.. and of course no limits and stops, its for manual opening.

Also attached is a picture of what errors the current program is making. For example, I thought of adding a small zone between the zones in which shops are not opened, but I can't get the code to work. I have already tested this strategy when opening manually, but it is very difficult. Could someone check the code and point out any bugs or help me fix it? 

Thanks in advance for any answer!

 
Your topic has been moved to the section: MQL4 and MetaTrader 4
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
  • Usually people who can't code don't receive free help on this forum.
  • If you show your attempts and describe your problem clearly, you will most probably receive an answer from the community. Use the CODE button (Alt-S) when inserting code.
  • To learn MQL4 programming, you can research the many available Articles on the subject, or examples in the Codebase, as well as reference the online Documentation.
  • If you do not want to learn to code, that is not a problem. You can either look at the Codebase if something free already exists, or in the Market for paid products (also sometimes free). However, recommendations or suggestions for Market products are not allowed on the forum, so you will have to do your own research.
  • Finally, you also have the option to hire a programmer in the Freelance section.
MQL4 Reference
MQL4 Reference
  • docs.mql4.com
MQL4 Reference
 
Fernando Carreiro #:
  • Usually people who can't code don't receive free help on this forum.
  • If you show your attempts and describe your problem clearly, you will most probably receive an answer from the community. Use the CODE button (Alt-S) when inserting code.
  • To learn MQL4 programming, you can research the many available Articles on the subject, or examples in the Codebase, as well as reference the online Documentation.
  • If you do not want to learn to code, that is not a problem. You can either look at the Codebase if something free already exists, or in the Market for paid products (also sometimes free). However, recommendations or suggestions for Market products are not allowed on the forum, so you will have to do your own research.
  • Finally, you also have the option to hire a programmer in the Freelance section.

I wrote that code, I just don't understand why it doesnt work the same way I want it to.
 
adamfouz #:

I wrote that code, I just don't understand why it doesnt work the same way I want it to.
extern double ZoneStart = 0.00210; // Počáteční hranice zóny
extern double ZoneStep = 0.00210;  // Velikost zóny
extern double TakeProfit = 0.002; // Velikost Take Profitu
double lastBuyZone = -1; // Sledování poslední buy zóny
double lastSellZone = -1; // Sledování poslední sell zóny
double startEquity = AccountEquity();
double startBalance = AccountBalance();
double dailyStop = AccountEquity() * 0.05;
double stopLoss = AccountEquity() - AccountEquity() * 0.01;
int lastDay = -1;


int start() {
 
  
    int tradeCount = OrdersTotal();
    int startTime = TimeCurrent();
    int day = DayOfWeek();
    int hour = Hour();
    int minutes = Minute();
   
    double currentEquity = AccountEquity();
    
    
    if (day != lastDay) {
      stopLoss = AccountEquity() - dailyStop;
      lastDay = day; 
      Print("Stop Loss updated");}
    
    
   /*if (currentEquity >= startBalance * 1.1) {
    // Cyklus pro zavření všech otevřených obchodů
    CloseAllTrades();
    if (day == 1) {
         startEquity = currentEquity; }
    return(0);
         }*/
   if (currentEquity <= stopLoss) {
    CloseAllTrades()
    
    ;
    Print("STOP DUE TO REACHING 5% DAILY DRAWDOWN");
    
    //startEquity = currentEquity; 
    return (0); }

   
    if (currentEquity >= startEquity * 1.01) {
    //if (currentEquity <= (startEquity - (startEquity * 0.02))) {
    // Cyklus pro zavření všech otevřených obchodů
    CloseAllTrades();
    startEquity = currentEquity;
    
         }
         
         
   
   
   
   /*if (day != 1) {
   CloseAllTrades();
   return(0); }*/
   
   /*if (hour <= 19 || hour >22) {
   CloseAllTrades();
   return(0); }*/
   
   
    double currentPrice = MarketInfo(Symbol(), MODE_ASK); // Aktuální cena
    int zoneIndex = (int)((currentPrice - ZoneStart) / ZoneStep); // Výpočet indexu zóny

    double zoneLowerBound = ZoneStart + zoneIndex * ZoneStep;
    double zoneUpperBound = zoneLowerBound + ZoneStep;

    // Kontrola a otevření BUY obchodů
    if (zoneIndex != lastBuyZone) {
        if (currentPrice - zoneLowerBound < zoneUpperBound - currentPrice) {
            if (!IsTradeOpen(OP_BUY, zoneLowerBound, zoneUpperBound)) {
                OrderSend(Symbol(), OP_BUY, 0.1, currentPrice, 0, 0, currentPrice + TakeProfit, "Buy Zone Trade", 0, 0, Green);
                lastBuyZone = zoneIndex;
            }
        }
    }

    // Kontrola a otevření SELL obchodů
    if (zoneIndex != lastSellZone) {
        if (currentPrice - zoneLowerBound >= zoneUpperBound - currentPrice) {
            if (!IsTradeOpen(OP_SELL, zoneLowerBound, zoneUpperBound)) {
                OrderSend(Symbol(), OP_SELL, 0.1, MarketInfo(Symbol(), MODE_BID), 0, 0, MarketInfo(Symbol(), MODE_BID) - TakeProfit, "Sell Zone Trade", 0, 0, Red);
                lastSellZone = zoneIndex;
            }
        }
    }

    return 0;
}

// Pomocná funkce pro kontrolu, zda v dané zóně již existuje obchod daného typu
bool IsTradeOpen(int type, double lowerBound, double upperBound) {
    for (int i = 0; i < OrdersTotal(); i++) {
        if (OrderSelect(i, SELECT_BY_POS) && OrderSymbol() == Symbol()) {
            double orderOpenPrice = OrderOpenPrice();
            if (OrderType() == type && orderOpenPrice >= lowerBound && orderOpenPrice < upperBound) {
                return true;
            }
        }
    }
    return false;
}

// Funkce na zavření všech obchodů
void CloseAllTrades() {
      for (int q = OrdersTotal() - 1; q >= 0; q--) {
        if (OrderSelect(q, SELECT_BY_POS, MODE_TRADES)) {
            if (OrderSymbol() == Symbol() && (OrderType() == OP_BUY || OrderType() == OP_SELL)) {
                int ticket1 = OrderTicket();
                double lots1 = OrderLots();      
                double closePrice1 = OrderType() == OP_BUY ? MarketInfo(OrderSymbol(), MODE_BID) : MarketInfo(OrderSymbol(), MODE_ASK);
                int result1 = OrderClose(ticket1, lots1, closePrice1, 0, clrNONE);

                if (result1 < 0) {
                    Print("OrderClose error: ", GetLastError());
                }
            }
        }
    }

}
Reason: