• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 12:38am
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 12:38am
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

Simple Moving Average Vs. Moving Average? 8 replies

Stop Loss vs. Hidden Stop Loss 26 replies

Trading using Simple Moving Average (SMA)14 and 200 7 replies

Does Moving average actually act as moving S/R ?? 20 replies

RSI EA using moving Average filter 0 replies

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
  • 2
Attachments: Using a moving average as a stop loss...
Exit Attachments
Tags: Using a moving average as a stop loss...
Cancel

Using a moving average as a stop loss...

  • Post #1
  • Quote
  • First Post: Feb 4, 2010 7:39pm Feb 4, 2010 7:39pm
  •  Fish
  • | Joined May 2009 | Status: Member | 34 Posts
Given the regular standard for setting a stop loss after an order is sent as:


{
ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, Ask - StopLoss * Point, Ask + TakeProfit*Point, "LONG Entry", 12345, 0, Green);
if(ticket > 0)
{
if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
Print("BUY order opened : ",OrderOpenPrice());
}
else
Print("Error opening BUY order : ", GetLastError());
return(0);
}

Say you want to use the current value of the 50EMA as the stop level. Not the close of the bar and the 50EMA, the current value of the 50EMA. This means that the stop level would change tick per tick as the price level (value of the 50EMA) changes.

The Ask - StopLoss * Point needs to be tweaked. I've tried to use StopLoss==50EMA (assuming 50EMA has been assigned the exp. mov avg set at 50) and it does compile. But in the Results column of the strategy tester the S/L column is blank and a few of the position should have been stopped out as the bar passed through the 50EMA.

Or, is this a circumstance where the strategy can only be tested live given the fact that the 50EMA is a bar on close value? How would you specify the current value of the moving average instead of the close if

50EMA = iMA(NULL,60, ShortEma, 0, MODE_EMA, PRICE_CLOSE, 0);

and there is no

50EMA = iMA(NULL,60, ShortEma, 0, MODE_EMA, PRICE_NOW, 0);

Thanks
  • Post #2
  • Quote
  • Feb 4, 2010 7:50pm Feb 4, 2010 7:50pm
  •  Hotch
  • | Joined Apr 2009 | Status: Member | 12 Posts
The 50EMA close should change with every tick (as the current price is the current close of the bar).

A suggestion would be to try

if( Bid < 50EMA )
{
OrderClose(...)
}

Sorry I can't be more help but I must get to bed.
 
 
  • Post #3
  • Quote
  • Feb 4, 2010 8:10pm Feb 4, 2010 8:10pm
  •  Turveyd
  • | Membership Revoked | Joined Aug 2006 | 11,977 Posts
Yep Close is Constantly Changing, if you want a slight lag to remove chop so intrabar moves don't move the SL then use OPEN!!


Don't run a set value is my kinda system so :-


Say you go long on the break over of 20ema.
Use 25ema for starters as your SL, it gives you a quick out incase the market turns again.
Then as your position gains profit and a possible trend establishes itself, move the EMA up to allow for a pull back within the trend.

say +1 EMA per 5pips, so at +20 your using a 24ema as your stop and so forth.

You'll have to play with the numbers with back test to find something that works, but in theory it should keep you in longer for the bigger moves.
Nothing to it, but to do it!!! Stick to the plan FOOL!!!!
 
 
  • Post #4
  • Quote
  • Feb 4, 2010 8:17pm Feb 4, 2010 8:17pm
  •  Fish
  • | Joined May 2009 | Status: Member | 34 Posts
Hmmm...I might need some help with this but I'll try to put my brain to work on it but I'm not promising anything

I'd just like to avoid what happened with the USDJPY today waiting for the next open especially on a 30min or 1hr timeframe that's why I like the if Ask > 50EMA or Bid < 50EMA. I ran that option but the Journal is full of errors.

I might have to post some more code but I'll give it a shot first.

I really dig this forum!

Thanks
 
 
  • Post #5
  • Quote
  • Feb 4, 2010 10:33pm Feb 4, 2010 10:33pm
  •  CodeMeister
  • Joined Sep 2009 | Status: Making Code While Making Pips | 1,672 Posts
Typical price is better than Close or Open if you want to work with an index of 0.
 
 
  • Post #6
  • Quote
  • Feb 6, 2010 7:45pm Feb 6, 2010 7:45pm
  •  Fish
  • | Joined May 2009 | Status: Member | 34 Posts
OK... I need help.

The logic with the code is the if the ema10 > ema35 and there is a Stochastic cross to buy, opposite for short.

I'd like the stop loss to be if there is a long position and price is less than the ema13 (price pulling back) then close all the positions, opposite for shorts. In other words, the stop loss is the ema13 and is constantly changing (adjusted). My previous thinking for the stop loss was that if price == ema13 but that is wrong.

You can see that I have:

if( Bid < ema13 )
{
OrderClose(12345,1,Bid,3,Red);
}

in there but this isn't working.

Also, if I want the maximum positions open at one time to be 5 or less than all need to change is this:

total = OrdersTotal();
if(total < 1)

to this:

total = OrdersTotal();
if(total < 6)

Correct?

And, sorry, but I'm hoping others can learn from this as well, will one magic number, 12345, track all positions or does a unique number have to be assigned to each position, up to 5?

The code:

//+------------------------------------------------------------------+
//| LoseYourShirt.mq4 |
//| Copyright 2010, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+

//---- input parameters


extern double TakeProfit=50;
extern double StopLoss=50;

extern double Lots=1;
extern double TrailingStop=30;

datetime Time1;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

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

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
int cnt, ticket, total;

double ema35, ema13, ema10, stosigcur, stosigpre, sicur, sipre;

//----
if(Bars < 100)
{
Print("bars less than 100");
return(0);
}
//----
if(TakeProfit < 0)
{
Print("TakeProfit less than 0");
return(0); // check TakeProfit
}
//----


ema35 = iMA(NULL,60,35,0,MODE_EMA,PRICE_CLOSE,0);
ema13 = iMA(NULL,60,13,0,MODE_EMA,PRICE_CLOSE,0);
ema10 = iMA(NULL,60,10,0,MODE_EMA,PRICE_CLOSE,0);
stosigcur = iStochastic(NULL,60,50,3,3,MODE_SMA,0,MODE_MAIN,0);
stosigpre = iStochastic(NULL,60,50,3,3,MODE_SMA,0,MODE_MAIN,1);
sicur = iStochastic(NULL,60,50,3,3,MODE_SMA,0,MODE_SIGNAL,0);
sipre = iStochastic(NULL,60,50,3,3,MODE_SMA,0,MODE_SIGNAL,1);

//----
total = OrdersTotal();
if(total < 1)
{

if(ema10>ema35 && stosigpre < sipre && stosigcur > sicur)
{
ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 3,Ask - StopLoss * Point,
Ask + TakeProfit*Point, "EURUSD LONG ", 12345, 0, Green);
if(ticket > 0)
{
if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
Print("BUY order opened : ",OrderOpenPrice());
}
if( Bid < ema13 )
{
OrderClose(12345,1,Bid,3,Red);
}


else
Print("Error opening BUY order : ", GetLastError());
return(0);
}

if(ema10<ema35 && stosigpre > sipre && stosigcur < sicur)
{
ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, 3 ,Bid + StopLoss * Point,
Bid - TakeProfit*Point, "EURUSD SHORT ", 12345, 0, Red);
if(ticket > 0)
{
if(OrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
Print("SELL order opened : ", OrderOpenPrice());
}
if( Ask > ema13 )
{
OrderClose(12345,1,Ask,3,Red);
}

else
Print("Error opening SELL order : ",GetLastError());
return(0);
}
return(0);
}
//----
for(cnt = 0; cnt < total; cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType() <= OP_SELL && OrderSymbol() == Symbol())
{
if(OrderType() == OP_BUY) // long position is opened
{
// check for trailing stop
if(TrailingStop > 0)
{
if(Bid - OrderOpenPrice() > Point*TrailingStop)
{
if(OrderStopLoss() < Bid - Point*TrailingStop)
{
OrderModify(OrderTicket(), OrderOpenPrice(),
Bid - Point*TrailingStop,
OrderTakeProfit(), 0, Green);
return(0);
}
}
}
}
else // go to short position
{
// check for trailing stop
if(TrailingStop > 0)
{
if((OrderOpenPrice() - Ask) > (Point*TrailingStop))
{
if((OrderStopLoss() > (Ask + Point*TrailingStop)) ||
(OrderStopLoss() == 0))
{
OrderModify(OrderTicket(), OrderOpenPrice(),
Ask + Point*TrailingStop,
OrderTakeProfit(), 0, Red);
return(0);
}
}
}
}
}
}
//----
return(0);
}
//+------------------------------------------------------------------+

Thanks people...
 
 
  • Post #7
  • Quote
  • Feb 7, 2010 10:19am Feb 7, 2010 10:19am
  •  Fish
  • | Joined May 2009 | Status: Member | 34 Posts
...still stuck
 
 
  • Post #8
  • Quote
  • Feb 7, 2010 10:48am Feb 7, 2010 10:48am
  •  sangmane
  • Joined Apr 2009 | Status: MT4 Programmer | 758 Posts
hi fish,

from your code, i see there is problems with your if-else logic

PHP Code
 if( Bid < ema13 )
{
OrderClose(12345,1,Bid,3,Red);
}

            
            else 
                Print(
"Error opening BUY order : ", GetLastError()); 
it imply that
if Bid<ema13 => close order.
if not, Print "Error opening BUY..."

make sure to do Bid<ema13 checking for closing orderdecision, only after checking if OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES) = True.

If you attach your mq4 file, i'll help you make correction
 
 
  • Post #9
  • Quote
  • Feb 7, 2010 11:30am Feb 7, 2010 11:30am
  •  Fish
  • | Joined May 2009 | Status: Member | 34 Posts
sangmane:


Here's the file. If the ema13 is the stop loss than the Take Profit code is irrelevant, correct?

Thanks my friend!
Attached File(s)
File Type: mq4 LoseYourShirt.mq4   5 KB | 394 downloads
 
 
  • Post #10
  • Quote
  • Edited 8:16am Feb 9, 2010 5:12am | Edited 8:16am
  •  sangmane
  • Joined Apr 2009 | Status: MT4 Programmer | 758 Posts
Hi Fish,

Sorry, I just get time to make some correction to your EA. The purposes of this EA only to show how to use ema as trailing stop. You must improve/tweak your buy/sell signal and ema period. If you don't want to use hard Take Profit, jusy give big value for take profit params (like 10000).

hth,

Edited: Wow, this post is my 100th post..
Attached File(s)
File Type: mq4 LoseYourShirt2.mq4   4 KB | 590 downloads
 
 
  • Post #11
  • Quote
  • Last Post: Feb 9, 2010 8:09am Feb 9, 2010 8:09am
  •  Fish
  • | Joined May 2009 | Status: Member | 34 Posts
sangmane:

Thank you very much my friend! I'm on a different machine right now but I'll check this out this evening and get back with you....
 
 
  • Platform Tech
  • /
  • Using a moving average as a stop loss...
  • 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