View Full Version : Exit Strategy in Initialize()
Is there a way for me to make a Strategy terminate?
I would like to be able to build into my code a bunch of checking logic that if not satisfied will terminate the script.
Is this possible?
NinjaTrader_Ray
03-30-2007, 01:59 AM
The suggested method is checking for the condition to stop your strategy from processing and return out of the OnBarUdpate() method.
if (StopStrategyFromRunningCondition == true)
return;
NinjaTrader_Ray
03-30-2007, 01:59 AM
I should add, this should be called from OnBarUpdate() not Initialize(), I am unsure of implications of it being called there.
Thanks Ray,
I have been trying to set alerts with no luck, nothing happens:
Alert("string", Priority.Low,"Position Reported as Flat","C:\\Documents and Settings\\Trader\\My Documents\\Finance\\alert wavs\\alarm2.wav",5,Color.Beige,Color.AliceBlue);
The double \\ were necessary as the compiler keeps saying @unknown escape sequence@ which makes sense...
Maybe the Log is better than the Alert window...
NinjaTrader_Ray
03-30-2007, 02:21 AM
Try this:
Alert("string", Priority.Low,"Position Reported as Flat",@"C:\Documents and Settings\Trader\My Documents\Finance\alert wavs\alarm2.wav",5,Color.Beige,Color.AliceBlue);
NinjaTrader_Ray
03-30-2007, 02:23 AM
Also, I changed my post below, development corrected me and suggested not to set Running = false.
What is suggested is leaving the strategy running but returning out of OnBarUpdate().
So,
if (TerminateStrategyCondition == true)
return;
Ray
Still no good.
Am using this code with private bool Init = true; declared in variables :
protected override void OnBarUpdate()
{
if (Init){
Alert("string", Priority.Low,"Position Reported as Flat",@"C:\Documents and Settings\Trader\My Documents\Finance\alert wavs\alarm2.wav",5,Color.Beige,Color.AliceBlue);
Log(Instrument + " reported FLAT", LogLevel.Information);
Init = false;
}
}
Ah yes the empty return... and the Log write works, only the Alert doesn't
NinjaTrader_Ray
03-30-2007, 02:55 AM
For clarification,
- The alert will not compile?
- The alert does not generate an alert?
If the latter, alerts only trigger on real-time data, not historical.
Ray
Compiles and runs fine:
Chart is real time with live data
the condition Init is true under all conditions and should trigger OnBarUpdate.
The alert window remains empty
NinjaTrader_Ray
03-30-2007, 03:06 AM
By the looks of your code sample, it should trigger only once on the first bar and not after that since you are setting Init= false.
If you create a new strategy an in the OnBarUpdate() include the following line, you will see alerts generated.
Alert("string", Priority.Low, "Position is reported as flat", @"C:\Program Files\NinjaTrader 6\Sounds\Alert1.wav", 5, Color.Black, Color.Yellow);
Ray