Login Page - Create Account

Support Board


Date/Time: Tue, 26 Nov 2024 02:49:55 +0000



[User Discussion] - Very low programming experience, help w/ simple trading system

View Count: 1404

[2014-04-06 21:09:02]
cazeek - Posts: 33
Hello,

I am attempting to work towards creating a robust trading system that I will backtest and implement down the line. Right now, I'm trying to just get a handle on the programming aspect, as I have very very little experience with it. I'm attempting to do the following in my code:

1) Draw 2 moving averages on my chart with the lengths determined by inputs in the study settings
2) Create arrows on the chart every time the two moving averages cross each other
3) Create a buy or sell order every time the two moving averages cross each other

Can you please review the code I have below and tell me what I need to add or change? I feel like this is very close, and there are no compile errors, but there are no lines or trades being created on when I add the study / run the backtest.

Thanks!


#include "sierrachart.h"

SCDLLName("TEST TRADING SYSTEM")

SCSFExport scsf_SC_MACrossover(SCStudyGraphRef sc)
{

  SCInputRef FastLength = sc.Input[0];
  SCInputRef SlowLength = sc.Input[1];

  SCSubgraphRef Buy = sc.Subgraph[0];
  SCSubgraphRef Sell = sc.Subgraph[1];
  
  SCSubgraphRef FastMA = sc.Subgraph[2];
  SCSubgraphRef SlowMA = sc.Subgraph[3];
  
  
  if (sc.SetDefaults)
  {

    // Set the configuration and defaults

    sc.GraphName = "MA Crossover";

    sc.StudyDescription = "Simple MA Crossover System";

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

    FastLength.Name = "Fast Moving Average Length";
    FastLength.SetInt(10);

    SlowLength.Name = "Slow Moving Average Length";
    SlowLength.SetInt(100);

    Buy.Name = "Buy";
    Buy.DrawStyle = DRAWSTYLE_ARROWUP;
    Buy.DataColor[sc.Index] = RGB(0,255,0);
    Buy.LineWidth = 2;

    Sell.Name = "Sell";
    Sell.DrawStyle = DRAWSTYLE_ARROWDOWN;
    Sell.DataColor[sc.Index] = RGB(255,0,0);
    Sell.LineWidth = 2;
    
    FastMA.Name = "Fast Moving Average";
    FastMA.DrawStyle = DRAWSTYLE_LINE;
    FastMA.DataColor[sc.Index] = RGB(0,255,255);
    FastMA.LineWidth = 2;
    
    SlowMA.Name = "Slow Moving Average";
    SlowMA.DrawStyle = DRAWSTYLE_LINE;
    SlowMA.DataColor[sc.Index] = RGB(125,255,255);
    SlowMA.LineWidth = 2;
    
    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;
  }

  // Calculate moving averages, store into FastMA and SlowMA
  sc.SimpleMovAvg(sc.Close,FastMA,FastLength.GetInt());
  sc.SimpleMovAvg(sc.Close,SlowMA,SlowLength.GetInt());  

  if (sc.CrossOver(FastMA, SlowMA) == CROSS_FROM_BOTTOM)
  {
    Buy[sc.Index] = 1;
    
    s_SCNewOrder order;
    order.OrderQuantity = 250000;
    order.OrderType = SCT_ORDERTYPE_MARKET;

    sc.BuyEntry(order);
  }
  
  if (sc.CrossOver(FastMA, SlowMA) == CROSS_FROM_TOP)
  {
    Sell[sc.Index] = 1;
    
    s_SCNewOrder order;
    order.OrderQuantity = 250000;
    order.OrderType = SCT_ORDERTYPE_MARKET;
    
    sc.SellEntry(order);
  }

}

[2014-04-06 21:25:17]
Zosimus - Posts: 345
Just from a very first glance:

Change Buy[sc.Index] = 1; to Buy[sc.Index] = sc.Low[sc.Index]- sc.TickSize;
change Sell[sc.Index] = 1; to Sell[sc.Index] = sc.High[sc.Index]+sc.Ticksize;
[2014-04-07 00:18:23]
Sierra Chart Engineering - Posts: 104368
Some comments:

1. The sc.SetDefaults section does not contain any of the automated trading related variables which define how the automated trading management of Sierra Chart is applied to the trading system. This is critical to have. You need to refer to the examples:
https://www.sierrachart.com/index.php?l=doc/doc_ACSILTrading.html#ExampleCode

More information can be found here:
https://www.sierrachart.com/index.php?l=doc/doc_AutoTradeManagment.php

2. Your code does not indicate any consideration of how to exit your position. You need some code to exit the position.

3. Check your current Position before submitting an entry order to make sure you are flat before entering the market. Although this depends upon how you are going to exit your position.


Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing
[2014-04-07 02:09:28]
cazeek - Posts: 33
Thanks guys,

I have it now working in terms of trade execution / back testing, but the actual moving averages and arrows are not showing up on the chart. It's actually a little weird, when I first load the study, and click "Apply", I can see the moving average lines, but as soon as I click "OK", the lines disappear. Can you please take a look at the code again and let me know what I'm missing?

SCSFExport scsf_SC_MACrossover(SCStudyGraphRef sc)
{

  SCInputRef FastLength = sc.Input[0];
  SCInputRef SlowLength = sc.Input[1];

  SCSubgraphRef Buy = sc.Subgraph[0];
  SCSubgraphRef Sell = sc.Subgraph[1];
  
  SCSubgraphRef FastMA = sc.Subgraph[2];
  SCSubgraphRef SlowMA = sc.Subgraph[3];
  
  
  if (sc.SetDefaults)
  {

    // Set the configuration and defaults

    sc.GraphName = "MA Crossover";

    sc.StudyDescription = "Simple MA Crossover System";

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

    // Automated Trade Variables
    sc.AllowMultipleEntriesInSameDirection = false;
    sc.MaximumPositionAllowed = 250000;
    sc.SupportReversals = true;  
    sc.SendOrdersToTradeService = false;
    sc.AllowOppositeEntryWithOpposingPositionOrOrders = false;
    sc.SupportAttachedOrdersForTrading = false;
    sc.CancelAllOrdersOnEntriesAndReversals= true;
    sc.AllowEntryWithWorkingOrders = false;
    sc.CancelAllWorkingOrdersOnExit = true;
    
    sc.AllowOnlyOneTradePerBar = true;

    sc.MaintainTradeStatisticsAndTradesData = true;
    
    //

    FastLength.Name = "Fast Moving Average Length";
    FastLength.SetInt(10);

    SlowLength.Name = "Slow Moving Average Length";
    SlowLength.SetInt(100);

    Buy.Name = "Buy";
    Buy.DrawStyle = DRAWSTYLE_ARROWUP;
    Buy.DataColor[sc.Index] = RGB(0,255,0);
    Buy.LineWidth = 2;

    Sell.Name = "Sell";
    Sell.DrawStyle = DRAWSTYLE_ARROWDOWN;
    Sell.DataColor[sc.Index] = RGB(255,0,0);
    Sell.LineWidth = 2;
    
    FastMA.Name = "Fast Moving Average";
    FastMA.DrawStyle = DRAWSTYLE_LINE;
    FastMA.DataColor[sc.Index] = RGB(0,255,255);
    FastMA.LineWidth = 2;
    
    SlowMA.Name = "Slow Moving Average";
    SlowMA.DrawStyle = DRAWSTYLE_LINE;
    SlowMA.DataColor[sc.Index] = RGB(125,255,255);
    SlowMA.LineWidth = 2;
    
    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;
  }

  // Calculate moving averages, store into FastMA and SlowMA
  sc.SimpleMovAvg(sc.Close,FastMA,FastLength.GetInt());
  sc.SimpleMovAvg(sc.Close,SlowMA,SlowLength.GetInt());  

  if (sc.CrossOver(FastMA, SlowMA) == CROSS_FROM_BOTTOM)
  {
    Buy[sc.Index] = sc.Low[sc.Index] - sc.TickSize;
    
    s_SCNewOrder order;
    order.OrderQuantity = 250000;
    order.OrderType = SCT_ORDERTYPE_MARKET;

    sc.BuyEntry(order);
  }
  
  if (sc.CrossOver(FastMA, SlowMA) == CROSS_FROM_TOP)
  {
    Sell[sc.Index] = sc.High[sc.Index] + sc.TickSize;
    
    s_SCNewOrder order;
    order.OrderQuantity = 250000;
    order.OrderType = SCT_ORDERTYPE_MARKET;
    
    sc.SellEntry(order);
  }

}

[2014-04-07 02:12:28]
Sierra Chart Engineering - Posts: 104368
Not sure why the moving average lines would disappear but make sure you add this in the SetDefaults code block:
Buy.DrawZeros = 0;
Sell.DrawZeros = 0;
Sierra Chart Support - Engineering Level

Your definitive source for support. Other responses are from users. Try to keep your questions brief and to the point. Be aware of support policy:
https://www.sierrachart.com/index.php?l=PostingInformation.php#GeneralInformation

For the most reliable, advanced, and zero cost futures order routing, *change* to the Teton service:
Sierra Chart Teton Futures Order Routing

To post a message in this thread, you need to log in with your Sierra Chart account:

Login

Login Page - Create Account