Forex
Google

Go Back   Forex Trading > Metatrader Training > Metatrader 4 mql 4 - Development course > Questions
Forex Forum Register FAQ Members List Calendar Today's Posts


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 12-29-2005, 07:24 PM
astro astro is offline
Junior Member
 
Join Date: Dec 2005
Posts: 6
astro is on a distinguished road
How do I set default Stop Loss / Take Profit

I manual trade and I notice I can set the default on the number of lots I buy/sell, but how do I set the default for Stop Loss / Take Profit so I don't have to waste time entering the numbers in manually?

Thank-you in advance.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 12-29-2005, 10:53 PM
codersguru's Avatar
codersguru codersguru is offline
Senior Member
 
Join Date: Oct 2005
Posts: 987
codersguru has a spectacular aura aboutcodersguru has a spectacular aura aboutcodersguru has a spectacular aura about
Arrow S/L and T/P default values!

Quote:
Originally Posted by astro
I manual trade and I notice I can set the default on the number of lots I buy/sell, but how do I set the default for Stop Loss / Take Profit so I don't have to waste time entering the numbers in manually?

Thank-you in advance.
astro,

You can not set a default value for S/L and T/P manually.
But it's an easy task to write a script in mql4 to do that.

For example you can run this script when you want to buy without using the New Order dialog:

PHP Code:
 //+------------------------------------------------------------------+
//|                                                   trade"buy".mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#include <stdlib.mqh>
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"
//+------------------------------------------------------------------+
//| script "buy"                                   |
//+------------------------------------------------------------------+
extern double TakeProfit 30;
extern double StopLoss 15;
extern double Lots 1.0;
extern double TrailingStop 15;

int start()
  {
   
int ticket;
//----
   
while(true)
     {
      
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Bid-StopLoss*Point,Ask+TakeProfit*Point,"buy opened",255,0,Lime);
      if(
ticket<=0)
        {
         
int error=GetLastError();
         Print(
"Error = ",ErrorDescription(error));
         if(
error==134) break;            // not enough money
         
if(error==135RefreshRates();   // prices changed
         
break;
        }
      
//---- remove break statement below and take trading for all money
      
else { OrderPrint(); break; }
      
//---- 10 seconds wait
      
Sleep(10000);
     }
      return(
0); // exit
       
}

if(
TrailingStop>0)  
              {                 
               if(
Bid-OrderOpenPrice()>Point*TrailingStop)
                 {
                  if(
OrderStopLoss()<Bid-Point*TrailingStop)
                    {
                     
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
                     return(
0);
                    }
               }
//----
   
return(0);
  }

//+------------------------------------------------------------------+ 
And you can run this script when you want to sell without using the New Order dialog:

PHP Code:
 //+------------------------------------------------------------------+
//|                                                  trade"sell".mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#include <stdlib.mqh>
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"
//+------------------------------------------------------------------+
//| script "sell"                                   |
//+------------------------------------------------------------------+
extern double TakeProfit 30;
extern double StopLoss 15;
extern double Lots 1.0;
extern double TrailingStop 0;

int start()
  {
   
int ticket;
//----
   
while(true)
     {
      
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Ask+StopLoss*Point,Bid-TakeProfit*Point,"sell opened",255,0,Red);
      if(
ticket<=0)
        {
         
int error=GetLastError();
         Print(
"Error = ",ErrorDescription(error));
         if(
error==134) break;            // not enough money
         
if(error==135RefreshRates();   // prices changed
         
break;
        }
      
//---- remove break statement below and take trading for all money
      
else { OrderPrint(); break; }
      
//---- 10 seconds wait
      
Sleep(10000);
     }
      return(
0); // exit
       
}

            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);
  }
//+------------------------------------------------------------------+ 
__________________
Hope it helps !
Coders' Guru
Senior MQL programmer:
www.xpworx.com/custom.htm
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-30-2005, 06:05 PM
astro astro is offline
Junior Member
 
Join Date: Dec 2005
Posts: 6
astro is on a distinguished road
Thank you very much

I will plug these in over the weeked and try them out next week. Thanks again.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-25-2006, 02:35 PM
Altern8 Altern8 is offline
Junior Member
 
Join Date: Nov 2005
Posts: 24
Altern8 is on a distinguished road
Hi codersguru,

Just a quick question. I changed the script a little, by changing the Op_SELL to OP_SELLSTOP, and works good.

Is it possible/can you add a little slice of code, that will delete the pending order after 30mins if not trade was taken?

I have attached the script.

Thankyou

-A8
Attached Files
File Type: mq4 Sell.mq4 (2.0 KB, 42 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-25-2006, 05:51 PM
codersguru's Avatar
codersguru codersguru is offline
Senior Member
 
Join Date: Oct 2005
Posts: 987
codersguru has a spectacular aura aboutcodersguru has a spectacular aura aboutcodersguru has a spectacular aura about
Arrow

Quote:
Originally Posted by Altern8
Hi codersguru,

Just a quick question. I changed the script a little, by changing the Op_SELL to OP_SELLSTOP, and works good.

Is it possible/can you add a little slice of code, that will delete the pending order after 30mins if not trade was taken?

I have attached the script.

Thankyou

-A8
A8,

You can set the expiration parameter in the OrderSend function for the pending order like this code:

PHP Code:
//+------------------------------------------------------------------+
//|                                                  trade"sell".mq4 |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#include <stdlib.mqh>
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"
//+------------------------------------------------------------------+
//| script "sell"                                   |
//+------------------------------------------------------------------+
extern double TakeProfit 120;
extern double StopLoss 15;
extern double Lots 0.1;
extern double TrailingStop 15;
extern double PipsFor 10;

int start()
  {
   
int ticket;
//----
   
while(true)
     {
      
datetime close_time CurTime() + TimeMinute(30); 
      
Alert ("This order will be closed at: " TimeToStr(close_time)); 
      
ticket=OrderSend(Symbol(),OP_SELLSTOP,Lots,Bid-PipsFor*Point,3,Ask+StopLoss*Point,Bid-TakeProfit*Point,"sell opened",255,close_time,Red);
      if(
ticket<=0)
        {
         
int error=GetLastError();
         Print(
"Error = ",ErrorDescription(error));
         if(
error==134) break;            // not enough money
         
if(error==135RefreshRates();   // prices changed
         
break;
        }
      
//---- remove break statement below and take trading for all money
      
else { OrderPrint(); break; }
      
//---- 10 seconds wait
      
Sleep(10000);
     }
      return(
0); // exit
       
}

            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);
  }
//+------------------------------------------------------------------+ 
__________________
Hope it helps !
Coders' Guru
Senior MQL programmer:
www.xpworx.com/custom.htm
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 01-25-2006, 06:46 PM
Altern8 Altern8 is offline
Junior Member
 
Join Date: Nov 2005
Posts: 24
Altern8 is on a distinguished road
Hi,

Thats great, thankyou

-A8
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 01-26-2006, 09:23 AM
Altern8 Altern8 is offline
Junior Member
 
Join Date: Nov 2005
Posts: 24
Altern8 is on a distinguished road
Hi codersguru,

Thankyou for you help, slightly off topic, is it possible to convert this to use in MT3 or do you known where I can d/load some MT3 scripts?

regards

-A8
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 01-26-2006, 12:47 PM
codersguru's Avatar
codersguru codersguru is offline
Senior Member
 
Join Date: Oct 2005
Posts: 987
codersguru has a spectacular aura aboutcodersguru has a spectacular aura aboutcodersguru has a spectacular aura about
Arrow

Quote:
Originally Posted by Altern8
Hi codersguru,

Thankyou for you help, slightly off topic, is it possible to convert this to use in MT3 or do you known where I can d/load some MT3 scripts?

regards

-A8
A8,

When I get the time I'll convert it to MQL2 for you.
__________________
Hope it helps !
Coders' Guru
Senior MQL programmer:
www.xpworx.com/custom.htm
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 06-13-2006, 11:55 PM
Aaragorn's Avatar
Aaragorn Aaragorn is offline
Senior Member
 
Join Date: Jun 2006
Location: USA
Posts: 801
Aaragorn is on a distinguished road
i saved theese in the scripts and clicked on "execute on charts" and nothing happened, what can i do.

it comes up with 2 warnings from the compiler

'TrailingStop' - expression on global scope not allowed
'{' - expression on global scope not allowed

Last edited by Aaragorn : 06-14-2006 at 12:05 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



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

vB 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
EA to close positions after profit/loss hit james2ko Expert Advisors - Metatrader 4 1 05-22-2007 04:03 AM
Stop Loss/Take Profit problem caldolegare Metatrader 4 4 02-17-2007 05:27 AM
Employ Someone and profit from Loss tony hadley General Discussion 1 01-08-2007 07:37 AM
Profit/Loss - 221/3 Oligarh Expert Advisors - Metatrader 4 81 12-05-2006 09:44 AM


All times are GMT. The time now is 03:57 PM.