Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //+------------------------------------------------------------------+
- //| ProjectName |
- //| Copyright 2020, CompanyName |
- //| http://www.companyname.net |
- //+------------------------------------------------------------------+
- #define SERIES(name,type) \
- class C##name \
- { \
- public: \
- type operator[](const int i){return i##name(NULL,0,i);} \
- }name;
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- SERIES(Open,double)
- SERIES(Low,double)
- SERIES(High,double)
- SERIES(Close,double)
- SERIES(Time,datetime)
- SERIES(Volume,long)
- #include <Trade\Trade.mqh> //Instatiate Trades Execution Library
- #include <Trade\OrderInfo.mqh> //Instatiate Library for Orders Information
- #include <Trade\PositionInfo.mqh> //Instatiate Library for Positions Information
- #include <Trade\SymbolInfo.mqh>
- #include <Trade\AccountInfo.mqh>
- //---
- CTrade m_trade; // Trades Info and Executions library
- COrderInfo m_order; //Library for Orders information
- CPositionInfo m_position; // Library for all position features and information
- CSymbolInfo m_symbol; // symbol info object
- CAccountInfo m_account; // account info wrapper
- enum MartiType
- {
- Multi = 1, // Multiplier
- Increament = 2, // Increament
- };
- enum TypeEntry
- {
- BuyOnly = 0, // Buy Only
- SellOnly = 1, // Sell Only
- Buy_Sell = 2, // Buy & Sell
- };
- input int MagicNo = 111;
- input ENUM_TIMEFRAMES TimeFrame = PERIOD_H1;
- input TypeEntry Entry_Mode = Buy_Sell;
- input double Lots=0.01;
- input double TakeProfit = 100;
- input double StopLoss = 100;
- input bool Use_Martingle = true;
- input double PipStep = 200;
- input MartiType Martingle_Type = Multi;
- input double Multiplier = 1.5;
- input double Increament_Lot = 0.01;
- input int Increament_Layer = 10;
- input int BuyUpLimit = 1000;
- input int SellUpLimit = 20;
- input int BuyDownLimit = 20;
- input int SellDownLimit = 1000;
- input string BETS_Desc = "----BE & TS settings----";
- input bool Use_Trailing = true;
- input double TrailingStart = 100;
- input double TrailingStep = 10;
- input bool Use_BE = true;
- input double BE_Pips = 100;
- input string MA1_Setting = "----MA1----";
- input int MA1_Period = 20;
- input ENUM_MA_METHOD MA1_Method = 3;
- input ENUM_APPLIED_PRICE MA1_ApplyTo = 2;
- input string MA2_Setting = "----MA1----";
- input int MA2_Period = 200;
- input ENUM_MA_METHOD MA2_Method = 3;
- input ENUM_APPLIED_PRICE MA2_ApplyTo = 2;
- input string MM_Desc = "----Money Management----";
- input double MaxDrawdown_USD=100.0;
- input double MinEquityStart = 100;
- input string TimeStart = "00:00";
- input string TimeStop = "23:59";
- double MonDD;
- double TueDD;
- double WedDD;
- double ThuDD;
- double FriDD;
- double SunDD;
- double MonProfit;
- double TueProfit;
- double WedProfit;
- double ThuProfit;
- double FriProfit;
- double SunProfit;
- int MaxTrade = 50;
- double MonLot;
- double TueLot;
- double WedLot;
- double ThuLot;
- double FriLot;
- double SunLot;
- int BuyLayer;
- int SellLayer;
- datetime NewDay;
- int Font_Size = 10;
- double Stoploss = 0; //StopLoss
- string Comment1 = "";
- int Slipage = 50;
- double LastLayer_SL = 500;
- bool SwitchSell = false;
- bool SwitchBuy = false;
- int LastCountBuy = 0;
- int LastCountSell = 0;
- datetime Trigtime = iTime(Symbol(),Period(),1);
- double inSL = 0;
- double inTP = 0;
- double inPips = 0;
- double inLots = 0;
- bool BuyStop;
- bool SellStop;
- double Spread;
- bool res3;
- double BuyPrice = 0 ;
- double SellPrice = 0 ;
- string ArrayLot10[10];
- string ArrayLot20[10];
- int NoLot10;
- int NoLot20;
- string StartString;
- string StopString;
- datetime StartTime;
- datetime StopTime;
- double mPoint;
- int StartHour;
- int StartMinute;
- int StopHour;
- int StopMinute;
- datetime Start,Stop;
- string sep = ",";
- ushort u_sep=StringGetCharacter(sep,0);
- double Ask; // Get the Ask Price
- double Bid; // Get the Bid Price
- datetime NewCandle;
- double Floating;
- double inLotBuy;
- double inLotSell;
- //+----------------------------------------------+
- //+------------------------------------------------------------------+
- //| Expert initialization function |
- //+------------------------------------------------------------------+
- int OnInit()
- {
- //----
- //m_symbol.Name(Symbol()); // sets symbol name
- m_trade.SetExpertMagicNumber(MagicNo); // sets magic number
- //RectLabelCreate(0,"RectBox",0,10,30,550,180,clrBlack,BORDER_FLAT,CORNER_LEFT_UPPER,clrWhite,STYLE_SOLID,2,false);
- return(INIT_SUCCEEDED);
- }
- //+------------------------------------------------------------------+
- //| Expert deinitialization function |
- //+------------------------------------------------------------------+
- void OnDeinit(const int reason)
- {
- //----
- //----
- }
- //+------------------------------------------------------------------+
- //| Expert tick function |
- //+------------------------------------------------------------------+
- void OnTick()
- {
- MqlDateTime STime;
- datetime time_current=TimeCurrent();
- TimeToStruct(time_current,STime);
- double shortMAData1[];// Goes into Setup
- double shortMAData2[];// Goes into Setup
- // Part 2: Prepare table for time series data; Creates control panel - this panel is used to determine the details of the indicator;
- ArraySetAsSeries(shortMAData1,true); // Goes into OnInit() // Ensures latest data is indexed as shift 0
- ArraySetAsSeries(shortMAData2,true); // Goes into OnInit() // Ensures latest data is indexed as shift 0
- int shortMAControlPanel1 = iMA(Symbol(), TimeFrame,MA1_Period,0,MA1_Method,MA1_ApplyTo); // Getting the Control Panel/Handle for short MA
- int shortMAControlPanel2 = iMA(Symbol(), TimeFrame,MA2_Period,0,MA2_Method,MA2_ApplyTo); // Getting the Control Panel/Handle for short MA
- // Part 3: Collect required indicator data and store in data table
- int numberOfShortMAData1 = CopyBuffer(shortMAControlPanel1, 0, 0, 20, shortMAData1); // Goes into OnTick() // Collects data from shift 0 to shift 19
- int numberOfShortMAData2 = CopyBuffer(shortMAControlPanel2, 0, 0, 20, shortMAData2); // Goes into OnTick() // Collects data from shift 0 to shift 19
- //Comment(STime.hour,";", STime.min);
- //Print("Time Current ",TimeToString(time_current,TIME_DATE|TIME_SECONDS)," day of week ",DayOfWeekDescription(STime.day_of_week));
- inLotBuy = Lots;
- inLotSell = Lots;
- //----
- //Comment(CalculateProfit());
- if(shortMAData1[1] > shortMAData2[1])
- {
- BuyLayer = BuyUpLimit;
- SellLayer = SellUpLimit;
- Comment("Up");
- }
- if(shortMAData1[1] < shortMAData2[1])
- {
- BuyLayer = BuyDownLimit;
- SellLayer = SellDownLimit;
- Comment("Down");
- }
- Start = StringToTime((string)STime.year+"."+(string)STime.mon+"."+(string)STime.day+" "+TimeStart);
- Stop = StringToTime((string)STime.year+"."+(string)STime.mon+"."+(string)STime.day+" "+TimeStop);
- //Print("Magic: ", m_trade.RequestMagic());
- //Print("Sell Order: ", CalculateSellOrders());
- if(TimeCurrent() >= Start && TimeCurrent() < Stop)
- {
- //NewCandle = Time[0];
- if(CalculateOrder(ORDER_TYPE_BUY) == 0)
- {
- Print("New Buy Order");
- if(TakeProfit == 0)
- {
- inTP = 0;
- }
- else
- inTP = Ask() + TakeProfit*Point();
- if(StopLoss == 0 || Use_Martingle)
- {
- inSL = 0;
- }
- else
- inSL = Ask() - StopLoss*Point();
- //res3 = OpenOrder(Symbol(),ORDER_TYPE_BUY,Lots, Ask,0,0,Comment1,MagicNo);
- //res3=OrderSend(Symbol(),ORDER_TYPE_BUY,Lots,Ask,Slipage,inSL,inTP,Comment1,MagicNo,0,clrBlue);OrderSend()
- res3 = m_trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,inLotBuy,Ask(),inSL,inTP,Comment1);// OrderOpen(Symbol(),ORDER_TYPE_BUY,Lots,)
- }
- if(CalculateOrder(ORDER_TYPE_SELL)== 0)
- {
- Print("New Sell Order");
- if(TakeProfit == 0)
- {
- inTP = 0;
- }
- else
- inTP = Bid() - TakeProfit*Point();
- if(StopLoss == 0 || Use_Martingle)
- {
- inSL = 0;
- }
- else
- inSL = Bid() + StopLoss*Point();
- 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);
- //res3=OrderSend(Symbol(),ORDER_TYPE_SELL,Lots,Bid,Slipage,inSL,inTP,Comment1,MagicNo,0,clrRed);
- }
- if(Use_Martingle)
- {
- if(CalculateOrder(ORDER_TYPE_BUY) >= 1)
- {
- ManageBuy();
- }
- if(CalculateOrder(ORDER_TYPE_SELL) >= 1)
- {
- ManageSell();
- }
- }
- }
- if(Use_Trailing)
- {
- TrailingStop(TrailingStart,TrailingStep);
- }
- if(Use_BE)
- {
- StartBE(BE_Pips);
- }
- //----
- }
- //+------------------------------------------------------------------+
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- void DisplayText(string objname, string objtext, string fontname, int fontsize, int clr, int x, int y)
- {
- ObjectCreate(0,objname,OBJ_LABEL,0,0,0);
- ObjectSetString(0,objname,OBJPROP_TEXT,objtext);
- ObjectSetInteger(0,objname,OBJPROP_FONTSIZE,fontsize);
- ObjectSetString(0,objname,OBJPROP_FONT,fontname);
- ObjectSetInteger(0,objname,OBJPROP_COLOR,clr);
- ObjectSetInteger(0,objname,OBJPROP_CORNER,0);
- ObjectSetInteger(0,objname,OBJPROP_XDISTANCE,x);
- ObjectSetInteger(0,objname,OBJPROP_YDISTANCE,y);
- ObjectSetInteger(0,objname,OBJPROP_BACK,false);
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- void DisplayText2(string objname, string objtext, string fontname, int fontsize, int clr, int x, int y)
- {
- ObjectCreate(0,objname,OBJ_LABEL,0,0,0);
- ObjectSetString(0,objname,OBJPROP_TEXT,objtext);
- ObjectSetInteger(0,objname,OBJPROP_FONTSIZE,fontsize);
- ObjectSetString(0,objname,OBJPROP_FONT,fontname);
- ObjectSetInteger(0,objname,OBJPROP_COLOR,clr);
- ObjectSetInteger(0,objname,OBJPROP_CORNER,0);
- ObjectSetInteger(0,objname,OBJPROP_XDISTANCE,x);
- ObjectSetInteger(0,objname,OBJPROP_YDISTANCE,y);
- ObjectSetInteger(0,objname,OBJPROP_BACK,true);
- }
- //+------------------------------------------------------------------+
- bool RectLabelCreate(const long chart_ID=0, // chart's ID
- const string name="RectLabel", // label name
- const int sub_window=0, // subwindow index
- const int x=0, // X coordinate
- const int y=0, // Y coordinate
- const int width=50, // width
- const int height=18, // height
- const color back_clr=clrBlack, // background color
- const ENUM_BORDER_TYPE border=BORDER_SUNKEN, // border type
- const ENUM_BASE_CORNER corner=CORNER_LEFT_UPPER, // chart corner for anchoring
- const color clr=clrRed, // flat border color (Flat)
- const ENUM_LINE_STYLE style=STYLE_SOLID, // flat border style
- const int line_width=1, // flat border width
- const bool back=false) // in the background
- {
- //--- reset the error value
- ResetLastError();
- //--- create a rectangle label
- if(!ObjectCreate(chart_ID,name,OBJ_RECTANGLE_LABEL,sub_window,0,0))
- {
- Print(__FUNCTION__,
- ": failed to create a rectangle label! Error code = ",GetLastError());
- return(false);
- }
- //--- set label coordinates
- ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);
- ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
- //--- set label size
- ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width);
- ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height);
- //--- set background color
- ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,back_clr);
- //--- set border type
- ObjectSetInteger(chart_ID,name,OBJPROP_BORDER_TYPE,border);
- //--- set the chart's corner, relative to which point coordinates are defined
- ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);
- //--- set flat border color (in Flat mode)
- ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
- //--- set flat border line style
- ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);
- //--- set flat border width
- ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,line_width);
- //--- display in the foreground (false) or background (true)
- ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
- //--- successful execution
- return(true);
- }
- //+------------------------------------------------------------------+
- //+------------------------------------------------------------------+
- //+------------------------------------------------------------------+
- //| Create a text label |
- //+------------------------------------------------------------------+
- bool LabelCreate(const long chart_ID=0, // chart's ID
- const string name="Label", // label name
- const int sub_window=0, // subwindow index
- const int x=0, // X coordinate
- const int y=0, // Y coordinate
- const ENUM_BASE_CORNER corner=CORNER_LEFT_UPPER, // chart corner for anchoring
- const string text="Label", // text
- const string font="Arial", // font
- const int font_size=10, // font size
- const color clr=clrRed, // color
- const double angle=0.0, // text slope
- const ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT_UPPER, // anchor type
- const bool back=false, // in the background
- const bool selection=false, // highlight to move
- const bool hidden=true, // hidden in the object list
- const long z_order=0) // priority for mouse click
- {
- //--- reset the error value
- ResetLastError();
- //--- create a text label
- if(!ObjectCreate(chart_ID,name,OBJ_LABEL,sub_window,0,0))
- {
- Print(__FUNCTION__,
- ": failed to create text label! Error code = ",GetLastError());
- return(false);
- }
- //--- set label coordinates
- ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);
- ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
- //--- set the chart's corner, relative to which point coordinates are defined
- ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);
- //--- set the text
- ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
- //--- set text font
- ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
- //--- set font size
- ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
- //--- set the slope angle of the text
- ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle);
- //--- set anchor type
- ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);
- //--- set color
- ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
- //--- display in the foreground (false) or background (true)
- ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
- //--- enable (true) or disable (false) the mode of moving the label by mouse
- ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
- ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
- //--- hide (true) or display (false) graphical object name in the object list
- ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
- //--- set the priority for receiving the event of a mouse click in the chart
- ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
- //--- successful execution
- return(true);
- }
- //+------------------------------------------------------------------+
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- //+------------------------------------------------------------------+
- //+------------------------------------------------------------------+
- double CalculateLot(ENUM_ORDER_TYPE type)
- {
- double val = 0;
- double _lot = 0;
- double profit = 0, swap = 0, comm = 0;
- for(int i=PositionsTotal()-1; i>=0; i--)
- {
- if(PositionGetTicket(i))
- if(PositionGetString(POSITION_SYMBOL)==Symbol() &&
- PositionGetInteger(POSITION_TYPE)==type &&
- PositionGetInteger(POSITION_MAGIC) == MagicNo)
- {
- _lot = PositionGetDouble(POSITION_VOLUME);
- val += _lot;
- }
- }
- return (NormalizeDouble(val,2));
- }
- //+------------------------------------------------------------------+
- double CalculateProfit(ENUM_ORDER_TYPE type)
- {
- double val = 0;
- double profit = 0, swap = 0, comm = 0;
- for(int i=PositionsTotal()-1; i>=0; i--)
- {
- if(PositionGetTicket(i))
- if(PositionGetString(POSITION_SYMBOL)==Symbol() &&
- PositionGetInteger(POSITION_TYPE)==type &&
- PositionGetInteger(POSITION_MAGIC) == MagicNo)
- {
- profit = PositionGetDouble(POSITION_PROFIT);
- swap = PositionGetDouble(POSITION_SWAP);
- comm = AccountInfoDouble(ACCOUNT_COMMISSION_BLOCKED);
- val += profit + swap + comm;
- }
- }
- return (NormalizeDouble(val,2));
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- double Bid()
- {
- return (SymbolInfoDouble(Symbol(), SYMBOL_BID));
- }
- //+------------------------------------------------------------------+
- //| Get current ask value |
- //+------------------------------------------------------------------+
- double Ask()
- {
- return (SymbolInfoDouble(Symbol(), SYMBOL_ASK));
- }
- //+------------------------------------------------------------------+
- //| |
- //+------------------------------------------------------------------+
- int CalculateOrder(ENUM_ORDER_TYPE type)
- {
- int count = 0;
- for(int i=PositionsTotal()-1; i>=0; i--)
- if(PositionGetTicket(i))
- if(PositionGetString(POSITION_SYMBOL)==Symbol())
- if(PositionGetInteger(POSITION_TYPE)==type && PositionGetInteger(POSITION_MAGIC) == MagicNo)
- {
- count++;
- }
- //---
- return(count);
- }
- //+------------------------------------------------------------------+
- //====================================================//
- //++++++++++++++++Marti Buy++++++++++++++++++++++++//
- //====================================================//
- void ManageBuy()
- {
- datetime lasttradetime = 0;
- datetime firsttradetime = 0;
- double lastopenprice=0;
- double maxlots = 0;
- double lasttp=0;
- ENUM_ORDER_TYPE lastordertype=-1;
- long lastorderticket=0;
- int y=0;
- int Layer = 0;
- int LayerPip = 0;
- int res = 0;
- bool tix;
- double lot2= 0;
- double lastlot = 0;
- double firstlot = 0;
- Layer = CalculateOrder(ORDER_TYPE_BUY);
- for(int i=PositionsTotal()-1; i>=0; i--)
- {
- if(PositionGetTicket(i))
- if(PositionGetString(POSITION_SYMBOL)==Symbol() &&
- PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_BUY &&
- PositionGetInteger(POSITION_MAGIC) == MagicNo)
- {
- if(lastopenprice == 0)
- {
- lasttradetime = (datetime)PositionGetInteger(POSITION_TIME);
- lastopenprice = PositionGetDouble(POSITION_PRICE_OPEN);
- lastordertype=(ENUM_ORDER_TYPE)PositionGetInteger(POSITION_TYPE);
- lastorderticket=PositionGetInteger(POSITION_TICKET);
- lasttp=PositionGetDouble(POSITION_PROFIT);
- lastlot = PositionGetDouble(POSITION_VOLUME);
- firsttradetime = (datetime)PositionGetInteger(POSITION_TIME);
- firstlot = PositionGetDouble(POSITION_VOLUME);
- }
- if(lasttradetime < (datetime)PositionGetInteger(POSITION_TIME) && lastopenprice != 0)
- {
- lasttradetime = (datetime)PositionGetInteger(POSITION_TIME);
- lastopenprice = PositionGetDouble(POSITION_PRICE_OPEN);
- lastordertype= (ENUM_ORDER_TYPE)PositionGetInteger(POSITION_TYPE);
- lastorderticket=PositionGetInteger(POSITION_TICKET);
- lasttp=PositionGetDouble(POSITION_PROFIT);
- lastlot = PositionGetDouble(POSITION_VOLUME);
- }
- if(firsttradetime > (datetime)PositionGetInteger(POSITION_TIME) && firstlot != 0)
- {
- firsttradetime = (datetime)PositionGetInteger(POSITION_TIME);
- firstlot = PositionGetDouble(POSITION_VOLUME);
- }
- }
- }
- if(Martingle_Type == 1)
- {
- lot2 = NormalizeDouble(firstlot*MathPow(Multiplier,Layer),2);
- }
- if(Martingle_Type == 2)
- {
- if(MathMod(Layer,Increament_Layer) == 0)
- {
- lot2 = NormalizeDouble(lastlot+Increament_Lot,2);
- }
- else
- lot2 = lastlot;
- }
- inPips = PipStep;
- inSL = 0;
- //Print("test");
- if(lastopenprice - Ask() >= inPips*_Point && Layer < BuyLayer)
- {
- //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()));
- tix = m_trade.PositionOpen(Symbol(),ORDER_TYPE_BUY,lot2,Ask(),0,0,Comment1);
- inTP = CalculateTP(ORDER_TYPE_BUY) + TakeProfit*_Point;
- Print("Ave TP:", DoubleToString(CalculateTP(ORDER_TYPE_BUY),Digits()), "; TP: ", DoubleToString(inTP,Digits()));
- if(tix)
- {
- ModTPSL(inTP,inSL, ORDER_TYPE_BUY);
- }
- }
- }
- //====================================================//
- //++++++++++++++++Marti Sell++++++++++++++++++++++++//
- //====================================================//
- void ManageSell()
- {
- datetime lasttradetime = 0;
- datetime firsttradetime = 0;
- double lastopenprice=0;
- double maxlots = 0;
- double lasttp=0;
- ENUM_ORDER_TYPE lastordertype=-1;
- long lastorderticket=0;
- int y=0;
- int Layer = 0;
- int LayerPip = 0;
- int res = 0;
- bool tix;
- double lot2= 0;
- double lastlot = 0;
- double firstlot = 0;
- Layer = CalculateOrder(ORDER_TYPE_SELL);
- for(int i=PositionsTotal()-1; i>=0; i--)
- {
- if(PositionGetTicket(i))
- if(PositionGetString(POSITION_SYMBOL)==Symbol() &&
- PositionGetInteger(POSITION_TYPE)==ORDER_TYPE_SELL &&
- PositionGetInteger(POSITION_MAGIC) == MagicNo)
- {
- if(lastopenprice == 0)
- {
- lasttradetime = (datetime)PositionGetInteger(POSITION_TIME);
- lastopenprice = PositionGetDouble(POSITION_PRICE_OPEN);
- lastordertype=(ENUM_ORDER_TYPE)PositionGetInteger(POSITION_TYPE);
- lastorderticket=PositionGetInteger(POSITION_TICKET);
- lasttp=PositionGetDouble(POSITION_PROFIT);
- lastlot = PositionGetDouble(POSITION_VOLUME);
- firsttradetime = (datetime)PositionGetInteger(POSITION_TIME);
- firstlot = PositionGetDouble(POSITION_VOLUME);
- }
- if(lasttradetime < (datetime)PositionGetInteger(POSITION_TIME) && lastopenprice != 0)
- {
- lasttradetime = (datetime)PositionGetInteger(POSITION_TIME);
- lastopenprice = PositionGetDouble(POSITION_PRICE_OPEN);
- lastordertype=(ENUM_ORDER_TYPE)PositionGetInteger(POSITION_TYPE);
- lastorderticket=PositionGetInteger(POSITION_TICKET);
- lasttp=PositionGetDouble(POSITION_PROFIT);
- lastlot = PositionGetDouble(POSITION_VOLUME);
- }
- if(firsttradetime > (datetime)PositionGetInteger(POSITION_TIME) && firstlot != 0)
- {
- firsttradetime = (datetime)PositionGetInteger(POSITION_TIME);
- firstlot = PositionGetDouble(POSITION_VOLUME);
- }
- }
- }
- if(Martingle_Type == 1)
- {
- lot2 = NormalizeDouble(firstlot*MathPow(Multiplier,Layer),2);
- }
- if(Martingle_Type == 2)
- {
- if(MathMod(Layer,Increament_Layer) == 0)
- {
- lot2 = NormalizeDouble(lastlot+Increament_Lot,2);
- }
- else
- lot2 = lastlot;
- }
- inPips = PipStep;
- inSL = 0;
- if(Bid() - lastopenprice >= inPips*_Point && Layer < SellLayer)
- {
- //Print("Order No.", Layer+1,";Layer: ", Layer,"; Lot: ", DoubleToStr(lot2,2), "; TakeProfit: ", DoubleToStr(inTP,Digits), "; Stoploss: ", DoubleToStr(inSL,Digits));
- //Print("Bid() - lastopenprice: ", DoubleToString(Bid() - lastopenprice,Digits()), "; inPips: ",DoubleToString(inPips*_Point,Digits()));
- //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()));
- //tix = OrderSend(Symbol(),OP_SELL,lot2,Bid,50,0,0,Comment1,MagicNo,0,clrOrange);
- tix = m_trade.PositionOpen(Symbol(),ORDER_TYPE_SELL,lot2,Bid(),0,0,Comment1);
- inTP = CalculateTP(ORDER_TYPE_SELL) - TakeProfit*_Point;
- Print("Ave TP:", DoubleToString(CalculateTP(ORDER_TYPE_SELL),Digits()), "; TP: ", DoubleToString(inTP,Digits()));
- if(tix)
- {
- ModTPSL(inTP,inSL, ORDER_TYPE_SELL);
- }
- }
- }
- //+------------------------------------------------------------------+
- double CalculateTP(ENUM_ORDER_TYPE _OrderType)
- {
- double profitpips = 0;
- double totallots = 0;
- double NewTP = 0;
- int result = 0;
- double OpenPrice =0;
- double OrderLot = 0;
- for(int i=PositionsTotal()-1; i>=0; i--)
- {
- if(PositionGetTicket(i))
- if(PositionGetString(POSITION_SYMBOL)==Symbol() &&
- PositionGetInteger(POSITION_TYPE)==_OrderType &&
- PositionGetInteger(POSITION_MAGIC) == MagicNo)
- {
- OpenPrice = PositionGetDouble(POSITION_PRICE_OPEN);
- OrderLot = PositionGetDouble(POSITION_VOLUME);
- profitpips += (OpenPrice*OrderLot);
- totallots += OrderLot;
- }
- }
- if(totallots != 0)
- {
- NewTP =NormalizeDouble(profitpips / totallots, _Digits);
- }
- return (NewTP);
- }
- //+------------------------------------------------------------------+
- void ModTPSL(double _TP,double _SL, ENUM_ORDER_TYPE _OrderType)
- {
- bool tix = false;
- for(int i=PositionsTotal()-1; i>=0; i--)
- {
- if(PositionGetTicket(i))
- if(PositionGetString(POSITION_SYMBOL)==Symbol() &&
- PositionGetInteger(POSITION_TYPE)==_OrderType &&
- PositionGetInteger(POSITION_MAGIC) == MagicNo)
- {
- tix = m_trade.PositionModify(PositionGetTicket(i),_SL,_TP);
- while(!tix)
- {
- Print("Failed to modify TP/SL. Retry...");
- tix = m_trade.PositionModify(PositionGetTicket(i),_SL,_TP);
- }
- }
- }
- }
- //+------------------------------------------------------------------+
- //====================================================//
- //++++++++++++++++Trailing++++++++++++++++++++++++//
- //====================================================//
- void TrailingStop(double PipTrail,double TrailStep)
- {
- bool tix = false;
- for(int i=PositionsTotal()-1; i>=0; i--)
- {
- if(PositionGetTicket(i))
- if(PositionGetString(POSITION_SYMBOL)==Symbol() &&
- PositionGetInteger(POSITION_MAGIC) == MagicNo)
- {
- if(PositionGetInteger(POSITION_TYPE)== ORDER_TYPE_BUY
- && (Bid() >= PositionGetDouble(POSITION_SL) + PipTrail*Point() + TrailStep*Point() || PositionGetDouble(POSITION_SL) < PositionGetDouble(POSITION_PRICE_OPEN) || PositionGetDouble(POSITION_SL) == 0) &&
- Bid() - PipTrail*Point() >= PositionGetDouble(POSITION_PRICE_OPEN))
- {
- tix = m_trade.PositionModify(PositionGetTicket(i),Bid() - PipTrail*Point(),PositionGetDouble(POSITION_TP));
- while(!tix)
- {
- Print("Failed to modify TP/SL. Retry...");
- tix = m_trade.PositionModify(PositionGetTicket(i),Bid() - PipTrail*Point(),PositionGetDouble(POSITION_TP));
- }
- //ModTPSL2(0,Bid - PipTrail*Point, OP_BUY);
- }
- if(PositionGetInteger(POSITION_TYPE)== ORDER_TYPE_SELL
- && (Ask() <= PositionGetDouble(POSITION_SL) - PipTrail*Point() - TrailStep*Point() || PositionGetDouble(POSITION_SL) > PositionGetDouble(POSITION_PRICE_OPEN) || PositionGetDouble(POSITION_SL) == 0) &&
- Ask() + PipTrail*Point() <= PositionGetDouble(POSITION_PRICE_OPEN))
- {
- tix = m_trade.PositionModify(PositionGetTicket(i),Ask() + PipTrail*Point(),PositionGetDouble(POSITION_TP));
- while(!tix)
- {
- Print("Failed to modify TP/SL. Retry...");
- tix = m_trade.PositionModify(PositionGetTicket(i),Ask() + PipTrail*Point(),PositionGetDouble(POSITION_TP));
- }
- //Print("Mod: ", OrderTicket());
- //ModTPSL2(0,Bid + PipTrail*Point, OP_SELL);
- }
- //Sleep(1000);
- }
- }
- }
- //====================================================//
- //++++++++++++++++Trailing++++++++++++++++++++++++//
- //====================================================//
- void StartBE(double PipTrail)
- {
- bool tix = false;
- for(int i=PositionsTotal()-1; i>=0; i--)
- {
- if(PositionGetTicket(i))
- if(PositionGetString(POSITION_SYMBOL)==Symbol() &&
- PositionGetInteger(POSITION_MAGIC) == MagicNo)
- {
- if(PositionGetInteger(POSITION_TYPE)== ORDER_TYPE_BUY
- && (PositionGetDouble(POSITION_SL) < PositionGetDouble(POSITION_PRICE_OPEN) || PositionGetDouble(POSITION_SL) == 0) &&
- Bid() - PipTrail*Point() >= PositionGetDouble(POSITION_PRICE_OPEN))
- {
- tix = m_trade.PositionModify(PositionGetTicket(i),PositionGetDouble(POSITION_PRICE_OPEN),PositionGetDouble(POSITION_TP));
- while(!tix)
- {
- Print("Failed to modify TP/SL. Retry...");
- tix = m_trade.PositionModify(PositionGetTicket(i),PositionGetDouble(POSITION_PRICE_OPEN),PositionGetDouble(POSITION_TP));
- }
- //ModTPSL2(0,Bid - PipTrail*Point, OP_BUY);
- }
- if(PositionGetInteger(POSITION_TYPE)== ORDER_TYPE_SELL
- && (PositionGetDouble(POSITION_SL) > PositionGetDouble(POSITION_PRICE_OPEN) || PositionGetDouble(POSITION_SL) == 0) &&
- Ask() + PipTrail*Point() <= PositionGetDouble(POSITION_PRICE_OPEN))
- {
- tix = m_trade.PositionModify(PositionGetTicket(i),PositionGetDouble(POSITION_PRICE_OPEN),PositionGetDouble(POSITION_TP));
- while(!tix)
- {
- Print("Failed to modify TP/SL. Retry...");
- tix = m_trade.PositionModify(PositionGetTicket(i),PositionGetDouble(POSITION_PRICE_OPEN),PositionGetDouble(POSITION_TP));
- }
- //Print("Mod: ", OrderTicket());
- //ModTPSL2(0,Bid + PipTrail*Point, OP_SELL);
- }
- //Sleep(1000);
- }
- }
- }
- //+------------------------------------------------------------------+
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement