NexusFi: Find Your Edge


Home Menu

 





NT7 - Strategies not fully isolated and disabling each other when using Add() for FX


Discussion in NinjaTrader

Updated
    1. trending_up 1,115 views
    2. thumb_up 0 thanks given
    3. group 1 followers
    1. forum 3 posts
    2. attach_file 0 attachments




 
Search this Thread

NT7 - Strategies not fully isolated and disabling each other when using Add() for FX

  #1 (permalink)
 warcious 
Brisbane + Australia
 
Experience: Advanced
Platform: NinjaTrader
Broker: CQG, NinjaTrader Brokerage
Trading: NQ, FDAX
Posts: 104 since Oct 2016
Thanks Given: 35
Thanks Received: 21

I have developed a risk positioning feature inside my Ninjatrader strategy for FX that calculates the lot-size for my strategy.

I have finished the risk positioning part of my strategy for Forex, that allows for correct calculation of the amount of units of a FX currency-pair to trade based on the TickValue, PointValue, stopLinePrice, entryLinePrice and a Risk_parameter (initially 0.1% - 3% risk).

It works great, BUT when I am running this code on a different chart (different currency pair), then once an order is being triggered on the second chart, it cancels out the orders on the first chart. This means that my strategies are not being isolated from each other.

I have used the function Add() inside Initialize() to add other currencypairs for this FX lot-size calculation.

The ONLY time this strategy is working, is when my second chart is running on the exact same currency pair, but as soon as I open a new chart with a different currency pair, then unfortunately the new chart with my strategy execution interferes with my previous charts running the same strategy. I think it has something to do with the Add() function and how all my potential currency pairs are loaded each time I open my strategy.

I have re-created this problematic behavior in both my eSignal datafeed as well as my Simulated datafeed.

This is my function for adding all currency-pairs for my risk positioning calculation:
LoadAllCurrencyPairs(int minute_chart) is being triggered from within Initialize() as:
this.LoadAllCurrencyPairs(1);

private void LoadAllCurrencyPairs(int minute_chart)
{
foreach (string pair in this._forextList)
{

this.Add("$" + pair, PeriodType.Minute, minute_chart);
}
}

These are my parameters in Initialize():
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
ExitOnClose = false;
RealtimeErrorHandling = RealtimeErrorHandling.TakeNoAction;
CalculateOnBarClose = false;
Enabled = true;
_currentOrder = null;
this.LoadAllCurrencyPairs();

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);
}
}
}


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.

My thoughts:
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?!?

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Better Renko Gaps
The Elite Circle
Exit Strategy
NinjaTrader
The space time continuum and the dynamics of a financial …
Emini and Emicro Index
NexusFi Journal Challenge - April 2024
Feedback and Announcements
ZombieSqueeze
Platforms and Indicators
 
  #3 (permalink)
 warcious 
Brisbane + Australia
 
Experience: Advanced
Platform: NinjaTrader
Broker: CQG, NinjaTrader Brokerage
Trading: NQ, FDAX
Posts: 104 since Oct 2016
Thanks Given: 35
Thanks Received: 21


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;
}
}


The "Deactivation" logic and code is below, and it does NOT intentionally disable other strategies. What happens is that when my first chart window, trading a currencypair is being activated and gets an entry, then when I open a different chart, trading a different currencypair. When this 2:nd chart gets an entry, then somehow it goes to Deactivation() function in strategy running on chart 2, and suddenly disables the 1:st chart window and strategy, so it stops working. As I explained above, it is almost like these strategies somehow effect eachother. BUT, this behaviour does NOT happen if I open a second chart using the same currencypair as I used on the first chart.
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);
}
}

Started this thread Reply With Quote
  #4 (permalink)
 warcious 
Brisbane + Australia
 
Experience: Advanced
Platform: NinjaTrader
Broker: CQG, NinjaTrader Brokerage
Trading: NQ, FDAX
Posts: 104 since Oct 2016
Thanks Given: 35
Thanks Received: 21

After doing further debugging I have replaced CancelAllOrders() with CancelOrder() as CancelAllOrders() is NOT supported in NT7, and it is still NOT working, and I get the very same erroneous behaviour 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!

Started this thread Reply With Quote




Last Updated on November 6, 2016


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts