Login Page - Create Account

Support Board


Date/Time: Sat, 20 Apr 2024 02:34:08 +0000



Post From: Very low programming experience, help w/ simple trading system

[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);
  }

}