Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

basic strategy programming

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

    basic strategy programming

    I wrote a simple "test" strategy: "if today's open price is higher/lower than yesterday's H/L - trade..." When I back test - no orders are generated (though conditions did occur).
    My questions (see code below):
    1.what do I use to init values each day? (during back test) [I use Initialize() - is it correct?]
    2. to get today's open I use CurrentDayOHL().CurrentOpen[0], for yesterday's low I use Bars.GetDayBar(1).Low - is it correct?]
    3. to get current price, I use Close[0] - is it correct?

    I listed the code below:
    // User defined variables (add any user defined variables below)
    private bool isLowerOpen = false;
    private bool isHigherOpen = false;
    private double refPrice = 0.0;
    private bool tradedToday = false;

    protected override void Initialize()
    {
    SetProfitTarget("Stg001", CalculationMode.Ticks, Profit);
    SetTrailStop("Stg001", CalculationMode.Ticks, Loss, false);
    CalculateOnBarClose = true;
    isLowerOpen = CurrentDayOHL().CurrentOpen[0] < Bars.GetDayBar(1).Low;
    isHigherOpen = CurrentDayOHL().CurrentOpen[0] > Bars.GetDayBar(1).High;
    if (isLowerOpen)
    refPrice = Bars.GetDayBar(1).Low;
    else if (isHigherOpen)
    refPrice = Bars.GetDayBar(1).High;
    }

    protected override void OnBarUpdate()
    {
    if (tradedToday)
    return;
    if (isLowerOpen && Close[0] > refPrice) {
    EnterLong(DefaultQuantity, "Stg001");
    tradedToday = true;
    }
    else if (isHigherOpen && Close[0] < refPrice) {
    EnterShort(DefaultQuantity, "Stg001");
    tradedToday = true;
    }
    }

    #2
    Originally posted by MarkZ View Post
    I wrote a simple "test" strategy: "if today's open price is higher/lower than yesterday's H/L - trade..." When I back test - no orders are generated (though conditions did occur).
    My questions (see code below):
    1.what do I use to init values each day? (during back test) [I use Initialize() - is it correct?]
    2. to get today's open I use CurrentDayOHL().CurrentOpen[0], for yesterday's low I use Bars.GetDayBar(1).Low - is it correct?]
    3. to get current price, I use Close[0] - is it correct?

    I listed the code below:
    // User defined variables (add any user defined variables below)
    private bool isLowerOpen = false;
    private bool isHigherOpen = false;
    private double refPrice = 0.0;
    private bool tradedToday = false;

    protected override void Initialize()
    {
    SetProfitTarget("Stg001", CalculationMode.Ticks, Profit);
    SetTrailStop("Stg001", CalculationMode.Ticks, Loss, false);
    CalculateOnBarClose = true;
    isLowerOpen = CurrentDayOHL().CurrentOpen[0] < Bars.GetDayBar(1).Low;
    isHigherOpen = CurrentDayOHL().CurrentOpen[0] > Bars.GetDayBar(1).High;
    if (isLowerOpen)
    refPrice = Bars.GetDayBar(1).Low;
    else if (isHigherOpen)
    refPrice = Bars.GetDayBar(1).High;
    }

    protected override void OnBarUpdate()
    {
    if (tradedToday)
    return;
    if (isLowerOpen && Close[0] > refPrice) {
    EnterLong(DefaultQuantity, "Stg001");
    tradedToday = true;
    }
    else if (isHigherOpen && Close[0] < refPrice) {
    EnterShort(DefaultQuantity, "Stg001");
    tradedToday = true;
    }
    }
    What are the errors in your log?

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by hazylizard, Today, 08:38 AM
    2 responses
    8 views
    0 likes
    Last Post hazylizard  
    Started by geddyisodin, Today, 05:20 AM
    2 responses
    18 views
    0 likes
    Last Post geddyisodin  
    Started by Max238, Today, 01:28 AM
    5 responses
    47 views
    0 likes
    Last Post Max238
    by Max238
     
    Started by giulyko00, Yesterday, 12:03 PM
    3 responses
    13 views
    0 likes
    Last Post NinjaTrader_BrandonH  
    Started by habeebft, Today, 07:27 AM
    1 response
    16 views
    0 likes
    Last Post NinjaTrader_ChristopherS  
    Working...
    X