• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • User/Email: Password:
  • 4:57am
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 4:57am
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

Please add alert to Parabolic Sar 2TF - Please Help! 0 replies

Please help with MetaTrader alarm indicator 2 replies

please please help going mad 4 replies

Metatrader data feed Help Please 12 replies

Please..need help for this indicator for metatrader 12 replies

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
  • 1
Attachments: help please metatrader EA
Exit Attachments
Tags: help please metatrader EA
Cancel

help please metatrader EA

  • Post #1
  • Quote
  • First Post: Edited Sep 22, 2011 9:35am Sep 20, 2011 12:13pm | Edited Sep 22, 2011 9:35am
  •  pgtips
  • | Joined Nov 2007 | Status: Member | 162 Posts
Hi

Here below is metatrader expert I have been using. I am having trouble getting it to work right since I changed it to improve it.

I moved 'close half' to after the second SL level and now it just goes mad and closes immediately half each time then all within few seconds.

I cannot understand out why. It must be the logic I use for this.

Its purpose if that if trade goes against you, at - 15 pips set a SL to - 20 pips. This is level 1.

If it goes in your favour at + 20 move SL to BE + 2 AND close half the position. This would be level 2.

At + 30 pips move remaining position SL to + 25 pips - level 3

Then trail in steps.

Unfortunately what mine does is change order immediately, and close instantly the position then close it all, before I can blink.

Can someone help me as to what i have done wrong.

I wonder if someone could also show me code I can use so it manages all orders I have open, no magic nos etc, just apply to everything

thank you very much in advance

PG

//| e-Smart_Tralling |
//+--------------------------------------------------------+

extern bool UseOneAccount = true; // not sure how this applies to all orders
extern bool UseCloseOneHalf = true;
extern int LevelProfit1 = -130; // if price hit - 13
extern int LevelMoving1 = -200; // move SL to -20
extern int LevelProfit2 = 200; // if price +ve
extern int LevelMoving2 = 10; // move SL to + 10
extern int LevelProfit3 = 550;
extern int LevelMoving3 = 300;
extern int TrailingStop = 10;
extern int TrailingStep = 50;
extern int Slippage = 2;
extern bool ShowComment = true;
extern bool UseSound = true;
string var_132 = "expert.wav";

//+------------------------------------------------------------------+

void deinit()
{
Comment("");
}

//+------------------------------------------------------------------+

void start()
{
double point;
int digits;
string msg = "";

for (int i = 0; i < OrdersTotal(); i++)
{
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if (UseOneAccount || (OrderSymbol() == Symbol()))
{
ThreeLevelSystemOfOutput();
digits = MarketInfo(OrderSymbol(),MODE_DIGITS);
point = MarketInfo(OrderSymbol(),MODE_POINT);
msg = msg + OrderSymbol() + " Öåíà: " + DoubleToStr(OrderOpenPrice(),digits) + " SL = " + DoubleToStr(
OrderStopLoss(),digits) + " (" + StopLossInPoint() + ")\n";
}
}
}

if (ShowComment) Comment(msg);
}

//+------------------------------------------------------------------+

void ThreeLevelSystemOfOutput()
{
int profit = ProfitPosition();
int sl = StopLossInPoint();
int spread = MarketInfo(OrderSymbol(),MODE_SPREAD);

if ((profit > LevelProfit1) && (profit <= LevelProfit2) && (sl < LevelMoving1)) ModifyStopLossInPoint(LevelMoving1);

if ((profit > LevelProfit2) && (profit <= LevelProfit3) && (sl < LevelMoving2)) ModifyStopLossInPoint(LevelMoving2);

{
ModifyStopLossInPoint(LevelMoving1);
if (UseCloseOneHalf) CloseOneHalf();
}

if ((profit > LevelProfit3) && (sl < LevelMoving3)) ModifyStopLossInPoint(LevelMoving3);
if (profit > LevelMoving3 + TrailingStop + TrailingStep) TrailingPositions();
}

//+------------------------------------------------------------------+

void CloseOneHalf()
{
bool result = false;
double lots = MathCeil(OrderLots() / 2.0 * 10.0) / 10.0;

if (lots > 0.0)
{
if (OrderType() == OP_BUY)
{
result = OrderClose(OrderTicket(),lots,Bid,Slippage,CLR_NONE);
}
if (OrderType() == OP_SELL)
{
result = OrderClose(OrderTicket(),lots,Ask,Slippage,CLR_NONE);
}
if (result && UseSound) PlaySound(var_132);
}
}

//+------------------------------------------------------------------+

void TrailingPositions()
{
double bid;
double ask;
double point = MarketInfo(OrderSymbol(),MODE_POINT);

if (OrderType() == OP_BUY)
{
bid = MarketInfo(OrderSymbol(),MODE_BID);
if (bid - OrderOpenPrice() > TrailingStop * point)
{
if (OrderStopLoss() < bid - (TrailingStop + TrailingStep - 1) * point)
{
ModifyStopLoss(bid - TrailingStop * point);
return;
}
}
}

if (OrderType() == OP_SELL)
{
ask = MarketInfo(OrderSymbol(),MODE_ASK);
if (OrderOpenPrice() - ask > TrailingStop * point)
{
if ((OrderStopLoss() > ask + (TrailingStop + TrailingStep - 1) * point) || (OrderStopLoss() == 0))
{
ModifyStopLoss(ask + TrailingStop * point);
}
}
}
}

//+------------------------------------------------------------------+

void ModifyStopLoss(double sl)
{
bool result = OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,CLR_NONE);
if (result && UseSound) PlaySound(var_132);
}

//+------------------------------------------------------------------+

void ModifyStopLossInPoint(int stoploss)
{
bool result;
double sl = 0;
double point = MarketInfo(OrderSymbol(),MODE_POINT);

if (OrderType() == OP_BUY) sl = OrderOpenPrice() + stoploss * point;
if (OrderType() == OP_SELL) sl = OrderOpenPrice() - stoploss * point;

result = OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,CLR_NONE);
if (result && UseSound) PlaySound(var_132);
}

//+------------------------------------------------------------------+

int ProfitPosition()
{
double bid;
double ask;
double point = MarketInfo(OrderSymbol(),MODE_POINT);
double profit = 0;

if (OrderType() == OP_BUY)
{
bid = MarketInfo(OrderSymbol(),MODE_BID);
profit = (bid - OrderOpenPrice()) / point;
}
if (OrderType() == OP_SELL)
{
ask = MarketInfo(OrderSymbol(),MODE_ASK);
profit = (OrderOpenPrice() - ask) / point;
}
return(MathRound(profit));
}

//+------------------------------------------------------------------+

int StopLossInPoint()
{
double point = MarketInfo(OrderSymbol(),MODE_POINT);
double sl = 0;

if (OrderType() == OP_BUY) sl = (OrderStopLoss() - OrderOpenPrice()) / point;
if (OrderType() == OP_SELL) sl = (OrderOpenPrice() - OrderStopLoss()) / point;
if (OrderStopLoss() == 0.0) sl = -OrderOpenPrice() / point;
return(MathRound(sl));
}
Attached File(s)
File Type: mq4 eTrail.mq4   5 KB | 190 downloads
  • Post #2
  • Quote
  • Last Post: Edited 11:45am Sep 22, 2011 9:37am | Edited 11:45am
  •  pgtips
  • | Joined Nov 2007 | Status: Member | 162 Posts
Well I've had a go at this but can't test as market hardly moving right now.

If anyone could cast their eye over the code would appreciate any input, I've taken code from other EAs and tried to make it work, think it does to a point. Not sure how to overcome 5 digit broker, I can never get used to entering 200 pip SL which = 20 ;-)

Revised and hopefully will work


//+-----------------------------------------------------------------------------+
//| e-smart-trailing3.mq4 |
//| Copyright 2011, Kev |
//|Special Thanks |
//|to coder who wrote e-smart trailing TrailMe.mq4 whos codes I have integrated |
//+-----------------------------------------------------------------------------+

// this EA hard sets sl and tp levels if not manuall entered on original trade
// it then monitors trade, if tp level 1 is hit it will close out portion of trade
// then move sl on remianing amounts. Probably too many levels , but if so just set
// level 4 same + 1 pip as 3 then it won't hurt.

//+----------------------User interface-----------------------------------------+


#property copyright ""
#property link ""


extern string SL_TP_Trail_Options="--------------------------";
extern int StopLoss.Pips = 200; // Initial s/l. Used by CheckInitialSLTP if manual trade has no initial S/L.
extern int TakeProfit.Pips = 1500; // Initial take profit - also used by CheckInitialSLTPif manual trade has no T/P
extern bool UseOneAccount = true;
bool gi_80 = FALSE;
extern bool UseCloseOneThird = true;
extern int LevelProfit1 = 200;
extern int LevelMoving1 = 20;
extern int LevelProfit2 = 210;
extern int LevelMoving2 = 30;
extern int LevelProfit3 = 310;
extern int LevelMoving3 = 150;
extern int LevelProfit4 = 500;
extern int LevelMoving4 = 370;
extern int TrailingStop = 100;
extern int TrailingStep = 100;
extern int Slippage = 20;
extern bool ShowComment = false;
extern bool UseSound = TRUE;
string gs_132 = "button.wav";
string gs_133 = "tada.wav";



int b.ticket, s.ticket,slip, TodaysRange;
void deinit() {
Comment("");
}

void start() {
double ld_0;
int li_8;


PosCounter (); // check for open positions. Sets b.ticket, s.ticket

if (s.ticket>0 || b.ticket>0)
{ CheckInitialSLTP (); // If no s/l t/p is defined in your manual entry,
// then the defaults will be immediately entered
}



string ls_12 = "";
for (int li_20 = 0; li_20 < OrdersTotal(); li_20++) {
if (OrderSelect(li_20, SELECT_BY_POS, MODE_TRADES)) {
if (gi_80 || OrderSymbol() == Symbol()) {
ThreeLevelSystemOfOutput();
li_8 = MarketInfo(OrderSymbol(), MODE_DIGITS);
ld_0 = MarketInfo(OrderSymbol(), MODE_POINT);
ls_12 = ls_12 + OrderSymbol() + " Bought at: " + DoubleToStr(OrderOpenPrice(), li_8) + " SL = " + DoubleToStr(OrderStopLoss(), li_8) + " (" + StopLossInPoint() + ")\n";
}
}
}
if (ShowComment) Comment(ls_12);
}

void PosCounter()
{ b.ticket=0;s.ticket=0;
for (int cnt=0;cnt<=OrdersTotal();cnt++)
{ OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol()==Symbol())
{ if (OrderType()==OP_SELL)
{ s.ticket=OrderTicket();
}
if (OrderType()==OP_BUY)
{ b.ticket=OrderTicket();
}
}
}
}


void ThreeLevelSystemOfOutput() {
int li_0 = ProfitPosition();
int li_4 = StopLossInPoint();
int li_8 = MarketInfo(OrderSymbol(), MODE_SPREAD);
if (li_0 > LevelProfit1 && li_0 <= LevelProfit2 && li_4 < LevelMoving1) {
ModifyStopLossInPoint(LevelMoving1);
if (UseCloseOneThird) CloseOneThird();
}
if (li_0 > LevelProfit2 && li_0 <= LevelProfit3 && li_4 < LevelMoving2) ModifyStopLossInPoint(LevelMoving2);
if (li_0 > LevelProfit3 && li_4 < LevelMoving3) ModifyStopLossInPoint(LevelMoving3);
if (li_0 > LevelProfit4 && li_4 < LevelMoving4) ModifyStopLossInPoint(LevelMoving4);
if (li_0 > LevelMoving4 + TrailingStop + TrailingStep) TrailingPositions();
}

void CloseOneThird() {
bool l_ord_close_0 = FALSE;
double ld_4 = MathCeil(10.0 * (OrderLots() / 3.0)) / 10.0;
if (ld_4 > 0.0) {
if (OrderType() == OP_BUY) l_ord_close_0 = OrderClose(OrderTicket(), ld_4, Bid, Slippage, CLR_NONE);
if (OrderType() == OP_SELL) l_ord_close_0 = OrderClose(OrderTicket(), ld_4, Ask, Slippage, CLR_NONE);
if (l_ord_close_0 && UseSound) PlaySound(gs_133);
}
}

void TrailingPositions() {
double ld_0;
double ld_8;
double ld_16 = MarketInfo(OrderSymbol(), MODE_POINT);
if (OrderType() == OP_BUY) {
ld_0 = MarketInfo(OrderSymbol(), MODE_BID);
if (ld_0 - OrderOpenPrice() > TrailingStop * ld_16) {
if (OrderStopLoss() < ld_0 - (TrailingStop + TrailingStep - 1) * ld_16) {
ModifyStopLoss(ld_0 - TrailingStop * ld_16);
return;
}
}
}
if (OrderType() == OP_SELL) {
ld_8 = MarketInfo(OrderSymbol(), MODE_ASK);
if (OrderOpenPrice() - ld_8 > TrailingStop * ld_16)
if (OrderStopLoss() > ld_8 + (TrailingStop + TrailingStep - 1) * ld_16 || OrderStopLoss() == 0.0) ModifyStopLoss(ld_8 + TrailingStop * ld_16);
}
}

void ModifyStopLoss(double ad_0) {
int l_bool_8 = OrderModify(OrderTicket(), OrderOpenPrice(), ad_0, OrderTakeProfit(), 0, CLR_NONE);
if (l_bool_8 && UseSound) PlaySound(gs_132);
}

void ModifyStopLossInPoint(int ai_0) {
double ld_8 = 0;
double ld_16 = MarketInfo(OrderSymbol(), MODE_POINT);
if (OrderType() == OP_BUY) ld_8 = OrderOpenPrice() + ai_0 * ld_16;
if (OrderType() == OP_SELL) ld_8 = OrderOpenPrice() - ai_0 * ld_16;
int l_bool_4 = OrderModify(OrderTicket(), OrderOpenPrice(), ld_8, OrderTakeProfit(), 0, CLR_NONE);
if (l_bool_4 && UseSound) PlaySound(gs_132);
}

int ProfitPosition() {
double ld_0;
double ld_8;
double ld_16 = MarketInfo(OrderSymbol(), MODE_POINT);
double ld_24 = 0;
if (OrderType() == OP_BUY) {
ld_0 = MarketInfo(OrderSymbol(), MODE_BID);
ld_24 = (ld_0 - OrderOpenPrice()) / ld_16;
}
if (OrderType() == OP_SELL) {
ld_8 = MarketInfo(OrderSymbol(), MODE_ASK);
ld_24 = (OrderOpenPrice() - ld_8) / ld_16;
}
return (MathRound(ld_24));
}

int StopLossInPoint() {
double ld_0 = MarketInfo(OrderSymbol(), MODE_POINT);
double ld_8 = 0;
if (OrderType() == OP_BUY) ld_8 = (OrderStopLoss() - OrderOpenPrice()) / ld_0;
if (OrderType() == OP_SELL) ld_8 = (OrderOpenPrice() - OrderStopLoss()) / ld_0;
if (OrderStopLoss() == 0.0) ld_8 = (-OrderOpenPrice()) / ld_0;
return (MathRound(ld_8));
}

void CheckInitialSLTP()
{ int sl,tp;
if (b.ticket>0)
{ OrderSelect(b.ticket,SELECT_BY_TICKET);
if (OrderStopLoss()==0 || OrderTakeProfit()==0)
{ if (OrderStopLoss ()==0) {sl=StopLoss.Pips;}
if (OrderTakeProfit()==0) {tp=TakeProfit.Pips;}
if ((sl>0 && OrderStopLoss()==0) || (tp>0 && OrderTakeProfit()==0))
{ OrderModify(b.ticket, OrderOpenPrice(), OrderOpenPrice()-sl*Point,OrderOpenPrice()+tp*Point,OrderExpiration()
,MediumSpringGreen);
if (OrderSelect(b.ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("Initial SL or TP is Set for Long Entry");
else Print("Error setting initial SL or TP for Long Entry");
}
}
}
if (s.ticket > 0)
{ OrderSelect(s.ticket,SELECT_BY_TICKET);
if (OrderStopLoss()==0 || OrderTakeProfit()==0)
{ if (OrderStopLoss ()==0) {sl=StopLoss.Pips;}
if (OrderTakeProfit()==0) {tp=TakeProfit.Pips;}
if ((sl>0 && OrderStopLoss()==0) || (tp>0 && OrderTakeProfit()==0))
{ OrderModify(s.ticket, OrderOpenPrice(), OrderOpenPrice()+sl*Point,OrderOpenPrice()-tp*Point,OrderExpiration()
,MediumVioletRed);
if (OrderSelect(s.ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("Initial SL or TP is Set for Short Entry");
else Print("Error setting initial SL or TP for Short Entry");
}
}
}
}
 
 
  • Platform Tech
  • /
  • help please metatrader EA
  • Reply to Thread
0 traders viewing now
Top of Page
  • Facebook
  • Twitter
About FF
  • Mission
  • Products
  • User Guide
  • Media Kit
  • Blog
  • Contact
FF Products
  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer
FF Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow FF
  • Facebook
  • Twitter

FF Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Forex Factory® is a brand of Fair Economy, Inc.

Terms of Service / ©2023