Login Page - Create Account

Support Board


Date/Time: Wed, 03 Sep 2025 09:08:33 +0000



Post From: How to set up data and backtest a study

[2025-08-04 16:03:35]
User944318 - Posts: 19
I am testing out the example code from Sierra Charts:

#include "sierrachart.h"

SCDLLName("SCTradingExample1")

/*
Overview
--------
A modified trading system that enters a new position or
reverses an existing one on the crossover of open and close prices.

When close crosses open from below (close > open), the system will go long.
When close crosses open from above (close < open), the system will go short.

This eliminates the need for external studies and uses built-in price data.

Comments
--------
* The system now uses sc.Open and sc.Close arrays directly
* No external study references needed
* Crossover logic compares current and previous bar relationships
* All other trading logic remains the same with reversals enabled

*/


SCSFExport scsf_SC_TradingCrossOverExample(SCStudyInterfaceRef sc)
{
  SCSubgraphRef Subgraph_BuyEntry = sc.Subgraph[0];
  SCSubgraphRef Subgraph_SellEntry = sc.Subgraph[1];

  if (sc.SetDefaults)
  {
    // Set the configuration and defaults
    sc.GraphName = "Trading CrossOver Example - Open vs Close";

    sc.StudyDescription = "A trading system that enters a new position or \
reverses an existing one on the crossover of open and close prices. \
When close crosses open from below (bullish bar), the system will go long. \
When close crosses open from above (bearish bar), the system will go short.";

    sc.AutoLoop = 1; // true
    sc.GraphRegion = 0;
    sc.CalculationPrecedence = LOW_PREC_LEVEL;

    Subgraph_BuyEntry.Name = "Bullish";
    Subgraph_BuyEntry.DrawStyle = DRAWSTYLE_POINT_ON_HIGH;
    Subgraph_BuyEntry.LineWidth = 3;
    Subgraph_BuyEntry.PrimaryColor = RGB(0, 255, 0); // Green

    Subgraph_SellEntry.Name = "Bearish";
    Subgraph_SellEntry.DrawStyle = DRAWSTYLE_POINT_ON_LOW;
    Subgraph_SellEntry.LineWidth = 3;
    Subgraph_SellEntry.PrimaryColor = RGB(255, 0, 0); // Red

    sc.AllowMultipleEntriesInSameDirection = false;
    sc.MaximumPositionAllowed = 5;
    sc.SupportReversals = true;

    // This is false by default. Orders will go to the simulation system always.
    sc.SendOrdersToTradeService = false;

    sc.AllowOppositeEntryWithOpposingPositionOrOrders = false;
    sc.SupportAttachedOrdersForTrading = false;

    sc.CancelAllOrdersOnEntriesAndReversals = true;
    sc.AllowEntryWithWorkingOrders = false;
    sc.CancelAllWorkingOrdersOnExit = true;
    sc.AllowOnlyOneTradePerBar = true;

    sc.MaintainTradeStatisticsAndTradesData = true;

    return;
  }

  // Only process at the close of the bar; if it has not closed don't do anything
  if (sc.GetBarHasClosedStatus() == BHCS_BAR_HAS_NOT_CLOSED)
  {
    return;
  }

  // Need at least 2 bars to detect a crossover
  if (sc.Index < 1)
    return;

  // Get current and previous bar data
  float CurrentOpen = sc.Open[sc.Index];
  float CurrentClose = sc.Close[sc.Index];
  float PreviousOpen = sc.Open[sc.Index - 1];
  float PreviousClose = sc.Close[sc.Index - 1];

  // Check for crossover conditions
  // Bullish crossover: Previous bar had close <= open, current bar has close > open
  bool BullishCrossover = (PreviousClose <= PreviousOpen) && (CurrentClose > CurrentOpen);
  
  // Bearish crossover: Previous bar had close >= open, current bar has close < open
  bool BearishCrossover = (PreviousClose >= PreviousOpen) && (CurrentClose < CurrentOpen);

  if (BullishCrossover)
  {
    // Mark the crossover on the chart
    Subgraph_BuyEntry[sc.Index] = sc.Low[sc.Index];
    sc.AddMessageToLog("Long", 0);

    // Create a market order and enter long
    s_SCNewOrder NewOrder;
    NewOrder.OrderQuantity = 1;
    NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
    NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;

    sc.BuyEntry(NewOrder);
  }

  if (BearishCrossover)
  {
    // Mark the crossover on the chart
    Subgraph_SellEntry[sc.Index] = sc.High[sc.Index];
    sc.AddMessageToLog("Short", 0);

    // Create a market order and enter short
    s_SCNewOrder NewOrder;
    NewOrder.OrderQuantity = 1;
    NewOrder.OrderType = SCT_ORDERTYPE_MARKET;
    NewOrder.TimeInForce = SCT_TIF_GOOD_TILL_CANCELED;

    sc.SellEntry(NewOrder);
  }
}