version 176
unknown
csharp
4 years ago
16 kB
12
Indexable
#region QS and PnL 🚀 public class TickHunter : Indicator #1 of 1
// QS Define a Chart object to refer to the chart on which the indicator resides
private Chart chartWindow;
/*QS for Hotkeys Keys And For Order Quantity (Anonymous Functions)
* State == State.DataLoaded) -> ChartControl.Dispatcher.InvokeAsync((Action)(() -> quantitySelector
* private void Add_Controls_To_Toolbar() -> ChartControl.Dispatcher.InvokeAsync((Action) -> quantitySelector
// https://ninjatrader.com/support/forum/forum/ninjatrader-8/indicator-development/101315-set-quantityupdown-value-from-keyboard?p=1191246#post1191246
* protected void ChartControl_PreviewKeyDown -> TriggerCustomEvent(o => & TriggerCustomEvent(o => -> quantitySelector.Value
*/
NinjaTrader.Gui.Tools.QuantityUpDown quantitySelector = null;
// QS
private bool Is_ToolBar_Controls_Added;
// QS https://stackoverflow.com/a/9301272/10789707
private int qs;
private double unRealizedPnLdouble;
#endregion QS and PnL 🚀 public class TickHunter : Indicator #1 of 1
protected override void OnStateChange()
{
#region QS 🚀 protected override void OnStateChange() TOP #1 of 2
NinjaTrader.Gui.Tools.QuantityUpDown quantitySelector;
#endregion QS 🚀 protected override void OnStateChange() TOP #1 of 2
State.SetDefaults
{
#region AccountName and ChartPosition 🚀 State.SetDefaults BOTTOM #2 of 2
AccountName = "Sim101";
ChartPosition = TextPosition.BottomRight;
#endregion AccountName and ChartPosition 🚀 State.SetDefaults) BOTTOM #2 of 2
#region QS 🚀 State == State.Historical #1 of 1
else if (State == State.Historical)
{
//Call the custom addButtonToToolbar method in State.Historical to ensure it is only done when applied to a chart
// -- not when loaded in the Indicators window
if (!Is_ToolBar_Controls_Added) Add_Controls_To_Toolbar();
}
#endregion 🚀 State == State.Historical #1 of 1
#region HOTKEYS and QS 🚀 State == State.DataLoaded #1 of 1
if (ChartControl != null)
{
ChartPanel.PreviewKeyDown += ChartPanel_KeyDown;
ChartPanel.PreviewKeyUp += ChartPanel_KeyUp;
ChartPanel.Dispatcher.InvokeAsync((Action)(() =>
{
quantitySelector = (Window.GetWindow(ChartPanel.Parent).FindFirst("ChartTraderControlQuantitySelector") as NinjaTrader.Gui.Tools.QuantityUpDown);
}));
}
if (ChartPanel != null)
{
ChartPanel.KeyDown += ChartPanel_KeyDown;
ChartPanel.KeyUp += ChartPanel_KeyUp;
}
// Find our account
lock (Account.All)
account = Account.All.FirstOrDefault(a => a.Name == AccountName);
// Subscribe to account item and order updates
if (account != null)
{
account.OrderUpdate += OnOrderUpdate;
account.ExecutionUpdate += OnExecutionUpdate;
account.PositionUpdate += OnPositionUpdate;
}
#endregion HOTKEYS and QS 🚀 State == State.DataLoaded #1 of 1
#region HOTKEYS 🚀 State == State.Terminated #1 of 1
if (ChartPanel != null)
{
ChartPanel.KeyDown -= ChartPanel_KeyDown;
ChartPanel.KeyUp -= ChartPanel_KeyUp;
}
if (account != null)
{
account.OrderUpdate -= OnOrderUpdate;
account.ExecutionUpdate -= OnExecutionUpdate;
account.PositionUpdate -= OnPositionUpdate;
}
DisposeCleanUp();
#endregion 🚀 State == State.Terminated #1 of 1
protected override void OnBarUpdate()
{
#region myFont for PnL 🚀 protected override void OnBarUpdate() #1 of 2
NinjaTrader.Gui.Tools.SimpleFont myFont = new NinjaTrader.Gui.Tools.SimpleFont("Courier New", 12) { Size = 15, Bold = true };
#endregion myFont for PnL 🚀 protected override void OnBarUpdate() #1 of 2
#region PnL Display 🚀 protected override void OnBarUpdate() #2 of 2
// To Display the unrealized PNL in Draw.TextFixed
foreach (Account acct in Account.All)
{
if (acct.Positions != null)
{
foreach (Position pos in acct.Positions)
{
if (pos.MarketPosition != MarketPosition.Flat)
{
unRealizedPnLdouble = Instrument.MasterInstrument.RoundToTickSize(pos.GetUnrealizedProfitLoss(PerformanceUnit.Points, Close[0]) / TickSize);
}
}
}
}
string unRealizedPnL = unRealizedPnLdouble.ToString("N0");
Draw.TextFixed(
this,
"unRealizedPnL",
"UnRPnL : " + unRealizedPnL,
ChartPosition,
(unRealizedPnL > 0 ? Brushes.Green : unRealizedPnL < 0 ? Brushes.Red : Brushes.White),
myFont,
Brushes.Transparent,
Brushes.Transparent,
0);
#endregion PnL Display 🚀 protected override void OnBarUpdate() #2 of 2
}
#region QuantitySelector 🚀 Add Controls To Toolbar #1 of 1
private void Add_Controls_To_Toolbar()
{
// Use this.Dispatcher to ensure code is executed on the proper thread
ChartControl.Dispatcher.InvokeAsync((Action)(() =>
{
//Obtain the Chart on which the indicator is configured
chartWindow = Window.GetWindow(this.ChartControl.Parent) as Chart;
if (chartWindow == null)
{
Print("chartWindow == null");
return;
}
quantitySelector = new NinjaTrader.Gui.Tools.QuantityUpDown();
//quantitySelector.ValueChanged += On_quantitySelector_ValueChanged;
quantitySelector.PreviewKeyDown += On_quantitySelector_PreviewKeyDown;
chartWindow.MainMenu.Add(quantitySelector);
Is_ToolBar_Controls_Added = true;
quantitySelector.Value = 1;
}));
}
#endregion QuantitySelector 🚀 Add Controls To Toolbar #1 of 1
#region HOTKEYS for QuantitySelector 🚀 On_quantitySelector_PreviewKeyDown #1 of 1
private void On_quantitySelector_PreviewKeyDown(object sender, KeyEventArgs p)
{
NinjaTrader.Gui.Tools.QuantityUpDown qs = sender as NinjaTrader.Gui.Tools.QuantityUpDown;
if (p.Key == Key.Delete || p.Key == Key.Back)
{
p.Handled = true;
qs.Value = 0;
}
if (p.Key >= Key.D0 && p.Key <= Key.D9)
{
p.Handled = true;
string number = p.Key.ToString();
string newnumber = qs.Value.ToString();
number = number.Replace("NumPad", "");
number = number.Replace("D", "");
int num = int.Parse(newnumber + number);
if (qs != null) qs.Value = num;
}
}
#endregion HOTKEYS for QuantitySelector 🚀 On_quantitySelector_PreviewKeyDown #1 of 1
#region QuantitySelector 🚀 DisposeCleanUp
private void DisposeCleanUp()
{
//ChartWindow Null Check
if (chartWindow != null)
{
//Dispatcher used to Assure Executed on UI Thread
ChartControl.Dispatcher.InvokeAsync((Action)(() =>
{
if( quantitySelector != null ) chartWindow.MainMenu.Remove(quantitySelector);
}));
}
}
#endregion QuantitySelector 🚀 DisposeCleanUp
#region HOTKEY NumPad 1 🚀 private void MoveSL() #1 of 2 (LONG: Move SL to Bid - 4 Ticks | SHORT: Move SL to Ask + 4 Ticks)
private void MoveSL()
{
foreach (Account acct in Account.All)
{
if (acct.Positions != null)
{
foreach (Position pos in acct.Positions)
{
if (pos.MarketPosition == MarketPosition.Long)
{
lock (account.Orders)
{
foreach (Order moveSLOrder in account.Orders)
{
moveSLOrder.StopPriceChanged = GetCurrentBid() - (4 * TickSize);
account.Change(new[] { moveSLOrder });
}
}
}
else if (pos.MarketPosition == MarketPosition.Short)
{
lock (account.Orders)
{
foreach (Order moveSLOrder in account.Orders)
{
moveSLOrder.StopPriceChanged = GetCurrentAsk() + (4 * TickSize);
account.Change(new[] { moveSLOrder });
}
}
}
}
}
}
}
#endregion HOTKEY Numpad 1 🚀 private void MoveSL() #1 of 2 (LONG: Move SL to Bid - 4 Ticks | SHORT: Move SL to Ask + 4 Ticks)
#region HOTKEY NumPad 2 🚀 private void MoveTP() #1 of 2 (LONG: Move SL to Bid - 4 Ticks | SHORT: Move SL to Ask + 4 Ticks)
private void MoveTP()
{
foreach (Account acct in Account.All)
{
if (acct.Positions != null)
{
foreach (Position pos in acct.Positions)
{
if (pos.MarketPosition == MarketPosition.Long)
{
lock (account.Orders)
{
foreach (Order moveTPOrder in account.Orders)
{
moveTPOrder.LimitPriceChanged = GetCurrentBid() - (4 * TickSize);
account.Change(new[] { moveTPOrder });
}
}
}
else if (pos.MarketPosition == MarketPosition.Short)
{
lock (account.Orders)
{
foreach (Order moveTPOrder in account.Orders)
{
moveTPOrder.LimitPriceChanged = GetCurrentAsk() + (4 * TickSize);
account.Change(new[] { moveTPOrder });
}
}
}
}
}
}
}
#endregion HOTKEY Numpad 2 🚀 private void MoveTP() #1 of 2 (LONG: Move SL to Bid - 4 Ticks | SHORT: Move SL to Ask + 4 Ticks)
#region HOTKEYS 🚀 protected void ChartPanel_KeyDown and ChartPanel_KeyUp #1 of 1
protected void ChartPanel_KeyDown(object sender, KeyEventArgs e)
{
#region HOTKEY buyMktOrder 🚀 NumPad 7
TriggerCustomEvent(o =>
{
Order buyMktOrder = null;
if (Keyboard.IsKeyDown(Key.NumPad7))
{
buyMktOrder = account.CreateOrder(
Instrument,
OrderAction.Buy,
OrderType.Market,
OrderEntry.Manual,
TimeInForce.Day,
quantitySelector.Value,
0,
0,
"",
"buyMktOrder"+DateTime.Now.ToString(),
DateTime.MaxValue,
null);
}
account.Submit(new[] { buyMktOrder });
}, null);
e.Handled = true;
#endregion HOTKEY buyMktOrder 🚀 NumPad 7
#region HOTKEY sellMktOrder 🚀 NumPad 8
TriggerCustomEvent(p =>
{
Order sellMktOrder = null;
if (Keyboard.IsKeyDown(Key.NumPad8))
{
sellMktOrder = account.CreateOrder(
Instrument,
OrderAction.Sell,
OrderType.Market,
OrderEntry.Manual,
TimeInForce.Day,
quantitySelector.Value,
0,
0,
"",
"sellMktOrder"+DateTime.Now.ToString(),
DateTime.MaxValue,
null);
}
account.Submit(new[] { sellMktOrder });
}, null);
e.Handled = true;
#endregion HOTKEY sellMktOrder 🚀 NumPad 8
#region HOTKEYS HandleStopLossPlus 🚀 NumPad 9 (SL+ Button)
TriggerCustomEvent(q =>
{
if (Keyboard.IsKeyDown(Key.NumPad9))
{
HandleStopLossPlus("keyPress9", 0);
}
}, null);
e.Handled = true;
#endregion HOTKEYS HandleStopLossPlus 🚀 NumPad 9 (SL+ Button)
#region HOTKEYS HandleBreakEvenPlus 🚀 NumPad 6 (BE+ Button)
TriggerCustomEvent(r =>
{
if (Keyboard.IsKeyDown(Key.NumPad6))
{
HandleBreakEvenPlus("keyPress6");
}
}, null);
e.Handled = true;
#endregion HOTKEYS HandleBreakEvenPlus 🚀 NumPad 6 (BE+ Button)
#region HOTKEYS HandleTakeProfitPlus 🚀 NumPad 3 (TP+ Button)
TriggerCustomEvent(r =>
{
if (Keyboard.IsKeyDown(Key.NumPad3))
{
HandleTakeProfitPlus("keyPress3");
}
}, null);
e.Handled = true;
#endregion HOTKEYS HandleTakeProfitPlus 🚀 NumPad 3 (TP+ Button)
#region HOTKEYS MoveSL() 🚀 NumPad 1 #2 of 2 (LONG: Move SL to Bid - 4 Ticks | SHORT: Move SL to Ask + 4 Ticks)
TriggerCustomEvent(s =>
{
if (Keyboard.IsKeyDown(Key.NumPad1))
{
MoveSL();
}
}, null);
e.Handled = true;
#endregion HOTKEYS MoveSL() 🚀 NumPad 1 #2 of 2 (LONG: Move SL to Bid - 4 Ticks | SHORT: Move SL to Ask + 4 Ticks)
#region HOTKEYS MoveTP() 🚀 NumPad 2 #2 of 2 (LONG: Move SL to Bid - 4 Ticks | SHORT: Move SL to Ask + 4 Ticks)
TriggerCustomEvent(s =>
{
if (Keyboard.IsKeyDown(Key.NumPad2))
{
MoveTP();
}
}, null);
e.Handled = true;
#endregion HOTKEYS MoveTP() 🚀 NumPad 2 #2 of 2 (LONG: Move SL to Bid - 4 Ticks | SHORT: Move SL to Ask + 4 Ticks)
#region EXTRA BUTTONS HOTKEYS TEMPLATES (just need to set the hotkey "?")
#region HOTKEYS HandleReverse 🚀 NumPad ? (REV Button)
TriggerCustomEvent(r =>
{
if (Keyboard.IsKeyDown(Key.NumPad?))
{
HandleReverse("? 0");
}
}, null);
e.Handled = true;
#endregion HOTKEYS 🚀 🚀 NumPad ? (REV Button)
#region HOTKEYS HandleSellSnap 🚀 NumPad ? (SNAP- Button)
TriggerCustomEvent(r =>
{
if (Keyboard.IsKeyDown(Key.NumPad?))
{
HandleSellSnap("? 2");
}
}, null);
e.Handled = true;
#endregion HOTKEYS 🚀 NumPad ? ( Button)
#region HOTKEYS HandleBuySnap 🚀 NumPad ? (SNAP+ Button)
TriggerCustomEvent(r =>
{
if (Keyboard.IsKeyDown(Key.NumPad?))
{
HandleBuySnap("? 3");
}
}, null);
e.Handled = true;
#endregion HOTKEYS 🚀 NumPad ? ( Button)
#region HOTKEYS HandleBuyPop 🚀 NumPad ? (POP+ Button)
TriggerCustomEvent(r =>
{
if (Keyboard.IsKeyDown(Key.NumPad?))
{
HandleBuyPop("? 4");
}
}, null);
e.Handled = true;
#endregion HOTKEYS 🚀 NumPad ? ( Button)
#region HOTKEYS HandleSellPop 🚀 NumPad ? (POP- Button)
TriggerCustomEvent(r =>
{
if (Keyboard.IsKeyDown(Key.NumPad?))
{
HandleSellPop("? 5");
}
}, null);
e.Handled = true;
#endregion HOTKEYS 🚀 NumPad ? ( Button)
#region HOTKEYS HandleBuyDrop 🚀 NumPad ? (DROP+ Button)
TriggerCustomEvent(r =>
{
if (Keyboard.IsKeyDown(Key.NumPad?))
{
HandleBuyDrop("? 6");
}
}, null);
e.Handled = true;
#endregion HOTKEYS 🚀 NumPad ? ( Button)
#region HOTKEYS HandleSellDrop 🚀 NumPad ? (DROP- Button)
TriggerCustomEvent(r =>
{
if (Keyboard.IsKeyDown(Key.NumPad?))
{
HandleSellDrop("? 7");
}
}, null);
e.Handled = true;
#endregion HOTKEYS 🚀 NumPad ? ( Button)
#region HOTKEYS HandlePopAutoJumpToSnap 🚀 NumPad ? ( Button)
TriggerCustomEvent(r =>
{
if (Keyboard.IsKeyDown(Key.NumPad?))
{
HandlePopAutoJumpToSnap("? 8");
}
}, null);
e.Handled = true;
#endregion HOTKEYS 🚀 NumPad ? ( Button)
#region HOTKEYS HandleDropAutoJumpToSnap 🚀 NumPad ? ( Button)
TriggerCustomEvent(r =>
{
if (Keyboard.IsKeyDown(Key.NumPad?))
{
HandleDropAutoJumpToSnap("? 9");
}
}, null);
e.Handled = true;
#endregion HOTKEYS 🚀 NumPad ? ( Button)
#endregion EXTRA BUTTONS HOTKEYS TEMPLATES (just need to set the hotkey "?")
}
private void ChartPanel_KeyUp(object sender, KeyEventArgs e)
{
}
#endregion HOTKEYS 🚀 protected void ChartPanel_KeyDown and ChartPanel_KeyUp #1 of 1
#region 🚀 Properties AccountName and ChartPosition
[TypeConverter(typeof(NinjaTrader.NinjaScript.AccountNameConverter))]
[Display(Name="AccountName", Order=0, GroupName="Parameters")]
public string AccountName { get; set; }
[NinjaScriptProperty]
[Display(Name = "Chart Position", Description = "Unrealized PnL TextPosition", GroupName="NinjaScriptProperties", Order = 1)]
public TextPosition ChartPosition
{ get; set; }
#endregion 🚀 Properties AccountName and ChartPositionEditor is loading...