Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
strategy terminates itself
Collapse
X
-
Hello,
Thank you for the reply.
You said that you have two strategies running and they are interfering with each other. Are you saying that one of the two strategies is cancelling the other strategies orders?
Also are these being run on the same instrument and account?
I look forward to being of further assistance.JesseNinjaTrader Customer Service
Comment
-
NinjaTrader_Jesse,
Originally posted by NinjaTrader_Jesse View PostYou said that you have two strategies running and they are interfering with each other. Are you saying that one of the two strategies is cancelling the other strategies orders?
Originally posted by NinjaTrader_Jesse View PostAlso are these being run on the same instrument and account?
As you can see they are both run on Sim101 and its 2 different currencypairs, USDCAD and USDSEK. I have re-created this problematic behaviour in both my eSignal datafeed as well as my Simulated datafeed.
As I also deal with risk positioning, then I need to add other currencypairs using the add() command in Initialize() in order to be able to do that. I was asking if maybe this is causing the problem?
Please see the attached code and trace file from my previous email where I have attached the code for LoadAllCurrencyPairs() that is using the Add() function and is called from within Initialize().
A summary of HOW the erroneous behaviour occurs:
I run 2 independent charts on 2 different currency pairs running my strategy.
Chart 1 got an order entry.
When entry on chart number 2 is enabled, it enters its Deactivation() function in my strategy on Chart 2 ( I use a message box for debugging), that disables the strategy on Chart 1.
In other words, after Chart 1 is in position and when Chart 2 enables its entry then the strategy in Chart 1 is disabled from the code in my strategy on Chart 2, but there is NO message box showing up on Chart 1.
I added this message box for debugging purposes.
Also noticed:
When I have a chart number X running my strategy that has taken an entry position, and then I go to my Positions tab and Close an open position I entered manually before, than this close ALSO disables my strategy in chart X
Also noticed:
WHEN, and only WHEN I run my strategy on 2 or more charts using the very same currency pair (Barsinprogress==0) then this erroneous behaviour does NOT appear.
It only appears when I use different chart windows with different currency pairs.
What this means is that the strategies are NOT fully isolated, and I think this has something to do with HOW I add the other currency pairs into my primary barsinprogress==0 chart. I have attached the function that adds my other currency pairs (barsinprogress !=0) in this thread.
Maybe the problems occurs when I open a new chart with a currency pair that has been already opened as a secondary currency pair in the first chart through my Add() function?!?
These are my parameters in Initialize():
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
ExitOnClose = false;
RealtimeErrorHandling = RealtimeErrorHandling.TakeNoAction;
CalculateOnBarClose = false;
Enabled = true;
_currentOrder = null;
this.LoadAllCurrencyPairs();
This is myLoadAllCurrencyPairs() function:
private void LoadAllCurrencyPairs()
{
foreach (string pair in this._forextList)
{
this.Add("$" + pair, PeriodType.Minute, 1);
}
}
Also, adding my OnBarUpdate() function:
protected override void OnBarUpdate()
{
try
{
if (_deActivate)
{
DeActivation();
return;
}
if (BarsInProgress != 0)
return;
else if (BarsInProgress == 0)
{
SettingVariablesAndGraphics();
if (_currentRayContainer != null)
{
ThisInstrumentOrders();
}
}
}
catch (Exception e)
{
if (_firstException)
{
_firstException = false;
CSTUtilities.ExceptionMessage(e);
}
}
}
Ninjatrader: What is causing this errorneous behaviour?
Thanks,
DanielLast edited by warcious; 11-02-2016, 11:30 PM.
Comment
-
Hello warcious,
Thank you for your posts on this matter.
We need to clear a few items up and I would like to request your Log, not just your Trace.
1. So your "deactivation" logic is your own and you intentionally disable other strategies? This seems to be contrary to your statement that it is unexpected.
What is your "deactivation" logic? Is this something you can provide to help us assist you in this matter?
I am confused on the overall logic for DeActivation(). Why would Add() have anything to do with it? Why would it only concern itself with other primary instruments that do not match the current strategies?
These are all questions I cannot answer without your code or you advising in detail.
2. The Message Box generally appears when you "deactivate" a strategy?
3. Closing a position that a strategy is managing from the the Positions tab should disable the strategy that is managing the position.
I look forward to your response.
Comment
-
Originally posted by NinjaTrader_PatrickH View PostHello warcious,
We need to clear a few items up and I would like to request your Log, not just your Trace.
1. So your "deactivation" logic is your own and you intentionally disable other strategies? This seems to be contrary to your statement that it is unexpected.
What is your "deactivation" logic? Is this something you can provide to help us assist you in this matter?
Let me know if this makes sense?
This is my Deactivate logic:
private void DeActivation()
{
if (_weGotError)
{
_currentOrder = null;
if (Position.MarketPosition != MarketPosition.Flat)
{
OtherWayToClosefullPosition();
return;
}
Deactivate();
CancelAllOrders(true, true);
_deActivate = false;
_weGotError = false;
return;
}
if (_checkBoxEnableBarEntry.Checked)
{
_checkBoxEnableBarEntry.Checked = false;
Deactivate();
}
//Two deactivations if something goes wrong
if (_deActivate && _strategyState == StrategyState.Enter && (_currentOrder == null) && Position.MarketPosition == MarketPosition.Flat)
{
CancelAllOrders(true, false);
Deactivate();
_deActivate = false;
return;
}
else
{
if (_strategyState == StrategyState.Enter)
{
if (_currentOrder != null && _currentOrder.OrderState != OrderState.Cancelled)
CancelAllOrders(true, false);
else
{
_deActivate = false;
_currentOrder = null;
}
}
else if (_strategyState == StrategyState.Exit)
{
if (Position.MarketPosition != MarketPosition.Flat)
{
OtherWayToClosefullPosition();
}
}
}
if (_deActivate) return;
Deactivate();
}
And the Deactivate function: Deactive()
private void Deactivate()
{
_strategyState = StrategyState.NotActive;
}
LoadAllCurrencyPairs():
private void LoadAllCurrencyPairs()
{
foreach (string pair in this._forextList)
{
this.MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
this.Add("$" + pair, PeriodType.Minute, 1);
}
}
I am confused on the overall logic for DeActivation(). Why would Add() have anything to do with it? Why would it only concern itself with other primary instruments that do not match the current strategies?
These are all questions I cannot answer without your code or you advising in detail.
2. The Message Box generally appears when you "deactivate" a strategy?
3. Closing a position that a strategy is managing from the the Positions tab should disable the strategy that is managing the position.
I look forward to your response.Last edited by warcious; 11-03-2016, 06:23 PM.
Comment
-
NinjaTrader_PatrickH:
After putting MessageBoxes everywhere where the CancelAllOrders() native functions is being called I found out that it is inside OnPositionUpdate() in my code that the CancelAllOrders() function is being called that cancel my other strategies:
protected override void OnPositionUpdate(IPosition position)
{
try
{
if (_strategyState == StrategyState.Enter && position.MarketPosition != MarketPosition.Flat)
{
_realQuantity = (int)_numericUpDownQuantity.Value;
_strategyState = StrategyState.Exit;
}
if (_strategyState == StrategyState.Exit && position.MarketPosition == MarketPosition.Flat)
{
SetNotActive();
}
base.OnPositionUpdate(position);
}
catch (Exception e)
{
CSTUtilities.ExceptionMessage(e);
}
}
private void SetNotActive()
{
CancelAllOrders(false, true);
_strategyState = StrategyState.NotActive;
MessageBox.Show("Deactivating inside SetNotActive() at time: " + Convert.ToString(DateTime.Now));
SetVisualToNotActive();
if (_deActivate)
{
_currentOrder = null;
_deActivate = false;
}
}
Comment
-
Thank you for the update, warcious.
CancelAllOrders() is not supported in NinjaTrader 7. The function is there but it is not supported for use in NinjaScript. The only overloads it has are to cancel all entries or all exits and it would appear it also disables strategies. I would recommend straying away from this function and potentially tracking your orders in your code and then cancelling them with CancelOrder().
Comment
-
Originally posted by NinjaTrader_PatrickH View PostThank you for the update, warcious.
CancelAllOrders() is not supported in NinjaTrader 7. The function is there but it is not supported for use in NinjaScript. The only overloads it has are to cancel all entries or all exits and it would appear it also disables strategies. I would recommend straying away from this function and potentially tracking your orders in your code and then cancelling them with CancelOrder().
After doing further debugging I have replaced CancelAllOrders() with CancelOrder() and it is NOT working, and I get the very same erroneous behavior using CancelOrder(), this is how my new SetNotActive() function looks like where this problem occurs predominantly.
private void SetNotActive()
{
//CancelAllOrders(false, true); // Cancel All Exit my orders, Entry = false, Exit = true
//CancelOrder(_currentExitOrder50);
//CancelOrder(_currentExitOrder75);
//CancelOrder(_currentExitOrder100);
//CancelOrder(_currentExitOrderOther);
//CancelOrder(_currentExitOrderSTOP);
if (_currentExitOrder50 != null) CancelOrder(_currentExitOrder50);
if (_currentExitOrder75 != null) CancelOrder(_currentExitOrder75);
if (_currentExitOrder100 != null) CancelOrder(_currentExitOrder100);
if (_currentExitOrderOther != null) CancelOrder(_currentExitOrderOther);
if (_currentExitOrderSTOP != null) CancelOrder(_currentExitOrderSTOP);
_strategyState = StrategyState.NotActive;
MessageBox.Show("Deactivating inside SetNotActive() at time: " + Convert.ToString(DateTime.Now));
SetVisualToNotActive();
if (_deActivate)
{
_currentOrder = null;
_deActivate = false;
}
}
BUT, can I really use CancelOrder() IF I am ONLY using market order type throughout my strategy and NOT limit order type for all my orders?
I am only using EnterLong(), EnterShort() together with ExitLong() and ExitShort() functions in my strategy due to the previous problem with incompatibility problems between using eSignal as preliminary datafeed and then placing LimitOrders through Ninjatrader7 and executing them through FXCM. This did NOT work at all unfortunately!Last edited by warcious; 11-06-2016, 08:52 PM.
Comment
-
Hello warcious,
Thank you for your response.
You would need to ensure the Market Orders IOrder objects is being set to null when filled if you are trying to ensure this is not CancelOrder() causing this item.
You may wish to look to the StrategyState code you have. At this point I would recommend debugging your strategy further.
You can find information on debugging at the following link: http://ninjatrader.com/support/forum...ead.php?t=3418
Comment
-
Originally posted by NinjaTrader_PatrickH View PostHello warcious,
You would need to ensure the Market Orders IOrder objects is being set to null when filled if you are trying to ensure this is not CancelOrder() causing this item.
You may wish to look to the StrategyState code you have. At this point I would recommend debugging your strategy further.
I have tried to debug by activating the TraceOrder function as well as making sure that each IOrder object is set to null after ExitLong() or ExitShort() function is being run (filled).
Unfortunately, the Output Window does not give any valuable output when my problem occurs.
My variables:
private IOrder _currentOrder;
private IOrder _currentExitOrder50;
private IOrder _currentExitOrder75;
private IOrder _currentExitOrder100;
private IOrder _currentExitOrderOther;
private IOrder _currentExitOrderSTOP;
Inside my OnStartUp() i make sure that each IOrder object is null:
protected override void OnStartUp()
{
try
{
// Add Toolbar Button
ButtonToThetop();
MessageBox.Show("
_currentExitOrder50: " + _currentExitOrder50
+ "\n_currentExitOrder75: " + _currentExitOrder75
+ "\n_currentExitOrder100: " + _currentExitOrder100
+ "\n_currentExitOrderOther: " + _currentExitOrderOther
+ "\n_currentExitOrderSTOP: " + _currentExitOrderSTOP
+ "\n__currentOrder: " + _currentOrder);
_currentOrder = null;
_currentExitOrder50 = null;
_currentExitOrder75 = null;
_currentExitOrder100 = null;
_currentExitOrderOther = null;
_currentExitOrderSTOP = null;
Example of how my ExitShort() code looks like:
if (CurrentPrice >= RayPrice(_currentRayContainer.StopRay))
{
_currentExitOrderSTOP = ExitShort("Exit Short STOP","");
MessageBox.Show("_currentExitOrderSTOP has ExitShort and this is is content before I null it: " + Convert.ToString(_currentExitOrderSTOP));
_currentExitOrderSTOP = null;
Example of how my ExitLong() code looks like:
_currentExitOrderSTOP = ExitLong("Exit Long STOP","");
MessageBox.Show("_currentExitOrderSTOP has ExitLong and this is is content before I null it: " + _currentExitOrderSTOP);
_currentExitOrderSTOP = null;
Patrick: Do I need to use the function OnExecution() maybe?Last edited by warcious; 12-07-2016, 12:03 AM.
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by judysamnt7, 03-13-2023, 09:11 AM
|
4 responses
57 views
0 likes
|
Last Post
by DynamicTest
Today, 08:25 PM
|
||
Started by ScottWalsh, Today, 06:52 PM
|
4 responses
36 views
0 likes
|
Last Post
by ScottWalsh
Today, 07:40 PM
|
||
Started by olisav57, Today, 07:39 PM
|
0 responses
7 views
0 likes
|
Last Post
by olisav57
Today, 07:39 PM
|
||
Started by trilliantrader, Today, 03:01 PM
|
2 responses
19 views
0 likes
|
Last Post
by helpwanted
Today, 07:25 PM
|
||
Started by cre8able, Today, 07:24 PM
|
0 responses
9 views
0 likes
|
Last Post
by cre8able
Today, 07:24 PM
|
Comment