I have been on the support forum before for this same issue, but it never led to any solution. I just can not understand my problem can't be solved by any of you people around here.
It's about the Trigger Custom Event. I want to use a timer to close all open positions minutes before the market closes. The ExitOnClose won't help me, because this will result in strategies being not in sync after that execution. So please skip that possible solution.
My problem is that the trigger itself works, but strangely the execution of the order is not. I know the trigger works, because the output window shows whatever I put as command on the trigger, but only the orders are not executed. I use the same lines of code to close open positions on an OnBarUpdate, which works fine. It just seems the trigger does not want to fire the order execution, where other command are fired.
On the last bar of a session (the flat bar), the closing of positions is fired (because of the OnBarUpdate) after all, but this is too late, because I want to avoid gaps between sessions and open positions in the weekends.
I added the code for your insight. Please tell me where to look, because I am just out of options here... the bold parts refer to the trigger.
Kind regards,
Francis
--------------------------------------
#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.Windows.Forms;
#endregion
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
/// </summary>
[Description("RenkoFibRetLongTest: ")]
public class RenkoFibRetLongTest: Strategy
{
#region Variables
private int intTimeClose = 221400; // Default setting for IntTimeClose
private int intTimeOpen = 221600; // Default setting for IntTimeOpen
private int intTimeCloseFriday = 231400; // Default setting for IntTimeCloseFriday
private int intTimeOpenFriday = 231600; // Default setting for IntTimeOpenFriday
private System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();
// 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()
{
CalculateOnBarClose = true;
MaxProcessedEvents = 1000;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnStartUp()
{
// Initiate our Timer object with an interval of 30000ms (30 seconds)
myTimer.Tick += new EventHandler(TimerEventProcessor);
myTimer.Interval = 30000;
myTimer.Start();
}
protected override void OnBarUpdate()
{
... Some Strategy Code...
}
// Timer's tick event handler. Called at every tick of 30000ms.
private void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
{
/* Important to use the TriggerCustomEvent() to ensure that NinjaScript indexes and pointers are correctly set.
Do not process your code here. Process your code in the MyCustomHandler method. */
TriggerCustomEvent(MyCustomHandler, 0, myTimer.Interval);
}
private void MyCustomHandler(object state)
{
if ((Time[0].DayOfWeek != DayOfWeek.Friday
&& ToTime(Time[0]) >= intTimeClose
&& ToTime(Time[0]) < intTimeOpen)
|| (Time[0].DayOfWeek == DayOfWeek.Friday
&& ToTime(Time[0]) >= intTimeCloseFriday
&& ToTime(Time[0]) < intTimeOpenFriday))
{
ExitLong();
}
}
// Important to clean up our resources
protected override void OnTermination()
{
// Cleans up our Timer object
myTimer.Dispose();
}
#region Properties
#endregion
}
}

Comment