Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Broker.StrategyLanguage.Strategy
- {
- public class InsideBar : BaseStrategyAdvisor
- {
- private double m_TP0;
- private double m_SL0;
- private double m_TP1;
- private double m_SL1;
- private IMarketOrder m_Order0;
- private IPriceOrder m_Order0_TP;
- private IPriceOrder m_Order0_SL;
- private IMarketOrder m_Order1;
- private IPriceOrder m_Order1_TP;
- private IPriceOrder m_Order1_SL;
- private bool Email_lock = true;
- public InsideBar(object ctx) :
- base(ctx) {}
- 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_Order0 =
- OrdersFactory.CreateMarketNextBar(new OrdersCreateParams(Lots.Default, "InsBarLE", OrderAction.Buy));
- m_Order0_SL =
- OrdersFactory.CreateStop(new OrdersCreateParams(Lots.Default, "InsBarLE_SL", OrderAction.Sell));
- m_Order0_TP =
- OrdersFactory.CreateLimit(new OrdersCreateParams(Lots.Default, "InsBarLE_TP", OrderAction.Sell));
- m_Order1 =
- OrdersFactory.CreateMarketNextBar(new OrdersCreateParams(Lots.Default, "InsBarSE", OrderAction.SellShort));
- m_Order1_SL =
- OrdersFactory.CreateStop(new OrdersCreateParams(Lots.Default, "InsBarsSE_SL", OrderAction.BuyToCover));
- m_Order1_TP =
- OrdersFactory.CreateLimit(new OrdersCreateParams(Lots.Default, "InsBarsSE_TP", OrderAction.BuyToCover));
- }
- protected override void Initialize() {
- }
- protected override void Destroy() {}
- protected override void Execute() {
- if(Bars.Status == BarStatus.Close)
- Email_lock = true;
- CheckTrailingStop();
- CheckExitLogic();
- if ((Functions.DoubleLess(Bars.High[0], Bars.High[1]) && Functions.DoubleGreater(Bars.Low[0], Bars.Low[1]))){
- if (Functions.DoubleGreater(Bars.Close[0], Bars.Open[0])){
- if(Limit > 0) m_TP0 = Bars.Close[0] + Limit * Point; else m_TP0 = 0;
- if(Stop > 0) m_SL0 = Bars.Close[0] - Stop * Point; else m_SL0 = 0;
- if(EmailAlert == true && Email_lock == true) {
- SendMail("Order0", "Order0 is traded at Price " + Bars.Close[0]);
- Email_lock = false;
- }
- m_Order0.Generate();
- }
- if (Functions.DoubleLess(Bars.Close[0], Bars.Open[0])){
- if(Limit > 0) m_TP1 = Bars.Close[0] - Limit * Point; else m_TP1 = 0;
- if(Stop > 0) m_SL1 = Bars.Close[0] + Stop * Point; else m_SL1 = 0;
- if(EmailAlert == true && Email_lock == true) {
- SendMail("Order1", "Order1 is traded at Price " + Bars.Close[0]);
- Email_lock = false;
- }
- m_Order1.Generate();
- }
- }
- }
- 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);
- }
- }
- }
Add Comment
Please, Sign In to add comment