View Full Version : Ninja Trader Strategy and Exits
Agamenon
11-06-2007, 09:08 AM
Hi, I´m programming my first module for Ninja Trader in C#, I use Kwik Indicators, and I complete it with not problem detecting and sending alerts for buy-sell situations.
The problem starts when I change my buy and sell alerts with EnterLon-Short and Exit Long-short commands. When I load My stratregy in chart, the historical orders affect my first purchase! The ExiLong and ExitShort enter and buy and sell positions instead of exit! What can i do? I need a flat position to enter, but the system load all strategy and get my flags and vars in game when i enter!:confused:
NinjaTrader_Ray
11-06-2007, 09:42 AM
Not sure if I completely understand but...
As you start a strategy, it will calculate it current position and orders state. It may be that the strategy calculates that it is long 1. You should then synchronize your brokerage account to this positon by placing an order that makes you live account 1 long. This can be done in the Orders tab.
Agamenon
11-06-2007, 10:56 AM
this is a example code:
case "Buy": // Case of severals options, from general Switch var TipoTrade
if (Position.Quantity > 0 )
{
ExitLong ( "Buy"); // Buy is the Name of EnterLong
Thread.Sleep(2000); // I try to delay to give time to exit succesfully
EnterShort ( NumeroVenta, "Sale"); // Get a Short Position
TipoTrade = "Sale" ; // a flag var that i use.
Alert("AlertaVenta2", Priority.High, "Venta KwikPivotPop. Estado Bandera: " + TipoTrade + " Posiciones: " + Position.Quantity.ToString(), @"C:\Archivos de Programa\NinjaTrader 6\sounds\Alert2.wav", 60, Color.LightPink, Color.Black); // Alert message
}
break;
In this case, the system give me several error messages about the handle of Long short operations.
The other code is this:
if (CrossBelow(KPA900(), KPAutoStop(), 1)) // Indicators from Kwiwk No problem with the event :)
{
if ( Position.Quantity > 0 && TipoTrade == "Compra")
{
ExitLong(); // I want exit of all Long positions to flat!
TipoTrade = "FreeforTrade"; // var flag
Alert("MyAlert3", Priority.High, "Saliendo de una Compra, Estado Bandera: " + TipoTrade + " Posiciones: " + Position.Quantity.ToString(), @"C:\Archivos de Programa\NinjaTrader 6\sounds\Alert2.wav", 60, Color.PaleGreen, Color.Black); // Alert
}
}
In this case, if i start the trade with a "historical" estrategy on course, it ExitLong dont work and get options instead of exit! :(
NinjaTrader_Josh
11-06-2007, 01:18 PM
In the first case you don't need to ExitLong before EnterShort. If you have an open long position and you do an EnterShort it will automatically close the long position and then reverse you into a short position.
In the second code you may want to check your NinjaScript settings. http://www.ninjatrader-support.com/HelpGuideV6/StrategiesTab.html
You want to choose "Wait until flat before executing live". Alternatively, you can use this in your code to prevent any flag setting/virtual trades on historical data.
if (Historical)
return;Place this in the beginning of OnBarUpdate() method.
http://www.ninjatrader-support.com/HelpGuideV6/Historical.html
Agamenon
11-07-2007, 06:38 AM
nice, them is correct to use EnterShort or Long to cut and get reverse in another contrary EnterLong-Short if my strategy needed! :) Thx