I succeeded to trap an int in bool flags (x1 and x2 below) but I've a hard time doing the same with orders from sublists.
I'm trying your method with several lists, as it seems simpler,
since I've just found out about the
.Except()
or .Intersect() uses:
I'm testing using 2 Sublists, y1 and y2.
I set it so y1 captures the orders of the 1st Entry group (with a incrementer and a bool doOnce...).
but the problem is y1 doesn't just capture the 1st Entry Group, instead it keeps adding the new Entry orders.
I don't know how to get the capture working and can't find an answer online.
I suspect the problem comes from my State.DataLoaded instances of y1 and y2. But I don't know why.
Is it the syntax that is wrong? Is y1 = new List<Order>(); telling to keep adding the 2nd Entry orders to y1 inspite the condition
if (doOncelistCount1stOrdersGroup && (LongEntriesCounter == 1)). Why?
Why does the list keep incrementing despite the incrementer and a bool doOnce?
y1
y1 = submitTargetsOrdersList;
Then,
y2 = submitTargetsOrdersList.Except(y1).ToList();
I tested printing the count (y1.Count) on 2 entries (1st 2 orders/contracts, 2nd 3 orders/ contracts).
and it does prints 2 on the 1st entry, but then it prints 5 on the 2nd entry.
x2 : 0
Buy
NP7. y1.Count : 2
x1 : 2
x2 : 3
Buy
NP7. y1.Count : 5
NP8. y1.Count : 5
NP8. y2.Count : 0
Error on triggering custom event for NinjaScript 'ProfitSnipers' on bar 5022: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
What am I missing to get the y1 list to capture only the 1st entry orders count? Thanks!
Below the reduced relevant snippets (if it's clearer also the script is attached):
public class ProfitSnipers : Indicator
{
private List<Order> submitTargetsOrdersList, submitStopLossesOrdersList, multiOrders, y1, y2;
private bool doOncelistCount1stOrdersGroup, doOncelistCount2ndOrdersGroup;
private int Orders1stGroupQty, Orders2ndGroupQty;
private int LongEntriesCounter, ShortEntriesCounter;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
doOncelistCount1stOrdersGroup = true;
doOncelistCount2ndOrdersGroup = true;
else if (State == State.DataLoaded)
{
y1 = new List<Order>();
y2 = new List<Order>();
private async void OnPreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs p)
{
await ChartControl.Dispatcher.InvokeAsync((Action) (() =>
{
if (p.Key == Key.Multiply)
{
++LongEntriesCounter;
if (doOncelistCount1stOrdersGroup && (LongEntriesCounter == 1))
{
Orders1stGroupQty = quantitySelector.Value;
y1 = submitTargetsOrdersList;
doOncelistCount1stOrdersGroup = false;
}
else
if (doOncelistCount2ndOrdersGroup && (LongEntriesCounter == 2))
{
Orders2ndGroupQty = quantitySelector.Value;
y2 = submitTargetsOrdersList.Except(y1).ToList();
doOncelistCount2ndOrdersGroup = false;
}
protected void ChartControl_PreviewKeyDown(object sender, KeyEventArgs e)
{
TriggerCustomEvent(TestNumPad7 =>
{
if (entryBuyMar****rder != null)
{
if (Keyboard.IsKeyDown(Key.NumPad7) && !(Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
{
Print("NP7. y1.Count : " + y1.Count);
for (int index = 0; index < Orders1stGroupQty; index++)
{
currentPTPrice = y1[index].LimitPrice + (ProfitTargetMove * TickSize);
y1[index].LimitPriceChanged = currentPTPrice;
}
myAccount.Change(y1);
}
}
---------------
TriggerCustomEvent(TestNumPad8 =>
{
if (entryBuyMar****rder != null)
{
if (Keyboard.IsKeyDown(Key.NumPad8) && !(Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
{
for (int index = 0; index < Orders2ndGroupQty; index++)
{
Print("NP8. y1.Count : " + y1.Count);
Print("NP8. y2.Count : " + y2.Count);
currentPTPrice = y2[index].LimitPrice + (ProfitTargetMove * TickSize);
y2[index].LimitPriceChanged = currentPTPrice;
}
myAccount.Change(y2);
}
}
protected override void OnBarUpdate()
{
if (myAccount != null && myAccount.Positions.Where(o => o.Instrument == Instrument).Count() > 0)
accountPosition = myAccount.Positions.Where(o => o.Instrument == Instrument).Last();
else
accountPosition = null;
if (accountPosition == null)
{
entryBuyMar****rder = null;
entrySellMar****rder = null;
profitTargetOrder = null;
stopLossOrder = null;
multiOrders.Clear();
submitTargetsOrdersList.Clear();
submitStopLossesOrdersList.Clear();
y1.Clear();
y2.Clear();
LongEntriesCounter = 0;
ShortEntriesCounter = 0;
}
else
{
}
}
Comment