• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

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:
    https://www.youtube.com/watch?v=K8v_...tu.be&t=48m45s

    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 jertrade, Today, 06:57 PM
    0 responses
    2 views
    0 likes
    Last Post jertrade  
    Started by Emma1, Today, 02:42 PM
    1 response
    17 views
    0 likes
    Last Post NinjaTrader_DrewO  
    Started by fx.practic, Today, 03:10 PM
    1 response
    6 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by jertrade, Today, 02:50 PM
    2 responses
    14 views
    0 likes
    Last Post jertrade  
    Started by RedDuke, Today, 01:59 PM
    1 response
    20 views
    0 likes
    Last Post NinjaTrader_Kate  
    Working...
    X