Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

auto strategy

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    auto strategy

    hello Why does strategy not want to work?Can you tell me what to do ? I put on 1 range bar


    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class : Strategy
    {

    private List<Order> stopLossOrders;
    private List<Order> profitTargetOrders;
    private bool doneForSession = false;
    private Account myAccount;
    private Order firstOrder = null;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Name = "Bwebmedia";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.UniqueEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 0;
    MaximumBarsLookBack = MaximumBarsLookBack.Infinite;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.AdoptAccountPosition;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = true;
    RealtimeErrorHandling = RealtimeErrorHandling.IgnoreAllErrors;
    StopTargetHandling = StopTargetHandling.ByStrategyPosition;
    BarsRequiredToTrade = 1;
    IsInstantiatedOnEachOptimizationIteration = true;




    lock (Account.All)
    myAccount = Account.All.FirstOrDefault(a => a.Name == "Sim101");
    }

    else if (State == State.Configure)
    {
    profitTargetOrders = new List<Order>();
    stopLossOrders = new List<Order>();

    SetProfitTarget("", CalculationMode.Ticks, 1);
    SetStopLoss("", CalculationMode.Ticks, 2, false);
    }
    }

    protected override void OnBarUpdate()
    {
    if (State == State.Historical) return;

    if (Bars.IsFirstBarOfSession && IsFirstTickOfBar)
    doneForSession = false;

    if (doneForSession)
    return;

    if (Close[0] > Open[0])
    {
    firstOrder = myAccount.CreateOrder(Instrument, OrderAction.Buy, OrderType.StopMarket, TimeInForce.Day, 1, 0, 1400, "", "", null);
    myAccount.Submit(new[] { firstOrder });

    EnterLong(1, "");
    }

    if (Position.Quantity == 1)
    EnterLong(1, "");

    else if (Position.Quantity == 2)
    doneForSession = true;

    }

    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
    {

    if (order.OrderState == OrderState.Submitted)
    {
    if (order.Name == "Stop loss")
    stopLossOrders.Add(order);

    else if (order.Name == "Profit target")
    profitTargetOrders.Add(order);
    }


    if (stopLossOrders.Contains(order))
    {
    if (order.OrderState == OrderState.Cancelled || order.OrderState == OrderState.Filled || order.OrderState == OrderState.Rejected)
    {
    Print(order);

    stopLossOrders.Remove(order);
    }
    else
    {
    Print("The order name " + order.Name + " stop price is currently " + stopPrice);
    }
    }

    if (profitTargetOrders.Contains(order))
    {
    if (order.OrderState == OrderState.Cancelled || order.OrderState == OrderState.Filled || order.OrderState == OrderState.Rejected)
    {
    Print(order);

    profitTargetOrders.Remove(order);
    }
    else
    {
    Print("The order name " + order.Name + " limit price is currently " + limitPrice);
    }
    }
    }
    #region Properties
    #endregion
    }
    }
    Last edited by Yesiam; 04-10-2017, 07:25 AM.

    #2
    Hello Yesiam,

    Thank you for writing in.

    What happens when you run the strategy? Do you see errors on the log tab?

    Are you able to run the strategy on a 1 minute bar?

    I would suggest adding print statements to check whether your conditions are becoming true. I’ve provided a link to a youtube video which covers an example of using prints to understand behavior:


    I’ve provided a link covering debugging which you may find helpful.
    Debugging: http://ninjatrader.com/support/forum...ead.php?t=3418

    I look forward to your reply.
    Alan P.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Yesterday, 05:17 AM
    0 responses
    59 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    134 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    74 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    45 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    50 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X