Created
September 16, 2011 13:54
-
-
Save Eugeny/1222175 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Broker.StrategyLanguage.Function; | |
namespace Broker.StrategyLanguage.Strategy | |
{ | |
public class KeltnerChanel : BaseStrategyAdvisor | |
{ | |
private ISeries<Double> m_price; | |
private int m_length = 20; | |
private double m_numatrs = 1.5; | |
private AverageFC m_averagefc1; | |
private AvgTrueRange m_avgtruerange1; | |
private bool m_condition1; | |
private bool m_condition2; | |
private double m_avg; | |
private double m_shift; | |
private SeriesVar<Double> m_upperband; | |
private SimpleVar<Boolean> m_setupl; | |
private SimpleVar<Double> m_crossinghigh; | |
private SeriesVar<Double> m_lowerband; | |
private SimpleVar<Boolean> m_setups; | |
private SimpleVar<Double> m_crossinglow; | |
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; | |
// EMA | |
private int m_fastlength = 9; | |
private int m_slowlength = 18; | |
private Average m_average1; | |
private Average m_average2; | |
private SeriesVar<Double> m_fastavg; | |
private SeriesVar<Double> m_slowavg; | |
// ;EMA | |
public KeltnerChanel (object ctx) : base(ctx) | |
{ | |
} | |
private ISeries<Double> price { | |
get { return m_price; } | |
} | |
[Input] | |
public int length { | |
get { return m_length; } | |
set { m_length = value; } | |
} | |
[Input] | |
public double numatrs { | |
get { return m_numatrs; } | |
set { m_numatrs = 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;} | |
} | |
// EMA | |
[Input] | |
public int fastlength{ | |
get { return m_fastlength; } | |
set { m_fastlength = value; } | |
} | |
[Input] | |
public int slowlength{ | |
get { return m_slowlength; } | |
set { m_slowlength = value; } | |
} | |
// ;EMA | |
protected override void Construct () | |
{ | |
// EMA | |
m_average1 = new Average(this); | |
m_average2 = new Average(this); | |
m_fastavg = new SeriesVar<Double>(this); | |
m_slowavg = new SeriesVar<Double>(this); | |
// ;EMA | |
m_averagefc1 = new AverageFC (this); | |
m_avgtruerange1 = new AvgTrueRange (this); | |
m_upperband = new SeriesVar<Double> (this); | |
m_setupl = new SimpleVar<Boolean> (this); | |
m_crossinghigh = new SimpleVar<Double> (this); | |
m_lowerband = new SeriesVar<Double> (this); | |
m_setups = new SimpleVar<Boolean> (this); | |
m_crossinglow = new SimpleVar<Double> (this); | |
m_Order0 = OrdersFactory.CreateStop (new OrdersCreateParams (Lots.Default, "KltChLE", OrderAction.Buy)); | |
m_Order0_SL = OrdersFactory.CreateStop (new OrdersCreateParams (Lots.Default, "KltChLE_SL", OrderAction.Sell)); | |
m_Order0_TP = OrdersFactory.CreateLimit (new OrdersCreateParams (Lots.Default, "KltChLE_TP", OrderAction.Sell)); | |
m_Order1 = OrdersFactory.CreateStop (new OrdersCreateParams (Lots.Default, "KltChSE", OrderAction.SellShort)); | |
m_Order1_SL = OrdersFactory.CreateStop (new OrdersCreateParams (Lots.Default, "KltChSE_SL", OrderAction.BuyToCover)); | |
m_Order1_TP = OrdersFactory.CreateLimit (new OrdersCreateParams (Lots.Default, "KltChSE_TP", OrderAction.BuyToCover)); | |
} | |
protected override void Initialize () | |
{ | |
m_price = Bars.Close; | |
m_averagefc1.price = price; | |
m_averagefc1.length = new SeriesExpression<Int32> (delegate { return length; }); | |
m_avgtruerange1.length = new SeriesExpression<Int32> (delegate { return length; }); | |
m_condition1 = default(bool); | |
m_condition2 = default(bool); | |
m_avg = 0; | |
m_shift = 0; | |
m_upperband.DefaultValue = 0; | |
m_setupl.DefaultValue = false; | |
m_crossinghigh.DefaultValue = 0; | |
m_lowerband.DefaultValue = 0; | |
m_setups.DefaultValue = false; | |
m_crossinglow.DefaultValue = 0; | |
// EMA | |
m_average1.price = price; | |
m_average1.length = new SeriesExpression<Int32>(delegate { return fastlength; }); | |
m_average2.price = price; | |
m_average2.length = new SeriesExpression<Int32>(delegate { return slowlength; }); | |
m_fastavg.DefaultValue = 0; | |
m_slowavg.DefaultValue = 0; | |
// ;EMA | |
} | |
protected override void Destroy () | |
{ | |
} | |
protected override void Execute () | |
{ | |
// EMA | |
m_fastavg.Value = m_average1[0]; | |
m_slowavg.Value = m_average2[0]; | |
// ;EMA | |
if (Bars.Status == BarStatus.Close) | |
Email_lock = true; | |
CheckTrailingStop (); | |
CheckExitLogic (); | |
m_avg = m_averagefc1 [0]; | |
m_shift = (numatrs * m_avgtruerange1 [0]); | |
m_upperband.Value = (m_avg + m_shift); | |
m_condition1 = (Functions.DoubleGreater (Bars.CurrentBar,1) && Functions.CrossesOver (this, price, m_upperband)); | |
if (m_condition1) { | |
m_setupl.Value = true; | |
m_crossinghigh.Value = Bars.High [0]; | |
} else { | |
m_condition1 = (m_setupl.Value && (Functions.DoubleLess (price [0], m_avg) ||Functions.DoubleGreaterEquals (Bars.High [ | |
0], (m_crossinghigh.Value + (1 * Bars.Point))))); | |
if (m_condition1) { | |
m_setupl.Value = false; | |
} | |
} | |
if (StrategyInfo.MarketPosition <= 0) { | |
if (m_setupl.Value && Functions.DoubleGreater(m_fastavg.Value, m_slowavg.Value)) { | |
m_price0 = m_crossinghigh.Value + (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); | |
} | |
} | |
m_lowerband.Value = (m_avg - m_shift); | |
m_condition2 = (Functions.DoubleGreater (Bars.CurrentBar, 1) && Functions.CrossesUnder (this, price, m_lowerband)); | |
if (m_condition2) { | |
m_setups.Value = true; | |
m_crossinglow.Value = Bars.Low [0]; | |
} else { | |
m_condition2 = (m_setups.Value && (Functions.DoubleGreater (price [0], m_avg) || Functions.DoubleLessEquals (Bars.Low [ | |
0], (m_crossinglow.Value - (1 * Bars.Point))))); | |
if (m_condition2) { | |
m_setups.Value = false; | |
} | |
} | |
if (StrategyInfo.MarketPosition >= 0) { | |
if (m_setups.Value && Functions.DoubleGreater(m_slowavg.Value, m_fastavg.Value)) { | |
m_price1 = m_crossinglow.Value - (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); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment