Martingale code not working.

 

Hello all! My code works untill there is a loosing position then it does not work any more. Can please you help?

#include <Trade/Trade.mqh>

CTrade trade;
bool isTradeAllowed = true;

double Lots = 0.2;
double LotFactor = 1.5;

int aMagic = 100;


int OnInit()
  {
   trade.SetExpertMagicNumber(aMagic);

   return(INIT_SUCCEEDED);
  }  


void OnDeinit(const int reason)
  {
   
  }


void OnTick()
  {
   if(isTradeAllowed)
     {
      double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
      double sl = ask + 400*_Point;
      double tp = ask - 400*_Point;
      
      ask = NormalizeDouble(ask,_Digits);
      tp =  NormalizeDouble(tp,_Digits);
      sl = NormalizeDouble(sl,_Digits);
        
    if(trade.Sell(Lots,_Symbol,ask,sl,tp)) 
      {
        isTradeAllowed = false;
      }
    }
  }
void  OnTradeTransaction(
   const MqlTradeTransaction&    trans,     // trade transaction structure 
   const MqlTradeRequest&        request,   // request structure 
   const MqlTradeResult&         result     // response structure 
   )
{
  if(trans.type == TRADE_TRANSACTION_DEAL_ADD)
  {
   CDealInfo deal;
   deal.Ticket(trans.deal);
   HistorySelect(TimeCurrent() - PeriodSeconds(PERIOD_D1),TimeCurrent()+10); 
     
   if(deal.Magic() == aMagic && deal.Symbol() == _Symbol)
   {
    if(deal.Entry() == DEAL_ENTRY_OUT)
    { 
     Print(__FUNCTION__," > Closed pos #", trans.position);
     if(deal.Profit() > 0) 
     {
      isTradeAllowed = true;
     }
     else 
     {
     if(deal.DealType() == DEAL_TYPE_BUY)
     {
      double lots = NormalizeDouble(deal.Volume()*LotFactor,_Digits);
     
      
      double bid = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
      double sl = bid - 400*_Point;
      double tp = bid + 400*_Point;
      
    trade.Buy(lots,_Symbol,bid,sl,tp);
     }
     
     else if(deal.DealType() == DEAL_TYPE_SELL)
     {
      double lots = deal.Volume()*LotFactor;
      lots = NormalizeDouble(lots,_Digits);
     
     double ask = SymbolInfoDouble(_Symbol,SYMBOL_BID);
      double sl = ask + 400*_Point;
      double tp = ask - 400*_Point;
            
      tp =  NormalizeDouble(tp,_Digits);
      sl = NormalizeDouble(sl,_Digits);
    
      trade.Sell(lots,_Symbol,ask,sl,tp);
      }
     }
    }
   }
  }
 } 
 

Martingale, Hedging and Grid

The forum threads

  1. Martingale vs. Non Martingale (Simplified RoR vs Profit and the Illusions)
  2. Definition: classical martingale, progressive dynamic growth martingale, progressive static growth martingale, anti martingale or inverse martingale
  3. Martingale, Hedging and Grid : MHG
  4. Featured Martingale EA to explore and expand, provided by a programmer 
  5. Does a safe Martingale exist ?
  6. Why people are still trying Martingale Systems?
  7. The Video - Trading The Martingale and Anti Martingale Strategies

CodeBase

  1. Angry Bird (Scalping) - expert for MetaTrader 4 - the settings and trades - look at this post.
  2. Three Typical Candles Martingale - expert for MetaTrader 5
  3. iRSI Martingale 10 Levels - expert for MetaTrader 5
  4. Grid Semiautomat Panel - expert for MetaTrader 5
  5. Grid Template EA - expert for MetaTrader 4
  6. VR---SETKA---3_v2 - expert for MetaTrader 5
  7. Grid - script for MetaTrader 5
  8. KNUX Martingale - expert for MetaTrader 4
  9. MacdPatternTraderAll0.01. Time+Martingale - expert for MetaTrader 4
  10. Pure_Martingale - expert for MetaTrader 4
  11. Trend_Catcher - expert for MetaTrader 5
  12. Ingrid Martingale - expert for MetaTrader 5
  13. MultiMartin - expert for MetaTrader 5
  14. N trades per set Martingale - expert for MetaTrader 4 
  15. Martingale Smart - expert for MetaTrader 4 
  16. Reversing Martingale EA - expert for MetaTrader 5
  17. VR Calculate Martingale Lite - indicator for MetaTrader 4
  18. VR Calculate Martingale Lite MT 5 - indicator for MetaTrader 5 
  19. Hoop master 2 - expert for MetaTrader 5
  20. Martingale EA - expert for MetaTrader 4
  21. Simple candle filter martingale EA - expert for MetaTrader 4
  22. RSI Martingale - expert for MetaTrader 5
  23. more here

The articles

============

 
  1.     if(trade.Sell(Lots,_Symbol,ask,sl,tp)) 
    
        trade.Buy(lots,_Symbol,bid,sl,tp);
    

    You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

  2. Martingale, guaranteed to blow your account eventually. If it's not profitable without, it is definitely not profitable with.
              Martingale vs. Non Martingale (Simplified RoR vs Profit and the Illusions) - MQL5 programming forum (2015)

    Why it won't work:
              Calculate Loss from Lot Pips - MQL5 programming forum (2017)
              THIS Trading Strategy is a LIE... I took 100,000 TRADES with the Martingale Strategy - YouTube (2020.12.12)

 
Sergey Golubev #:

Martingale, Hedging and Grid

The forum threads

  1. Martingale vs. Non Martingale (Simplified RoR vs Profit and the Illusions)
  2. Definition: classical martingale, progressive dynamic growth martingale, progressive static growth martingale, anti martingale or inverse martingale
  3. Martingale, Hedging and Grid : MHG
  4. Featured Martingale EA to explore and expand, provided by a programmer 
  5. Does a safe Martingale exist ?
  6. Why people are still trying Martingale Systems?
  7. The Video - Trading The Martingale and Anti Martingale Strategies

CodeBase

  1. Angry Bird (Scalping) - expert for MetaTrader 4 - the settings and trades - look at this post.
  2. Three Typical Candles Martingale - expert for MetaTrader 5
  3. iRSI Martingale 10 Levels - expert for MetaTrader 5
  4. Grid Semiautomat Panel - expert for MetaTrader 5
  5. Grid Template EA - expert for MetaTrader 4
  6. VR---SETKA---3_v2 - expert for MetaTrader 5
  7. Grid - script for MetaTrader 5
  8. KNUX Martingale - expert for MetaTrader 4
  9. MacdPatternTraderAll0.01. Time+Martingale - expert for MetaTrader 4
  10. Pure_Martingale - expert for MetaTrader 4
  11. Trend_Catcher - expert for MetaTrader 5
  12. Ingrid Martingale - expert for MetaTrader 5
  13. MultiMartin - expert for MetaTrader 5
  14. N trades per set Martingale - expert for MetaTrader 4 
  15. Martingale Smart - expert for MetaTrader 4 
  16. Reversing Martingale EA - expert for MetaTrader 5
  17. VR Calculate Martingale Lite - indicator for MetaTrader 4
  18. VR Calculate Martingale Lite MT 5 - indicator for MetaTrader 5 
  19. Hoop master 2 - expert for MetaTrader 5
  20. Martingale EA - expert for MetaTrader 4
  21. Simple candle filter martingale EA - expert for MetaTrader 4
  22. RSI Martingale - expert for MetaTrader 5
  23. more here

The articles

============

Thanks for this. But this is for mq4 not mql5 
 
William Roeder #:
  1. You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit and open at the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)
      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes. My GBPJPY shows average spread = 26 points, but average maximum spread = 134 (your broker will be similar).

  2. Martingale, guaranteed to blow your account eventually. If it's not profitable without, it is definitely not profitable with.
              Martingale vs. Non Martingale (Simplified RoR vs Profit and the Illusions) - MQL5 programming forum (2015)

    Why it won't work:
              Calculate Loss from Lot Pips - MQL5 programming forum (2017)
              THIS Trading Strategy is a LIE... I took 100,000 TRADES with the Martingale Strategy - YouTube (2020.12.12)

Thanks. I want it anyway, whether the article says it's good or bad.
Can you help with what's wrong with the code?
 
Worldwide Fox #:
Thanks. I want it anyway, whether the article says it's good or bad.
Can you help with what's wrong with the code?

You have to do SetMarginMode() = Hedging. Buy must be Ask price and Sell must be Bid price. The terms of this trading price are not arbitrarily determined by you, whether you want it or not. If you do not follow the rules, then until your computer is damaged and replace again your EA will not work.

Reason: