Now, the key points in it, (based on my possibly faulty understanding) are as follows:
- I did SetStopLoss() as 100% and SetProfitTarget() as 0%. This means, (I think, correct me if I am wrong), that I am willing to suffer any level of loss and obtain any profit however minimal. In other words, an order should be placed successfully in any situation regardless of market conditions.
- I set the BuyOffset (a local variable in my code, search for it) to be negative, which means that I am willing to buy at price higher than current ask. This again, as far as the mental model in my head goes, means that each long order that I place should get executed instantly.
Well, so that's the theory. In practice, as you can see, I am doing a bunch of write to log file actions using LogWrite() method, and in the log I can see clearly that whereas OnBarUpdate() happens every minute (and hence the line with EnterLongLimit() gets executed) the OnExecution event runs fairly rarely. Moreover, I simply don't understand what is it that triggers it. And why doesn't it run on each OnBarUpdate() as I think it should?
#region Using declarations
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Collections;
using System.Drawing.Drawing2D;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Data;
using NinjaTrader.Indicator;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Strategy;
#endregion
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
/// Enter the description of your strategy here
/// </summary>
[Description("Enter the description of your strategy here")]
public class MyCustomStrategy3 : Strategy
{
#region Variables
// Wizard generated variables
private int p = 30; // Default setting for P
private int l = 10; // Default setting for L
// User defined variables (add any user defined variables below)
#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()
{
LogInitialize(STR_FOLDER_DESKTOP + "logNinja3.txt");
LogWrite("BEGIN 1 time=" + DateTime.Now);
SetProfitTarget("", CalculationMode.Percent, 0);
SetStopLoss("", CalculationMode.Percent, 100, false);
this.AccountSize = 10000;
CalculateOnBarClose = true;
}
const int WIDTH_MOVING_WINDOW = 5;
IOrder order;
protected override void OnExecution(IExecution execution)
{
LogWrite("exec=" + execution);
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
RunStrategy();
}
void RunStrategy()
{
double ask = GetCurrentAsk();
double bid = GetCurrentBid();
LogWrite("RS time=" + this.Time + " bid=" + bid);
double buyPrice = bid - BuyOffset; //this is nonsense, still not sure about strategy logic
int numToBuy = 10;
this.order = EnterLongLimit(0, true, numToBuy, buyPrice, "long");
}
const double BuyOffset = -1; const double SellOffset = -1;
public static readonly String STR_FOLDER_DESKTOP = Environment.GetFolderPath(Environment.SpecialFolde r.Desktop) + @"\";
static StreamWriter writerLog;
static int counterLog = 0;
public static void LogInitialize(String strFilename)
{
if(writerLog != null)
writerLog.Close();
writerLog = new StreamWriter(strFilename);
counterLog = 1;
} //end LogInitialize()
public static void LogWrite(String str)
{
String strNumberPrefix = counterLog + ") ";
counterLog++;
writerLog.WriteLine("");
writerLog.WriteLine(strNumberPrefix + str);
writerLog.Flush();
} //end LogWriteWithFlush()
#region Properties
[Description("P")]
[Category("Parameters")]
public int P
{
get { return p; }
set { p = Math.Max(1, value); }
}
[Description("L")]
[Category("Parameters")]
public int L
{
get { return l; }
set { l = Math.Max(1, value); }
}
#endregion
}
}
Comment