#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TO_CSVHDR "Asset,Weight%%,Margin,Lots,MarginCost"
#define TO_CSV "\n%s,%.0f,%.0f,%.0f,%.0f"
char* Filename = "Data\\MyStrategy.csv"; // rotation list
char* Fileformat = "ss,f,f,f";
char* Outfile = "Log\\Rotation.log";
char* TickerSymbol = "*";
// char* TickerSymbol = "*-STK-SMART-USD!STOOQ:*";
#define NEWWEIGHT 1 // weight percent field number
#define NEWLOTS 3 // lots field number
#define LEVERAGE 1
double AssetInt[12]; // Assuming AssetInt is an array of doubles
void folioTrade(int LS) {
// Assuming used_assets is an array of asset names
for (int i = 0; i < sizeof(used_assets) / sizeof(used_assets[0]); i++) {
asset(used_assets[i]);
if ((OpenLots > NewLots && LS < 0) || (OpenLots < NewLots && LS > 0)) {
enterLong(NewLots - OpenLots);
}
}
}
int main() {
int i;
int Records;
double Invest = 0;
LookBack = 0;
StartDate = NOW;
set(LOGFILE);
SaveMode = 0;
Filename = file_select("Data", "Rotation Lists\0*.csv\0\0");
Records = dataParse(1, Fileformat, Filename);
if (!Records) {
return quit("CSV file not found");
}
printf("\nSubscribing %i assets..", Records);
brokerCommand(SET_PRICETYPE, 8); // get prices fast
char Box[50000] = "\n---------------------------------";
int Changed = 0;
for (i = 0; i < Records; i++) {
printf(".");
char* Ticker = dataStr(1, i, 0);
assetAdd(Ticker, TickerSymbol);
if (!asset(Ticker)) continue;
NewLots = dataVar(1, i, NEWLOTS);
OpenLots = brokerCommand(GET_POSITION, Ticker);
double Margin = NewLots * priceC(0) / LEVERAGE;
Invest += Margin;
if (OpenLots != NewLots) {
Changed = 1;
strcat(Box, strf("\n%s \t%d -> %d@%s ($%.2f)",
Asset, OpenLots, NewLots, sftoa(priceC(0), 2), Margin));
} else if (OpenLots) {
strcat(Box, strf("\n%s \t%d@%s ($%.2f)",
Asset, OpenLots, sftoa(priceC(0), 2), Margin));
}
}
strcat(Box, "\n---------------------------------");
strcat(Box, strf("\nTotal margin $%s at %s",
sftoa(Invest, 4), strdate(YMDHMS, NOW)));
if (!Changed) {
printf("\nPortfolio unchanged");
} else {
strcat(Box, "\nModify Portfolio?");
if (msg(Box)) {
strcat(Box, " Y");
setf(TradeMode, TR_GTC);
folioTrade(-1); // Short
folioTrade(1); // Long
file_append(Outfile, Box, 0);
exec("Editor", Outfile, 0);
} else {
printf("\nNo positions entered");
}
}
return 0;
}