Skip to content

Instantly share code, notes, and snippets.

@spencerdavis2000
Last active November 19, 2016 22:40
Show Gist options
  • Save spencerdavis2000/2d3693a05033417b62c6f2dc60a6ef1c to your computer and use it in GitHub Desktop.
Save spencerdavis2000/2d3693a05033417b62c6f2dc60a6ef1c to your computer and use it in GitHub Desktop.
Using a Class with a "dynamic array container" such as a List or ArrayList to create a datastructure format used bar by bar for trading purposes.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MoneyManagement
{
class Program
{
static void Main(string[] args)
{
// this represents out "container" to hold stuff.
// it is built on top of an array, but made dynamic so size is not needed to be specified
// the "type" is MoneyManagement
List<MoneyManagement> moneyManagement = new List<MoneyManagement>(); // our array with type MoneyManagement
// on bar 768 this information gets added to our data structure
moneyManagement.Add(new MoneyManagement(99.76, 100.76, 5000, 2.3, 786)); // information gets added for currentBar
// on bar 787 this information gets added to our data structure
moneyManagement.Add(new MoneyManagement(99.85, 100.76, 5000, 2.1, 787)); // information gets added for currentBar
// this looks at the zeroth element of our List (dynamic array) and analyzes the profit target
if (moneyManagement[0].getProfitTarget() > 100.05)
{
ExitPosition();
}
}
private static void ExitPosition()
{
Console.WriteLine("you exited your position");
Console.Read();
}
}
class MoneyManagement
{
private double stopLoss;
private double profitTarget;
private double accountValue;
private double riskReward;
private int currentBar;
// default constructor
public MoneyManagement()
{
stopLoss = 0;
profitTarget = 0;
accountValue = 0;
riskReward = 0;
currentBar = 0;
}
// constructor with items in it mostly used when we need to have a MoneyManagement array type
public MoneyManagement(double stopLoss, double profitTarget, double accountValue, double riskReward, int currentBar)
{
// note: this.stopLoss references the one above. stopLoss on the right side is the input
this.stopLoss = stopLoss;
this.profitTarget = profitTarget;
this.accountValue = accountValue;
this.riskReward = riskReward;
this.currentBar = currentBar;
}
public void setStopLoss(double stopLoss)
{
this.stopLoss = stopLoss;
}
public void setProfitTarget(double profitTarget)
{
this.profitTarget = profitTarget;
}
public double getStopLoss()
{
return stopLoss;
}
public double getProfitTarget()
{
return profitTarget;
}
}
}
@spencerdavis2000
Copy link
Author

spencerdavis2000 commented Nov 19, 2016

I wrote this example in C# because C# and Java are easier for me than C++, but C++ is awesome as well.

In trading, there are lots of things going on BarByBar.
In NinjaTrader, this "event" is called OnBarUpdate()

protected override void OnBarUpdate()

It is essentially a loop that keeps happening every interval specified

  • 15 min bar is every 15 minutes.

  • 1 minute bar is every 1 minute.

  • 25 tick bar is every 25 ticks.

  • 1 tick is every tick

This provides a way to store data inside of a dynamic array such as an ArrayList or List of "type Class".
In this case, the MoneyManagement is the "type", just like double or string can also be the type.

Notice how I put in there "currentBar". In NinjaTrader, it has a method called CurrentBar which keeps track of the bar count.
This way, if we want to look at bar 501, we know exactly what our data should be on that bar.

This is our datastructure given by the "blueprint of the Class" inside of our dynamic array called a List

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment