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

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

MQL4 email alert when certain conditions are met 4 replies

Indicator needed to email when conditions are met 0 replies

Help! - I need trailing Stop on every new order placed 4 replies

Programmer wanted for this template (need email, IM or sms alert upon conditions met) 1 reply

How to SendMail only once when conditions are met??? 14 replies

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
  • 1
Attachments: Need Help withTrailing Stop EA that closes half order at certian conditions met
Exit Attachments
Tags: Need Help withTrailing Stop EA that closes half order at certian conditions met
Cancel

Need Help withTrailing Stop EA that closes half order at certian conditions met

  • Post #1
  • Quote
  • First Post: Edited Jun 14, 2009 6:21pm Jun 12, 2009 9:49am | Edited Jun 14, 2009 6:21pm
  •  fxtrading24
  • | Joined Jun 2008 | Status: Member | 209 Posts
I will try to explain this the best I can.
I need an ea that will first close all orders at profit
second a trailing stop added
third a close orders at loss
Here seems to be the tricky part when the ea hits the trailing stop at whatever i input lets say 20 pips, now I want the option lets say the market goes the other direction against me. I want to be able to have the ea close a percentage of the order say at 10 pips. This way if it does go to 0 I at least get something out of the market.

I mainly scalp, and many times I set my trailing stop and it gets me out at zero this way I would gain something.
Thanks much in advance.

Thanks in advance, I think this would make a great trailing stop EA
It Is Not The Exit That Concerns Me, As It Is The Re-Entry
  • Post #2
  • Quote
  • Jun 18, 2009 12:34pm Jun 18, 2009 12:34pm
  •  fxtrading24
  • | Joined Jun 2008 | Status: Member | 209 Posts
I have enclosed an ea that maybe can be modified here is the details and the ea . I sure would appreciate some help.

I will try to explain this the best I can. I have enclosed an ea that maybe can be modified
I need an ea that will first close all orders at profit
second a trailing stop added
third a close orders at loss in pips
fourth read below I tried to explain the best I can. NOTE: this needs to all be in pips not dollars like 0.5 etc.
Here seems to be the tricky part when the ea hits the trailing stop at whatever i input lets say 20 pips, now I want the option lets say the market goes the other direction against me. I want to be able to have the ea close a percentage of the order say at 10 pips. This way if it does go to 0 I at least get something out of the market. This would be great for scalping for all of us. Maybe somebody can you look at this. I have enclosed a trailing ea maybe you can just add to it.
Thanks let me know what you think.
fxtrading24

So the inputs would be
All possessions
True or False
Profit Trailing True or False
Trailing stop
Profit Target
Trailing Step
Hard Stop

Then Close Partial oreder( This is when the trailing stop has been hit here you would add the amount of order to close if the trailing stop heads back to 0)
Close order amount This is when the trailing stop has been hit here you would add how many pips before it closes the partial order.

What I am getting at is lets say you go long eur/usd and you set your trailing stop at 10 ok so you hit the trailing stop and then the market reverses and lets say you want the ea to close half the order at 5 pips you still gain if it goes to 0.

Thanks much
fxtrading24
Attached File(s)
File Type: zip TrailingStopEA.zip   2 KB | 284 downloads
It Is Not The Exit That Concerns Me, As It Is The Re-Entry
 
 
  • Post #3
  • Quote
  • Last Post: Jun 19, 2009 2:38pm Jun 19, 2009 2:38pm
  •  fxtrading24
  • | Joined Jun 2008 | Status: Member | 209 Posts
Here is the source code I need modified like I explained in post 1 I sure would appreciate some help.
Thankd
fxtrading24

//+------------------------------------------------------------------+
//| TrailingStop.mq4 |
//|
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright Steve Wilson - Forex TSD Forum"
#property link "http://www.forex-tsd.com"

//+--------------------------------------------------------------------+
// This trailing stoploss is made of features found in other Experts. |
// 1. Trailing Stop Loss which is chart specific |
// 2. AllPositions - Trailing Stop Loss which is account specific |
// Attach to any chart and it will modify stop loss on all trades. |
// 3. ProfitTrailing - Modify only trades that are in profit |
// 4. Hedge Option - Does not open positions but will close all trades |
// if a specified target is reached on your trading account. |
// Activate it by giving a value to ProfitTarget. Disables stop |
// loss by setting value to 5000 |
//+--------------------------------------------------------------------+

extern bool AllPositions = false; // (True To modify all positions ) False(modify chart positions)
extern bool ProfitTrailing = True; //(To Trail only postions on profit not loss )
extern int TrailingStop=5; // Minimum is 5
extern int ProfitTarget=0; // Setting this value will calculate all open trades and close if profit reached.
extern int TrailingStep = 1;
extern bool UseSound = True;
extern string NameFileSound = "expert.wav";

bool runnable=true;
bool init=true;
bool result;
double pBid, pAsk, pp;
int i=0;

datetime timeprev=0;

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{

if (ProfitTarget>0)
{

TrailingStop=0;
ChartComment();
ClearStops();
CloseAllTrades();

}



if (AllPositions == false){
//+------------------------------------------------------------------+
//| Expert start function - Chart Specific |
//+------------------------------------------------------------------+

// Clean the chart
if (ProfitTarget==0)
CleanChart();


//Trailing Stop
TrailingAlls(TrailingStop);

//Close/Open
if(timeprev==Time[0])
return(0);
timeprev=Time[0];

return(0);
}
else
{
//+------------------------------------------------------------------+
//| Expert Start Function - All Positions |
//+------------------------------------------------------------------+

// Clean the chart
if (ProfitTarget==0)
CleanChart();

for (int i=0; i<OrdersTotal(); i++) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
TrailingPositions();
}
}
return(0);
}
}

void TrailingAlls(int trail)
{
if (trail==0)
return;

double stopcrnt;
double stopcal;

int trade;
int trades=OrdersTotal();

for(trade=0;trade<trades;trade++)
{
OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);

if(OrderSymbol()!=Symbol())
continue;

pp = MarketInfo(OrderSymbol(), MODE_POINT);


//Long

if(OrderType()==OP_BUY)
{

pBid = MarketInfo(OrderSymbol(), MODE_BID);
if (!ProfitTrailing || (pBid-OrderOpenPrice())>TrailingStop*pp) {
if (OrderStopLoss()<pBid-(TrailingStop+TrailingStep-1)*pp) {

stopcrnt=OrderStopLoss();
stopcal=Bid-(trail*Point);
if(stopcrnt==0)
{
OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Blue);
}
else
{
if(stopcal>stopcrnt)
{
OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Blue);
}
}
}

return;
}
}//Long

//Shrt
if(OrderType()==OP_SELL)
{


pAsk = MarketInfo(OrderSymbol(), MODE_ASK);
if (!ProfitTrailing || OrderOpenPrice()-pAsk>TrailingStop*pp) {
if (OrderStopLoss()>pAsk+(TrailingStop+TrailingStep-1)*pp || OrderStopLoss()==0) {

stopcrnt=OrderStopLoss();
stopcal=Ask+(trail*Point);
if(stopcrnt==0)
{
OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Red);
}
else
{
if(stopcal<stopcrnt)
{
OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Red);
}
}

}
return;
}
}//Shrt

}//for

}


//+------------------------------------------------------------------+
//| Functions for All Positions |
//+------------------------------------------------------------------+
void TrailingPositions() {
// double pBid, pAsk, pp;

pp = MarketInfo(OrderSymbol(), MODE_POINT);
if (OrderType()==OP_BUY) {
pBid = MarketInfo(OrderSymbol(), MODE_BID);
if (!ProfitTrailing || (pBid-OrderOpenPrice())>TrailingStop*pp) {
if (OrderStopLoss()<pBid-(TrailingStop+TrailingStep-1)*pp) {
ModifyStopLoss(pBid-TrailingStop*pp);
return;
}
}
}
if (OrderType()==OP_SELL) {
pAsk = MarketInfo(OrderSymbol(), MODE_ASK);
if (!ProfitTrailing || OrderOpenPrice()-pAsk>TrailingStop*pp) {
if (OrderStopLoss()>pAsk+(TrailingStop+TrailingStep-1)*pp || OrderStopLoss()==0) {
ModifyStopLoss(pAsk+TrailingStop*pp);
return;
}
}
}
}

//+------------------------------------------------------------------+
//| Modify StopLoss |
//+------------------------------------------------------------------+
void ModifyStopLoss(double ldStopLoss) {
bool fm;

fm=OrderModify(OrderTicket(),OrderOpenPrice(),ldStopLoss,OrderTakeProfit(),0,CLR_NONE);
if (fm && UseSound) PlaySound(NameFileSound);
}
//+------------------------------------------------------------------+




void ChartComment()
{
string sComment = "";
string sp = "****************************n";
string NL = "n";

sComment = NL + sp;
sComment = sComment + "Current Profits (USD) = $" + DoubleToStr(AccountProfit(),2) + NL;
sComment = sComment + "Profit Target (USD) = $" + ProfitTarget + NL;
sComment = sComment + "Open Trades = " + OrdersTotal() + NL;
sComment = sComment + "Stop Loss Disabled = ("+ TrailingStop+")" + NL;
sComment = sComment + NL + sp;

Comment(sComment);
}

void CleanChart()
{
string sComment = "";
string sp = "****************************n";
string NL = "n";

sComment = sComment + NL;
sComment = sComment + NL;
sComment = sComment + NL;
sComment = sComment + NL;
sComment = sComment + NL;
sComment = sComment + NL;
sComment = sComment + NL;
Comment(sComment);
}

// Close all open trades when in profit
void CloseAllTrades(){
if (AccountProfit() >= ProfitTarget){
for (i=OrdersTotal()-1;i>=0;i--){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
if (OrderType()==OP_SELL)result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),3,CLR_NONE);
if(result!=TRUE) Print("LastError = ", GetLastError());
if (OrderType()==OP_BUY)result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,CLR_NONE);
if(result!=TRUE) Print("LastError = ", GetLastError());
if (OrderType()==OP_BUYSTOP)result=OrderDelete(OrderTicket());
if(result!=TRUE) Print("LastError = ", GetLastError());
if (OrderType()==OP_SELLSTOP)result=OrderDelete(OrderTicket());
if(result!=TRUE) Print("LastError = ", GetLastError());

}
else Print( "Error when order select ", GetLastError());
}
}
}

// Function to clear all existing Stop Losses
void ClearStops(){
for (int i=0;i<OrdersTotal();i++){
if (OrderSelect(i,SELECT_BY_POS)){
OrderSelect(i,SELECT_BY_POS);
OrderModify(OrderTicket(),OrderOpenPrice(),0,OrderTakeProfit(),OrderExpiration(),Brown);
}
else Print( "Error when clearing Stops ", GetLastError());
}
return(0);

}
It Is Not The Exit That Concerns Me, As It Is The Re-Entry
 
 
  • Platform Tech
  • /
  • Need Help withTrailing Stop EA that closes half order at certian conditions met
  • 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