Trade FOREX with FXCM

  • Award-Winning Platform
  • 24/7 Customer Support
  • Trade Directly on Charts
  • Free $50K Practice Account
Register


Results 1 to 15 of 15
  1. #1
    Lucas Izidoro is offline Member
    Join Date
    Oct 2010
    Posts
    151

    INDICATOR ENTRY/EXIT - Price Channel Strategy

    Price Channel Strategy

    External Inputs and Default Values: These are parameters you will be able to change at any time prior to activating the strategy through the inputs window.

    Indicator Settings
    Length = 20

    Other Settings
    Limit = 0
    Stop = 0
    Use SSL = False
    Port = 25
    Trailing Stop = 0
    Email Alert = False

    Order Notation

    PChLE = Buy Entry Order
    PChLE_SL = Stop loss order for a long position
    PChLE_TP = Limit order for a long position
    PChSE = Sell Entry Order
    PChSE_SL = Stop loss order for a short position
    PChSE_TP = Limit order for a short position

    Buying logic

    If the trader is short or flat, this strategy advisor will set an entry order to buy at a price equal to the high price of the current bar plus a tenth of a pip after the close of that bar.

    Selling Logic

    If the trader is long or flat, this strategy advisor will set an entry order to sell at a price equal to the low price of the current bar minus a tenth of a pip after the close of that bar.

    Stop loss / Limit Orders

    Traders using this strategy can also establish stop loss and limit orders in the input window. If there is a value in those fields, a stop loss and limit order will automatically be placed however many pips away from the entry price as specified by the trader.

    Trailing Stop

    If the user has a stop loss activated, and a value of X in the trailing stop input field, the stop loss will be updated by X number of pips for every X-pip move in favor of the position.

    Code:
    using System;
    using Broker.StrategyLanguage.Function;
    namespace Broker.StrategyLanguage.Strategy
    {
    	public class PriceChannel : BaseStrategyAdvisor
    	{
    		private int m_length = 20;
    		private HighestFC m_highestfc1;
    		private LowestFC m_lowestfc1;
    		private double m_TP0;
    		private double m_SL0;
    		private double m_TP1;
    		private double m_SL1;
    		private double m_price0, m_price1;
    		private IPriceOrder m_Order0;
    		private IPriceOrder m_Order0_TP;
    		private IPriceOrder m_Order0_SL;
    		private IPriceOrder m_Order1;
    		private IPriceOrder m_Order1_TP;
    		private IPriceOrder m_Order1_SL;
    		private bool Email_lock = true;
    		public PriceChannel(object ctx) :
    			base(ctx) {}
    		[Input]
    		public int length{
    			get { return m_length; }
    			set { m_length = value; }
    		}
    		
    		private int m_Limit = 0;
    		[Input]
    		public int Limit
    		{
    			get{return m_Limit;}
    			set{m_Limit=value;}
    		}
    
    		private int m_Stop = 0;
    		[Input]
    		public int Stop
    		{
    			get{return m_Stop;}
    			set{m_Stop=value;}
    		}
    		
    		private string m_Username = "";
    		[Input]
    		public string Username
    		{
    			get{return m_Username;}
    			set{m_Username=value;}
    		}
    
    		private string m_Password = "";
    		[Input]
    		public string Password
    		{
    			get{return m_Password;}
    			set{m_Password=value;}
    		}
    
    		private string m_Smtp = "";
    		[Input]
    		public string Smtp
    		{
    			get{return m_Smtp;}
    			set{m_Smtp=value;}
    		}
    
    		private string m_From = "";
    		[Input]
    		public string From
    		{
    			get{return m_From;}
    			set{m_From=value;}
    		}
    
    		private string m_To = "";
    		[Input]
    		public string To
    		{
    			get{return m_To;}
    			set{m_To=value;}
    		}
    
    		private bool m_UseSSL = false;
    		[Input]
    		public bool UseSSL
    		{
    			get{return m_UseSSL;}
    			set{m_UseSSL=value;}
    		}
    
    		private int m_Port = 25;
    		[Input]
    		public int Port
    		{
    			get{return m_Port;}
    			set{m_Port=value;}
    		}
    		
    		private int m_TrailingStop = 0;
    		[Input]
    		public int TrailingStop
    		{
    			get{return m_TrailingStop;}
    			set{m_TrailingStop=value;}
    		}
    		
    		private bool m_EmailAlert = false;
    		[Input]
    		public bool EmailAlert
    		{
    			get{return m_EmailAlert;}
    			set{m_EmailAlert=value;}
    		}
    		
    		protected override void Construct() {
    			m_highestfc1 = new HighestFC(this);
    			m_lowestfc1 = new LowestFC(this);
    			m_Order0 = OrdersFactory.CreateStop(new OrdersCreateParams(Lots.Default, "PChLE", OrderAction.Buy));
    			m_Order0_SL = OrdersFactory.CreateStop(new OrdersCreateParams(Lots.Default, "PChLE_SL", OrderAction.Sell));
    			m_Order0_TP = OrdersFactory.CreateLimit(new OrdersCreateParams(Lots.Default, "PChLE_TP", OrderAction.Sell));
    			m_Order1 = OrdersFactory.CreateStop(new OrdersCreateParams(Lots.Default, "PChSE", OrderAction.SellShort));
    			m_Order1_SL = OrdersFactory.CreateStop(new OrdersCreateParams(Lots.Default, "PChSE_SL", OrderAction.BuyToCover));
    			m_Order1_TP = OrdersFactory.CreateLimit(new OrdersCreateParams(Lots.Default, "PChSE_TP", OrderAction.BuyToCover));
    		}
    		protected override void Initialize() {
    			m_highestfc1.pricevalue = Bars.High;
    			m_highestfc1.len = new Serie---pression<Int32>(delegate { return length; });
    			m_lowestfc1.pricevalue = Bars.Low;
    			m_lowestfc1.len = new Serie---pression<Int32>(delegate { return length; });
    		}
    		protected override void Destroy() {}
    		protected override void Execute() {
    			Output.WriteLine(Bars.Point+"");
    			if(Bars.Status == BarStatus.Close)
    				Email_lock = true;
    			
    			CheckTrailingStop();
    			CheckExitLogic();
    			if(StrategyInfo.MarketPosition <= 0)
    			{
    				m_price0 = m_highestfc1[0] + (1*Bars.Point);
    				if(Limit > 0) m_TP0 = m_price0 + Limit * Point;	else m_TP0 = 0;
    				if(Stop > 0) m_SL0 = m_price0 - Stop * Point;	else m_SL0 = 0;
    				if(m_EmailAlert == true && Email_lock == true) {
    					SendMail("Order0", "Order0 is traded at Price " + m_price0);
    					Email_lock = false;
    				}
    				m_Order0.Generate(m_price0);
    			}
    			
    			if(StrategyInfo.MarketPosition >= 0)
    			{
    				m_price1 = m_lowestfc1[0] - (1*Bars.Point);
    				if(Limit > 0) m_TP1 = m_price1 - Limit * Point;	else m_TP1 = 0;
    				if(Stop > 0) m_SL1 = m_price1 + Stop * Point;	else m_SL1 = 0;
    				if(m_EmailAlert == true && Email_lock == true) {
    					SendMail("Order1", "Order1 is traded at Price " + m_price1);
    					Email_lock = false;
    				}
    				m_Order1.Generate(m_price1);
    			}
    		}
    		
    		private void CheckExitLogic() {
    			if(StrategyInfo.MarketPosition == 0) return;
    			if(StrategyInfo.MarketPosition > 0) {
    				if(m_TP0 > 0)
    					m_Order0_TP.Generate(m_TP0);
    				if(m_SL0 > 0)
    					m_Order0_SL.Generate(m_SL0);
    			}
    			if(StrategyInfo.MarketPosition < 0) {
    				if(m_TP1 > 0)
    					m_Order1_TP.Generate(m_TP1);
    				if(m_SL1 > 0)
    					m_Order1_SL.Generate(m_SL1);
    			}
    		}
    		
    		private void CheckTrailingStop() {
    			if(m_TrailingStop == 0) return;
    			if(StrategyInfo.MarketPosition > 0) {
    				if(m_SL0 >0 && (Bars.Close[0] - m_SL0) > (Stop + m_TrailingStop) * Point)
    					m_SL0 = Bars.Close[0] - Stop * Point;
    			}
    			if(StrategyInfo.MarketPosition < 0) {
    				if(m_SL1 > 0 && (m_SL1 - Bars.Close[0]) > (Stop + m_TrailingStop) * Point)
    					m_SL1 = Bars.Close[0] + Stop * Point;
    			}
    		}
    		
    		private double Point    //Returns the point value
    		{
    			get{
    				if(Bars.Point < 0.01)
    				{
    					return(Bars.Point * 10);
    				}
    				else                        
    				{
    					return Bars.Point;
    				}
    			}
    		}
    		
    		protected void SendMail(string subject, string msgToSend)
    		{
    			try 
    			{
    				System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(Smtp); 
    				smtpClient.Credentials = new System.Net.NetworkCredential(Username, Password); 
    				smtpClient.EnableSsl = UseSSL;
    				smtpClient.Port = Port;
    				// send our email 
    				smtpClient.Send(From, To, subject, msgToSend); 
    			}
    			catch (Exception e) 
    			{
    				Console.WriteLine(e.Message);
    			}
    		}
    	}
    }
    Email Alerts

    Not every strategy has an email alert function embedded within its code. To verify if the strategy you are using does, open the Format Strategy Advisor window for that stEA and see if it has the fields highlighted below.



    The highlighted fields should be filled in as follows in order to activate the email alerts:

    Username – the user name of the email you would like to use as the sender
    Password – the password used to log on to your email account
    Smtp – This will vary depending on your email provider (gmail’s SMTP is smtp.gmail.com and yahoo’s is plus.smtp.mail.yahoo.com)
    From – the full email address from which the email alert will be sent
    To- the full email address to which the email alert will be sent (it can be the same as the address in the 'from' field)
    UseSSL – the use of SSL also depends on the email provider. Please verify whether or not your email provider uses SSL. If so, this field should be changed to “True.”
    EmailAlert – this field should be changed to “True,” if you would like to activate email alerts
    รูป รูป  
    ไฟล์แนบข้อความ ไฟล์แนบข้อความ
    Last edited by Lucas Izidoro; 11-24-2010 at 06:19 PM.

  2. #2
    Sqrrl99 is offline Member
    Join Date
    Dec 2010
    Posts
    13

    Stops

    Lucas,

    Thankyou for the price channel strategy. I am having a problem with the stops while backtesting. Everytime a stoploss hits, the strategy reenters the trade immediately rather than waiting to trigger at the next channel breakthru. This is causing a large amount of consecutive losses. Is theire anyway that you can help me with this?

    Thanks,

    Jason

  3. #3
    James Stanley's Avatar
    James Stanley is offline Moderator
    Join Date
    Aug 2010
    Posts
    453
    Quote Originally Posted by Sqrrl99 View Post
    Lucas,

    Thankyou for the price channel strategy. I am having a problem with the stops while backtesting. Everytime a stoploss hits, the strategy reenters the trade immediately rather than waiting to trigger at the next channel breakthru. This is causing a large amount of consecutive losses. Is theire anyway that you can help me with this?

    Thanks,

    Jason
    Hey Jason -

    I think the attached code should take care of it. I basically just added stops and profit targets to the current price channel code out there. I ran a quick back-test and everything looks like it was running cleanly. Let me know if this isn't working correctly for you.

    Thanks Jason.
    ไฟล์แนบข้อความ ไฟล์แนบข้อความ

  4. #4
    Rhj1991 is offline Member
    Join Date
    Nov 2010
    Posts
    36
    Im using a micro account and i just started using this it places limit orders really high are these the correct limit orders or are they being stretched out? Eur usd entry 1.3393 limit at 1.4831 it just entered a double buy order idk why and a stop at 1.3372? Is it suppose to be like this? Im sorry im new to strategy trader.

  5. #5
    Rhj1991 is offline Member
    Join Date
    Nov 2010
    Posts
    36
    hmm does it automatically decide when to close the position? i thought those were limits and stops but im not sure

  6. #6
    Sqrrl99 is offline Member
    Join Date
    Dec 2010
    Posts
    13
    Play with the optimizer for a bit before going live. Those limit/stop orders are in your currency, not pips. try smaller limit stops and you might get better results.

  7. #7
    Rhj1991 is offline Member
    Join Date
    Nov 2010
    Posts
    36
    Thanks sqrrl what are your settings on the optimizer thing and for what currency do you use it on? I clicked on the optimizer and i dont understand how to use it.

  8. #8
    jdavis is offline FXCM Automated Platform Specialist
    Join Date
    Jan 2010
    Posts
    818
    Please spend a few minutes in our Webinar forum. We have among other things a webinar on how to use optimization for backtesting.

    LINK/

    -jd
    API Team

  9. #9
    Sqrrl99 is offline Member
    Join Date
    Dec 2010
    Posts
    13
    Hey James,

    Thanks again for all your help. Earlier today you showed how to code breakeven into your strategies. I have been trying to add breakeven to the pricechannelwithstopsandlimits strategy, but I keep getting an error that I am unable to correct. Can you help me out with this?

    Thanks,

    Jason

  10. #10
    James Stanley's Avatar
    James Stanley is offline Moderator
    Join Date
    Aug 2010
    Posts
    453
    Quote Originally Posted by Sqrrl99 View Post
    Hey James,

    Thanks again for all your help. Earlier today you showed how to code breakeven into your strategies. I have been trying to add breakeven to the pricechannelwithstopsandlimits strategy, but I keep getting an error that I am unable to correct. Can you help me out with this?

    Thanks,

    Jason
    Hey Jason - Great to hear from you.

    Unfortunately my que of coding work is really full with the strategies that I'm getting ready for the Tuesday/Thursday sessions.

    If its something simple - I can definitely help.

    But lets try this to get you the most expedient answer/help. The "Coding Strategy Advisors," has quite a few folks that assist in answering posts/helping put together code.

    If you could - please enclose the code, as well as the message that your receiving upon compilation. That way - if one of our other programmers can get to it quicker - you can get the fastest assistance.

    Coding Strategy Advisors
    DailyFX PLUS Trading Course

    FREE for all LIVE FXCM CLIENTS

  11. #11
    malbertolli is offline Member
    Join Date
    Aug 2010
    Posts
    9
    Quote Originally Posted by James Stanley View Post
    Hey Jason -

    I think the attached code should take care of it. I basically just added stops and profit targets to the current price channel code out there. I ran a quick back-test and everything looks like it was running cleanly. Let me know if this isn't working correctly for you.

    Thanks Jason.

    Hi James, could you help me adding a trailing stop to the "PriceChannelswithStopandLimit.fxd" strategy?

    Regards,
    Mariano
    ไฟล์แนบข้อความ ไฟล์แนบข้อความ

  12. #12
    malbertolli is offline Member
    Join Date
    Aug 2010
    Posts
    9
    Quote Originally Posted by malbertolli View Post
    Hi James, could you help me adding a trailing stop to the "PriceChannelswithStopandLimit.fxd" strategy?

    Regards,
    Mariano
    Can anyone help with this?? I add the following code to the SA:

    private double m_Trail_Amount = 50;
    [Input]
    public double Trail_Amount{
    get { return m_Trail_Amount; }
    set { m_Trail_Amount = value; }
    }


    GenerateDollarTrailing(Trail_Amount);
    }

    ...."
    And if the Trailing stop is lower than the STOP, it will execute the Trailing stop as "STOP"... Is this right?

  13. #13
    jpschan is offline FXCM Automated Platform Specialist
    Join Date
    Dec 2010
    Posts
    1,061
    Hi malbertolli,

    If you are talking about short position then the answer is yes. If it's long then no.

    Regards,
    jpschan

  14. #14
    AlphaTrader is offline Member
    Join Date
    Aug 2009
    Posts
    284
    Quote Originally Posted by Lucas Izidoro View Post
    Buying logic

    If the trader is short or flat, this strategy advisor will set an entry order to buy at a price equal to the high price of the current bar plus a tenth of a pip after the close of that bar.

    Selling Logic

    If the trader is long or flat, this strategy advisor will set an entry order to sell at a price equal to the low price of the current bar minus a tenth of a pip after the close of that bar.
    Is it the price of the current bar or the channel? I see that the code says channel. Do you agree?

    Alpha

  15. #15
    jpschan is offline FXCM Automated Platform Specialist
    Join Date
    Dec 2010
    Posts
    1,061
    Hi AlphaTrader,

    You are right, it's the value of the channel.

    Regards,
    jpschan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Disclaimer: Trading foreign exchange on margin carries a high level of risk, and may not be suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to trade foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with foreign exchange trading, and seek advice from an independent financial advisor if you have any doubts. Any opinions, news, research, analyses, prices, or other information contained on this website is provided as general market commentary and does not constitute investment advice. Forex Capital Markets LLC. will not accept liability for any loss or damage, including without limitation to, any loss of profit, which may arise directly or indirectly from use of or reliance on such information.