include include string ExpertComment = "SELL"; int Slippage = 3; double StopLoss = 30; double TakeProfit = 90; bool MoneyManagement = true; double Lots = 0.1; double risk = 1.0; double myPoint; // this handles 5 digit pricing correctly int init() { myPoint = SetPoint(); } //+------------------------------------------------------------------+ //| Get number of lots for this trade | //| If MoneyManagement is true the function will return the risk | //| percent based on AccountBalance. Corrections are made for | //| Standard and Mini accounts | //| If MoneyManagement if false then the value of mLots is returned | //| This value is also corrected for the type of account. | //| A check is also made to make certain that the maximum lots | //| allowed by the broker is not exceeded | //+------------------------------------------------------------------+ double GetLots(bool mMoneyManagement, double mRisk, double mLots) { double lot, MinLot, MaxLot; MinLot = MarketInfo(Symbol(), MODE_MINLOT); MaxLot = MarketInfo(Symbol(), MODE_MAXLOT); if(mMoneyManagement) { lot=NormalizeDouble(MathFloor(AccountBalance()*mRisk/30000),1); // Check if mini or standard Accountif(MinLot < 1.0) { lot = MathFloor(lot*10)/10; if (lot < 0.1) lot = 0.1; } else { if (lot >= 1.0) lot = MathFloor(lot); else lot = 1.0; }
} else { lot = mLots; } if (lot > MaxLot) lot = MaxLot; return(lot); } //+------------------------------------------------------------------+ //| script | //+------------------------------------------------------------------+ int start() { double mStopLoss = Bid - StopLoss * myPoint; double mTakeProfit = Bid + TakeProfit * myPoint; double Lots = GetLots(MoneyManagement, risk, Lots); int ticket = OpenTrade(OP_SELL, Lots, Slippage, mStopLoss, mTakeProfit, ExpertComment, 255, CLR_NONE); if(ticket<0) { int error = GetLastError(); Print("Error = ", ErrorDescription(error)); return; } //---- OrderPrint(); 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 = mTakeProfit; TPprice = ValidTakeProfit(OP_BUY,Ask, TPprice); } STprice = 0; if (mStopLoss > 0) { STprice = mStopLoss; 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=mTakeProfit; TPprice = ValidTakeProfit(OP_SELL,Bid, TPprice);} STprice = 0; if (mStopLoss > 0) { STprice = mStopLoss; 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); } //+------------------------------------------------------------------+