Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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:
    Dive into manipulating C# code from within an unlocked NinjaScript strategy using the NinjaScript Editor.NinjaTrader 7 is an award winning end to end online ...


    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 trilliantrader, 04-18-2024, 08:16 AM
    4 responses
    18 views
    0 likes
    Last Post trilliantrader  
    Started by mgco4you, Today, 09:46 PM
    1 response
    7 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by wzgy0920, Today, 09:53 PM
    0 responses
    9 views
    0 likes
    Last Post wzgy0920  
    Started by Rapine Heihei, Today, 08:19 PM
    1 response
    10 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by Rapine Heihei, Today, 08:25 PM
    0 responses
    10 views
    0 likes
    Last Post Rapine Heihei  
    Working...
    X