August 20th, 2013, 10:17 PM
stock market trading script...
The methods used below are internal to the automated trading software I use, I hope it's not too confusing. I've tried to comment below as much as possible. Basically the script Enters a stock position if there is a cross over of a fast moving average above a slow moving average. But previous to this I want to make sure two price bars or 10 minutes have elapsed so too many trades aren't being entered. The Beginning of the code initializes 5 Indexes which is 5 different Stock instruments or a small portfolio. My main issue is highlighted in yellow, I'm trying to have it count and store the number of trades that have taken place per stock instrument. I am unsure exactly how to do this. Do I create a variable for each stock instrument to store the # of trades, if so how would I go about making that work?
// This method is used to configure the strategy and is called once before any strategy method is called.
protected override void Initialize()
//5min Price Bars for each stock instrument..
Add("ABC",PeriodType.Minute, 5); //Index 0, stock instrument 1
Add("ACE",PeriodType.Minute, 5); //Index 1, stock instrument 2
Add("ACN",PeriodType.Minute, 5); //Index 2, stock instrument 3
Add("ADT",PeriodType.Minute, 5); //Index 3, stock instrument 4
Add("SCTY",PeriodType.Minute, 5); //Index 4, stock instrument 5
// Called on each bar update event (incoming tick)
protected override void OnBarUpdate()
//loop to iterate each Index(stock) through the Entry Conditions
for (int Index = 0; series < 5; Index ++)
if (BarsInProgress == Index )
// Enter trade if there has been 0 trades for the particular Index
bool enterTrade = false;
if (//Number of Entries Per Index// < 1)
enterTrade = true;
//else if there has been a previous trade for the particular Index make sure two 5min bars have elapsed
enterTrade = BarsSinceEntry(Index , "", 0) > 2;
//Conditions for Stock Entry, Fast Moving average cross above Slow Moving Average 1 bar ago
if (SMA(BarsArray[Index],Fast) > SMA(BarsArray[Index],Slow))
//Enter the trade with 200 shares for particular Index
EnterLong(Index , 200, "");
//Store trade has taken place for the particular Index
Entries Per Index++;
Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!