Last active
September 6, 2016 18:13
-
-
Save rymoore99/19e27eae333d61b8bd41 to your computer and use it in GitHub Desktop.
This file contains 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
#region Using declarations | |
using System; | |
using System.ComponentModel; | |
using System.Diagnostics; | |
using System.Drawing; | |
using System.Drawing.Drawing2D; | |
using System.Xml.Serialization; | |
using NinjaTrader.Cbi; | |
using NinjaTrader.Data; | |
using NinjaTrader.Indicator; | |
using NinjaTrader.Gui.Chart; | |
using NinjaTrader.Strategy; | |
using System.Data.SqlClient; | |
#endregion | |
// This namespace holds all strategies and is required. Do not change it. | |
namespace NinjaTrader.Strategy | |
{ | |
/// <summary> | |
/// An example crossover strategy | |
/// </summary> | |
[Description("An example crossover strategy")] | |
public class DemoCrossoverStrategy4 : Strategy | |
{ | |
private SqlConnection connection; | |
#region Variables | |
private int sma1Val = 10; | |
private int sma2Val = 20; | |
private int profitTarget = 10; | |
private int stopLoss = 20; | |
#endregion | |
/// <summary> | |
/// This method is used to configure the strategy and is called once before any strategy method is called. | |
/// </summary> | |
protected override void Initialize() | |
{ | |
ClearOutputWindow(); | |
CalculateOnBarClose = true; | |
// we'll set the stop loss for our positions globally | |
SetStopLoss(CalculationMode.Percent, Convert.ToDouble(stopLoss) / 100); | |
SetProfitTarget(CalculationMode.Percent, Convert.ToDouble(profitTarget) / 100); | |
connection = new SqlConnection("server=RYANMOOREA9CA\\EXPRESS2012;" + | |
"Trusted_Connection=yes;" + | |
"database=NinjaTest1; " + | |
"connection timeout=30"); | |
try { | |
this.connection.Open(); | |
} | |
catch { | |
Print("Error opening SQL Connection"); | |
} | |
} | |
/// <summary> | |
/// Called on each bar update event (incoming tick) | |
/// </summary> | |
protected override void OnBarUpdate() | |
{ | |
var sma1 = SMA(sma1Val); | |
var sma2 = SMA(sma2Val); | |
if (CrossAbove(sma1, sma2, 1)) { | |
EnterLong(); | |
var msg = string.Format("Entering long. SMA1={0}, SMA2={1}, Price={2}", sma1[0], sma2[0], Close[0]); | |
LogInfo(msg); | |
} | |
} | |
protected override void OnTermination() | |
{ | |
if (this.connection != null) | |
this.connection.Close(); | |
} | |
private void LogInfo(string msg) { | |
LogToDB(1, msg); | |
Print("INFO: " + msg); | |
} | |
private void LogError(string msg) { | |
LogToDB(2, msg); | |
Print("ERROR: " + msg); | |
} | |
private void LogToDB(int logLevel, string msg) { | |
try | |
{ | |
var query = "INSERT INTO Logs (LogLevel, Message, LogDate) VALUES (@logLevel, @message, GETDATE())"; | |
using (var cmd = new SqlCommand(query, this.connection)) { | |
cmd.Parameters.AddWithValue("@logLevel", logLevel); | |
cmd.Parameters.AddWithValue("@message", msg); | |
cmd.ExecuteNonQuery(); | |
} | |
} | |
catch (Exception e) | |
{ | |
Print("Error Logging to Database: " + e.ToString()); | |
} | |
} | |
#region Properties | |
[Description("First SMA Value")] | |
[GridCategory("SMA Parameters")] | |
public int SMA1 | |
{ | |
get { return sma1Val; } | |
set { sma1Val = Math.Max(1, value); } | |
} | |
[Description("Second SMA Value")] | |
[GridCategory("SMA Parameters")] | |
public int SMA2 | |
{ | |
get { return sma2Val; } | |
set { sma2Val = Math.Max(1, value); } | |
} | |
[Description("Stop Loss %")] | |
[GridCategory("Target Parameters")] | |
public int StopLoss | |
{ | |
get { return stopLoss; } | |
set { stopLoss = Math.Max(1, value); } | |
} | |
[Description("Profit Target %")] | |
[GridCategory("Target Parameters")] | |
public int ProfitTarget | |
{ | |
get { return profitTarget; } | |
set { profitTarget = Math.Max(1, value); } | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment