NEW BRUNSWICK
Experience: Intermediate
Platform: ninjatrader
Trading: nq
Posts: 11 since Jun 2020
Thanks Given: 2
Thanks Received: 0
|
below is a atm strategy that i want to build but i dont know why is not working how is suppose to
protected override void OnBarUpdate()
{
if (CurrentBar < BarsRequiredToTrade)
return;
if(State == State.Historical)
return;
if (orderIdL.Length == 0 &&
atmStrategyIdL.Length == 0
&& (Stochastics1.K[0] < 20)
{
isAtmStrategyCreatedL = false;
atmStrategyIdL = GetAtmStrategyUniqueId();
orderIdL = GetAtmStrategyUniqueId();
AtmStrategyCreate(OrderAction.Buy, OrderType.Market, Low[0], 0, TimeInForce.Day, orderIdL, "ES-LONG", atmStrategyIdL, (atmCallbackErrorCode, atmCallBackId) => {
if (atmCallbackErrorCode == ErrorCode.NoError && atmCallBackId == atmStrategyIdL)
isAtmStrategyCreatedL = true;
});
}
if (orderIdS.Length == 0 &&
atmStrategyIdS.Length == 0
&& (Stochastics1.K[0] > 80)
{
CandleOutlineBrush = Brushes.Crimson;
BarBrush = Brushes.Crimson;
}
{
isAtmStrategyCreatedS = false;
atmStrategyIdS = GetAtmStrategyUniqueId();
orderIdS = GetAtmStrategyUniqueId();
AtmStrategyCreate(OrderAction.Buy, OrderType.Market, Low[0], 0, TimeInForce.Day, orderIdS, "ES-SHORT", atmStrategyIdS, (atmCallbackErrorCode, atmCallBackId) => {
if (atmCallbackErrorCode == ErrorCode.NoError && atmCallBackId == atmStrategyIdS)
isAtmStrategyCreatedS = true;
});
}
if (!isAtmStrategyCreatedS)
return;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (orderIdL.Length > 0)
{
string[] status = GetAtmStrategyEntryOrderStatus(orderIdL);
// If the status call can't find the order specified, the return array length will be zero otherwise it will hold elements
if (status.GetLength(0) > 0)
{
// Print out some information about the order to the output window
Print("The entry order average fill price is: " + status[0]);
Print("The entry order filled amount is: " + status[1]);
Print("The entry order order state is: " + status[2]);
// If the order state is terminal, reset the order id value
if (status[2] == "Filled" || status[2] == "Cancelled" || status[2] == "Rejected")
orderIdL = string.Empty;
}
} // If the strategy has terminated reset the strategy id
else if (atmStrategyIdL.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyIdL) == Cbi.MarketPosition.Flat)
atmStrategyIdL = string.Empty;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (orderIdS.Length > 0)
{
string[] status = GetAtmStrategyEntryOrderStatus(orderIdS);
// If the status call can't find the order specified, the return array length will be zero otherwise it will hold elements
if (status.GetLength(0) > 0)
{
// Print out some information about the order to the output window
Print("The entry order average fill price is: " + status[0]);
Print("The entry order filled amount is: " + status[1]);
Print("The entry order order state is: " + status[2]);
// If the order state is terminal, reset the order id value
if (status[2] == "Filled" || status[2] == "Cancelled" || status[2] == "Rejected")
orderIdS = string.Empty;
}
} // If the strategy has terminated reset the strategy id
else if (atmStrategyIdS.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyIdS) == Cbi.MarketPosition.Flat)
atmStrategyIdS = string.Empty;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (atmStrategyIdL.Length > 0)
{
// You can change the stop price
if (GetAtmStrategyMarketPosition(atmStrategyIdL) != MarketPosition.Flat)
AtmStrategyChangeStopTarget(0, Low[0] - 3 * TickSize, "STOP1", atmStrategyIdL);
// Print some information about the strategy to the output window, please note you access the ATM strategy specific position object here
// the ATM would run self contained and would not have an impact on your NinjaScript strategy position and PnL
Print("The current ATM Strategy market position is: " + GetAtmStrategyMarketPosition(atmStrategyIdL));
Print("The current ATM Strategy position quantity is: " + GetAtmStrategyPositionQuantity(atmStrategyIdL));
Print("The current ATM Strategy average price is: " + GetAtmStrategyPositionAveragePrice(atmStrategyIdL));
Print("The current ATM Strategy Unrealized PnL is: " + GetAtmStrategyUnrealizedProfitLoss(atmStrategyIdL));
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (atmStrategyIdS.Length > 0)
{
// You can change the stop price
if (GetAtmStrategyMarketPosition(atmStrategyIdS) != MarketPosition.Flat)
AtmStrategyChangeStopTarget(0, Low[0] - 3 * TickSize, "STOP1", atmStrategyIdS);
// Print some information about the strategy to the output window, please note you access the ATM strategy specific position object here
// the ATM would run self contained and would not have an impact on your NinjaScript strategy position and PnL
Print("The current ATM Strategy market position is: " + GetAtmStrategyMarketPosition(atmStrategyIdS));
Print("The current ATM Strategy position quantity is: " + GetAtmStrategyPositionQuantity(atmStrategyIdS));
Print("The current ATM Strategy average price is: " + GetAtmStrategyPositionAveragePrice(atmStrategyIdS));
Print("The current ATM Strategy Unrealized PnL is: " + GetAtmStrategyUnrealizedProfitLoss(atmStrategyIdS));
}
}
}
}}
|