Forex
Google
New signals service!

Go Back   Forex Trading > Discussion Areas > General Discussion


Register in Forex TSD!
Trading Systems Leaders in this forum (automated trading systems) are winning more than 3000 pips in a month (30000$ investing one lot every time).
Click here to register and get more information

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-20-2006, 11:01 PM
jpsdyb's Avatar
Member
 
Join Date: Dec 2005
Posts: 73
jpsdyb is on a distinguished road
How to creat equity trail stop

Looking for a way my EA can use a trailing stop based on equity for all trades via magic number EA.

I would like it to work something like this:
If equity profit = + 200, set equity stop (or equity trailing stop) at +100, target profit = +500.

So if I do not hit my profit target of 500 cause market retraced, at least I can guarantee that all trades will be closed out with +100 to my equity/balance.

Thank you
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 09-09-2006, 07:16 PM
jpsdyb's Avatar
Member
 
Join Date: Dec 2005
Posts: 73
jpsdyb is on a distinguished road
any smart people that can help code this?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-10-2006, 09:12 AM
phoenix's Avatar
Senior Member
 
Join Date: May 2006
Posts: 287
phoenix is on a distinguished road
  1. creat a function to defind how much you can get from 1 pip , due to your entry lotsize
  2. creat another function to defind how many pips you gain or loss from your equity
  3. and that by your pips-gain-from-equity function you will know how many pips far from current price to mark as a new sl
but this will more difficult if you trade more than 1 order at a time

hope this idea helps you
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 06-07-2008, 09:07 PM
Junior Member
 
Join Date: Jul 2006
Posts: 29
AirforceMook is on a distinguished road
Red face

This would be extremely difficult to code if you're actually trying to set the stoploss values on the orders. If you can leave your EA running at all times, as I would think you do, then it's not too difficult.

Please see the attached code. I haven't had time to test it, so be sure to look at it prior to use.

Settings:
equity_lock How much you want to ensure you keep, once lock_start has been hit.
equity_lock_start At what profit do we start locking in equity?
equity_target At this amount of profit we close all orders

Example:
100,130,500 - If we get to $130 profit, then we'll lock in $100. If we get to $500 profit, we close all orders.

Realize that because this is a stand-alone EA, it's not very friendly. It ignores magic numbers and manual orders and will take everything into account.
Attached Files
File Type: mq4 EquityWatch.mq4 (2.8 KB, 42 views)
File Type: ex4 EquityWatch.ex4 (2.8 KB, 9 views)
__________________
//AirforceMook//
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 06-07-2008, 10:48 PM
Senior Member
 
Join Date: Oct 2005
Location: Porto/Portugal
Posts: 257
hellkas is on a distinguished road
Quote:
Originally Posted by AirforceMook View Post
This would be extremely difficult to code if you're actually trying to set the stoploss values on the orders. If you can leave your EA running at all times, as I would think you do, then it's not too difficult.

Please see the attached code. I haven't had time to test it, so be sure to look at it prior to use.

Settings:
equity_lock How much you want to ensure you keep, once lock_start has been hit.
equity_lock_start At what profit do we start locking in equity?
equity_target At this amount of profit we close all orders

Example:
100,130,500 - If we get to $130 profit, then we'll lock in $100. If we get to $500 profit, we close all orders.

Realize that because this is a stand-alone EA, it's not very friendly. It ignores magic numbers and manual orders and will take everything into account.
Very nice idea.. I'll try it..

Thank you
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 06-08-2008, 02:45 AM
Senior Member
 
Join Date: Feb 2007
Posts: 713
FerruFx is on a distinguished road
I give as a bonus with my differents offers a CloseAll with global $ amount. One of the features is a global trailing stop in $ amount. In other words, it works exactly like the standard MT4 trailing stop but this one is in $ and for a global profit (all order at the same time).

All targets are in $ and are hidden.

Not sure that is what you need.

FerruFx
__________________
THE HEART of FOREX & THE PROBABILITY METER - Trade with 100% confidence and ... Stress Less!!!
Coding services: Experts Advisors, indicators, alerts, etc ... more info by PM
NEW: video presentation of the Probability Meter ... 24hrs action on the website
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 06-08-2008, 04:25 AM
wolfe's Avatar
Senior Member
 
Join Date: Jan 2006
Posts: 682
wolfe is on a distinguished road
I did something like this. Keep track of all orders via $ amount. Assuming all your orders have the same magic number, first you have to figure your total profit by magic number. I created a function to do this:

PHP Code:
double OPBM(int intMagic)//OrderProfitByMagic
{
double dblProfit=0;
int intPOS=0;
bool boolTerm=false;
while(
boolTerm==false)
{
if(
OrderSelect(intPOS,SELECT_BY_POS))
{
if(
OrderMagicNumber()==intMagicdblProfit=dblProfit+OrderProfit();
intPOS++;
}
else
boolTerm=true;
}
return(
dblProfit);

Insert this piece of code after the last ending bracket of your EA. Then anytime you need to get the total value of all open orders with a specific magic number simply call the function. Ex.

PHP Code:
OPBM(Your_Magic_Number_Here
Next create a few external variables:

PHP Code:
extern bool    Use_Trailing_Stop=true;//select true to use a trailing stop based on total $amount
extern double  Trail_Start=10;//TS will start after this $Profit amount is reached
extern double  TSLoss_Percent=50;//%Percentage of your HIGHEST profit you can lose before close all is performed 
After your Trail_Start value is hit, you will need to keep track of your highest achieved profit value, and an allowed loss value, and store those values. I stored them as a Global Variables.

PHP Code:
Total_Profit=OPBM(Your_Magic_Number_Here);

if ((
Use_Trailing_Stop==true) && (OPBM(Your_Magic_Number_Here)>Trail_Start))  
{
 
Highest_Profit=GlobalVariableGet(TFX_Highest_Profit);
 if (
Total_Profit Highest_Profit)
  {
   
Highest_Profit=GlobalVariableSet(TFX_Highest_Profit,Total_Profit);
   
Highest_Profit=GlobalVariableGet(TFX_Highest_Profit);
   
Loss_Percent=TSLoss_Percent*0.01;
   Print(
"                                                                                                 Loss percent = ",Loss_Percent);
   
Allowed_Loss=GlobalVariableGet(TFX_Highest_Profit)*Loss_Percent;
   
Allowed_Loss=GlobalVariableSet(TFX_Allowed_Loss,Allowed_Loss);
   
Allowed_Loss=GlobalVariableGet(TFX_Allowed_Loss);
   
Trail_Engaged=true;
   Print(
"                                                                                      Highest Profit = ",Highest_Profit);
   Print(
"                                                                                        Allowed Loss = ",Allowed_Loss);
  }

Then you need code to close orders when you fall below your allowed loss value, maybe something like this:

PHP Code:
if ((Use_Trailing_Stop==true) && (Trail_Engaged==true)) 
{
 if (
OPBM(Your_Magic_Number_Here) <= ((GlobalVariableGet(TFX_Highest_Profit))-(GlobalVariableGet(TFX_Allowed_Loss))))
  {
    
CBM(Your_Magic_Number_Here);
  } 
The CBM() is a Close By Magic function I made:
PHP Code:
int CBM(int intMagic)//CloseByMagic
{
int intOffset=0;
int Count OTBM(intMagic);

while(
OTBM(intMagic)>&& Count 0)
{
OrderSelect(intOffset,SELECT_BY_POS);
if(
OrderMagicNumber()==intMagic)
{
if(
OrderType()==OP_BUYOrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),999,Red);
else if(
OrderType()==OP_SELLOrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),999,Orange);
Count--;
}
else {
intOffset++;
}
}
return(
0);

To use the CBM function, you also will need this function:

PHP Code:
int OTBM(int intMagic)//OrdersTotalByMagic
{
int intCount=0;
int intPOS=0;
bool boolTerm=false;
while(
boolTerm==false)
{
if(
OrderSelect(intPOS,SELECT_BY_POS))
{
if(
OrderMagicNumber()==intMagicintCount++;
intPOS++;
}
else
boolTerm=true;
}
return(
intCount);

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
trail SL EA jonas Expert Advisors - Metatrader 4 2 09-22-2007 09:13 AM
Trailing EQUITY STOP FEATURE? tyger01 Metatrader 4 1 12-21-2006 03:25 PM
If the current day equity is more than the repvious equity babarmughal Expert Advisors - Metatrader 4 6 12-17-2006 11:48 PM
Account equity percentage stop loss...help! faifarni Expert Advisors - Metatrader 4 3 08-08-2006 05:36 AM


All times are GMT. The time now is 01:53 AM.



Search Engine Friendly URLs by vBSEO 3.2.0 ©2008, Crawlability, Inc.