Author: 2019 © NELODI.com
0 Views
0 Downloads
0 Favorites
NLD_Expert
ÿþ//+-------------------------------------------+

//|                            NLD_Expert.mq5 |

//|                     http://www.nelodi.com |

//|             Copyright (c) 2019 NELODI.com |

//+-------------------------------------------+

#property copyright   "2019 © NELODI.com"

#property link        "http://www.nelodi.com"

#property description "NELODI - Expert"



#property strict



#include "NLD_Include.mqh"



//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

input double  iCommisForex=0.06;    // Forex Commission ($) {0.06}

input double  iCommisStock=0.36;    // Stock Commission ($) {0.36}

input bool    iHedgingAllowed=true; // Hedging Allowed? {true}



//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

double  iMarginStop=0;        // Low Margin Level (Balance%) {0}

bool    iCloseOnMargin=false; // Close best on Low Margin? {false}

bool    iAllSymbols=false;    // Global (ALL Symbols)? {false}

int     iCheckInterval=250;   // Check Interval (ms) {250}



long lastCheckTime=0;

int needContinue=0,doneContinue=0;

string toContinue[100];

//+------------------------------------------------------------------+

int OnInit(void) {

   if(iAllSymbols)

      PrepareAccount("",iCommisForex,iCommisStock,iMarginStop);

   else

      PrepareAccount(_Symbol,iCommisForex,iCommisStock,iMarginStop);



   Clear_ExpertData();



   ChartSetInteger(0,CHART_SHOW_DATE_SCALE,false);

   ChartSetInteger(0,CHART_SHOW_PRICE_SCALE,false);



   ObjectSetInteger(0,"Btn_Best",OBJPROP_ZORDER,1000);

   ObjectSetInteger(0,"Btn_Sell",OBJPROP_ZORDER,1000);

   ObjectSetInteger(0,"Btn_Buy",OBJPROP_ZORDER,1000);

   ObjectSetInteger(0,"Btn_SellP_BuyL",OBJPROP_ZORDER,1000);

   ObjectSetInteger(0,"Btn_BuyP_SellL",OBJPROP_ZORDER,1000);

   ObjectSetInteger(0,"Btn_SellProfit",OBJPROP_ZORDER,1000);

   ObjectSetInteger(0,"Btn_BuyProfit",OBJPROP_ZORDER,1000);

   ObjectSetInteger(0,"Btn_SellLoss",OBJPROP_ZORDER,1000);

   ObjectSetInteger(0,"Btn_BuyLoss",OBJPROP_ZORDER,1000);

   ObjectSetInteger(0,"Btn_SellTotal",OBJPROP_ZORDER,1000);

   ObjectSetInteger(0,"Btn_BuyTotal",OBJPROP_ZORDER,1000);

   ObjectSetInteger(0,"Btn_Profit",OBJPROP_ZORDER,1000);

   ObjectSetInteger(0,"Btn_Loss",OBJPROP_ZORDER,1000);

   ObjectSetInteger(0,"Btn_Total",OBJPROP_ZORDER,1000);



   ObjectSetInteger(0,"Btn_Best",OBJPROP_STATE,false);

   ObjectSetInteger(0,"Btn_Hedge",OBJPROP_STATE,false);

   ObjectSetInteger(0,"Btn_Sell",OBJPROP_STATE,false);

   ObjectSetInteger(0,"Btn_Buy",OBJPROP_STATE,false);

   ObjectSetInteger(0,"Btn_SellP_BuyL",OBJPROP_STATE,false);

   ObjectSetInteger(0,"Btn_BuyP_SellL",OBJPROP_STATE,false);

   ObjectSetInteger(0,"Btn_SellProfit",OBJPROP_STATE,false);

   ObjectSetInteger(0,"Btn_BuyProfit",OBJPROP_STATE,false);

   ObjectSetInteger(0,"Btn_SellLoss",OBJPROP_STATE,false);

   ObjectSetInteger(0,"Btn_BuyLoss",OBJPROP_STATE,false);

   ObjectSetInteger(0,"Btn_SellTotal",OBJPROP_STATE,false);

   ObjectSetInteger(0,"Btn_BuyTotal",OBJPROP_STATE,false);

   ObjectSetInteger(0,"Btn_Profit",OBJPROP_STATE,false);

   ObjectSetInteger(0,"Btn_Loss",OBJPROP_STATE,false);

   ObjectSetInteger(0,"Btn_Total",OBJPROP_STATE,false);



   lastCheckTime=0;

   doUpdateInfo();

   doUpdateChart();

   doForceUpdate();



   EventSetMillisecondTimer(iCheckInterval);



   return(INIT_SUCCEEDED);

}

//+------------------------------------------------------------------+

void OnDeinit(const int reason) {

   EventKillTimer();

}

//+------------------------------------------------------------------+

bool needUpdate=false;

//+------------------------------------------------------------------+

void OnTrade(void) {

   needUpdate=true;

}

//+------------------------------------------------------------------+

void OnTimer(void) {

   if(needContinue>0) {

      string sparam=toContinue[doneContinue];

      doneContinue++;

      if(doneContinue>=needContinue) {

         needContinue=0;

         doneContinue=0;

      }

      doExec(sparam);

   } else {

      if(iCloseOnMargin) {

         double accbal=AccountInfoDouble(ACCOUNT_BALANCE);

         double accmar=AccountInfoDouble(ACCOUNT_MARGIN_FREE);



         if(AccountInfoInteger(ACCOUNT_MARGIN_SO_MODE)==ACCOUNT_STOPOUT_MODE_MONEY)

            accmar+=AccountInfoDouble(ACCOUNT_MARGIN_SO_SO);

         else

            accmar+=AccountInfoDouble(ACCOUNT_MARGIN)*(100.0-AccountInfoDouble(ACCOUNT_MARGIN_SO_SO))/100.0;



         if(accmar<accbal*xMarginStop/100.0)

            doCloseBest(true,true,true,true,"LM|");

      }

      if(needUpdate) {

         needUpdate=false;

         if(doUpdateInfo()) {

            doUpdateChart();

            doForceUpdate();

         }

      }

   }

}

//+------------------------------------------------------------------+

void OnChartEvent(const int id,         // Event identifier

                  const long& lparam,   // Event parameter of long type

                  const double& dparam, // Event parameter of double type

                  const string& sparam  // Event parameter of string type

                 ) {

   if(id==CHARTEVENT_OBJECT_CLICK) {

      if(ObjectGetInteger(0,sparam,OBJPROP_STATE) && (StringSubstr(sparam,0,4)=="Btn_")) {

         ObjectSetInteger(0,sparam,OBJPROP_STATE,false);

         if(!ObjectGetInteger(0,sparam,OBJPROP_HIDDEN))

            doExec(sparam);

      }

   } else if(id==CHARTEVENT_CUSTOM) {

      if(StringSubstr(sparam,0,4)=="Btn_") {

         ObjectSetInteger(0,sparam,OBJPROP_STATE,false);

         doExec(sparam);

      }

   }

}

//+------------------------------------------------------------------+

void doExec(const string sparam) {

   if((sparam=="Btn_BuyProfit") || (sparam=="Btn_BuyP_SellL")) {

      ObjectSetInteger(0,sparam,OBJPROP_BGCOLOR,clrBlue);

      if(!doCloseAll(true,false,true,false)) {

         toContinue[needContinue]=sparam;

         needContinue++;

      }

   }

   if((sparam=="Btn_SellProfit") || (sparam=="Btn_SellP_BuyL")) {

      ObjectSetInteger(0,sparam,OBJPROP_BGCOLOR,clrBlue);

      if(!doCloseAll(false,true,true,false)) {

         toContinue[needContinue]=sparam;

         needContinue++;

      }

   }



   if((sparam=="Btn_BuyLoss") || (sparam=="Btn_SellP_BuyL")) {

      ObjectSetInteger(0,sparam,OBJPROP_BGCOLOR,clrBlue);

      if(!doCloseAll(true,false,false,true)) {

         toContinue[needContinue]=sparam;

         needContinue++;

      }

   }

   if((sparam=="Btn_SellLoss") || (sparam=="Btn_BuyP_SellL")) {

      ObjectSetInteger(0,sparam,OBJPROP_BGCOLOR,clrBlue);

      if(!doCloseAll(false,true,false,true)) {

         toContinue[needContinue]=sparam;

         needContinue++;

      }

   }



   if(sparam=="Btn_BuyTotal") {

      ObjectSetInteger(0,sparam,OBJPROP_BGCOLOR,clrBlue);

      if(!doCloseAll(true,false,true,true)) {

         toContinue[needContinue]=sparam;

         needContinue++;

      }

   }

   if(sparam=="Btn_SellTotal") {

      ObjectSetInteger(0,sparam,OBJPROP_BGCOLOR,clrBlue);

      if(!doCloseAll(false,true,true,true)) {

         toContinue[needContinue]=sparam;

         needContinue++;

      }

   }



   if(sparam=="Btn_Profit") {

      ObjectSetInteger(0,sparam,OBJPROP_BGCOLOR,clrBlue);

      if(!doCloseAll(true,true,true,false)) {

         toContinue[needContinue]=sparam;

         needContinue++;

      }

   }

   if(sparam=="Btn_Loss") {

      ObjectSetInteger(0,sparam,OBJPROP_BGCOLOR,clrBlue);

      if(!doCloseAll(true,true,false,true)) {

         toContinue[needContinue]=sparam;

         needContinue++;

      }

   }

   if(sparam=="Btn_Total") {

      ObjectSetInteger(0,sparam,OBJPROP_BGCOLOR,clrBlue);

      if(!doCloseAll(true,true,true,true)) {

         toContinue[needContinue]=sparam;

         needContinue++;

      }

   }



   if(sparam=="Btn_Buy") {

      ObjectSetInteger(0,sparam,OBJPROP_BGCOLOR,C'0,255,255');

      if(iHedgingAllowed)

         doBuyNow(0,0);

      else

         doCloseBestSellOrOpenBuy(0,0);

      needContinue=0;

      doneContinue=0;

   }

   if(sparam=="Btn_Sell") {

      ObjectSetInteger(0,sparam,OBJPROP_BGCOLOR,C'255,0,255');

      if(iHedgingAllowed)

         doSellNow(0,0);

      else

         doCloseBestBuyOrOpenSell(0,0);

      needContinue=0;

      doneContinue=0;

   }

   if(sparam=="Btn_Hedge") {

      ObjectSetInteger(0,sparam,OBJPROP_BGCOLOR,C'255,255,0');

      doHedgeNow(0,0);

      needContinue=0;

      doneContinue=0;

   }

   if(sparam=="Btn_Best") {

      ObjectSetInteger(0,sparam,OBJPROP_BGCOLOR,C'0,255,255');

      doCloseBest(true,true,true,true);

      needContinue=0;

      doneContinue=0;

   }



   if(sparam=="Btn_AutoBuy")

      if(needContinue<=0)

         doBuyNow(0,0);

   if(sparam=="Btn_AutoSell")

      if(needContinue<=0)

         doSellNow(0,0);



   doForceUpdate();

}

//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

bool doCloseAll(bool buying,bool selling,bool withProfit,bool withLoss) {

   if(tradeAllowed()) {

      int cnt=Count_Positions();

      while(cnt>0) {

         cnt--;

         if(selectAnyPosition(cnt)) {

            if(buying && isBuy)

               if((withProfit && buyProfit>0) || (withLoss && buyProfit<=0)) {

                  int digits=(int)SymbolInfoInteger(lastSymbol,SYMBOL_DIGITS);

                  if(!closeNow(DoubleToString(buyProfit,2)+" @"+DoubleToString(buyCurrent,digits)+"|"+TimeToString(TimeCurrent(),TIME_MINUTES|TIME_SECONDS))) {

                     Sleep(100);

                     return(false);

                  }

               }

            if(selling && isSell)

               if((withProfit && sellProfit>0) || (withLoss && sellProfit<=0)) {

                  int digits=(int)SymbolInfoInteger(lastSymbol,SYMBOL_DIGITS);

                  if(!closeNow(DoubleToString(sellProfit,2)+" @"+DoubleToString(sellCurrent,digits)+"|"+TimeToString(TimeCurrent(),TIME_MINUTES|TIME_SECONDS))) {

                     Sleep(100);

                     return(false);

                  }

               }

         }

      }

   }

   return(true);

}

//+------------------------------------------------------------------+

bool doCloseBest(bool buying,bool selling,bool withProfit,bool withLoss,string comm="") {

   bool res=false;

   int bestIdx=-1;

   bestProfit=0;

   bestType="";

   if(tradeAllowed()) {

      int cnt=Count_Positions();

      while(cnt>0) {

         cnt--;

         if(selectAnyPosition(cnt)) {

            if(buying && isBuy)

               if((withProfit && buyProfit>0) || (withLoss && buyProfit<=0))

                  if((bestIdx<0) || (buyProfit>bestProfit)) {

                     bestIdx=cnt;

                     bestProfit=buyProfit;

                     bestType="B";

                  }

            if(selling && isSell)

               if((withProfit && sellProfit>0) || (withLoss && sellProfit<=0))

                  if((bestIdx<0) || (sellProfit>bestProfit)) {

                     bestIdx=cnt;

                     bestProfit=sellProfit;

                     bestType="S";

                  }

         }

      }

   }

   if(bestIdx>=0)

      if(selectAnyPosition(bestIdx)) {

         int digits=(int)SymbolInfoInteger(lastSymbol,SYMBOL_DIGITS);

         res=closeNow(comm+DoubleToString(isBuy?buyProfit:sellProfit,2)+" @"+DoubleToString(isBuy?buyCurrent:sellCurrent,digits)+"|"+TimeToString(TimeCurrent(),TIME_MINUTES|TIME_SECONDS));

      }

   return(res);

}

//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

void doBuyNow(double stopLossPrice,double takeProfitPrice) {

   if(tradeAllowed() && MA_Symbol!="") {

      double bestPrice=0,worstPrice=0,myExp=0;

      int cnt=countBuys(bestPrice,worstPrice,myExp);

      if((bestPrice==0) || (currentBuyPrice<bestPrice))

         bestPrice=currentBuyPrice;

      int digts=(int)SymbolInfoInteger(MA_Symbol,SYMBOL_DIGITS);

      openBuy(minTradeVol,bestPrice,stopLossPrice,takeProfitPrice,"@"+DoubleToString(currentBuyPrice,digts)+"|"+TimeToString(TimeCurrent(),TIME_MINUTES|TIME_SECONDS));

   }

}

//+------------------------------------------------------------------+

void doSellNow(double stopLossPrice,double takeProfitPrice) {

   if(tradeAllowed() && MA_Symbol!="") {

      double bestPrice=0,worstPrice=0,myExp=0;

      int cnt=countSells(bestPrice,worstPrice,myExp);

      if((bestPrice==0) || (currentSellPrice>bestPrice))

         bestPrice=currentSellPrice;

      int digts=(int)SymbolInfoInteger(MA_Symbol,SYMBOL_DIGITS);

      openSell(minTradeVol,bestPrice,stopLossPrice,takeProfitPrice,"@"+DoubleToString(currentSellPrice,digts)+"|"+TimeToString(TimeCurrent(),TIME_MINUTES|TIME_SECONDS));

   }

}

//+------------------------------------------------------------------+

void doCloseBestSellOrOpenBuy(double stopLossPrice,double takeProfitPrice) {

   if(tradeAllowed() && MA_Symbol!="") {

      if(!doCloseBest(false,true,true,true)) {

         double bestPrice=0,worstPrice=0,myExp=0;

         int cnt=countBuys(bestPrice,worstPrice,myExp);

         if((bestPrice==0) || (currentBuyPrice<bestPrice))

            bestPrice=currentBuyPrice;

         int digts=(int)SymbolInfoInteger(MA_Symbol,SYMBOL_DIGITS);

         openBuy(minTradeVol,bestPrice,stopLossPrice,takeProfitPrice,"@"+DoubleToString(currentBuyPrice,digts)+"|"+TimeToString(TimeCurrent(),TIME_MINUTES|TIME_SECONDS));

      }

   }

}

//+------------------------------------------------------------------+

void doCloseBestBuyOrOpenSell(double stopLossPrice,double takeProfitPrice) {

   if(tradeAllowed() && MA_Symbol!="") {

      if(!doCloseBest(true,false,true,true)) {

         double bestPrice=0,worstPrice=0,myExp=0;

         int cnt=countSells(bestPrice,worstPrice,myExp);

         if((bestPrice==0) || (currentSellPrice>bestPrice))

            bestPrice=currentSellPrice;

         int digts=(int)SymbolInfoInteger(MA_Symbol,SYMBOL_DIGITS);

         openSell(minTradeVol,bestPrice,stopLossPrice,takeProfitPrice,"@"+DoubleToString(currentSellPrice,digts)+"|"+TimeToString(TimeCurrent(),TIME_MINUTES|TIME_SECONDS));

      }

   }

}

//+------------------------------------------------------------------+

void doHedgeNow(double stopLossPrice,double takeProfitPrice) {

   if(tradeAllowed() && MA_Symbol!="") {

      double volBuy=0,volSell=0,myVol=0;

      countHedge(volBuy,volSell);

      if(volBuy>volSell) {

         myVol=volBuy-volSell;

         double price=SymbolInfoDouble(MA_Symbol,SYMBOL_BID);

         int digts=(int)SymbolInfoInteger(MA_Symbol,SYMBOL_DIGITS);

         openSell(myVol,price,stopLossPrice,takeProfitPrice,"@"+DoubleToString(price,digts)+"|"+TimeToString(TimeCurrent(),TIME_MINUTES|TIME_SECONDS));

      } else if(volSell>volBuy) {

         myVol=volSell-volBuy;

         double price=SymbolInfoDouble(MA_Symbol,SYMBOL_ASK);

         int digts=(int)SymbolInfoInteger(MA_Symbol,SYMBOL_DIGITS);

         openBuy(myVol,price,stopLossPrice,takeProfitPrice,"@"+DoubleToString(price,digts)+"|"+TimeToString(TimeCurrent(),TIME_MINUTES|TIME_SECONDS));

      }

   }

}

//+------------------------------------------------------------------+

void doForceUpdate(void) {

   ChartRedraw(0);

}

//+------------------------------------------------------------------+

void doUpdateAll() {

   if(doUpdateInfo()) {

      doUpdateChart();

      doForceUpdate();

   }

}

//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---