//+------------------------------------------------------------------+ //| OpenBasketScript.mq4 | //| Copyright © 2010, Robert Hill | //+------------------------------------------------------------------+ #property copyright "Copyright © 2010, Robert Hill" // allow script to accept inputs from user #property show_inputs #include #include //+----------------------------------------------------------------------+ //| To modify Lotsize of script change value listed here, right now | //| its set to 1.0 change that value if you desire different lotsize | //| I have found that it is important to keep the decimal in place, | //| but really dont know why yet, but fractional lots work too (ex... | //| 0.1, 0.3, 1.12), you get the picture. | //| AGAIN DONT FORGET TO SAVE AND COMPILE AFTER MAKING ANY MODIFICATIONS | //+----------------------------------------------------------------------+ extern string pair1 = "GBPUSD"; extern string order1 = "BUY"; extern double lots1 = 0.1; // change this number to change the lotsize extern double stoploss1 = 40; // change this number to change the stoploss extern double takeprofit1 = 40; // change this number to change the takeprofit extern string comment1 = "Multi1"; extern string pair2 = "EURUSD"; extern string order2 = "BUY"; extern double lots2 = 0.1; // change this number to change the lotsize extern double stoploss2 = 40; // change this number to change the stoploss extern double takeprofit2 = 40; // change this number to change the takeprofit extern string comment2 = "Multi2"; extern string pair3 = "USDCHF"; extern string order3 = "SELL"; extern double lots3 = 0.1; // change this number to change the lotsize extern double stoploss3 = 40; // change this number to change the stoploss extern double takeprofit3 = 40; // change this number to change the takeprofit extern string comment3 = "Multi3"; extern string ExpertComment = "Multi"; extern int Slippage = 3; //+---Don't forget to save and compile after changing the Lotsize----+ double myPoint; // this handles 5 digit pricing correctly int init() { } //+------------------------------------------------------------------+ //| Each of the following lines produces an individual order | //| 3 lines produces 3 orders, to increase or decrease the number | //| of individual orders either add, or take away identical lines | //| within the parenthesis. Again dont forget to save and compile | //| time you modify the code. | //+------------------------------------------------------------------+ int start() { OpenOneTrade(pair1, order1, lots1, stoploss1, takeprofit1, comment1); OpenOneTrade(pair2, order2, lots2, stoploss2, takeprofit2, comment2); OpenOneTrade(pair3, order3, lots3, stoploss3, takeprofit3, comment3); //---- return(0); } int OpenOneTrade(string mSymbol, string mSignal, double mLots, double mStoploss, double mTakeProfit, string mComment) { int cmd; if (mSignal == "BUY") cmd = OP_BUY; if (mSignal == "SELL") cmd = OP_SELL; int ticket = OpenTrade(mSymbol, cmd, mLots, Slippage, mStoploss, mTakeProfit, mComment, 0, CLR_NONE); if(ticket<0) { int error = GetLastError(); Print("Error for " + mSymbol + " = " + ErrorDescription(error)); } } int OpenTrade(string mySymbol, int signal, double mLots, int mSlippage, double mStopLoss, double mTakeProfit, string msg, int MagicNumber, color mColor) { int ticket, err; double TPprice,STprice, BuyPrice, SellPrice, OP; int myDigits; RefreshRates(); myPoint = SetPoint(mySymbol); myDigits = MarketInfo(mySymbol, MODE_DIGITS); BuyPrice = MarketInfo(mySymbol, MODE_ASK); SellPrice = MarketInfo(mySymbol, MODE_BID); if (signal==OP_BUY) { ticket=OrderSend(mySymbol,OP_BUY,mLots,BuyPrice,mSlippage,0,0,msg,MagicNumber,0,mColor); if (ticket > 0) { if (OrderSelect( ticket,SELECT_BY_TICKET, MODE_TRADES) ) { OP = OrderOpenPrice(); if (mStopLoss != 0 || mTakeProfit != 0) { TPprice = 0; if (mTakeProfit > 0) { TPprice = OP + mTakeProfit * myPoint; TPprice = ValidTakeProfit(mySymbol,OP_BUY,OP, TPprice); } STprice = 0; if (mStopLoss > 0) { STprice = OP - mStopLoss * myPoint; STprice = ValidStopLoss(mySymbol,OP_BUY,OP, STprice); } // Normalize stoploss / takeprofit to the proper # of digits. if (myDigits > 0) { STprice = NormalizeDouble( STprice, myDigits); TPprice = NormalizeDouble( TPprice, myDigits); } OrderModify(ticket, OP, STprice, TPprice, 0, mColor); } } } } else if (signal==OP_SELL) { ticket=OrderSend(Symbol(),OP_SELL,mLots,SellPrice,mSlippage,0,0,msg,MagicNumber,0,mColor); if (ticket > 0) { if (OrderSelect( ticket,SELECT_BY_TICKET, MODE_TRADES) ) { OP = OrderOpenPrice(); if (mStopLoss != 0 || mTakeProfit != 0) { TPprice = 0; if (mTakeProfit > 0) { TPprice = OP - mTakeProfit * myPoint; TPprice = ValidTakeProfit(mySymbol,OP_SELL,OP, TPprice); } STprice = 0; if (mStopLoss > 0) { STprice = OP + mStopLoss * myPoint; STprice = ValidStopLoss(mySymbol,OP_SELL,OP, STprice); } // Normalize stoploss / takeprofit to the proper # of digits. if (myDigits > 0) { STprice = NormalizeDouble( STprice, myDigits); TPprice = NormalizeDouble( TPprice, myDigits); } OrderModify(ticket, OP, STprice, TPprice, 0, mColor); } } } } return(ticket); } double ValidStopLoss(string mySymbol, int type, double price, double SL) { double newSL, temp; int myDigits; if (SL < 0.1) return(SL); temp = MarketInfo(mySymbol,MODE_STOPLEVEL) * myPoint; myDigits = MarketInfo(mySymbol, MODE_DIGITS); newSL = SL; if (type == OP_BUY) { if((price - newSL) < temp) newSL = price - temp; } if (type == OP_SELL) { if((newSL-price) < temp) newSL = price + temp; } newSL = NormalizeDouble(newSL,myDigits); return(newSL); } double ValidTakeProfit(string mySymbol, int type, double price, double TP) { double newTP, temp; int myDigits; if (TP < 0.1) return(TP); temp = MarketInfo(mySymbol, MODE_STOPLEVEL) * myPoint; myDigits = MarketInfo(mySymbol, MODE_DIGITS); newTP = TP; if (type == OP_BUY) { if((TP - price) < temp) newTP = price + temp; } if (type == OP_SELL) { if((price - TP) < temp) newTP = price - temp; } newTP = NormalizeDouble(newTP,myDigits); return(newTP); } double SetPoint(string mySymbol) { double mPoint; if (MarketInfo(mySymbol, MODE_DIGITS) < 4) mPoint = 0.01; else mPoint = 0.0001; return(mPoint); } //+------------------------------------------------------------------+