Advertisement
__blueandred__

Ea

Feb 11th, 2024
819
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 33.27 KB | None | 0 0
  1. //+------------------------------------------------------------------+
  2. //|                                                      ProjectName |
  3. //|                                      Copyright 2020, CompanyName |
  4. //|                                       http://www.companyname.net |
  5. //+------------------------------------------------------------------+
  6.  
  7. #define SERIES(name,type)                               \
  8. class C##name                                           \
  9. {                                                       \
  10. public:                                                 \
  11. type operator[](const int i){return i##name(NULL,0,i);} \
  12. }name;
  13. //+------------------------------------------------------------------+
  14. //|                                                                  |
  15. //+------------------------------------------------------------------+
  16. SERIES(Open,double)
  17. SERIES(Low,double)
  18. SERIES(High,double)
  19. SERIES(Close,double)
  20. SERIES(Time,datetime)
  21. SERIES(Volume,long)
  22.  
  23. #include <Trade\Trade.mqh> //Instatiate Trades Execution Library
  24. #include <Trade\OrderInfo.mqh> //Instatiate Library for Orders Information
  25. #include <Trade\PositionInfo.mqh> //Instatiate Library for Positions Information
  26. #include <Trade\SymbolInfo.mqh>
  27. #include <Trade\AccountInfo.mqh>
  28. //---
  29. CTrade         m_trade; // Trades Info and Executions library
  30. COrderInfo     m_order; //Library for Orders information
  31. CPositionInfo  m_position; // Library for all position features and information
  32. CSymbolInfo    m_symbol;                     // symbol info object
  33. CAccountInfo   m_account;                    // account info wrapper
  34. enum MartiType
  35.   {
  36.    Multi = 1, // Multiplier
  37.    Increament = 2, // Increament
  38.   };
  39. enum TypeEntry
  40.   {
  41.    BuyOnly = 0, // Buy Only
  42.    SellOnly = 1, // Sell Only
  43.    Buy_Sell = 2, // Buy & Sell
  44.   };
  45.  
  46. input int MagicNo = 111;
  47. input ENUM_TIMEFRAMES TimeFrame = PERIOD_H1;
  48. input TypeEntry Entry_Mode = Buy_Sell;
  49. input double Lots=0.01;
  50. input double TakeProfit = 100;
  51. input double StopLoss = 100;
  52. input bool Use_Martingle = true;
  53. input double PipStep =  200;
  54. input MartiType Martingle_Type = Multi;
  55. input double Multiplier = 1.5;
  56. input double Increament_Lot = 0.01;
  57. input int Increament_Layer = 10;
  58. input int BuyUpLimit = 1000;
  59. input int SellUpLimit = 20;
  60. input int BuyDownLimit = 20;
  61. input int SellDownLimit = 1000;
  62. input string BETS_Desc = "----BE & TS settings----";
  63. input bool Use_Trailing = true;
  64. input double TrailingStart = 100;
  65. input double TrailingStep = 10;
  66. input bool Use_BE = true;
  67. input double BE_Pips = 100;
  68.  
  69. input string MA1_Setting = "----MA1----";
  70. input int MA1_Period = 20;
  71. input ENUM_MA_METHOD MA1_Method = 3;
  72. input ENUM_APPLIED_PRICE MA1_ApplyTo = 2;
  73. input string MA2_Setting = "----MA1----";
  74. input int MA2_Period = 200;
  75. input ENUM_MA_METHOD MA2_Method = 3;
  76. input ENUM_APPLIED_PRICE MA2_ApplyTo = 2;
  77. input string MM_Desc = "----Money Management----";
  78. input double MaxDrawdown_USD=100.0;
  79. input double MinEquityStart = 100;
  80. input string TimeStart = "00:00";
  81. input string TimeStop = "23:59";
  82.  
  83.  
  84.  
  85. double MonDD;
  86. double TueDD;
  87. double WedDD;
  88. double ThuDD;
  89. double FriDD;
  90. double SunDD;
  91.  
  92. double MonProfit;
  93. double TueProfit;
  94. double WedProfit;
  95. double ThuProfit;
  96. double FriProfit;
  97. double SunProfit;
  98. int MaxTrade = 50;
  99. double MonLot;
  100. double TueLot;
  101. double WedLot;
  102. double ThuLot;
  103. double FriLot;
  104. double SunLot;
  105.  
  106. int BuyLayer;
  107. int SellLayer;
  108.  
  109. datetime NewDay;
  110.  
  111. int Font_Size = 10;
  112.  
  113. double Stoploss = 0; //StopLoss
  114. string Comment1 = "";
  115.  
  116. int Slipage = 50;
  117.  
  118.  
  119. double LastLayer_SL = 500;
  120.  
  121. bool SwitchSell = false;
  122. bool SwitchBuy = false;
  123.  
  124. int LastCountBuy = 0;
  125. int LastCountSell = 0;
  126.  
  127. datetime Trigtime = iTime(Symbol(),Period(),1);
  128.  
  129. double inSL = 0;
  130. double inTP = 0;
  131. double inPips = 0;
  132. double inLots = 0;
  133.  
  134. bool BuyStop;
  135. bool SellStop;
  136.  
  137. double Spread;
  138.  
  139. bool res3;
  140.  
  141. double BuyPrice = 0 ;
  142. double SellPrice = 0 ;
  143.  
  144. string ArrayLot10[10];
  145. string ArrayLot20[10];
  146. int NoLot10;
  147. int NoLot20;
  148.  
  149. string StartString;
  150. string StopString;
  151. datetime StartTime;
  152. datetime StopTime;
  153.  
  154.  
  155. double mPoint;
  156.  
  157. int StartHour;
  158. int StartMinute;
  159. int StopHour;
  160. int StopMinute;
  161.  
  162. datetime Start,Stop;
  163.  
  164. string sep = ",";
  165. ushort u_sep=StringGetCharacter(sep,0);
  166.  
  167. double Ask; // Get the Ask Price
  168.  
  169. double Bid; // Get the Bid Price
  170. datetime NewCandle;
  171.  
  172. double Floating;
  173.  
  174.  
  175. double inLotBuy;
  176. double inLotSell;
  177. //+----------------------------------------------+
  178. //+------------------------------------------------------------------+
  179. //| Expert initialization function                                   |
  180. //+------------------------------------------------------------------+
  181. int OnInit()
  182.   {
  183. //----
  184. //m_symbol.Name(Symbol());                  // sets symbol name
  185.    m_trade.SetExpertMagicNumber(MagicNo);      // sets magic number
  186.  
  187. //RectLabelCreate(0,"RectBox",0,10,30,550,180,clrBlack,BORDER_FLAT,CORNER_LEFT_UPPER,clrWhite,STYLE_SOLID,2,false);
  188.  
  189.    return(INIT_SUCCEEDED);
  190.   }
  191. //+------------------------------------------------------------------+
  192. //| Expert deinitialization function                                 |
  193. //+------------------------------------------------------------------+
  194. void OnDeinit(const int reason)
  195.   {
  196. //----
  197.  
  198. //----
  199.   }
  200. //+------------------------------------------------------------------+
  201. //| Expert tick function                                             |
  202. //+------------------------------------------------------------------+
  203. void OnTick()
  204.   {
  205.    MqlDateTime STime;
  206.    datetime time_current=TimeCurrent();
  207.    TimeToStruct(time_current,STime);
  208.  
  209.    double shortMAData1[];// Goes into Setup
  210.    double shortMAData2[];// Goes into Setup
  211. // Part 2: Prepare table for time series data; Creates control panel - this panel is used to determine the details of the indicator;
  212.    ArraySetAsSeries(shortMAData1,true);  // Goes into OnInit() // Ensures latest data is indexed as shift 0
  213.    ArraySetAsSeries(shortMAData2,true);  // Goes into OnInit() // Ensures latest data is indexed as shift 0
  214.    int shortMAControlPanel1 = iMA(Symbol(), TimeFrame,MA1_Period,0,MA1_Method,MA1_ApplyTo); // Getting the Control Panel/Handle for short MA
  215.    int shortMAControlPanel2 = iMA(Symbol(), TimeFrame,MA2_Period,0,MA2_Method,MA2_ApplyTo); // Getting the Control Panel/Handle for short MA
  216.  
  217. // Part 3: Collect required indicator data and store in data table
  218.    int numberOfShortMAData1 = CopyBuffer(shortMAControlPanel1, 0, 0, 20, shortMAData1); // Goes into OnTick() // Collects data from shift 0 to shift 19
  219.    int numberOfShortMAData2 = CopyBuffer(shortMAControlPanel2, 0, 0, 20, shortMAData2); // Goes into OnTick() // Collects data from shift 0 to shift 19
  220.  
  221. //Comment(STime.hour,";", STime.min);
  222. //Print("Time Current ",TimeToString(time_current,TIME_DATE|TIME_SECONDS)," day of week ",DayOfWeekDescription(STime.day_of_week));
  223.  
  224.    inLotBuy = Lots;
  225.    inLotSell = Lots;
  226. //----
  227. //Comment(CalculateProfit());
  228.  
  229.    if(shortMAData1[1] > shortMAData2[1])
  230.      {
  231.       BuyLayer = BuyUpLimit;
  232.       SellLayer = SellUpLimit;
  233.       Comment("Up");
  234.      }
  235.    if(shortMAData1[1] < shortMAData2[1])
  236.      {
  237.       BuyLayer = BuyDownLimit;
  238.       SellLayer = SellDownLimit;
  239.       Comment("Down");
  240.      }
  241.    Start = StringToTime((string)STime.year+"."+(string)STime.mon+"."+(string)STime.day+" "+TimeStart);
  242.    Stop = StringToTime((string)STime.year+"."+(string)STime.mon+"."+(string)STime.day+" "+TimeStop);
  243.  
  244. //Print("Magic: ", m_trade.RequestMagic());
  245. //Print("Sell Order: ", CalculateSellOrders());
  246.  
  247.    if(TimeCurrent() >= Start && TimeCurrent() < Stop)
  248.      {
  249.       //NewCandle = Time[0];
  250.       if(CalculateOrder(ORDER_TYPE_BUY) == 0)
  251.         {
  252.          Print("New Buy Order");
  253.          if(TakeProfit == 0)
  254.            {
  255.             inTP = 0;
  256.            }
  257.          else
  258.             inTP =  Ask() + TakeProfit*Point();
  259.  
  260.          if(StopLoss == 0 || Use_Martingle)
  261.            {
  262.             inSL = 0;
  263.            }
  264.          else
  265.             inSL =  Ask() - StopLoss*Point();
  266.  
  267.  
  268.          //res3 = OpenOrder(Symbol(),ORDER_TYPE_BUY,Lots, Ask,0,0,Comment1,MagicNo);
  269.          //res3=OrderSend(Symbol(),ORDER_TYPE_BUY,Lots,Ask,Slipage,inSL,inTP,Comment1,MagicNo,0,clrBlue);OrderSend()
  270.          res3 = m_trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,inLotBuy,Ask(),inSL,inTP,Comment1);// OrderOpen(Symbol(),ORDER_TYPE_BUY,Lots,)
  271.         }
  272.  
  273.       if(CalculateOrder(ORDER_TYPE_SELL)== 0)
  274.         {
  275.          Print("New Sell Order");
  276.          if(TakeProfit == 0)
  277.            {
  278.             inTP = 0;
  279.            }
  280.          else
  281.             inTP = Bid() - TakeProfit*Point();
  282.  
  283.          if(StopLoss == 0 || Use_Martingle)
  284.            {
  285.             inSL = 0;
  286.            }
  287.          else
  288.             inSL =  Bid() + StopLoss*Point();
  289.            
  290.          res3 = m_trade.PositionOpen(Symbol(),ORDER_TYPE_SELL,inLotSell,Bid(),inSL,inTP,Comment1);// res3 = OpenOrder(Symbol(),ORDER_TYPE_SELL,Lots, Bid,0,0,Comment1,MagicNo);
  291.          //res3=OrderSend(Symbol(),ORDER_TYPE_SELL,Lots,Bid,Slipage,inSL,inTP,Comment1,MagicNo,0,clrRed);
  292.         }
  293.  
  294.  
  295.       if(Use_Martingle)
  296.         {
  297.          if(CalculateOrder(ORDER_TYPE_BUY)  >= 1)
  298.            {
  299.             ManageBuy();
  300.            }
  301.          if(CalculateOrder(ORDER_TYPE_SELL)  >= 1)
  302.            {
  303.             ManageSell();
  304.            }
  305.         }
  306.      }
  307.  
  308.    if(Use_Trailing)
  309.      {
  310.       TrailingStop(TrailingStart,TrailingStep);
  311.      }
  312.    if(Use_BE)
  313.      {
  314.       StartBE(BE_Pips);
  315.      }
  316. //----
  317.   }
  318.  
  319.  
  320.  
  321.  
  322. //+------------------------------------------------------------------+
  323.  
  324. //+------------------------------------------------------------------+
  325. //|                                                                  |
  326. //+------------------------------------------------------------------+
  327. void DisplayText(string objname, string objtext, string fontname, int fontsize, int clr, int x, int y)
  328.   {
  329.    ObjectCreate(0,objname,OBJ_LABEL,0,0,0);
  330.  
  331.    ObjectSetString(0,objname,OBJPROP_TEXT,objtext);
  332.    ObjectSetInteger(0,objname,OBJPROP_FONTSIZE,fontsize);
  333.    ObjectSetString(0,objname,OBJPROP_FONT,fontname);
  334.    ObjectSetInteger(0,objname,OBJPROP_COLOR,clr);
  335.  
  336.    ObjectSetInteger(0,objname,OBJPROP_CORNER,0);
  337.    ObjectSetInteger(0,objname,OBJPROP_XDISTANCE,x);
  338.    ObjectSetInteger(0,objname,OBJPROP_YDISTANCE,y);
  339.    ObjectSetInteger(0,objname,OBJPROP_BACK,false);
  340.   }
  341.  
  342. //+------------------------------------------------------------------+
  343. //|                                                                  |
  344. //+------------------------------------------------------------------+
  345. void DisplayText2(string objname, string objtext, string fontname, int fontsize, int clr, int x, int y)
  346.   {
  347.    ObjectCreate(0,objname,OBJ_LABEL,0,0,0);
  348.  
  349.    ObjectSetString(0,objname,OBJPROP_TEXT,objtext);
  350.    ObjectSetInteger(0,objname,OBJPROP_FONTSIZE,fontsize);
  351.    ObjectSetString(0,objname,OBJPROP_FONT,fontname);
  352.    ObjectSetInteger(0,objname,OBJPROP_COLOR,clr);
  353.  
  354.    ObjectSetInteger(0,objname,OBJPROP_CORNER,0);
  355.    ObjectSetInteger(0,objname,OBJPROP_XDISTANCE,x);
  356.    ObjectSetInteger(0,objname,OBJPROP_YDISTANCE,y);
  357.    ObjectSetInteger(0,objname,OBJPROP_BACK,true);
  358.   }
  359.  
  360. //+------------------------------------------------------------------+
  361. bool RectLabelCreate(const long             chart_ID=0,               // chart's ID
  362.                      const string           name="RectLabel",         // label name
  363.                      const int              sub_window=0,             // subwindow index
  364.                      const int              x=0,                      // X coordinate
  365.                      const int              y=0,                      // Y coordinate
  366.                      const int              width=50,                 // width
  367.                      const int              height=18,                // height
  368.                      const color            back_clr=clrBlack,  // background color
  369.                      const ENUM_BORDER_TYPE border=BORDER_SUNKEN,     // border type
  370.                      const ENUM_BASE_CORNER corner=CORNER_LEFT_UPPER, // chart corner for anchoring
  371.                      const color            clr=clrRed,               // flat border color (Flat)
  372.                      const ENUM_LINE_STYLE  style=STYLE_SOLID,        // flat border style
  373.                      const int              line_width=1,             // flat border width
  374.                      const bool             back=false)               // in the background
  375.  
  376.   {
  377. //--- reset the error value
  378.    ResetLastError();
  379. //--- create a rectangle label
  380.    if(!ObjectCreate(chart_ID,name,OBJ_RECTANGLE_LABEL,sub_window,0,0))
  381.      {
  382.       Print(__FUNCTION__,
  383.             ": failed to create a rectangle label! Error code = ",GetLastError());
  384.       return(false);
  385.      }
  386. //--- set label coordinates
  387.    ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);
  388.    ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
  389. //--- set label size
  390.    ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width);
  391.    ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height);
  392. //--- set background color
  393.    ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,back_clr);
  394. //--- set border type
  395.    ObjectSetInteger(chart_ID,name,OBJPROP_BORDER_TYPE,border);
  396. //--- set the chart's corner, relative to which point coordinates are defined
  397.    ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);
  398. //--- set flat border color (in Flat mode)
  399.    ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
  400. //--- set flat border line style
  401.    ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);
  402. //--- set flat border width
  403.    ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,line_width);
  404. //--- display in the foreground (false) or background (true)
  405.    ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
  406.  
  407.  
  408.  
  409. //--- successful execution
  410.    return(true);
  411.   }
  412.  
  413. //+------------------------------------------------------------------+
  414. //+------------------------------------------------------------------+
  415. //+------------------------------------------------------------------+
  416. //| Create a text label                                              |
  417. //+------------------------------------------------------------------+
  418. bool LabelCreate(const long              chart_ID=0,               // chart's ID
  419.                  const string            name="Label",             // label name
  420.                  const int               sub_window=0,             // subwindow index
  421.                  const int               x=0,                      // X coordinate
  422.                  const int               y=0,                      // Y coordinate
  423.                  const ENUM_BASE_CORNER  corner=CORNER_LEFT_UPPER, // chart corner for anchoring
  424.                  const string            text="Label",             // text
  425.                  const string            font="Arial",             // font
  426.                  const int               font_size=10,             // font size
  427.                  const color             clr=clrRed,               // color
  428.                  const double            angle=0.0,                // text slope
  429.                  const ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT_UPPER, // anchor type
  430.                  const bool              back=false,               // in the background
  431.                  const bool              selection=false,          // highlight to move
  432.                  const bool              hidden=true,              // hidden in the object list
  433.                  const long              z_order=0)                // priority for mouse click
  434.   {
  435. //--- reset the error value
  436.    ResetLastError();
  437. //--- create a text label
  438.    if(!ObjectCreate(chart_ID,name,OBJ_LABEL,sub_window,0,0))
  439.      {
  440.       Print(__FUNCTION__,
  441.             ": failed to create text label! Error code = ",GetLastError());
  442.       return(false);
  443.      }
  444. //--- set label coordinates
  445.    ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);
  446.    ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
  447. //--- set the chart's corner, relative to which point coordinates are defined
  448.    ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);
  449. //--- set the text
  450.    ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
  451. //--- set text font
  452.    ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
  453. //--- set font size
  454.    ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
  455. //--- set the slope angle of the text
  456.    ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle);
  457. //--- set anchor type
  458.    ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);
  459. //--- set color
  460.    ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
  461. //--- display in the foreground (false) or background (true)
  462.    ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
  463. //--- enable (true) or disable (false) the mode of moving the label by mouse
  464.    ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
  465.    ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
  466. //--- hide (true) or display (false) graphical object name in the object list
  467.    ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
  468. //--- set the priority for receiving the event of a mouse click in the chart
  469.    ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
  470. //--- successful execution
  471.    return(true);
  472.   }
  473. //+------------------------------------------------------------------+
  474.  
  475.  
  476. //+------------------------------------------------------------------+
  477. //|                                                                  |
  478. //+------------------------------------------------------------------+
  479. //+------------------------------------------------------------------+
  480.  
  481. //+------------------------------------------------------------------+
  482. double CalculateLot(ENUM_ORDER_TYPE type)
  483.   {
  484.    double val = 0;
  485.    double _lot = 0;
  486.    double profit = 0, swap = 0, comm = 0;
  487.    for(int i=PositionsTotal()-1; i>=0; i--)
  488.      {
  489.       if(PositionGetTicket(i))
  490.          if(PositionGetString(POSITION_SYMBOL)==Symbol() &&
  491.             PositionGetInteger(POSITION_TYPE)==type &&
  492.             PositionGetInteger(POSITION_MAGIC) == MagicNo)
  493.            {
  494.             _lot = PositionGetDouble(POSITION_VOLUME);
  495.  
  496.             val += _lot;
  497.            }
  498.      }
  499.    return (NormalizeDouble(val,2));
  500.   }
  501. //+------------------------------------------------------------------+
  502. double CalculateProfit(ENUM_ORDER_TYPE type)
  503.   {
  504.    double val = 0;
  505.    double profit = 0, swap = 0, comm = 0;
  506.    for(int i=PositionsTotal()-1; i>=0; i--)
  507.      {
  508.       if(PositionGetTicket(i))
  509.          if(PositionGetString(POSITION_SYMBOL)==Symbol() &&
  510.             PositionGetInteger(POSITION_TYPE)==type &&
  511.             PositionGetInteger(POSITION_MAGIC) == MagicNo)
  512.            {
  513.             profit = PositionGetDouble(POSITION_PROFIT);
  514.             swap = PositionGetDouble(POSITION_SWAP);
  515.             comm = AccountInfoDouble(ACCOUNT_COMMISSION_BLOCKED);
  516.             val += profit + swap + comm;
  517.            }
  518.      }
  519.    return (NormalizeDouble(val,2));
  520.   }
  521.  
  522. //+------------------------------------------------------------------+
  523. //|                                                                  |
  524. //+------------------------------------------------------------------+
  525. double Bid()
  526.   {
  527.    return (SymbolInfoDouble(Symbol(), SYMBOL_BID));
  528.   }
  529.  
  530. //+------------------------------------------------------------------+
  531. //| Get current ask value                                            |
  532. //+------------------------------------------------------------------+
  533. double Ask()
  534.   {
  535.    return (SymbolInfoDouble(Symbol(), SYMBOL_ASK));
  536.   }
  537.  
  538. //+------------------------------------------------------------------+
  539. //|                                                                  |
  540. //+------------------------------------------------------------------+
  541. int CalculateOrder(ENUM_ORDER_TYPE type)
  542.   {
  543.    int count = 0;
  544.    for(int i=PositionsTotal()-1; i>=0; i--)
  545.       if(PositionGetTicket(i))
  546.          if(PositionGetString(POSITION_SYMBOL)==Symbol())
  547.             if(PositionGetInteger(POSITION_TYPE)==type && PositionGetInteger(POSITION_MAGIC) == MagicNo)
  548.               {
  549.                count++;
  550.               }
  551. //---
  552.    return(count);
  553.   }
  554. //+------------------------------------------------------------------+
  555.  
  556. //====================================================//
  557. //++++++++++++++++Marti Buy++++++++++++++++++++++++//
  558. //====================================================//
  559. void ManageBuy()
  560.   {
  561.    datetime lasttradetime = 0;
  562.    datetime firsttradetime = 0;
  563.    double lastopenprice=0;
  564.    double maxlots = 0;
  565.    double lasttp=0;
  566.    ENUM_ORDER_TYPE lastordertype=-1;
  567.    long lastorderticket=0;
  568.    int y=0;
  569.    int Layer = 0;
  570.    int LayerPip = 0;
  571.    int res = 0;
  572.    bool tix;
  573.    double lot2= 0;
  574.    double lastlot = 0;
  575.    double firstlot = 0;
  576.  
  577.  
  578.    Layer  = CalculateOrder(ORDER_TYPE_BUY);
  579.  
  580.  
  581.    for(int i=PositionsTotal()-1; i>=0; i--)
  582.      {
  583.       if(PositionGetTicket(i))
  584.          if(PositionGetString(POSITION_SYMBOL)==Symbol() &&
  585.             PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_BUY &&
  586.             PositionGetInteger(POSITION_MAGIC) == MagicNo)
  587.            {
  588.             if(lastopenprice == 0)
  589.               {
  590.                lasttradetime = (datetime)PositionGetInteger(POSITION_TIME);
  591.                lastopenprice = PositionGetDouble(POSITION_PRICE_OPEN);
  592.                lastordertype=(ENUM_ORDER_TYPE)PositionGetInteger(POSITION_TYPE);
  593.                lastorderticket=PositionGetInteger(POSITION_TICKET);
  594.                lasttp=PositionGetDouble(POSITION_PROFIT);
  595.                lastlot = PositionGetDouble(POSITION_VOLUME);
  596.                firsttradetime = (datetime)PositionGetInteger(POSITION_TIME);
  597.                firstlot = PositionGetDouble(POSITION_VOLUME);
  598.  
  599.               }
  600.             if(lasttradetime < (datetime)PositionGetInteger(POSITION_TIME) && lastopenprice != 0)
  601.               {
  602.                lasttradetime = (datetime)PositionGetInteger(POSITION_TIME);
  603.                lastopenprice = PositionGetDouble(POSITION_PRICE_OPEN);
  604.                lastordertype= (ENUM_ORDER_TYPE)PositionGetInteger(POSITION_TYPE);
  605.                lastorderticket=PositionGetInteger(POSITION_TICKET);
  606.                lasttp=PositionGetDouble(POSITION_PROFIT);
  607.                lastlot = PositionGetDouble(POSITION_VOLUME);
  608.               }
  609.             if(firsttradetime > (datetime)PositionGetInteger(POSITION_TIME) && firstlot != 0)
  610.               {
  611.                firsttradetime = (datetime)PositionGetInteger(POSITION_TIME);
  612.                firstlot = PositionGetDouble(POSITION_VOLUME);
  613.               }
  614.            }
  615.      }
  616.  
  617.    if(Martingle_Type == 1)
  618.      {
  619.       lot2 = NormalizeDouble(firstlot*MathPow(Multiplier,Layer),2);
  620.      }
  621.  
  622.    if(Martingle_Type == 2)
  623.      {
  624.       if(MathMod(Layer,Increament_Layer) == 0)
  625.         {
  626.          lot2 = NormalizeDouble(lastlot+Increament_Lot,2);
  627.         }
  628.       else
  629.          lot2 = lastlot;
  630.      }
  631.  
  632.    inPips = PipStep;
  633.  
  634.  
  635.    inSL = 0;
  636.  
  637. //Print("test");
  638.    if(lastopenprice - Ask() >= inPips*_Point && Layer < BuyLayer)
  639.      {
  640.  
  641.       //Print("Order No.", Layer+1,";Layer: ", Layer,"; Lot: ", DoubleToString(lot2,2), "; TakeProfit: ", DoubleToString(inTP,Digits()), "; Stoploss: ", DoubleToString(inSL,Digits()), "; lastopenprice: ", DoubleToString(lastopenprice,Digits()), "; Ask: ", DoubleToString(Ask(),Digits()));
  642.       tix = m_trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,lot2,Ask(),0,0,Comment1);
  643.  
  644.       inTP = CalculateTP(ORDER_TYPE_BUY) + TakeProfit*_Point;
  645.  
  646.       Print("Ave TP:", DoubleToString(CalculateTP(ORDER_TYPE_BUY),Digits()), "; TP: ", DoubleToString(inTP,Digits()));
  647.  
  648.       if(tix)
  649.         {
  650.          ModTPSL(inTP,inSL, ORDER_TYPE_BUY);
  651.         }
  652.      }
  653.   }
  654. //====================================================//
  655. //++++++++++++++++Marti Sell++++++++++++++++++++++++//
  656. //====================================================//
  657. void ManageSell()
  658.   {
  659.    datetime lasttradetime = 0;
  660.    datetime firsttradetime = 0;
  661.    double lastopenprice=0;
  662.    double maxlots = 0;
  663.    double lasttp=0;
  664.    ENUM_ORDER_TYPE lastordertype=-1;
  665.    long lastorderticket=0;
  666.    int y=0;
  667.    int Layer = 0;
  668.    int LayerPip = 0;
  669.    int res = 0;
  670.    bool tix;
  671.    double lot2= 0;
  672.    double lastlot = 0;
  673.    double firstlot = 0;
  674.  
  675.    Layer  = CalculateOrder(ORDER_TYPE_SELL);
  676.  
  677.    for(int i=PositionsTotal()-1; i>=0; i--)
  678.      {
  679.       if(PositionGetTicket(i))
  680.          if(PositionGetString(POSITION_SYMBOL)==Symbol() &&
  681.             PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_SELL &&
  682.             PositionGetInteger(POSITION_MAGIC) == MagicNo)
  683.            {
  684.             if(lastopenprice == 0)
  685.               {
  686.                lasttradetime = (datetime)PositionGetInteger(POSITION_TIME);
  687.                lastopenprice = PositionGetDouble(POSITION_PRICE_OPEN);
  688.                lastordertype=(ENUM_ORDER_TYPE)PositionGetInteger(POSITION_TYPE);
  689.                lastorderticket=PositionGetInteger(POSITION_TICKET);
  690.                lasttp=PositionGetDouble(POSITION_PROFIT);
  691.                lastlot = PositionGetDouble(POSITION_VOLUME);
  692.                firsttradetime = (datetime)PositionGetInteger(POSITION_TIME);
  693.                firstlot = PositionGetDouble(POSITION_VOLUME);
  694.               }
  695.             if(lasttradetime < (datetime)PositionGetInteger(POSITION_TIME) && lastopenprice != 0)
  696.               {
  697.                lasttradetime = (datetime)PositionGetInteger(POSITION_TIME);
  698.                lastopenprice = PositionGetDouble(POSITION_PRICE_OPEN);
  699.                lastordertype=(ENUM_ORDER_TYPE)PositionGetInteger(POSITION_TYPE);
  700.                lastorderticket=PositionGetInteger(POSITION_TICKET);
  701.                lasttp=PositionGetDouble(POSITION_PROFIT);
  702.                lastlot = PositionGetDouble(POSITION_VOLUME);
  703.               }
  704.             if(firsttradetime > (datetime)PositionGetInteger(POSITION_TIME) && firstlot != 0)
  705.               {
  706.                firsttradetime = (datetime)PositionGetInteger(POSITION_TIME);
  707.                firstlot = PositionGetDouble(POSITION_VOLUME);
  708.               }
  709.            }
  710.      }
  711.  
  712.    if(Martingle_Type == 1)
  713.      {
  714.       lot2 = NormalizeDouble(firstlot*MathPow(Multiplier,Layer),2);
  715.      }
  716.  
  717.    if(Martingle_Type == 2)
  718.      {
  719.       if(MathMod(Layer,Increament_Layer) == 0)
  720.         {
  721.          lot2 = NormalizeDouble(lastlot+Increament_Lot,2);
  722.         }
  723.       else
  724.          lot2 = lastlot;
  725.      }
  726.    inPips = PipStep;
  727.  
  728.  
  729.    inSL = 0;
  730.  
  731.    if(Bid() - lastopenprice >= inPips*_Point && Layer < SellLayer)
  732.      {
  733.       //Print("Order No.", Layer+1,";Layer: ", Layer,"; Lot: ", DoubleToStr(lot2,2), "; TakeProfit: ", DoubleToStr(inTP,Digits), "; Stoploss: ", DoubleToStr(inSL,Digits));
  734.  
  735.       //Print("Bid() - lastopenprice: ", DoubleToString(Bid() - lastopenprice,Digits()), "; inPips: ",DoubleToString(inPips*_Point,Digits()));
  736.       //Print("Order No.", Layer+1,";Layer: ", Layer,"; Lot: ", DoubleToString(lot2,2), "; TakeProfit: ", DoubleToString(inTP,Digits()), "; Stoploss: ", DoubleToString(inSL,Digits()), "; lastopenprice: ", DoubleToString(lastopenprice,Digits()), "; Bid: ", DoubleToString(Bid(),Digits()));
  737.  
  738.       //tix = OrderSend(Symbol(),OP_SELL,lot2,Bid,50,0,0,Comment1,MagicNo,0,clrOrange);
  739.       tix = m_trade.PositionOpen(Symbol(),ORDER_TYPE_SELL,lot2,Bid(),0,0,Comment1);
  740.       inTP = CalculateTP(ORDER_TYPE_SELL) - TakeProfit*_Point;
  741.  
  742.       Print("Ave TP:", DoubleToString(CalculateTP(ORDER_TYPE_SELL),Digits()), "; TP: ", DoubleToString(inTP,Digits()));
  743.  
  744.       if(tix)
  745.         {
  746.          ModTPSL(inTP,inSL, ORDER_TYPE_SELL);
  747.         }
  748.  
  749.      }
  750.   }
  751. //+------------------------------------------------------------------+
  752. double CalculateTP(ENUM_ORDER_TYPE _OrderType)
  753.   {
  754.    double profitpips = 0;
  755.    double totallots = 0;
  756.    double NewTP = 0;
  757.    int result = 0;
  758.    double OpenPrice =0;
  759.    double OrderLot = 0;
  760.  
  761.    for(int i=PositionsTotal()-1; i>=0; i--)
  762.      {
  763.       if(PositionGetTicket(i))
  764.          if(PositionGetString(POSITION_SYMBOL)==Symbol() &&
  765.             PositionGetInteger(POSITION_TYPE)==_OrderType &&
  766.             PositionGetInteger(POSITION_MAGIC) == MagicNo)
  767.            {
  768.             OpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
  769.             OrderLot = PositionGetDouble(POSITION_VOLUME);
  770.             profitpips += (OpenPrice*OrderLot);
  771.             totallots += OrderLot;
  772.            }
  773.      }
  774.  
  775.    if(totallots != 0)
  776.      {
  777.       NewTP =NormalizeDouble(profitpips  / totallots, _Digits);
  778.      }
  779.  
  780.    return (NewTP);
  781.   }
  782. //+------------------------------------------------------------------+
  783. void ModTPSL(double _TP,double _SL, ENUM_ORDER_TYPE _OrderType)
  784.   {
  785.    bool tix = false;
  786.  
  787.    for(int i=PositionsTotal()-1; i>=0; i--)
  788.      {
  789.       if(PositionGetTicket(i))
  790.          if(PositionGetString(POSITION_SYMBOL)==Symbol() &&
  791.             PositionGetInteger(POSITION_TYPE)==_OrderType &&
  792.             PositionGetInteger(POSITION_MAGIC) == MagicNo)
  793.            {
  794.             tix = m_trade.PositionModify(PositionGetTicket(i),_SL,_TP);
  795.  
  796.             while(!tix)
  797.               {
  798.                Print("Failed to modify TP/SL. Retry...");
  799.                tix = m_trade.PositionModify(PositionGetTicket(i),_SL,_TP);
  800.               }
  801.            }
  802.  
  803.      }
  804.   }
  805. //+------------------------------------------------------------------+
  806. //====================================================//
  807. //++++++++++++++++Trailing++++++++++++++++++++++++//
  808. //====================================================//
  809. void TrailingStop(double PipTrail,double TrailStep)
  810.   {
  811.    bool tix = false;
  812.  
  813.    for(int i=PositionsTotal()-1; i>=0; i--)
  814.      {
  815.       if(PositionGetTicket(i))
  816.          if(PositionGetString(POSITION_SYMBOL)==Symbol() &&
  817.             PositionGetInteger(POSITION_MAGIC) == MagicNo)
  818.            {
  819.  
  820.             if(PositionGetInteger(POSITION_TYPE)== ORDER_TYPE_BUY
  821.                && (Bid() >=  PositionGetDouble(POSITION_SL) + PipTrail*Point() + TrailStep*Point() || PositionGetDouble(POSITION_SL) < PositionGetDouble(POSITION_PRICE_OPEN) || PositionGetDouble(POSITION_SL) == 0) &&
  822.                Bid() - PipTrail*Point() >= PositionGetDouble(POSITION_PRICE_OPEN))
  823.               {
  824.  
  825.                tix = m_trade.PositionModify(PositionGetTicket(i),Bid() - PipTrail*Point(),PositionGetDouble(POSITION_TP));
  826.  
  827.                while(!tix)
  828.                  {
  829.                   Print("Failed to modify TP/SL. Retry...");
  830.                   tix = m_trade.PositionModify(PositionGetTicket(i),Bid() - PipTrail*Point(),PositionGetDouble(POSITION_TP));
  831.                  }
  832.  
  833.                //ModTPSL2(0,Bid - PipTrail*Point, OP_BUY);
  834.               }
  835.  
  836.             if(PositionGetInteger(POSITION_TYPE)== ORDER_TYPE_SELL
  837.                && (Ask() <=  PositionGetDouble(POSITION_SL) - PipTrail*Point() -  TrailStep*Point() || PositionGetDouble(POSITION_SL) > PositionGetDouble(POSITION_PRICE_OPEN) || PositionGetDouble(POSITION_SL) == 0) &&
  838.                Ask() + PipTrail*Point() <= PositionGetDouble(POSITION_PRICE_OPEN))
  839.               {
  840.                tix = m_trade.PositionModify(PositionGetTicket(i),Ask() + PipTrail*Point(),PositionGetDouble(POSITION_TP));
  841.  
  842.                while(!tix)
  843.                  {
  844.                   Print("Failed to modify TP/SL. Retry...");
  845.                   tix = m_trade.PositionModify(PositionGetTicket(i),Ask() + PipTrail*Point(),PositionGetDouble(POSITION_TP));
  846.                  }
  847.                //Print("Mod: ", OrderTicket());
  848.                //ModTPSL2(0,Bid + PipTrail*Point, OP_SELL);
  849.               }
  850.             //Sleep(1000);
  851.            }
  852.  
  853.  
  854.  
  855.      }
  856.  
  857.   }
  858.  
  859.   //====================================================//
  860. //++++++++++++++++Trailing++++++++++++++++++++++++//
  861. //====================================================//
  862. void StartBE(double PipTrail)
  863.   {
  864.    bool tix = false;
  865.  
  866.    for(int i=PositionsTotal()-1; i>=0; i--)
  867.      {
  868.       if(PositionGetTicket(i))
  869.          if(PositionGetString(POSITION_SYMBOL)==Symbol() &&
  870.             PositionGetInteger(POSITION_MAGIC) == MagicNo)
  871.            {
  872.  
  873.             if(PositionGetInteger(POSITION_TYPE)== ORDER_TYPE_BUY
  874.                && (PositionGetDouble(POSITION_SL) < PositionGetDouble(POSITION_PRICE_OPEN) || PositionGetDouble(POSITION_SL) == 0) &&
  875.                Bid() - PipTrail*Point() >= PositionGetDouble(POSITION_PRICE_OPEN))
  876.               {
  877.  
  878.                tix = m_trade.PositionModify(PositionGetTicket(i),PositionGetDouble(POSITION_PRICE_OPEN),PositionGetDouble(POSITION_TP));
  879.  
  880.                while(!tix)
  881.                  {
  882.                   Print("Failed to modify TP/SL. Retry...");
  883.                   tix = m_trade.PositionModify(PositionGetTicket(i),PositionGetDouble(POSITION_PRICE_OPEN),PositionGetDouble(POSITION_TP));
  884.                  }
  885.  
  886.                //ModTPSL2(0,Bid - PipTrail*Point, OP_BUY);
  887.               }
  888.  
  889.             if(PositionGetInteger(POSITION_TYPE)== ORDER_TYPE_SELL
  890.                && (PositionGetDouble(POSITION_SL) > PositionGetDouble(POSITION_PRICE_OPEN) || PositionGetDouble(POSITION_SL) == 0) &&
  891.                Ask() + PipTrail*Point() <= PositionGetDouble(POSITION_PRICE_OPEN))
  892.               {
  893.                tix = m_trade.PositionModify(PositionGetTicket(i),PositionGetDouble(POSITION_PRICE_OPEN),PositionGetDouble(POSITION_TP));
  894.  
  895.                while(!tix)
  896.                  {
  897.                   Print("Failed to modify TP/SL. Retry...");
  898.                   tix = m_trade.PositionModify(PositionGetTicket(i),PositionGetDouble(POSITION_PRICE_OPEN),PositionGetDouble(POSITION_TP));
  899.                  }
  900.                //Print("Mod: ", OrderTicket());
  901.                //ModTPSL2(0,Bid + PipTrail*Point, OP_SELL);
  902.               }
  903.             //Sleep(1000);
  904.            }
  905.  
  906.  
  907.  
  908.      }
  909.  
  910.   }
  911. //+------------------------------------------------------------------+
  912.          
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement