//+------------------------------------------------------------------+ //| trade.mq4 | //| Copyright © 2004, MetaQuotes Software Corp. | //| http://www.metaquotes.net/ | //+------------------------------------------------------------------+ #property copyright "Copyright © 2004, MetaQuotes Software Corp." #property link "http://www.metaquotes.net/" #include #include // allow script to accept inputs from user #property show_inputs //+----------------------------------------------------------------------+ //| 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 double Lots = 0.2; // change this number to change the lotsize extern double stoploss = 30; // change this number to change the stoploss extern double takeprofit = 20; // change this number to change the takeprofit extern string ExpertComment = "JMBUYER"; 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() { myPoint = SetPoint(); } //+------------------------------------------------------------------+ //| 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() { int ticket = OpenTrade(OP_BUY, Lots, Slippage, stoploss, takeprofit, ExpertComment, 0, CLR_NONE); if(ticket<0) { int error = GetLastError(); Print("Error = ", ErrorDescription(error)); return; } //---- return(0); } int OpenTrade(int signal, double mLots, int mSlippage, double mStopLoss, double mTakeProfit, string msg, int MagicNumber, color mColor) { int ticket, err; double TPprice,STprice; RefreshRates(); if (signal==OP_BUY) { ticket=OrderSend(Symbol(),OP_BUY,mLots,Ask,mSlippage,0,0,msg,MagicNumber,0,mColor); if (ticket > 0) { if (OrderSelect( ticket,SELECT_BY_TICKET, MODE_TRADES) ) { if (mStopLoss != 0 || mTakeProfit != 0) { TPprice = 0; if (mTakeProfit > 0) { TPprice = OrderOpenPrice() + mTakeProfit * myPoint; TPprice = ValidTakeProfit(OP_BUY,Ask, TPprice); } STprice = 0; if (mStopLoss > 0) { STprice = OrderOpenPrice() - mStopLoss * myPoint; STprice = ValidStopLoss(OP_BUY,Bid, STprice); } // Normalize stoploss / takeprofit to the proper # of digits. if (Digits > 0) { STprice = NormalizeDouble( STprice, Digits); TPprice = NormalizeDouble( TPprice, Digits); } OrderModify(ticket, OrderOpenPrice(), STprice, TPprice, 0, mColor); } } } } else if (signal==OP_SELL) { ticket=OrderSend(Symbol(),OP_SELL,mLots,Bid,mSlippage,0,0,msg,MagicNumber,0,mColor); if (ticket > 0) { if (OrderSelect( ticket,SELECT_BY_TICKET, MODE_TRADES) ) { if (mStopLoss != 0 || mTakeProfit != 0) { TPprice = 0; if (mTakeProfit > 0) { TPprice=OrderOpenPrice() - mTakeProfit * myPoint; TPprice = ValidTakeProfit(OP_SELL,Bid, TPprice); } STprice = 0; if (mStopLoss > 0) { STprice = OrderOpenPrice() + mStopLoss * myPoint; STprice = ValidStopLoss(OP_SELL,Ask, STprice); } // Normalize stoploss / takeprofit to the proper # of digits. if (Digits > 0) { STprice = NormalizeDouble( STprice, Digits); TPprice = NormalizeDouble( TPprice, Digits); } OrderModify(ticket, OrderOpenPrice(), STprice, TPprice, 0, mColor); } } } } return(ticket); } double ValidStopLoss(int type, double price, double SL) { double newSL, temp; if (SL < 0.1) return(SL); temp = MarketInfo(Symbol(),MODE_STOPLEVEL) * myPoint; 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,Digits); return(newSL); } double ValidTakeProfit(int type, double price, double TP) { double newTP, temp; if (TP < 0.1) return(TP); temp = MarketInfo(Symbol(), MODE_STOPLEVEL) * myPoint; 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,Digits); return(newTP); } double SetPoint() { double mPoint; if (Digits < 4) mPoint = 0.01; else mPoint = 0.0001; return(mPoint); } //+------------------------------------------------------------------+